diff --git a/src/main/java/hudson/plugins/ec2/win/winrm/WinRM.java b/src/main/java/hudson/plugins/ec2/win/winrm/WinRM.java index 39416b0ac..ed34b5063 100644 --- a/src/main/java/hudson/plugins/ec2/win/winrm/WinRM.java +++ b/src/main/java/hudson/plugins/ec2/win/winrm/WinRM.java @@ -12,7 +12,7 @@ public class WinRM { private final String username; private final String password; private int timeout = 60; - private boolean allowSelfSignedCertificate; + private final boolean allowSelfSignedCertificate; private boolean useHTTPS; diff --git a/src/main/java/hudson/plugins/ec2/win/winrm/WinRMClient.java b/src/main/java/hudson/plugins/ec2/win/winrm/WinRMClient.java index ec483c605..2e76ebbf1 100644 --- a/src/main/java/hudson/plugins/ec2/win/winrm/WinRMClient.java +++ b/src/main/java/hudson/plugins/ec2/win/winrm/WinRMClient.java @@ -14,7 +14,6 @@ import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.stream.Stream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; @@ -32,6 +31,7 @@ import org.apache.http.config.RegistryBuilder; import org.apache.http.config.SocketConfig; import org.apache.http.conn.HttpHostConnectException; +import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.auth.BasicSchemeFactory; import org.apache.http.impl.client.BasicAuthCache; @@ -51,7 +51,6 @@ public class WinRMClient { private static final Logger LOGGER = Logger.getLogger(WinRMClient.class.getName()); - private static final String APPLICATION_SOAP_XML = "application/soap+xml"; private final URL url; private final String username; @@ -61,14 +60,12 @@ public class WinRMClient { private String commandId; private int exitCode; - private SimpleNamespaceContext namespaceContext; - private final RequestFactory factory; - private final ThreadLocal authCache = new ThreadLocal(); + private final ThreadLocal authCache = new ThreadLocal<>(); private boolean useHTTPS; private BasicCredentialsProvider credsProvider; - private boolean allowSelfSignedCertificate; + private final boolean allowSelfSignedCertificate; @Deprecated public WinRMClient(URL url, String username, String password) { @@ -137,7 +134,7 @@ public boolean slurpOutput(FastPipedOutputStream stdout, FastPipedOutputStream s Document response = sendRequest(request); XPath xpath = DocumentHelper.createXPath("//" + Namespaces.NS_WIN_SHELL.getPrefix() + ":Stream"); - namespaceContext = new SimpleNamespaceContext(); + SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(); namespaceContext.addNamespace(Namespaces.NS_WIN_SHELL.getPrefix(), Namespaces.NS_WIN_SHELL.getURI()); xpath.setNamespaceContext(namespaceContext); @@ -224,7 +221,7 @@ public boolean retryRequest( // sleep before retrying, increase the sleep time on each re-try int sleepTime = executionCount * 5; try { - Thread.sleep(sleepTime * 1000); + TimeUnit.SECONDS.sleep(sleepTime); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException("Exception while executing command", e); @@ -257,7 +254,7 @@ private Document sendRequest(Document request, int retry) { try { HttpPost post = new HttpPost(url.toURI()); - HttpEntity entity = new StringEntity(request.asXML(), APPLICATION_SOAP_XML, "UTF-8"); + HttpEntity entity = new StringEntity(request.asXML(), ContentType.APPLICATION_SOAP_XML); post.setEntity(entity); LOGGER.log(Level.FINEST, () -> "Request:\nPOST " + url + "\n" + request.asXML()); @@ -269,7 +266,7 @@ private Document sendRequest(Document request, int retry) { // check for possible timeout if (response.getStatusLine().getStatusCode() == 500 - && (responseEntity.getContentType() != null && entity.getContentType().getValue().startsWith(APPLICATION_SOAP_XML))) { + && (responseEntity.getContentType() != null && entity.getContentType().getValue().startsWith(ContentType.APPLICATION_SOAP_XML.getMimeType()))) { String respStr = EntityUtils.toString(responseEntity); if (respStr.contains("TimedOut")) { return DocumentHelper.parseText(respStr); @@ -302,7 +299,7 @@ private Document sendRequest(Document request, int retry) { } if (responseEntity.getContentType() == null - || !entity.getContentType().getValue().startsWith(APPLICATION_SOAP_XML)) { + || !entity.getContentType().getValue().startsWith(ContentType.APPLICATION_SOAP_XML.getMimeType())) { throw new RuntimeException("Unexpected WinRM content type: " + entity.getContentType()); } diff --git a/src/main/java/hudson/plugins/ec2/win/winrm/WinRMConnectionManagerFactory.java b/src/main/java/hudson/plugins/ec2/win/winrm/WinRMConnectionManagerFactory.java index dee3c274a..0721343c5 100644 --- a/src/main/java/hudson/plugins/ec2/win/winrm/WinRMConnectionManagerFactory.java +++ b/src/main/java/hudson/plugins/ec2/win/winrm/WinRMConnectionManagerFactory.java @@ -23,7 +23,7 @@ public class WinRMConnectionManagerFactory { static final WinRMHttpConnectionManager SSL_ALLOW_SELF_SIGNED = new WinRMHttpConnectionManager(true); static class WinRMHttpConnectionManager { - private PoolingHttpClientConnectionManager connectionManager; + private final PoolingHttpClientConnectionManager connectionManager; private SSLConnectionSocketFactory socketFactory; final static int DEFAULT_MAX_PER_ROUTE = 50;