|
| 1 | +package com.qualisystems.pythonDriverPlugin; |
| 2 | + |
| 3 | +import org.w3c.dom.Document; |
| 4 | +import org.w3c.dom.NodeList; |
| 5 | + |
| 6 | +import javax.xml.parsers.DocumentBuilder; |
| 7 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 8 | +import java.io.DataOutputStream; |
| 9 | +import java.io.File; |
| 10 | +import java.net.HttpURLConnection; |
| 11 | +import java.net.URL; |
| 12 | +import java.net.InetAddress; |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.util.Base64; |
| 15 | + |
| 16 | +public class ResourceManagementService { |
| 17 | + |
| 18 | + private static final String TargetServerURLFormat = "http://%s:%s/%s"; |
| 19 | + |
| 20 | + private static final String LoginEndpointPath = "ResourceManagerApiService/Logon"; |
| 21 | + private static final String LogonRequestFormat = |
| 22 | + "<Logon>\n" + |
| 23 | + "<username>%s</username>\n" + |
| 24 | + "<password>%s</password>\n" + |
| 25 | + "<domainName>%s</domainName>\n" + |
| 26 | + "</Logon>"; |
| 27 | + |
| 28 | + private static final String UpdateDriverEndpointPath = "ResourceManagerApiService/UpdateDriver"; |
| 29 | + private static final String UpdateDriverRequestFormat = |
| 30 | + "<UpdateDriver>\n" + |
| 31 | + "<driverName>%s</driverName>\n" + |
| 32 | + "<driverFile>%s</driverFile>\n" + |
| 33 | + "<driverFileName>%s</driverFileName>\n" + |
| 34 | + "</UpdateDriver>"; |
| 35 | + |
| 36 | + public static ResourceManagementService OpenConnection(String serverAddress, int port, String username, String password, String domain) throws Exception { |
| 37 | + |
| 38 | + ResourceManagementService resourceManagementService = new ResourceManagementService(serverAddress, port); |
| 39 | + |
| 40 | + resourceManagementService.login(username, password, domain); |
| 41 | + |
| 42 | + return resourceManagementService; |
| 43 | + } |
| 44 | + |
| 45 | + private void login(String username, String password, String domain) throws Exception { |
| 46 | + |
| 47 | + String serverURL = String.format(TargetServerURLFormat, _serverAddress, _port, LoginEndpointPath); |
| 48 | + |
| 49 | + Document doc = sendMessage(new URL(serverURL), String.format(LogonRequestFormat, username, password, domain)); |
| 50 | + |
| 51 | + NodeList tokenElement = doc.getElementsByTagName("Token"); |
| 52 | + |
| 53 | + if (tokenElement.getLength() != 1) |
| 54 | + throw new Exception("No token element in logon response"); |
| 55 | + |
| 56 | + _authToken = tokenElement.item(0).getAttributes().getNamedItem("Token").getTextContent(); |
| 57 | + } |
| 58 | + |
| 59 | + public void updateDriver(String driverName, File newDriverFile) throws Exception { |
| 60 | + |
| 61 | + String base64DriverFile = |
| 62 | + Base64.getEncoder().encodeToString(Files.readAllBytes(newDriverFile.toPath())); |
| 63 | + |
| 64 | + String serverURL = String.format(TargetServerURLFormat, _serverAddress, _port, UpdateDriverEndpointPath); |
| 65 | + |
| 66 | + sendMessage(new URL(serverURL), String.format(UpdateDriverRequestFormat, driverName, base64DriverFile, newDriverFile.getName())); |
| 67 | + } |
| 68 | + |
| 69 | + private Document sendMessage(URL requestURL, String message) throws Exception { |
| 70 | + |
| 71 | + HttpURLConnection con = (HttpURLConnection) requestURL.openConnection(); |
| 72 | + |
| 73 | + con.setRequestMethod("POST"); |
| 74 | + con.setRequestProperty("DateTimeFormat", "MM/dd/yyyy HH:mm"); |
| 75 | + con.setRequestProperty("ClientTimeZoneId", "UTC"); |
| 76 | + con.setRequestProperty("Content-Type", "text/xml"); |
| 77 | + con.setRequestProperty("Accept", "*/*"); |
| 78 | + con.setRequestProperty("Authorization", String.format("MachineName=%s;Token=%s", _hostname, _authToken)); |
| 79 | + con.setRequestProperty("Host", _serverAddress + ":" + Integer.toString(_port)); |
| 80 | + |
| 81 | + con.setDoOutput(true); |
| 82 | + |
| 83 | + DataOutputStream wr = new DataOutputStream(con.getOutputStream()); |
| 84 | + |
| 85 | + wr.writeBytes(message); |
| 86 | + |
| 87 | + wr.flush(); |
| 88 | + wr.close(); |
| 89 | + |
| 90 | + int responseCode = con.getResponseCode(); |
| 91 | + |
| 92 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 93 | + DocumentBuilder builder = factory.newDocumentBuilder(); |
| 94 | + |
| 95 | + Document responseXml = builder.parse(con.getInputStream()); |
| 96 | + |
| 97 | + NodeList elements = responseXml.getElementsByTagName("Error"); |
| 98 | + |
| 99 | + if (elements.getLength() > 0) |
| 100 | + throw new Exception(String.format("API Message: %s", elements.item(0).getTextContent())); |
| 101 | + |
| 102 | + if (!isSuccessResponseCode(responseCode)) |
| 103 | + throw new Exception("Error making request, Response code: " + responseCode); |
| 104 | + |
| 105 | + return responseXml; |
| 106 | + } |
| 107 | + |
| 108 | + private boolean isSuccessResponseCode(int responseCode) { |
| 109 | + return responseCode >= 200 && responseCode < 300; |
| 110 | + } |
| 111 | + |
| 112 | + private final String _serverAddress; |
| 113 | + private final String _hostname; |
| 114 | + private final int _port; |
| 115 | + |
| 116 | + private String _authToken; |
| 117 | + |
| 118 | + private ResourceManagementService(String serverAddress, int port) { |
| 119 | + |
| 120 | + _serverAddress = serverAddress; |
| 121 | + _port = port; |
| 122 | + |
| 123 | + String hostname; |
| 124 | + |
| 125 | + try { |
| 126 | + hostname = InetAddress.getLocalHost().getHostName(); |
| 127 | + } catch (Exception ex) { |
| 128 | + hostname = "localhost"; |
| 129 | + } |
| 130 | + |
| 131 | + _hostname = hostname; |
| 132 | + } |
| 133 | +} |
0 commit comments