From d5f3810292e6c85d123de7aa28fa0c860a21c183 Mon Sep 17 00:00:00 2001 From: emartin Date: Tue, 23 Apr 2019 14:49:01 +0000 Subject: Fix minor bugs in ves publisher * Publisher now uses existing RequestID * Introduce basicAuth for publisher * Clean up ves string by removing new lines * Update pm-mapper topic name in blueprint * Update reconfigure script to use https Change-Id: Ib743127b04077b063c73415b1475be83e200ec3b Issue-ID: DCAEGEN2-1444 Signed-off-by: emartin --- .../pmmapper/messagerouter/VESPublisher.java | 10 ++++++++- .../services/pmmapper/model/MapperConfig.java | 20 ++++++++++++----- .../services/pmmapper/utils/RequestSender.java | 26 ++++++++++++++++++---- 3 files changed, 45 insertions(+), 11 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/messagerouter/VESPublisher.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/messagerouter/VESPublisher.java index 77b0545..46d40e4 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/messagerouter/VESPublisher.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/messagerouter/VESPublisher.java @@ -20,6 +20,8 @@ package org.onap.dcaegen2.services.pmmapper.messagerouter; +import java.nio.charset.StandardCharsets; +import java.util.Base64; import java.util.List; import org.onap.dcaegen2.services.pmmapper.exceptions.MRPublisherException; @@ -61,7 +63,13 @@ public class VESPublisher { private void publish(String ves) { try { String topicUrl = config.getPublisherTopicUrl(); - sender.send("POST", topicUrl, ves); + ves = ves.replaceAll("\n", ""); + String userCredentials = topicUrl.startsWith("https") ? Base64.getEncoder() + .encodeToString((this.config.getPublisherUserName() + ":" + + this.config.getPublisherPassword()) + .getBytes(StandardCharsets.UTF_8)) + : ""; + sender.send("POST", topicUrl, ves, userCredentials); } catch (Exception e) { throw new MRPublisherException(e.getMessage(), e); } diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MapperConfig.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MapperConfig.java index 390fa0d..0630d53 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MapperConfig.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MapperConfig.java @@ -118,6 +118,12 @@ public class MapperConfig implements Configurable{ @GSONRequired @SerializedName("dmaap_info") DmaapInfo dmaapInfo; + + @SerializedName("aaf_username") + private String aafUsername; + + @SerializedName("aaf_password") + private String aafPassword; } @Getter @@ -133,12 +139,6 @@ public class MapperConfig implements Configurable{ @SerializedName("subscriber_id") private String subscriberId; - @SerializedName("aaf_username") - private String aafUsername; - - @SerializedName("aaf_password") - private String aafPassword; - @SerializedName("client_role") private String clientRole; @@ -149,6 +149,14 @@ public class MapperConfig implements Configurable{ private String topicUrl; } + public String getPublisherUserName() { + return this.getStreamsPublishes().getDmaapPublisher().getAafUsername(); + } + + public String getPublisherPassword() { + return this.getStreamsPublishes().getDmaapPublisher().getAafPassword(); + } + @Override public void reconfigure(MapperConfig mapperConfig) { if(!this.equals(mapperConfig)) { diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSender.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSender.java index fdbae59..c8e29f3 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSender.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSender.java @@ -27,6 +27,8 @@ import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.Optional; import java.util.UUID; import java.util.stream.Collectors; @@ -38,6 +40,7 @@ import org.slf4j.LoggerFactory; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; +import org.jboss.logging.MDC; public class RequestSender { private static final int MAX_RETRIES = 5; @@ -67,17 +70,28 @@ public class RequestSender { return send(method,urlString,""); } + /** + * Works just like {@link RequestSender#send(method,urlString,body, basicAuth)}, except {@code basicAuth } + * is set to empty String by default. + * @see RequestSender#send(String,String,String,String) + */ + public String send(String method, final String urlString, final String body) throws Exception { + return send(method,urlString,body,""); + } + /** * Sends an http request to a given endpoint. * @param method of the outbound request * @param urlString representing given endpoint * @param body of the request as json + * @param encodedCredentials base64-encoded username password credentials * @return http response body * @throws Exception */ - public String send(String method, final String urlString, final String body) throws Exception { + public String send(String method, final String urlString, final String body, final String encodedCredentials) throws Exception { final UUID invocationID = logger.invoke(ONAPLogConstants.InvocationMode.SYNCHRONOUS); - final UUID requestID = UUID.randomUUID(); + String requestID = Optional.ofNullable((String)MDC.get(ONAPLogConstants.MDCs.REQUEST_ID)) + .orElse( UUID.randomUUID().toString()); String result = ""; for (int i = 1; i <= MAX_RETRIES; i++) { @@ -89,6 +103,10 @@ public class RequestSender { HttpsURLConnection.setDefaultSSLSocketFactory(SSLContext.getDefault().getSocketFactory()); } + if(!encodedCredentials.isEmpty()) { + connection.setRequestProperty("Authorization", "Basic " + encodedCredentials); + } + if(!body.isEmpty()) { setMessageBody(connection, body); } @@ -116,10 +134,10 @@ public class RequestSender { return result; } - private HttpURLConnection getHttpURLConnection(String method, URL url, UUID invocationID, UUID requestID) throws IOException { + private HttpURLConnection getHttpURLConnection(String method, URL url, UUID invocationID, String requestID) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setReadTimeout(DEFAULT_READ_TIMEOUT); - connection.setRequestProperty(ONAPLogConstants.Headers.REQUEST_ID, requestID.toString()); + connection.setRequestProperty(ONAPLogConstants.Headers.REQUEST_ID, requestID); connection.setRequestProperty(ONAPLogConstants.Headers.INVOCATION_ID, invocationID.toString()); connection.setRequestProperty(ONAPLogConstants.Headers.PARTNER_NAME, MapperConfig.CLIENT_NAME); connection.setRequestMethod(method); -- cgit 1.2.3-korg