* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights
* reserved.
+ * Modifications Copyright © 2018 IBM.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
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;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.Feature;
+import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
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;
private String uebServers;
private String defaultUebTemplateFileName = "/opt/bvc/restapi/templates/default-ueb-message.json";
+ private String responseReceivedMessage = "Response received. Time: {}";
+ private String responseHttpCodeMessage = "HTTP response code: {}";
+ private String requestPostingException = "Exception while posting http request to client ";
+ private static String skipSendingMessage = "skipSending";
+ private static String responsePrefix = "responsePrefix";
+ private static String restapiUrlString = "restapiUrl";
+
public RestapiCallNode() {
String configDir = System.getProperty(PROPERTIES_DIR_KEY, DEFAULT_PROPERTIES_DIR);
try (FileInputStream in = new FileInputStream(configDir + "/" + DME2_PROPERTIES_FILE_NAME)) {
Properties props = new Properties();
props.load(in);
- this.retryPolicyStore = new RetryPolicyStore();
- this.retryPolicyStore.setProxyServers(props.getProperty("proxyUrl"));
+ retryPolicyStore = new RetryPolicyStore();
+ retryPolicyStore.setProxyServers(props.getProperty("proxyUrl"));
log.info("DME2 support enabled");
} catch (Exception e) {
log.warn("DME2 properties could not be read, DME2 support will not be enabled.", e);
try (FileInputStream in = new FileInputStream(configDir + "/" + UEB_PROPERTIES_FILE_NAME)) {
Properties props = new Properties();
props.load(in);
- this.uebServers = props.getProperty("servers");
+ uebServers = props.getProperty("servers");
log.info("UEB support enabled");
} catch (Exception e) {
log.warn("UEB properties could not be read, UEB support will not be enabled.", e);
p.templateFileName = parseParam(paramMap, "templateFileName",
false, null);
p.requestBody = parseParam(paramMap, "requestBody", false, null);
- p.restapiUrl = parseParam(paramMap, "restapiUrl", true, null);
+ p.restapiUrl = parseParam(paramMap, restapiUrlString, true, null);
validateUrl(p.restapiUrl);
p.restapiUser = parseParam(paramMap, "restapiUser", false, null);
p.restapiPassword = parseParam(paramMap, "restapiPassword", false,
"unspecified"));
p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod",
false, "post"));
- p.responsePrefix = parseParam(paramMap, "responsePrefix", false, null);
+ p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
p.listNameList = getListNameList(paramMap);
- String skipSendingStr = paramMap.get("skipSending");
+ String skipSendingStr = paramMap.get(skipSendingMessage);
p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
p.convertResponse = valueOf(parseParam(paramMap, "convertResponse",
false, "true"));
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;
}
}
log.error("Error sending the request: " + e.getMessage(), e);
- String prefix = parseParam(paramMap, "responsePrefix", false, null);
- if (retryPolicy == null || shouldRetry == false) {
+ String prefix = parseParam(paramMap, responsePrefix, false, null);
+ if (retryPolicy == null || !shouldRetry) {
setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
} else {
if (retryCount == null) {
try {
retryCount = retryCount + 1;
if (retryCount < retryPolicy.getMaximumRetries() + 1) {
- URI uri = new URI(paramMap.get("restapiUrl"));
+ URI uri = new URI(paramMap.get(restapiUrlString));
String hostname = uri.getHost();
String retryString = retryPolicy.getNextHostName(uri.toString());
URI uriTwo = new URI(retryString);
URI retryUri = UriBuilder.fromUri(uri).host(uriTwo.getHost()).port(uriTwo.getPort()).scheme(
uriTwo.getScheme()).build();
- paramMap.put("restapiUrl", retryUri.toString());
+ paramMap.put(restapiUrlString, retryUri.toString());
log.debug("URL was set to {}", retryUri.toString());
log.debug("Failed to communicate with host {}. Request will be re-attempted using the host {}.",
hostname, retryString);
throws SvcLogicException {
log.info("Building {} started", format);
long t1 = System.currentTimeMillis();
+ String originalTemplate = template;
template = expandRepeats(ctx, template, 1);
String var1 = template.substring(i1 + 2, i2);
String value1 = format == Format.XML ? XmlJsonUtil.getXml(mm, var1) : XmlJsonUtil.getJson(mm, var1);
- // log.info(" " + var1 + ": " + value1);
if (value1 == null || value1.trim().length() == 0) {
// delete the whole element (line)
int i3 = template.lastIndexOf('\n', i1);
}
String req = format == Format.XML
- ? XmlJsonUtil.removeEmptyStructXml(ss.toString()) : XmlJsonUtil.removeEmptyStructJson(ss.toString());
+ ? XmlJsonUtil.removeEmptyStructXml(ss.toString()) : XmlJsonUtil.removeEmptyStructJson(originalTemplate, ss.toString());
if (format == Format.JSON) {
req = XmlJsonUtil.removeLastCommaJson(req);
}
long t2 = System.currentTimeMillis();
- log.info("Building {} completed. Time: {}", format, (t2 - t1));
+ log.info("Building {} completed. Time: {}", format, t2 - t1);
return req;
}
return addAuthType(c, p);
}
- protected Client addAuthType(Client client, Parameters p) throws SvcLogicException {
+ public Client addAuthType(Client client, Parameters p) throws SvcLogicException {
if (p.authtype == AuthType.Unspecified) {
if (p.restapiUser != null && p.restapiPassword != null) {
client.register(HttpAuthenticationFeature.basic(p.restapiUser, p.restapiPassword));
.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);
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("", "");
invocationBuilder.header("X-ECOMP-RequestID", org.slf4j.MDC.get("X-ECOMP-RequestID"));
+ invocationBuilder.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
+
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("Exception while posting http request to client " +
+ throw new SvcLogicException(requestPostingException +
e.getLocalizedMessage(), e);
}
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();
- log.info("Response received. Time: {}", (t2 - t1));
- log.info("HTTP response code: {}", r.code);
+ log.info(responseReceivedMessage, t2 - t1);
+ log.info(responseHttpCodeMessage, r.code);
log.info("HTTP response message: {}", r.message);
logHeaders(r.headers);
log.info("HTTP response: {}", r.body);
r = new HttpResponse();
r.code = 500;
r.message = e.getMessage();
- String prefix = parseParam(paramMap, "responsePrefix", false, null);
+ String prefix = parseParam(paramMap, responsePrefix, false, null);
setResponseStatus(ctx, prefix, r);
}
p.user = parseParam(paramMap, "user", false, null);
p.password = parseParam(paramMap, "password", false, null);
p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod", false, "post"));
- p.responsePrefix = parseParam(paramMap, "responsePrefix", false, null);
- String skipSendingStr = paramMap.get("skipSending");
+ p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
+ String skipSendingStr = paramMap.get(skipSendingMessage);
p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
p.oAuthConsumerKey = parseParam(paramMap, "oAuthConsumerKey", false, null);
p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null);
r = new HttpResponse();
r.code = 500;
r.message = e.getMessage();
- String prefix = parseParam(paramMap, "responsePrefix", false, null);
+ String prefix = parseParam(paramMap, responsePrefix, false, null);
setResponseStatus(ctx, prefix, r);
}
throw new SvcLogicException("Http operation" + p.httpMethod + "not supported");
}
} catch (ProcessingException e) {
- throw new SvcLogicException("Exception while posting http request to client " +
+ throw new SvcLogicException(requestPostingException +
e.getLocalizedMessage(), e);
}
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 {
throw new SvcLogicException("Http operation" + p.httpMethod + "not supported");
}
} catch (ProcessingException e) {
- throw new SvcLogicException("Exception while posting http request to client " +
+ throw new SvcLogicException(requestPostingException +
e.getLocalizedMessage(), e);
}
}
long t2 = System.currentTimeMillis();
- log.info("Response received. Time: {}", (t2 - t1));
- log.info("HTTP response code: {}", r.code);
+ log.info(responseReceivedMessage, t2 - t1);
+ log.info(responseHttpCodeMessage, r.code);
log.info("HTTP response message: {}", r.message);
logHeaders(r.headers);
log.info("HTTP response: {}", r.body);
p.topic = parseParam(paramMap, "topic", true, null);
p.templateFileName = parseParam(paramMap, "templateFileName", false, null);
p.rootVarName = parseParam(paramMap, "rootVarName", false, null);
- p.responsePrefix = parseParam(paramMap, "responsePrefix", false, null);
- String skipSendingStr = paramMap.get("skipSending");
+ p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
+ String skipSendingStr = paramMap.get(skipSendingMessage);
p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
return p;
}
try {
response = invocationBuilder.post(Entity.entity(request, tt1));
} catch (ProcessingException e) {
- throw new SvcLogicException("Exception while posting http request to client " +
+ throw new SvcLogicException(requestPostingException +
e.getLocalizedMessage(), e);
}
r.code = response.getStatus();
}
long t2 = System.currentTimeMillis();
- log.info("Response received. Time: {}", (t2 - t1));
- log.info("HTTP response code: {}", r.code);
+ log.info(responseReceivedMessage, t2 - t1);
+ log.info(responseHttpCodeMessage, r.code);
logHeaders(r.headers);
log.info("HTTP response:\n {}", r.body);
public String responsePrefix;
public boolean skipSending;
}
-}
\ No newline at end of file
+}