Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

win: cleanup some windows warnings and deprecated code #664

Merged
merged 1 commit into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/ec2/win/winrm/WinRM.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
19 changes: 8 additions & 11 deletions src/main/java/hudson/plugins/ec2/win/winrm/WinRMClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -61,14 +60,12 @@ public class WinRMClient {
private String commandId;
private int exitCode;

private SimpleNamespaceContext namespaceContext;

private final RequestFactory factory;

private final ThreadLocal<BasicAuthCache> authCache = new ThreadLocal<BasicAuthCache>();
private final ThreadLocal<BasicAuthCache> 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) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand All @@ -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);
Expand Down Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down