Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Artifactory cloud issue (#12)
Browse files Browse the repository at this point in the history
Fixed connection test
  • Loading branch information
bitionaire authored Aug 24, 2016
1 parent 9801b60 commit 08afe52
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 26 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.oneandone</groupId>
<artifactId>go-maven-poller</artifactId>
<version>1.1.3</version>
<version>1.1.4</version>

<url>https://github.com/1and1/go-maven-poller</url>

Expand Down Expand Up @@ -37,7 +37,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.8</version>
<version>1.16.10</version>
<scope>provided</scope>
</dependency>

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

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

0 comments on commit 08afe52

Please sign in to comment.