Added HTTP client utils
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-api@144232 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
614b4bdf4f
commit
4494c364cc
5
pom.xml
5
pom.xml
|
@ -51,6 +51,11 @@
|
||||||
<artifactId>information-system-model</artifactId>
|
<artifactId>information-system-model</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.gcube.common</groupId>
|
||||||
|
<artifactId>authorization-client</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
<artifactId>slf4j-api</artifactId>
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
|
|
@ -0,0 +1,198 @@
|
||||||
|
package org.gcube.informationsystem.resourceregistry.api.rest.httputils;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||||
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
|
import org.gcube.informationsystem.impl.utils.ISMapper;
|
||||||
|
import org.gcube.informationsystem.model.ISManageable;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ExceptionMapper;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class HTTPInputs<C> {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory
|
||||||
|
.getLogger(HTTPInputs.class);
|
||||||
|
|
||||||
|
public enum HTTPMETHOD {
|
||||||
|
GET, POST, PUT, DELETE;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.name();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final String PARAM_STARTER = "?";
|
||||||
|
public static final String PARAM_EQUALS = "=";
|
||||||
|
public static final String PARAM_SEPARATOR = "&";
|
||||||
|
public static final String UTF8 = "UTF-8";
|
||||||
|
|
||||||
|
protected final String path;
|
||||||
|
protected final HTTPMETHOD method;
|
||||||
|
protected final String urlParameters;
|
||||||
|
|
||||||
|
protected String getParametersDataString(
|
||||||
|
Map<String, String> parameters)
|
||||||
|
throws UnsupportedEncodingException {
|
||||||
|
|
||||||
|
if (parameters == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
boolean first = true;
|
||||||
|
for (String key : parameters.keySet()) {
|
||||||
|
if (first) {
|
||||||
|
first = false;
|
||||||
|
} else {
|
||||||
|
result.append(PARAM_SEPARATOR);
|
||||||
|
}
|
||||||
|
|
||||||
|
result.append(URLEncoder.encode(key, UTF8));
|
||||||
|
result.append(PARAM_EQUALS);
|
||||||
|
result.append(URLEncoder.encode(parameters.get(key), UTF8));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param path
|
||||||
|
* @param method
|
||||||
|
* @param requestProperties
|
||||||
|
* @throws UnsupportedEncodingException
|
||||||
|
*/
|
||||||
|
public HTTPInputs(String path, HTTPMETHOD method,
|
||||||
|
Map<String, String> parameters)
|
||||||
|
throws UnsupportedEncodingException {
|
||||||
|
super();
|
||||||
|
this.path = path;
|
||||||
|
this.method = method;
|
||||||
|
this.urlParameters = getParametersDataString(parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the path
|
||||||
|
*/
|
||||||
|
public String getPath() {
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the method
|
||||||
|
*/
|
||||||
|
public HTTPMETHOD getMethod() {
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the urlParameters
|
||||||
|
*/
|
||||||
|
public String getUrlParameters() {
|
||||||
|
return urlParameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected HttpURLConnection getConnection(URL url, String userAgent)
|
||||||
|
throws Exception {
|
||||||
|
|
||||||
|
url = new URL(url + "?" + urlParameters);
|
||||||
|
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url
|
||||||
|
.openConnection();
|
||||||
|
|
||||||
|
if (SecurityTokenProvider.instance.get() == null) {
|
||||||
|
if (ScopeProvider.instance.get() == null) {
|
||||||
|
throw new RuntimeException(
|
||||||
|
"Null Token and Scope. Please set your token first.");
|
||||||
|
}
|
||||||
|
connection.setRequestProperty("gcube-scope",
|
||||||
|
ScopeProvider.instance.get());
|
||||||
|
} else {
|
||||||
|
connection.setRequestProperty(org.gcube.common.authorization.client.Constants.TOKEN_HEADER_ENTRY,
|
||||||
|
SecurityTokenProvider.instance.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
|
||||||
|
connection.setRequestProperty("Content-type", "application/json");
|
||||||
|
connection.setRequestProperty("User-Agent", userAgent);
|
||||||
|
|
||||||
|
connection.setRequestMethod(method.toString());
|
||||||
|
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected StringBuilder getStringBuilder(InputStream inputStream) throws IOException{
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
try (BufferedReader reader = new BufferedReader(
|
||||||
|
new InputStreamReader(inputStream))) {
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
result.append(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public C call(Class<C> clz, URL url, String userAgent) throws Exception {
|
||||||
|
HttpURLConnection connection = getConnection(url, userAgent);
|
||||||
|
|
||||||
|
String responseMessage = connection.getResponseMessage();
|
||||||
|
int responseCode = connection.getResponseCode();
|
||||||
|
|
||||||
|
if (responseCode != HttpURLConnection.HTTP_OK) {
|
||||||
|
|
||||||
|
logger.error("Response code for {} is {} : {}",
|
||||||
|
connection.getURL(), responseCode,
|
||||||
|
responseMessage);
|
||||||
|
|
||||||
|
StringBuilder result = getStringBuilder(connection.getErrorStream());
|
||||||
|
String res = result.toString();
|
||||||
|
|
||||||
|
ResourceRegistryException rre = null;
|
||||||
|
try {
|
||||||
|
rre = ExceptionMapper.unmarshal(ResourceRegistryException.class, res);
|
||||||
|
}catch (Exception e) {
|
||||||
|
rre = new ResourceRegistryException(responseMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw rre;
|
||||||
|
|
||||||
|
}else{
|
||||||
|
logger.debug("Response code for {} is {} : {}",
|
||||||
|
connection.getURL(), responseCode,
|
||||||
|
responseMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder result = getStringBuilder(connection.getInputStream());
|
||||||
|
|
||||||
|
String res = result.toString();
|
||||||
|
logger.trace("Server returned content : {}", res);
|
||||||
|
|
||||||
|
if(Boolean.class.isAssignableFrom(clz)){
|
||||||
|
return (C) ((Boolean) Boolean.valueOf(res)) ;
|
||||||
|
}else if(ISManageable.class.isAssignableFrom(clz)){
|
||||||
|
return (C) ISMapper.unmarshal((Class<ISManageable>) clz, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (C) res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue