From ed8fc543198ec2a878f7e4e2966fa66a6c4986a9 Mon Sep 17 00:00:00 2001 From: pwielebs Date: Thu, 12 Apr 2018 10:34:26 +0200 Subject: http patch request added Change-Id: I9b3e339008d790b253e2b94e4b307b9a86907988 Issue-ID: DCAEGEN2-407 Signed-off-by: pwielebs --- .../service/AAIExtendedHttpClientImpl.java | 33 ++++++++++++++++++++++ .../services/utils/HttpRequestDetails.java | 4 +++ .../onap/dcaegen2/services/utils/RequestVerbs.java | 3 +- 3 files changed, 39 insertions(+), 1 deletion(-) (limited to 'prh-aai-client/src/main') diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java index 133a537e..048d9fef 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java @@ -23,9 +23,11 @@ import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPatch; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.utils.URIBuilder; +import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.util.EntityUtils; import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; @@ -38,6 +40,7 @@ import org.springframework.beans.factory.annotation.Autowired; import javax.annotation.Nonnull; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; import java.util.Iterator; @@ -151,6 +154,9 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { return new HttpGet(extendedURI); } else if (isExtendedURINotNull(extendedURI) && (httpRequestDetails.requestVerb().equals(RequestVerbs.PUT))) { return new HttpPut(extendedURI); + } else if (isExtendedURINotNull(extendedURI) && + isPatchRequestValid(httpRequestDetails.requestVerb(),httpRequestDetails.jsonBody())) { + return createHttpPatch(extendedURI, httpRequestDetails.jsonBody()); } else { return null; } @@ -159,4 +165,31 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { private Boolean isExtendedURINotNull(URI extendedURI) { return extendedURI != null ? true : false; } + + private Optional createStringEntity(Optional jsonBody) { + return Optional.of(parseJson(jsonBody).get()); + } + + private HttpPatch createHttpPatch(URI extendedURI, Optional jsonBody) { + HttpPatch httpPatch = new HttpPatch(extendedURI); + Optional stringEntity = createStringEntity(jsonBody); + httpPatch.setEntity(stringEntity.get()); + return httpPatch; + } + + private Optional parseJson(Optional jsonBody) { + Optional stringEntity = Optional.empty(); + + try { + stringEntity = Optional.of(new StringEntity(jsonBody.get())); + } catch (UnsupportedEncodingException e) { + logger.error("Exception while parsing JSON: {}", e); + } + + return stringEntity; + } + + private Boolean isPatchRequestValid(RequestVerbs requestVerb, Optional jsonBody) { + return requestVerb == RequestVerbs.PATCH && jsonBody.isPresent(); + } } diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java index 896e3068..574cb22f 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java @@ -24,6 +24,7 @@ import org.immutables.value.Value; import java.io.Serializable; import java.util.Map; +import java.util.Optional; @Value.Immutable(prehash = true) @Value.Style(stagedBuilder = true) @@ -34,6 +35,9 @@ public abstract class HttpRequestDetails implements Serializable { @Value.Parameter public abstract String aaiAPIPath(); + @Value.Parameter + public abstract Optional jsonBody(); + @Value.Parameter public abstract Map queryParameters(); diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/RequestVerbs.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/RequestVerbs.java index f350aa48..9b331de0 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/RequestVerbs.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/RequestVerbs.java @@ -22,7 +22,8 @@ package org.onap.dcaegen2.services.utils; public enum RequestVerbs { GET, - PUT; + PUT, + PATCH; private RequestVerbs() {}; } -- cgit 1.2.3-korg