X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=restapi-call-node%2Fprovider%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fccsdk%2Fsli%2Fplugins%2Frestapicall%2FRestapiCallNode.java;h=a7235fcb53e20884c929767780da67f2792cac9f;hb=e4c9cfddd142785d527f29ff5c2b5883cc255033;hp=83b12c767f60c163f4b11e0a49953e84dca8a00f;hpb=565e49329424de19e35a142e54d1ee7f5c7c57ef;p=ccsdk%2Fsli%2Fplugins.git diff --git a/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java b/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java index 83b12c76..a7235fcb 100644 --- a/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java +++ b/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java @@ -26,6 +26,7 @@ import static java.lang.Boolean.valueOf; import static javax.ws.rs.client.Entity.entity; import static org.onap.ccsdk.sli.plugins.restapicall.AuthType.fromString; +import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.SocketException; @@ -51,16 +52,17 @@ import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.client.Invocation; import javax.ws.rs.client.WebTarget; -import javax.ws.rs.core.EntityTag; -import javax.ws.rs.core.Feature; -import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriBuilder; +import javax.ws.rs.core.*; + import org.apache.commons.lang3.StringUtils; import org.glassfish.jersey.client.ClientProperties; +import org.glassfish.jersey.client.HttpUrlConnectorProvider; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.glassfish.jersey.client.oauth1.ConsumerCredentials; import org.glassfish.jersey.client.oauth1.OAuth1ClientSupport; +import org.glassfish.jersey.media.multipart.MultiPart; +import org.glassfish.jersey.media.multipart.MultiPartFeature; +import org.glassfish.jersey.media.multipart.file.FileDataBodyPart; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin; @@ -165,6 +167,12 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { false, null)); p.returnRequestPayload = valueOf(parseParam( paramMap, "returnRequestPayload", false, null)); + p.accept = parseParam(paramMap, "accept", + false, null); + p.multipartFormData = valueOf(parseParam(paramMap, "multipartFormData", + false, "false")); + p.multipartFile = parseParam(paramMap, "multipartFile", + false, null); return p; } @@ -620,6 +628,8 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { .build(); } client.property(ClientProperties.CONNECT_TIMEOUT, 5000); + // Needed to support additional HTTP methods such as PATCH + client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true); WebTarget webTarget = addAuthType(client, p).target(p.restapiUrl); @@ -630,15 +640,19 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { HttpResponse r = new HttpResponse(); r.code = 200; - if (!p.skipSending) { - String tt = p.format == Format.XML ? "application/xml" : "application/json"; - String tt1 = tt + ";charset=UTF-8"; - if (p.contentType != null) { - tt = p.contentType; - tt1 = p.contentType; - } + String accept = p.accept; + if(accept == null) { + accept = p.format == Format.XML ? "application/xml" : "application/json"; + } - Invocation.Builder invocationBuilder = webTarget.request(tt1).accept(tt); + String contentType = p.contentType; + if(contentType == null) { + contentType = accept + ";charset=UTF-8"; + } + + if (!p.skipSending && !p.multipartFormData) { + + Invocation.Builder invocationBuilder = webTarget.request(contentType).accept(accept); if (p.format == Format.NONE) { invocationBuilder.header("", ""); @@ -658,7 +672,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { Response response; try { - response = invocationBuilder.method(p.httpMethod.toString(), entity(request, tt1)); + response = invocationBuilder.method(p.httpMethod.toString(), entity(request, contentType)); } catch (ProcessingException | IllegalStateException e) { throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e); @@ -673,6 +687,55 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { if (response.hasEntity() && r.code != 204) { r.body = response.readEntity(String.class); } + } else if (!p.skipSending && p.multipartFormData) { + + WebTarget wt = client.register(MultiPartFeature.class).target(p.restapiUrl); + + MultiPart multiPart = new MultiPart(); + multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); + + FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file", + new File(p.multipartFile), + MediaType.APPLICATION_OCTET_STREAM_TYPE); + multiPart.bodyPart(fileDataBodyPart); + + + Invocation.Builder invocationBuilder = wt.request(contentType).accept(accept); + + if (p.format == Format.NONE) { + invocationBuilder.header("", ""); + } + + if (p.customHttpHeaders != null && p.customHttpHeaders.length() > 0) { + String[] keyValuePairs = p.customHttpHeaders.split(","); + for (String singlePair : keyValuePairs) { + int equalPosition = singlePair.indexOf('='); + invocationBuilder.header(singlePair.substring(0, equalPosition), + singlePair.substring(equalPosition + 1, singlePair.length())); + } + } + + invocationBuilder.header("X-ECOMP-RequestID", org.slf4j.MDC.get("X-ECOMP-RequestID")); + + Response response; + + try { + response = invocationBuilder.method(p.httpMethod.toString(), entity(multiPart, multiPart.getMediaType())); + } catch (ProcessingException | IllegalStateException e) { + throw new SvcLogicException(requestPostingException + + e.getLocalizedMessage(), e); + } + + r.code = response.getStatus(); + r.headers = response.getStringHeaders(); + EntityTag etag = response.getEntityTag(); + if (etag != null) { + r.message = etag.getValue(); + } + if (response.hasEntity() && r.code != 204) { + r.body = response.readEntity(String.class); + } + } long t2 = System.currentTimeMillis(); @@ -851,7 +914,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { log.info("Got response code 301. Sending same request to URL: {}", newUrl); - webTarget = client.target(newUrl); + webTarget = client.target(newUrl); invocationBuilder = webTarget.request(tt).accept(tt); try { @@ -1012,4 +1075,4 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { public String responsePrefix; public boolean skipSending; } -} \ No newline at end of file +}