package org.gcube.gcat.utils; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringWriter; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response.Status; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.scope.api.ScopeProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class HTTPCall { private static final Logger logger = LoggerFactory.getLogger(HTTPCall.class); protected static final String USER_AGENT_KEY = "User-Agent"; protected static final String USER_AGENT_NAME = Constants.CATALOGUE_NAME; public enum HTTPMETHOD { HEAD, GET, POST, PUT, DELETE; @Override public String toString() { return this.name(); } } public static final String PATH_SEPARATOR = "/"; 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 address; protected final String userAgent; protected Map headers; /** * When the target service is a gCube Service it adds the HTTP header * to provide gCube authorization token and/or scope */ protected boolean gCubeTargetService; public boolean isgCubeTargetService() { return gCubeTargetService; } public void setgCubeTargetService(boolean gCubeTargetService) { this.gCubeTargetService = gCubeTargetService; } public HTTPCall(String address) { this(address, HTTPCall.USER_AGENT_NAME); } protected HTTPCall(String address, String userAgent) { this.address = address; this.userAgent = userAgent; this.gCubeTargetService = true; this.headers = new HashMap<>(); addHeader(USER_AGENT_KEY, this.userAgent); } public void addHeader(String key, String value) { headers.put(key, value); } 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(); } protected URL getURL(String address, String path, String urlParameters) throws MalformedURLException { StringWriter stringWriter = new StringWriter(); stringWriter.append(address); if(address.endsWith(PATH_SEPARATOR)) { if(path.startsWith(PATH_SEPARATOR)) { path = path.substring(1); } } else { if(path.compareTo("") != 0 && !path.startsWith(PATH_SEPARATOR)) { stringWriter.append(PATH_SEPARATOR); } } stringWriter.append(path); if(urlParameters != null) { stringWriter.append(PARAM_STARTER); stringWriter.append(urlParameters); } 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, InputStream inputStream, String contentType) throws Exception { URL url = getURL(address, path, urlParameters); return getConnection(url, method, body, inputStream, contentType); } public URL getFinalURL(URL url) { try { URL finalURL = url; HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setInstanceFollowRedirects(false); int responseCode = connection.getResponseCode(); String responseMessage = connection.getResponseMessage(); if(responseCode >= Status.BAD_REQUEST.getStatusCode()) { Status status = Status.fromStatusCode(responseCode); throw new WebApplicationException(responseMessage, status); } if(responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_SEE_OTHER || responseCode == Status.TEMPORARY_REDIRECT.getStatusCode() || responseCode == 308) { finalURL = getURL(connection.getHeaderField("Location")); finalURL = getFinalURL(finalURL); } return finalURL; }catch (WebApplicationException e) { throw e; }catch (Exception e) { throw new InternalServerErrorException(e); } } protected HttpURLConnection getConnection(URL url, HTTPMETHOD method, String body, InputStream inputStream, String contentType) throws Exception { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); if(gCubeTargetService) { 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", contentType); for(String key : headers.keySet()) { connection.setRequestProperty(key, headers.get(key)); } connection.setRequestMethod(method.toString()); if(inputStream != null && (method == HTTPMETHOD.POST || method == HTTPMETHOD.PUT)) { DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); byte[] buffer = new byte[1024]; int len; while((len = inputStream.read(buffer)) > 0) { wr.write(buffer, 0, len); } wr.flush(); wr.close(); } if(body != null && (method == HTTPMETHOD.POST || method == HTTPMETHOD.PUT)) { DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(body); wr.flush(); wr.close(); } int responseCode = connection.getResponseCode(); String responseMessage = connection.getResponseMessage(); logger.trace("{} {} : {} - {}", method, connection.getURL(), responseCode, responseMessage); // 308 Permanent Redirect https://tools.ietf.org/html/rfc7538#section-3 if(responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_SEE_OTHER || responseCode == Status.TEMPORARY_REDIRECT.getStatusCode() || responseCode == 308) { URL redirectURL = getURL(connection.getHeaderField("Location")); logger.trace("{} is going to be redirect to {}", url.toString(), redirectURL.toString()); connection = getConnection(redirectURL, method, body, inputStream, contentType); } 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; } public String call(String path, HTTPMETHOD method, Map parameters, String contentType) { return call(path, method, parameters, null, contentType); } public String call(String path, HTTPMETHOD method, String body, String contentType) { return call(path, method, null, body, contentType); } public String call(String path, HTTPMETHOD method, Map parameters, String body, String contentType) { return call(path, method, body, null, parameters, contentType); } public String call(String path, HTTPMETHOD method, InputStream inputStream, Map parameters, String contentType) { return call(path, method, null, inputStream, parameters, contentType); } private String call(String path, HTTPMETHOD method, String body, InputStream inputStream, Map parameters, String contentType) { HttpURLConnection connection; try { String urlParameters = getParametersDataString(parameters); connection = getConnection(path, urlParameters, method, body, inputStream, contentType); int responseCode = connection.getResponseCode(); String responseMessage = connection.getResponseMessage(); logger.info("{} {} : {} - {}", method, connection.getURL(), responseCode, responseMessage); if(responseCode >= Status.BAD_REQUEST.getStatusCode()) { try { StringBuilder result = getStringBuilder(connection.getErrorStream()); String res = result.toString(); logger.trace("Server returned content : {}", res); return res; }catch (Exception e) { Status status = Status.fromStatusCode(responseCode); throw new WebApplicationException(responseMessage, status); } } StringBuilder result = getStringBuilder(connection.getInputStream()); String res = result.toString(); logger.trace("Server returned content : {}", res); connection.disconnect(); return res; } catch(WebApplicationException e) { throw e; } catch(Exception e) { throw new InternalServerErrorException(e); } } }