X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Futil%2FHttpConnectionManager.java;h=4e97f29b735f3088c3764161b5664decbecfc4d9;hb=a6d09fbe1046057b72247f97ac72a521949409ce;hp=9443301b1ce1868a21c36096f5dae3ae0b2806c8;hpb=c62d1b74275b8d5e9d4cef6c308ef16440cfa500;p=clamp.git diff --git a/src/main/java/org/onap/clamp/util/HttpConnectionManager.java b/src/main/java/org/onap/clamp/util/HttpConnectionManager.java index 9443301b..4e97f29b 100644 --- a/src/main/java/org/onap/clamp/util/HttpConnectionManager.java +++ b/src/main/java/org/onap/clamp/util/HttpConnectionManager.java @@ -27,17 +27,22 @@ package org.onap.clamp.util; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; +import sun.misc.BASE64Encoder; + import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; +import java.security.GeneralSecurityException; import javax.net.ssl.HttpsURLConnection; import javax.ws.rs.BadRequestException; +import org.apache.commons.codec.DecoderException; import org.apache.commons.io.IOUtils; +import org.onap.clamp.clds.util.CryptoUtils; import org.onap.clamp.clds.util.LoggingUtils; import org.springframework.stereotype.Component; @@ -50,13 +55,15 @@ public class HttpConnectionManager { protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); private static final String REQUEST_FAILED_LOG = "Request Failed - response payload="; - private String doHttpsQuery(URL url, String requestMethod, String payload, String contentType, String target) throws IOException { + private String doHttpsQuery(URL url, String requestMethod, String payload, String contentType, String target, String userName, String password) throws IOException { LoggingUtils utils = new LoggingUtils(logger); logger.info("Using HTTPS URL:" + url.toString()); HttpsURLConnection secureConnection = (HttpsURLConnection) url.openConnection(); secureConnection = utils.invokeHttps(secureConnection, target, requestMethod); secureConnection.setRequestMethod(requestMethod); - secureConnection.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId()); + if (userName != null && password != null) { + secureConnection.setRequestProperty("Authorization", "Basic " + generateBasicAuth(userName, password)); + } if (payload != null && contentType != null) { secureConnection.setRequestProperty("Content-Type", contentType); secureConnection.setDoOutput(true); @@ -84,12 +91,15 @@ public class HttpConnectionManager { } } - private String doHttpQuery(URL url, String requestMethod, String payload, String contentType, String target) throws IOException { + private String doHttpQuery(URL url, String requestMethod, String payload, String contentType, String target, String userName, String password) throws IOException { LoggingUtils utils = new LoggingUtils(logger); logger.info("Using HTTP URL:" + url); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection = utils.invoke(connection, target, requestMethod); connection.setRequestMethod(requestMethod); + if (userName != null && password != null) { + connection.setRequestProperty("Authorization", "Basic " + generateBasicAuth(userName, password)); + } if (payload != null && contentType != null) { connection.setRequestProperty("Content-Type", contentType); connection.setDoOutput(true); @@ -134,13 +144,27 @@ public class HttpConnectionManager { * @throws IOException * In case of issue with the streams */ - public String doGeneralHttpQuery(String url, String requestMethod, String payload, String contentType, String target) + public String doGeneralHttpQuery(String url, String requestMethod, String payload, String contentType, String target, String userName, String password) throws IOException { URL urlObj = new URL(url); if (url.contains("https://")) { // Support for HTTPS - return doHttpsQuery(urlObj, requestMethod, payload, contentType, target); + return doHttpsQuery(urlObj, requestMethod, payload, contentType, target, userName, password); } else { // Support for HTTP - return doHttpQuery(urlObj, requestMethod, payload, contentType, target); + return doHttpQuery(urlObj, requestMethod, payload, contentType, target, userName, password); + } + } + + private String generateBasicAuth(String userName, String encodedPassword) { + String password = ""; + try { + password = CryptoUtils.decrypt(encodedPassword); + } catch (GeneralSecurityException e) { + logger.error("Unable to decrypt the password", e); + } catch (DecoderException e) { + logger.error("Exception caught when decoding the HEX String Key for encryption", e); } + BASE64Encoder enc = new sun.misc.BASE64Encoder(); + String userpassword = userName + ":" + password; + return enc.encode( userpassword.getBytes() ); } }