From 08afe52704d844d9f88f973ab1b1d6fc6c8d30b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johann=20B=C3=B6hler?= Date: Wed, 24 Aug 2016 13:51:50 +0200 Subject: [PATCH] Artifactory cloud issue (#12) Fixed connection test --- pom.xml | 4 +- .../maven/client/RepositoryConnector.java | 89 ++++++++++++++----- 2 files changed, 67 insertions(+), 26 deletions(-) diff --git a/pom.xml b/pom.xml index 84ce07ec..88b3c386 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.oneandone go-maven-poller - 1.1.3 + 1.1.4 https://github.com/1and1/go-maven-poller @@ -37,7 +37,7 @@ org.projectlombok lombok - 1.16.8 + 1.16.10 provided diff --git a/src/main/java/com/oneandone/go/plugin/maven/client/RepositoryConnector.java b/src/main/java/com/oneandone/go/plugin/maven/client/RepositoryConnector.java index 3eaa8325..b2de4448 100644 --- a/src/main/java/com/oneandone/go/plugin/maven/client/RepositoryConnector.java +++ b/src/main/java/com/oneandone/go/plugin/maven/client/RepositoryConnector.java @@ -14,6 +14,8 @@ import org.apache.http.client.CredentialsProvider; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpHead; +import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.conn.params.ConnRoutePNames; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.DefaultHttpRequestRetryHandler; @@ -22,6 +24,9 @@ import org.apache.http.params.CoreConnectionPNames; import org.apache.http.util.EntityUtils; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; @@ -85,8 +90,9 @@ public RepositoryResponse doHttpRequest(final String url) { String responseBody; HttpGet method = null; try { - method = createGetMethod(url); - HttpResponse response = client.execute(method); + method = new HttpGet(url); + method.setHeader("Accept", "application/xml"); + HttpResponse response = getResponse(client, method); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new RuntimeException(String.format("HTTP %s, %s", response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } @@ -104,22 +110,6 @@ public RepositoryResponse doHttpRequest(final String url) { } } - /** - * Creates a HTTP {@code GET} on the specified url. - * - * @param url the URL - * @return the HTTP {@code GET} operation - */ - private HttpGet createGetMethod(final String url) { - final HttpGet method = new HttpGet(url); - method.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10 * 1000); - method.setHeader("Accept", "application/xml"); - if (repoConfig.getProxy() != null) { - method.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, HttpHost.create(repoConfig.getProxy())); - } - return method; - } - /** * Returns a new HTTP client by the specified repository configuration. * @@ -150,24 +140,75 @@ public boolean testConnection() { boolean result = false; final HttpClient client = createHttpClient(); - HttpGet method = null; + HttpRequestBase headRequest = null; + HttpRequestBase getRequest = null; try { - method = createGetMethod(url); - - final HttpResponse response = client.execute(method); + headRequest = new HttpHead(url); + headRequest.setHeader("Accept", "*/*"); + HttpResponse response = getResponse(client, headRequest); result = (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK); + + if (!result) { + LOGGER.warn("http HEAD failed for repository '" + url + "' will proceed with GET request"); + getRequest = new HttpGet(url); + getRequest.setHeader("Accept", "*/*"); + response = getResponse(client, getRequest); + result = (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK); + } + + if (!result) { + final StringBuilder builder = new StringBuilder(); + if (response.getEntity() != null) { + try (final BufferedReader bReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))) { + String line; + while ((line = bReader.readLine()) != null) { + builder.append(line); + } + } + } + + if (builder.length() == 0) { + LOGGER.error(String.format("expected HTTP status 200 but got %d on check of url '%s'", response.getStatusLine().getStatusCode(), url)); + } else { + LOGGER.error(String.format( + "expected HTTP status 200 but got %d on check of url '%s', with entity: %s", + response.getStatusLine().getStatusCode(), + url, + builder.toString() + )); + } + } } catch (final Exception e) { final String message = String.format("Exception while connecting to %s\n%s", url, e.getMessage()); LOGGER.error(message); throw new RuntimeException(message, e); } finally { - if (method != null) { - method.releaseConnection(); + if (headRequest != null) { + headRequest.releaseConnection(); + } + if (getRequest != null) { + getRequest.releaseConnection(); } } return result; } + /** + * Set socket timeout and set optional proxy by repository configuration and return the response of the specified request base. + * + * @param client the http client + * @param method the request + * @return the response + * @throws IOException thrown in case the connection was aborted + */ + private HttpResponse getResponse(final HttpClient client, final HttpRequestBase method) throws IOException { + method.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10 * 1000); + if (repoConfig.getProxy() != null) { + method.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, HttpHost.create(repoConfig.getProxy())); + } + return client.execute(method); + } + /** * Constructs and executes a snapshot version request. *