Resource Registry clients now follows redirects and support https. Fixes #8757

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-api@150484 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-06-22 15:25:14 +00:00
parent 40b1f7c1bb
commit 4e47079eaf
1 changed files with 36 additions and 6 deletions

View File

@ -89,7 +89,7 @@ public class HTTPCall {
return result.toString();
}
protected URL getURL(String path, String urlParameters) throws MalformedURLException {
protected URL getURL(String address, String path, String urlParameters) throws MalformedURLException {
StringWriter stringWriter = new StringWriter();
stringWriter.append(address);
@ -111,15 +111,26 @@ public class HTTPCall {
stringWriter.append(urlParameters);
}
return new URL(stringWriter.toString());
return getURL(stringWriter.toString());
}
protected URL getURL(String urlString) throws MalformedURLException{
URL url = new URL(urlString);
if(url.getProtocol().compareTo("https")==0){
url = new URL(url.getProtocol(), url.getHost(), url.getDefaultPort(), url.getFile());
}
return url;
}
protected HttpURLConnection getConnection(String path, String urlParameters, HTTPMETHOD method, String body)
throws Exception {
URL url = getURL(path, urlParameters);
URL url = getURL(address, path, urlParameters);
return getConnection(url, method, body);
}
protected HttpURLConnection getConnection(URL url, HTTPMETHOD method, String body) throws Exception {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if (SecurityTokenProvider.instance.get() == null) {
@ -141,6 +152,7 @@ public class HTTPCall {
connection.setRequestMethod(method.toString());
if (body != null
&& (method == HTTPMETHOD.POST || method == HTTPMETHOD.PUT)) {
@ -151,6 +163,23 @@ public class HTTPCall {
wr.close();
}
int responseCode = connection.getResponseCode();
String responseMessage = connection.getResponseMessage();
logger.trace("{} {} : {} - {}",
method, connection.getURL(), responseCode, responseMessage);
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP ||
responseCode == HttpURLConnection.HTTP_MOVED_PERM ||
responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
URL redirectURL = getURL(connection.getHeaderField("Location"));
logger.trace("{} is going to be redirect to {}", url.toString(), redirectURL.toString());
connection = getConnection(redirectURL, method, body);
}
return connection;
}
@ -216,6 +245,7 @@ public class HTTPCall {
String responseMessage = connection.getResponseMessage();
logger.info("{} {} : {} - {}",
method, connection.getURL(), responseCode, responseMessage);
if(method == HTTPMETHOD.HEAD){
if(responseCode == HttpURLConnection.HTTP_NO_CONTENT){
return null;