2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * Modifications Copyright © 2018 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.ccsdk.sli.plugins.restapicall;
25 import static java.lang.Boolean.valueOf;
26 import static javax.ws.rs.client.Entity.entity;
27 import static org.onap.ccsdk.sli.plugins.restapicall.AuthType.fromString;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.net.SocketException;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
35 import java.security.KeyStore;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.HashSet;
40 import java.util.Iterator;
41 import java.util.List;
43 import java.util.Map.Entry;
44 import java.util.Properties;
46 import javax.net.ssl.HttpsURLConnection;
47 import javax.net.ssl.KeyManagerFactory;
48 import javax.net.ssl.SSLContext;
49 import javax.ws.rs.ProcessingException;
50 import javax.ws.rs.client.Client;
51 import javax.ws.rs.client.ClientBuilder;
52 import javax.ws.rs.client.Entity;
53 import javax.ws.rs.client.Invocation;
54 import javax.ws.rs.client.WebTarget;
55 import javax.ws.rs.core.EntityTag;
56 import javax.ws.rs.core.Feature;
57 import javax.ws.rs.core.MediaType;
58 import javax.ws.rs.core.MultivaluedMap;
59 import javax.ws.rs.core.Response;
60 import org.apache.commons.lang3.StringUtils;
61 import org.codehaus.jettison.json.JSONException;
62 import org.codehaus.jettison.json.JSONObject;
63 import org.glassfish.jersey.client.ClientProperties;
64 import org.glassfish.jersey.client.HttpUrlConnectorProvider;
65 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
66 import org.glassfish.jersey.client.oauth1.ConsumerCredentials;
67 import org.glassfish.jersey.client.oauth1.OAuth1ClientSupport;
68 import org.glassfish.jersey.media.multipart.MultiPart;
69 import org.glassfish.jersey.media.multipart.MultiPartFeature;
70 import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
71 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
72 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
73 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
77 public class RestapiCallNode implements SvcLogicJavaPlugin {
79 protected static final String PARTNERS_FILE_NAME = "partners.json";
80 protected static final String UEB_PROPERTIES_FILE_NAME = "ueb.properties";
81 protected static final String DEFAULT_PROPERTIES_DIR = "/opt/onap/ccsdk/data/properties";
82 protected static final String PROPERTIES_DIR_KEY = "SDNC_CONFIG_DIR";
83 protected static final int DEFAULT_HTTP_CONNECT_TIMEOUT_MS = 30000; // 30 seconds
84 protected static final int DEFAULT_HTTP_READ_TIMEOUT_MS = 600000; // 10 minutes
86 private static final Logger log = LoggerFactory.getLogger(RestapiCallNode.class);
87 private String uebServers;
88 private String defaultUebTemplateFileName = "/opt/bvc/restapi/templates/default-ueb-message.json";
90 private String responseReceivedMessage = "Response received. Time: {}";
91 private String responseHttpCodeMessage = "HTTP response code: {}";
92 private String requestPostingException = "Exception while posting http request to client ";
93 protected static final String skipSendingMessage = "skipSending";
94 protected static final String responsePrefix = "responsePrefix";
95 protected static final String restapiUrlString = "restapiUrl";
96 protected static final String restapiUserKey = "restapiUser";
97 protected static final String restapiPasswordKey = "restapiPassword";
98 protected Integer httpConnectTimeout;
99 protected Integer httpReadTimeout;
101 protected HashMap<String, PartnerDetails> partnerStore;
103 public RestapiCallNode() {
104 String configDir = System.getProperty(PROPERTIES_DIR_KEY, DEFAULT_PROPERTIES_DIR);
106 String jsonString = readFile(configDir + "/" + PARTNERS_FILE_NAME);
107 JSONObject partners = new JSONObject(jsonString);
108 partnerStore = new HashMap<>();
109 loadPartners(partners);
110 log.info("Partners support enabled");
111 } catch (Exception e) {
112 log.warn("Partners file could not be read, Partner support will not be enabled.", e);
115 try (FileInputStream in = new FileInputStream(configDir + "/" + UEB_PROPERTIES_FILE_NAME)) {
116 Properties props = new Properties();
118 uebServers = props.getProperty("servers");
119 log.info("UEB support enabled");
120 } catch (Exception e) {
121 log.warn("UEB properties could not be read, UEB support will not be enabled.", e);
123 httpConnectTimeout = readOptionalInteger("HTTP_CONNECT_TIMEOUT_MS",DEFAULT_HTTP_CONNECT_TIMEOUT_MS);
124 httpReadTimeout = readOptionalInteger("HTTP_READ_TIMEOUT_MS",DEFAULT_HTTP_READ_TIMEOUT_MS);
127 protected void loadPartners(JSONObject partners) {
128 Iterator<String> keys = partners.keys();
129 String partnerUserKey = "user";
130 String partnerPasswordKey = "password";
131 String partnerUrlKey = "url";
133 while (keys.hasNext()) {
134 String partnerKey = keys.next();
136 JSONObject partnerObject = (JSONObject) partners.get(partnerKey);
137 if (partnerObject.has(partnerUserKey) && partnerObject.has(partnerPasswordKey)) {
139 if (partnerObject.has(partnerUrlKey)) {
140 url = partnerObject.getString(partnerUrlKey);
142 String userName = partnerObject.getString(partnerUserKey);
143 String password = partnerObject.getString(partnerPasswordKey);
144 PartnerDetails details = new PartnerDetails(userName, password, url);
145 partnerStore.put(partnerKey, details);
146 log.info("mapped partner using partner key " + partnerKey);
148 log.info("Partner " + partnerKey + " is missing required keys, it won't be mapped");
150 } catch (JSONException e) {
151 log.info("Couldn't map the partner using partner key " + partnerKey, e);
157 * Returns parameters from the parameter map.
159 * @param paramMap parameter map
160 * @param p parameters instance
161 * @return parameters filed instance
162 * @throws SvcLogicException when svc logic exception occurs
164 public static Parameters getParameters(Map<String, String> paramMap, Parameters p) throws SvcLogicException {
166 p.templateFileName = parseParam(paramMap, "templateFileName", false, null);
167 p.requestBody = parseParam(paramMap, "requestBody", false, null);
168 p.restapiUrl = parseParam(paramMap, restapiUrlString, true, null);
169 validateUrl(p.restapiUrl);
170 p.restapiUrlSuffix = parseParam(paramMap, "restapiUrlSuffix", false, null);
171 p.restapiUser = parseParam(paramMap, restapiUserKey, false, null);
172 p.restapiPassword = parseParam(paramMap, restapiPasswordKey, false, null);
173 if (p.restapiUrlSuffix != null) {
174 p.restapiUrl = p.restapiUrl + p.restapiUrlSuffix;
175 validateUrl(p.restapiUrl);
177 p.oAuthConsumerKey = parseParam(paramMap, "oAuthConsumerKey", false, null);
178 p.oAuthConsumerSecret = parseParam(paramMap, "oAuthConsumerSecret", false, null);
179 p.oAuthSignatureMethod = parseParam(paramMap, "oAuthSignatureMethod", false, null);
180 p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null);
181 p.contentType = parseParam(paramMap, "contentType", false, null);
182 p.format = Format.fromString(parseParam(paramMap, "format", false, "json"));
183 p.authtype = fromString(parseParam(paramMap, "authType", false, "unspecified"));
184 p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod", false, "post"));
185 p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
186 p.listNameList = getListNameList(paramMap);
187 String skipSendingStr = paramMap.get(skipSendingMessage);
188 p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
189 p.convertResponse = valueOf(parseParam(paramMap, "convertResponse", false, "true"));
190 p.trustStoreFileName = parseParam(paramMap, "trustStoreFileName", false, null);
191 p.trustStorePassword = parseParam(paramMap, "trustStorePassword", false, null);
192 p.keyStoreFileName = parseParam(paramMap, "keyStoreFileName", false, null);
193 p.keyStorePassword = parseParam(paramMap, "keyStorePassword", false, null);
194 p.ssl = p.trustStoreFileName != null && p.trustStorePassword != null && p.keyStoreFileName != null
195 && p.keyStorePassword != null;
196 p.customHttpHeaders = parseParam(paramMap, "customHttpHeaders", false, null);
197 p.partner = parseParam(paramMap, "partner", false, null);
198 p.dumpHeaders = valueOf(parseParam(paramMap, "dumpHeaders", false, null));
199 p.returnRequestPayload = valueOf(parseParam(paramMap, "returnRequestPayload", false, null));
200 p.accept = parseParam(paramMap, "accept", false, null);
201 p.multipartFormData = valueOf(parseParam(paramMap, "multipartFormData", false, "false"));
202 p.multipartFile = parseParam(paramMap, "multipartFile", false, null);
207 * Validates the given URL in the parameters.
209 * @param restapiUrl rest api URL
210 * @throws SvcLogicException when URL validation fails
212 private static void validateUrl(String restapiUrl) throws SvcLogicException {
213 if (restapiUrl.contains(",")) {
214 String[] urls = restapiUrl.split(",");
215 for (String url : urls) {
220 URI.create(restapiUrl);
221 } catch (IllegalArgumentException e) {
222 throw new SvcLogicException("Invalid input of url " + e.getLocalizedMessage(), e);
228 * Returns the list of list name.
230 * @param paramMap parameters map
231 * @return list of list name
233 private static Set<String> getListNameList(Map<String, String> paramMap) {
234 Set<String> ll = new HashSet<>();
235 for (Map.Entry<String, String> entry : paramMap.entrySet()) {
236 if (entry.getKey().startsWith("listName")) {
237 ll.add(entry.getValue());
244 * Parses the parameter string map of property, validates if required, assigns default value if
245 * present and returns the value.
247 * @param paramMap string param map
248 * @param name name of the property
249 * @param required if value required
250 * @param def default value
251 * @return value of the property
252 * @throws SvcLogicException if required parameter value is empty
254 public static String parseParam(Map<String, String> paramMap, String name, boolean required, String def)
255 throws SvcLogicException {
256 String s = paramMap.get(name);
258 if (s == null || s.trim().length() == 0) {
262 throw new SvcLogicException("Parameter " + name + " is required in RestapiCallNode");
266 StringBuilder value = new StringBuilder();
268 int i1 = s.indexOf('%');
270 int i2 = s.indexOf('%', i1 + 1);
275 String varName = s.substring(i1 + 1, i2);
276 String varValue = System.getenv(varName);
277 if (varValue == null) {
278 varValue = "%" + varName + "%";
281 value.append(s.substring(i, i1));
282 value.append(varValue);
285 i1 = s.indexOf('%', i);
287 value.append(s.substring(i));
289 log.info("Parameter {}: [{}]", name, maskPassword(name, value));
291 return value.toString();
294 private static Object maskPassword(String name, Object value) {
295 String[] pwdNames = {"pwd", "passwd", "password", "Pwd", "Passwd", "Password"};
296 for (String pwdName : pwdNames) {
297 if (name.contains(pwdName)) {
305 * Allows Directed Graphs the ability to interact with REST APIs.
307 * @param paramMap HashMap<String,String> of parameters passed by the DG to this function
311 * <th>Mandatory/Optional</th>
312 * <th>description</th>
313 * <th>example values</th></thead> <tbody>
315 * <td>templateFileName</td>
317 * <td>full path to template file that can be used to build a request</td>
318 * <td>/sdncopt/bvc/restapi/templates/vnf_service-configuration-operation_minimal.json</td>
321 * <td>restapiUrl</td>
323 * <td>url to send the request to</td>
324 * <td>https://sdncodl:8543/restconf/operations/L3VNF-API:create-update-vnf-request</td>
327 * <td>restapiUser</td>
329 * <td>user name to use for http basic authentication</td>
333 * <td>restapiPassword</td>
335 * <td>unencrypted password to use for http basic authentication</td>
336 * <td>plain_password</td>
339 * <td>oAuthConsumerKey</td>
341 * <td>Consumer key to use for http oAuth authentication</td>
345 * <td>oAuthConsumerSecret</td>
347 * <td>Consumer secret to use for http oAuth authentication</td>
348 * <td>plain_secret</td>
351 * <td>oAuthSignatureMethod</td>
353 * <td>Consumer method to use for http oAuth authentication</td>
357 * <td>oAuthVersion</td>
359 * <td>Version http oAuth authentication</td>
363 * <td>contentType</td>
365 * <td>http content type to set in the http header</td>
366 * <td>usually application/json or application/xml</td>
371 * <td>should match request body format</td>
372 * <td>json or xml</td>
375 * <td>httpMethod</td>
377 * <td>http method to use when sending the request</td>
378 * <td>get post put delete patch</td>
381 * <td>responsePrefix</td>
383 * <td>location the response will be written to in context memory</td>
384 * <td>tmp.restapi.result</td>
387 * <td>listName[i]</td>
389 * <td>Used for processing XML responses with repeating
390 * elements.</td>vpn-information.vrf-details
394 * <td>skipSending</td>
397 * <td>true or false</td>
400 * <td>convertResponse</td>
402 * <td>whether the response should be converted</td>
403 * <td>true or false</td>
406 * <td>customHttpHeaders</td>
408 * <td>a list additional http headers to be passed in, follow the format in the example</td>
409 * <td>X-CSI-MessageId=messageId,headerFieldName=headerFieldValue</td>
412 * <td>dumpHeaders</td>
414 * <td>when true writes http header content to context memory</td>
415 * <td>true or false</td>
420 * <td>used to retrieve username, password and url if partner store exists</td>
424 * <td>returnRequestPayload</td>
426 * <td>used to return payload built in the request</td>
427 * <td>true or false</td>
431 * @param ctx Reference to context memory
432 * @throws SvcLogicException
434 * @see String#split(String, int)
436 public void sendRequest(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
437 sendRequest(paramMap, ctx, null);
440 protected void sendRequest(Map<String, String> paramMap, SvcLogicContext ctx, RetryPolicy retryPolicy)
441 throws SvcLogicException {
443 HttpResponse r = new HttpResponse();
445 handlePartner(paramMap);
446 Parameters p = getParameters(paramMap, new Parameters());
447 if (p.restapiUrl.contains(",") && retryPolicy == null) {
448 String[] urls = p.restapiUrl.split(",");
449 retryPolicy = new RetryPolicy(urls, urls.length * 2);
450 p.restapiUrl = urls[0];
452 String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
455 if (p.templateFileName != null) {
456 String reqTemplate = readFile(p.templateFileName);
457 req = buildXmlJsonRequest(ctx, reqTemplate, p.format);
458 } else if (p.requestBody != null) {
461 r = sendHttpRequest(req, p);
462 setResponseStatus(ctx, p.responsePrefix, r);
464 if (p.dumpHeaders && r.headers != null) {
465 for (Entry<String, List<String>> a : r.headers.entrySet()) {
466 ctx.setAttribute(pp + "header." + a.getKey(), StringUtils.join(a.getValue(), ","));
470 if (p.returnRequestPayload && req != null) {
471 ctx.setAttribute(pp + "httpRequest", req);
474 if (r.body != null && r.body.trim().length() > 0) {
475 ctx.setAttribute(pp + "httpResponse", r.body);
477 if (p.convertResponse) {
478 Map<String, String> mm = null;
479 if (p.format == Format.XML) {
480 mm = XmlParser.convertToProperties(r.body, p.listNameList);
481 } else if (p.format == Format.JSON) {
482 mm = JsonParser.convertToProperties(r.body);
486 for (Map.Entry<String, String> entry : mm.entrySet()) {
487 ctx.setAttribute(pp + entry.getKey(), entry.getValue());
492 } catch (SvcLogicException e) {
493 boolean shouldRetry = false;
494 if (e.getCause().getCause() instanceof SocketException) {
498 log.error("Error sending the request: " + e.getMessage(), e);
499 String prefix = parseParam(paramMap, responsePrefix, false, null);
500 if (retryPolicy == null || !shouldRetry) {
501 setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
503 log.debug(retryPolicy.getRetryMessage());
505 // calling getNextHostName increments the retry count so it should be called before shouldRetry
506 String retryString = retryPolicy.getNextHostName();
507 if (retryPolicy.shouldRetry()) {
508 paramMap.put(restapiUrlString, retryString);
509 log.debug("retry attempt {} will use the retry url {}", retryPolicy.getRetryCount(),
511 sendRequest(paramMap, ctx, retryPolicy);
513 log.debug("Maximum retries reached, won't attempt to retry. Calling setFailureResponseStatus.");
514 setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
516 } catch (Exception ex) {
517 String retryErrorMessage = "Retry attempt " + retryPolicy.getRetryCount()
518 + "has failed with error message " + ex.getMessage();
519 setFailureResponseStatus(ctx, prefix, retryErrorMessage, r);
524 if (r != null && r.code >= 300) {
525 throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
529 protected void handlePartner(Map<String, String> paramMap) {
530 String partner = paramMap.get("partner");
531 if (partner != null && partner.length() > 0) {
532 PartnerDetails details = partnerStore.get(partner);
533 paramMap.put(restapiUserKey, details.username);
534 paramMap.put(restapiPasswordKey, details.password);
535 if (paramMap.get(restapiUrlString) == null) {
536 paramMap.put(restapiUrlString, details.url);
541 protected String buildXmlJsonRequest(SvcLogicContext ctx, String template, Format format) throws SvcLogicException {
542 log.info("Building {} started", format);
543 long t1 = System.currentTimeMillis();
544 String originalTemplate = template;
546 template = expandRepeats(ctx, template, 1);
548 Map<String, String> mm = new HashMap<>();
549 for (String s : ctx.getAttributeKeySet()) {
550 mm.put(s, ctx.getAttribute(s));
553 StringBuilder ss = new StringBuilder();
555 while (i < template.length()) {
556 int i1 = template.indexOf("${", i);
558 ss.append(template.substring(i));
562 int i2 = template.indexOf('}', i1 + 2);
564 throw new SvcLogicException("Template error: Matching } not found");
567 String var1 = template.substring(i1 + 2, i2);
568 String value1 = format == Format.XML ? XmlJsonUtil.getXml(mm, var1) : XmlJsonUtil.getJson(mm, var1);
569 if (value1 == null || value1.trim().length() == 0) {
570 // delete the whole element (line)
571 int i3 = template.lastIndexOf('\n', i1);
575 int i4 = template.indexOf('\n', i1);
577 i4 = template.length();
581 ss.append(template.substring(i, i3));
585 ss.append(template.substring(i, i1)).append(value1);
590 String req = format == Format.XML ? XmlJsonUtil.removeEmptyStructXml(ss.toString())
591 : XmlJsonUtil.removeEmptyStructJson(originalTemplate, ss.toString());
593 if (format == Format.JSON) {
594 req = XmlJsonUtil.removeLastCommaJson(req);
597 long t2 = System.currentTimeMillis();
598 log.info("Building {} completed. Time: {}", format, t2 - t1);
603 protected String expandRepeats(SvcLogicContext ctx, String template, int level) throws SvcLogicException {
604 StringBuilder newTemplate = new StringBuilder();
606 while (k < template.length()) {
607 int i1 = template.indexOf("${repeat:", k);
609 newTemplate.append(template.substring(k));
613 int i2 = template.indexOf(':', i1 + 9);
615 throw new SvcLogicException(
616 "Template error: Context variable name followed by : is required after repeat");
619 // Find the closing }, store in i3
623 while (nn > 0 && i < template.length()) {
624 i3 = template.indexOf('}', i);
626 throw new SvcLogicException("Template error: Matching } not found");
628 int i32 = template.indexOf('{', i);
629 if (i32 >= 0 && i32 < i3) {
638 String var1 = template.substring(i1 + 9, i2);
639 String value1 = ctx.getAttribute(var1);
640 log.info(" {}:{}", var1, value1);
643 n = Integer.parseInt(value1);
644 } catch (NumberFormatException e) {
645 log.info("value1 not set or not a number, n will remain set at zero");
648 newTemplate.append(template.substring(k, i1));
650 String rpt = template.substring(i2 + 1, i3);
652 for (int ii = 0; ii < n; ii++) {
653 String ss = rpt.replaceAll("\\[\\$\\{" + level + "\\}\\]", "[" + ii + "]");
654 if (ii == n - 1 && ss.trim().endsWith(",")) {
655 int i4 = ss.lastIndexOf(',');
657 ss = ss.substring(0, i4) + ss.substring(i4 + 1);
660 newTemplate.append(ss);
667 return newTemplate.toString();
670 return expandRepeats(ctx, newTemplate.toString(), level + 1);
673 protected String readFile(String fileName) throws SvcLogicException {
675 byte[] encoded = Files.readAllBytes(Paths.get(fileName));
676 return new String(encoded, "UTF-8");
677 } catch (IOException | SecurityException e) {
678 throw new SvcLogicException("Unable to read file " + fileName + e.getLocalizedMessage(), e);
682 protected Client addAuthType(Client c, FileParam fp) throws SvcLogicException {
683 Parameters p = new Parameters();
684 p.restapiUser = fp.user;
685 p.restapiPassword = fp.password;
686 p.oAuthConsumerKey = fp.oAuthConsumerKey;
687 p.oAuthVersion = fp.oAuthVersion;
688 p.oAuthConsumerSecret = fp.oAuthConsumerSecret;
689 p.oAuthSignatureMethod = fp.oAuthSignatureMethod;
690 p.authtype = fp.authtype;
691 return addAuthType(c, p);
694 public Client addAuthType(Client client, Parameters p) throws SvcLogicException {
695 if (p.authtype == AuthType.Unspecified) {
696 if (p.restapiUser != null && p.restapiPassword != null) {
697 client.register(HttpAuthenticationFeature.basic(p.restapiUser, p.restapiPassword));
698 } else if (p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null && p.oAuthSignatureMethod != null) {
699 Feature oAuth1Feature =
700 OAuth1ClientSupport.builder(new ConsumerCredentials(p.oAuthConsumerKey, p.oAuthConsumerSecret))
701 .version(p.oAuthVersion).signatureMethod(p.oAuthSignatureMethod).feature().build();
702 client.register(oAuth1Feature);
706 if (p.authtype == AuthType.DIGEST) {
707 if (p.restapiUser != null && p.restapiPassword != null) {
708 client.register(HttpAuthenticationFeature.digest(p.restapiUser, p.restapiPassword));
710 throw new SvcLogicException(
711 "oAUTH authentication type selected but all restapiUser and restapiPassword "
712 + "parameters doesn't exist",
715 } else if (p.authtype == AuthType.BASIC) {
716 if (p.restapiUser != null && p.restapiPassword != null) {
717 client.register(HttpAuthenticationFeature.basic(p.restapiUser, p.restapiPassword));
719 throw new SvcLogicException(
720 "oAUTH authentication type selected but all restapiUser and restapiPassword "
721 + "parameters doesn't exist",
724 } else if (p.authtype == AuthType.OAUTH) {
725 if (p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null && p.oAuthSignatureMethod != null) {
726 Feature oAuth1Feature = OAuth1ClientSupport
727 .builder(new ConsumerCredentials(p.oAuthConsumerKey, p.oAuthConsumerSecret))
728 .version(p.oAuthVersion).signatureMethod(p.oAuthSignatureMethod).feature().build();
729 client.register(oAuth1Feature);
731 throw new SvcLogicException(
732 "oAUTH authentication type selected but all oAuthConsumerKey, oAuthConsumerSecret "
733 + "and oAuthSignatureMethod parameters doesn't exist",
742 * Receives the http response for the http request sent.
744 * @param request request msg
745 * @param p parameters
746 * @return HTTP response
747 * @throws SvcLogicException when sending http request fails
749 public HttpResponse sendHttpRequest(String request, Parameters p) throws SvcLogicException {
751 SSLContext ssl = null;
752 if (p.ssl && p.restapiUrl.startsWith("https")) {
753 ssl = createSSLContext(p);
758 HttpsURLConnection.setDefaultSSLSocketFactory(ssl.getSocketFactory());
759 client = ClientBuilder.newBuilder().sslContext(ssl).hostnameVerifier((s, sslSession) -> true).build();
761 client = ClientBuilder.newBuilder().hostnameVerifier((s, sslSession) -> true).build();
763 setClientTimeouts(client);
764 // Needed to support additional HTTP methods such as PATCH
765 client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
767 WebTarget webTarget = addAuthType(client, p).target(p.restapiUrl);
769 log.info("Sending request below to url " + p.restapiUrl);
771 long t1 = System.currentTimeMillis();
773 HttpResponse r = new HttpResponse();
775 String accept = p.accept;
776 if (accept == null) {
777 accept = p.format == Format.XML ? "application/xml" : "application/json";
780 String contentType = p.contentType;
781 if (contentType == null) {
782 contentType = accept + ";charset=UTF-8";
785 if (!p.skipSending && !p.multipartFormData) {
787 Invocation.Builder invocationBuilder = webTarget.request(contentType).accept(accept);
789 if (p.format == Format.NONE) {
790 invocationBuilder.header("", "");
793 if (p.customHttpHeaders != null && p.customHttpHeaders.length() > 0) {
794 String[] keyValuePairs = p.customHttpHeaders.split(",");
795 for (String singlePair : keyValuePairs) {
796 int equalPosition = singlePair.indexOf('=');
797 invocationBuilder.header(singlePair.substring(0, equalPosition),
798 singlePair.substring(equalPosition + 1, singlePair.length()));
802 invocationBuilder.header("X-ECOMP-RequestID", org.slf4j.MDC.get("X-ECOMP-RequestID"));
804 invocationBuilder.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
809 response = invocationBuilder.method(p.httpMethod.toString(), entity(request, contentType));
810 } catch (ProcessingException | IllegalStateException e) {
811 throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e);
814 r.code = response.getStatus();
815 r.headers = response.getStringHeaders();
816 EntityTag etag = response.getEntityTag();
818 r.message = etag.getValue();
820 if (response.hasEntity() && r.code != 204) {
821 r.body = response.readEntity(String.class);
823 } else if (!p.skipSending && p.multipartFormData) {
825 WebTarget wt = client.register(MultiPartFeature.class).target(p.restapiUrl);
827 MultiPart multiPart = new MultiPart();
828 multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
830 FileDataBodyPart fileDataBodyPart =
831 new FileDataBodyPart("file", new File(p.multipartFile), MediaType.APPLICATION_OCTET_STREAM_TYPE);
832 multiPart.bodyPart(fileDataBodyPart);
835 Invocation.Builder invocationBuilder = wt.request(contentType).accept(accept);
837 if (p.format == Format.NONE) {
838 invocationBuilder.header("", "");
841 if (p.customHttpHeaders != null && p.customHttpHeaders.length() > 0) {
842 String[] keyValuePairs = p.customHttpHeaders.split(",");
843 for (String singlePair : keyValuePairs) {
844 int equalPosition = singlePair.indexOf('=');
845 invocationBuilder.header(singlePair.substring(0, equalPosition),
846 singlePair.substring(equalPosition + 1, singlePair.length()));
850 invocationBuilder.header("X-ECOMP-RequestID", org.slf4j.MDC.get("X-ECOMP-RequestID"));
856 invocationBuilder.method(p.httpMethod.toString(), entity(multiPart, multiPart.getMediaType()));
857 } catch (ProcessingException | IllegalStateException e) {
858 throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e);
861 r.code = response.getStatus();
862 r.headers = response.getStringHeaders();
863 EntityTag etag = response.getEntityTag();
865 r.message = etag.getValue();
867 if (response.hasEntity() && r.code != 204) {
868 r.body = response.readEntity(String.class);
873 long t2 = System.currentTimeMillis();
874 log.info(responseReceivedMessage, t2 - t1);
875 log.info(responseHttpCodeMessage, r.code);
876 log.info("HTTP response message: {}", r.message);
877 logHeaders(r.headers);
878 log.info("HTTP response: {}", r.body);
883 protected SSLContext createSSLContext(Parameters p) {
884 try (FileInputStream in = new FileInputStream(p.keyStoreFileName)) {
885 System.setProperty("jsse.enableSNIExtension", "false");
886 System.setProperty("javax.net.ssl.trustStore", p.trustStoreFileName);
887 System.setProperty("javax.net.ssl.trustStorePassword", p.trustStorePassword);
889 HttpsURLConnection.setDefaultHostnameVerifier((string, ssls) -> true);
891 KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
892 KeyStore ks = KeyStore.getInstance("PKCS12");
893 char[] pwd = p.keyStorePassword.toCharArray();
897 SSLContext ctx = SSLContext.getInstance("TLS");
898 ctx.init(kmf.getKeyManagers(), null, null);
900 } catch (Exception e) {
901 log.error("Error creating SSLContext: {}", e.getMessage(), e);
906 protected void setFailureResponseStatus(SvcLogicContext ctx, String prefix, String errorMessage,
909 resp.message = errorMessage;
910 String pp = prefix != null ? prefix + '.' : "";
911 ctx.setAttribute(pp + "response-code", String.valueOf(resp.code));
912 ctx.setAttribute(pp + "response-message", resp.message);
915 protected void setResponseStatus(SvcLogicContext ctx, String prefix, HttpResponse r) {
916 String pp = prefix != null ? prefix + '.' : "";
917 ctx.setAttribute(pp + "response-code", String.valueOf(r.code));
918 ctx.setAttribute(pp + "response-message", r.message);
921 public void sendFile(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
922 HttpResponse r = null;
924 FileParam p = getFileParameters(paramMap);
925 byte[] data = Files.readAllBytes(Paths.get(p.fileName));
927 r = sendHttpData(data, p);
928 setResponseStatus(ctx, p.responsePrefix, r);
930 } catch (SvcLogicException | IOException e) {
931 log.error("Error sending the request: {}", e.getMessage(), e);
933 r = new HttpResponse();
935 r.message = e.getMessage();
936 String prefix = parseParam(paramMap, responsePrefix, false, null);
937 setResponseStatus(ctx, prefix, r);
940 if (r != null && r.code >= 300) {
941 throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
945 private FileParam getFileParameters(Map<String, String> paramMap) throws SvcLogicException {
946 FileParam p = new FileParam();
947 p.fileName = parseParam(paramMap, "fileName", true, null);
948 p.url = parseParam(paramMap, "url", true, null);
949 p.user = parseParam(paramMap, "user", false, null);
950 p.password = parseParam(paramMap, "password", false, null);
951 p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod", false, "post"));
952 p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
953 String skipSendingStr = paramMap.get(skipSendingMessage);
954 p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
955 p.oAuthConsumerKey = parseParam(paramMap, "oAuthConsumerKey", false, null);
956 p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null);
957 p.oAuthConsumerSecret = parseParam(paramMap, "oAuthConsumerSecret", false, null);
958 p.oAuthSignatureMethod = parseParam(paramMap, "oAuthSignatureMethod", false, null);
959 p.authtype = fromString(parseParam(paramMap, "authType", false, "unspecified"));
963 public void postMessageOnUeb(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
966 UebParam p = getUebParameters(paramMap);
968 String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
972 if (p.templateFileName == null) {
973 log.info("No template file name specified. Using default UEB template: {}", defaultUebTemplateFileName);
974 p.templateFileName = defaultUebTemplateFileName;
977 String reqTemplate = readFile(p.templateFileName);
978 reqTemplate = reqTemplate.replaceAll("rootVarName", p.rootVarName);
979 req = buildXmlJsonRequest(ctx, reqTemplate, Format.JSON);
981 r = postOnUeb(req, p);
982 setResponseStatus(ctx, p.responsePrefix, r);
983 if (r.body != null) {
984 ctx.setAttribute(pp + "httpResponse", r.body);
987 } catch (SvcLogicException e) {
988 log.error("Error sending the request: {}", e.getMessage(), e);
990 r = new HttpResponse();
992 r.message = e.getMessage();
993 String prefix = parseParam(paramMap, responsePrefix, false, null);
994 setResponseStatus(ctx, prefix, r);
998 throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
1002 protected HttpResponse sendHttpData(byte[] data, FileParam p) throws SvcLogicException {
1004 Client client = ClientBuilder.newBuilder().build();
1005 setClientTimeouts(client);
1006 client.property(ClientProperties.FOLLOW_REDIRECTS, true);
1007 WebTarget webTarget = addAuthType(client, p).target(p.url);
1009 log.info("Sending file");
1010 long t1 = System.currentTimeMillis();
1012 HttpResponse r = new HttpResponse();
1015 if (!p.skipSending) {
1016 String tt = "application/octet-stream";
1017 Invocation.Builder invocationBuilder = webTarget.request(tt).accept(tt);
1022 if (p.httpMethod == HttpMethod.POST) {
1023 response = invocationBuilder.post(Entity.entity(data, tt));
1024 } else if (p.httpMethod == HttpMethod.PUT) {
1025 response = invocationBuilder.put(Entity.entity(data, tt));
1027 throw new SvcLogicException("Http operation" + p.httpMethod + "not supported");
1029 } catch (ProcessingException e) {
1030 throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e);
1033 r.code = response.getStatus();
1034 r.headers = response.getStringHeaders();
1035 EntityTag etag = response.getEntityTag();
1037 r.message = etag.getValue();
1039 if (response.hasEntity() && r.code != 204) {
1040 r.body = response.readEntity(String.class);
1043 if (r.code == 301) {
1044 String newUrl = response.getStringHeaders().getFirst("Location");
1046 log.info("Got response code 301. Sending same request to URL: {}", newUrl);
1048 webTarget = client.target(newUrl);
1049 invocationBuilder = webTarget.request(tt).accept(tt);
1052 if (p.httpMethod == HttpMethod.POST) {
1053 response = invocationBuilder.post(Entity.entity(data, tt));
1054 } else if (p.httpMethod == HttpMethod.PUT) {
1055 response = invocationBuilder.put(Entity.entity(data, tt));
1057 throw new SvcLogicException("Http operation" + p.httpMethod + "not supported");
1059 } catch (ProcessingException e) {
1060 throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e);
1063 r.code = response.getStatus();
1064 etag = response.getEntityTag();
1066 r.message = etag.getValue();
1068 if (response.hasEntity() && r.code != 204) {
1069 r.body = response.readEntity(String.class);
1074 long t2 = System.currentTimeMillis();
1075 log.info(responseReceivedMessage, t2 - t1);
1076 log.info(responseHttpCodeMessage, r.code);
1077 log.info("HTTP response message: {}", r.message);
1078 logHeaders(r.headers);
1079 log.info("HTTP response: {}", r.body);
1084 private UebParam getUebParameters(Map<String, String> paramMap) throws SvcLogicException {
1085 UebParam p = new UebParam();
1086 p.topic = parseParam(paramMap, "topic", true, null);
1087 p.templateFileName = parseParam(paramMap, "templateFileName", false, null);
1088 p.rootVarName = parseParam(paramMap, "rootVarName", false, null);
1089 p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
1090 String skipSendingStr = paramMap.get(skipSendingMessage);
1091 p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
1095 protected void logProperties(Map<String, Object> mm) {
1096 List<String> ll = new ArrayList<>();
1097 for (Object o : mm.keySet()) {
1100 Collections.sort(ll);
1102 log.info("Properties:");
1103 for (String name : ll) {
1104 log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
1108 protected void logHeaders(MultivaluedMap<String, String> mm) {
1109 log.info("HTTP response headers:");
1115 List<String> ll = new ArrayList<>();
1116 for (Object o : mm.keySet()) {
1119 Collections.sort(ll);
1121 for (String name : ll) {
1122 log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
1126 protected HttpResponse postOnUeb(String request, UebParam p) throws SvcLogicException {
1127 String[] urls = uebServers.split(" ");
1128 for (int i = 0; i < urls.length; i++) {
1129 if (!urls[i].endsWith("/")) {
1132 urls[i] += "events/" + p.topic;
1135 Client client = ClientBuilder.newBuilder().build();
1136 setClientTimeouts(client);
1137 WebTarget webTarget = client.target(urls[0]);
1139 log.info("UEB URL: {}", urls[0]);
1140 log.info("Sending request:");
1142 long t1 = System.currentTimeMillis();
1144 HttpResponse r = new HttpResponse();
1147 if (!p.skipSending) {
1148 String tt = "application/json";
1149 String tt1 = tt + ";charset=UTF-8";
1152 Invocation.Builder invocationBuilder = webTarget.request(tt1).accept(tt);
1155 response = invocationBuilder.post(Entity.entity(request, tt1));
1156 } catch (ProcessingException e) {
1157 throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e);
1159 r.code = response.getStatus();
1160 r.headers = response.getStringHeaders();
1161 if (response.hasEntity()) {
1162 r.body = response.readEntity(String.class);
1166 long t2 = System.currentTimeMillis();
1167 log.info(responseReceivedMessage, t2 - t1);
1168 log.info(responseHttpCodeMessage, r.code);
1169 logHeaders(r.headers);
1170 log.info("HTTP response:\n {}", r.body);
1175 public void setUebServers(String uebServers) {
1176 this.uebServers = uebServers;
1179 public void setDefaultUebTemplateFileName(String defaultUebTemplateFileName) {
1180 this.defaultUebTemplateFileName = defaultUebTemplateFileName;
1183 protected void setClientTimeouts(Client client) {
1184 client.property(ClientProperties.CONNECT_TIMEOUT, httpConnectTimeout);
1185 client.property(ClientProperties.READ_TIMEOUT, httpReadTimeout);
1188 protected Integer readOptionalInteger(String propertyName, Integer defaultValue) {
1189 String stringValue = System.getProperty(propertyName);
1190 if (stringValue != null && stringValue.length() > 0) {
1192 return Integer.valueOf(stringValue);
1193 } catch (NumberFormatException e) {
1194 log.warn("property " + propertyName + " had the value " + stringValue + " that could not be converted to an Integer, default " + defaultValue + " will be used instead", e);
1197 return defaultValue;
1201 private static class FileParam {
1203 public String fileName;
1206 public String password;
1207 public HttpMethod httpMethod;
1208 public String responsePrefix;
1209 public boolean skipSending;
1210 public String oAuthConsumerKey;
1211 public String oAuthConsumerSecret;
1212 public String oAuthSignatureMethod;
1213 public String oAuthVersion;
1214 public AuthType authtype;
1217 private static class UebParam {
1219 public String topic;
1220 public String templateFileName;
1221 public String rootVarName;
1222 public String responsePrefix;
1223 public boolean skipSending;