diff options
author | Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> | 2021-01-29 10:43:17 +0100 |
---|---|---|
committer | Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> | 2021-02-02 09:32:13 +0100 |
commit | a8cafe1c784ae68b2a7cca313b362d14b13fd631 (patch) | |
tree | 9ed2ff5b311cd65fb079bccc8ebef7744c0afc05 /pnfsimulator/src/main/java/org/onap | |
parent | 6650865ef8836239d17fcbfcfb08871298787e80 (diff) |
add basic auth using username and password
Signed-off-by: Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com>
Change-Id: I67bc2c25149c8e2d4943f23cfa8d726cdb95995f
Issue-ID: INT-1805
Diffstat (limited to 'pnfsimulator/src/main/java/org/onap')
-rw-r--r-- | pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientAdapterImpl.java | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientAdapterImpl.java b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientAdapterImpl.java index 4f249b9..ba668fc 100644 --- a/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientAdapterImpl.java +++ b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientAdapterImpl.java @@ -20,6 +20,8 @@ package org.onap.pnfsimulator.simulator.client; +import org.apache.commons.codec.binary.Base64; +import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; @@ -35,6 +37,9 @@ import org.slf4j.MarkerFactory; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; import java.util.UUID; @@ -71,22 +76,27 @@ public class HttpClientAdapterImpl implements HttpClientAdapter { LOGGER.info(INVOKE, "Message sent, ves response code: {}", response.getStatusLine()); vesResponse = responseFactory.create(response); EntityUtils.consumeQuietly(response.getEntity()); //response has to be fully consumed otherwise apache won't release connection - } catch (IOException e) { + } catch (IOException | URISyntaxException e) { LOGGER.warn("Error sending message to ves: {}", e.getMessage(), e.getCause()); vesResponse = new HttpResponseAdapter(421, String.format("Fail to connect with ves: %s", e.getMessage())); } return vesResponse; } - private HttpResponse sendAndRetrieve(String content) throws IOException { + private HttpResponse sendAndRetrieve(String content) throws IOException, URISyntaxException { HttpPost request = createRequest(content); HttpResponse httpResponse = client.execute(request); request.releaseConnection(); return httpResponse; } - private HttpPost createRequest(String content) throws UnsupportedEncodingException { - HttpPost request = new HttpPost(this.targetUrl); + private HttpPost createRequest(String content) throws UnsupportedEncodingException, URISyntaxException { + LOGGER.info("sending request using address: {}", this.targetUrl); + URI targetAddress = new URI(this.targetUrl); + HttpPost request = new HttpPost(targetAddress); + if(urlContainsUserInfo(targetAddress)) { + request.addHeader(HttpHeaders.AUTHORIZATION, getAuthenticationHeaderForUser(targetAddress.getUserInfo())); + } StringEntity stringEntity = new StringEntity(content); request.addHeader(CONTENT_TYPE, APPLICATION_JSON); request.addHeader(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID)); @@ -95,4 +105,15 @@ public class HttpClientAdapterImpl implements HttpClientAdapter { return request; } + private boolean urlContainsUserInfo(URI targetAddress) { + return targetAddress.getUserInfo() != null && !targetAddress.getUserInfo().isEmpty(); + } + + private String getAuthenticationHeaderForUser(String userInfo) { + final byte[] encodedUserInfo = Base64.encodeBase64( + userInfo.getBytes(StandardCharsets.ISO_8859_1) + ); + return String.format("Basic %s", new String(encodedUserInfo)); + } + } |