package com.amazonwebservices.blogs.containers.sigv4.util; import java.io.BufferedReader; import java.io.DataOutputStream; 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.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; /** * Various Http helper routines */ public class HttpUtils { private static final Logger logger = LogManager.getLogger(HttpUtils.class); /** * Makes a http request to the specified endpoint */ public static String invokeHttpRequest(URL endpointUrl, String httpMethod, Map headers, String requestBody) { HttpURLConnection connection = createHttpConnection(endpointUrl, httpMethod, headers); try { if ( requestBody != null ) { DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(requestBody); wr.flush(); wr.close(); } } catch (Exception e) { throw new RuntimeException("Request failed. " + e.getMessage(), e); } return executeHttpRequest(connection); } public static String executeHttpRequest(HttpURLConnection connection) { try { // Get Response InputStream is; try { is = connection.getInputStream(); } catch (IOException e) { is = connection.getErrorStream(); } BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; StringBuffer response = new StringBuffer(); while ((line = rd.readLine()) != null) { response.append(line); response.append('\r'); } rd.close(); return response.toString(); } catch (Exception e) { throw new RuntimeException("Request failed. " + e.getMessage(), e); } finally { if (connection != null) { connection.disconnect(); } } } public static HttpURLConnection createHttpConnection(URL endpointUrl, String httpMethod, Map headers) { try { HttpURLConnection connection = (HttpURLConnection) endpointUrl.openConnection(); connection.setRequestMethod(httpMethod); if ( headers != null ) { logger.info("Request headers"); for ( String headerKey : headers.keySet() ) { logger.info(headerKey + ": " + headers.get(headerKey)); connection.setRequestProperty(headerKey, headers.get(headerKey)); } } connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); return connection; } catch (Exception e) { throw new RuntimeException("Cannot create connection. " + e.getMessage(), e); } } public static String urlEncode(String url, boolean keepPathSlash) { String encoded; try { encoded = URLEncoder.encode(url, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException("UTF-8 encoding is not supported.", e); } if ( keepPathSlash ) { encoded = encoded.replace("%2F", "/"); } return encoded; } }