From 4494c364cc8efd98e0b43fe3cf7588a8457fd7da Mon Sep 17 00:00:00 2001 From: "luca.frosini" Date: Fri, 24 Feb 2017 09:44:57 +0000 Subject: [PATCH] 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 --- pom.xml | 5 + .../api/rest/httputils/HTTPInputs.java | 198 ++++++++++++++++++ 2 files changed, 203 insertions(+) create mode 100644 src/main/java/org/gcube/informationsystem/resourceregistry/api/rest/httputils/HTTPInputs.java diff --git a/pom.xml b/pom.xml index 0bb6b03..ec42c52 100644 --- a/pom.xml +++ b/pom.xml @@ -51,6 +51,11 @@ information-system-model + + org.gcube.common + authorization-client + + org.slf4j slf4j-api diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/api/rest/httputils/HTTPInputs.java b/src/main/java/org/gcube/informationsystem/resourceregistry/api/rest/httputils/HTTPInputs.java new file mode 100644 index 0000000..2125c05 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/api/rest/httputils/HTTPInputs.java @@ -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 { + + 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 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 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 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) clz, res); + } + + return (C) res; + } + + +}