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=521c66cb397da2377c0688af5b1e7202a42e5501;hb=f360b7b2bc9ed8d321dade9cadafe2ebda4f46ab;hp=e5503435bb4f0547773bc94f56f8d2d80a12d889;hpb=6a8381e0f724955e520ef03b775024b4e515d837;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 e5503435..521c66cb 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 @@ -4,6 +4,7 @@ * ================================================================================ * 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. @@ -57,6 +58,7 @@ 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; @@ -78,6 +80,13 @@ public class RestapiCallNode implements 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); @@ -115,7 +124,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { 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, @@ -134,9 +143,9 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { "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")); @@ -157,6 +166,8 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { false, null)); p.returnRequestPayload = valueOf(parseParam( paramMap, "returnRequestPayload", false, null)); + p.accept = parseParam(paramMap, "accept", + false, null); return p; } @@ -344,8 +355,8 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { } 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) { @@ -357,13 +368,13 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { 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); @@ -392,6 +403,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { throws SvcLogicException { log.info("Building {} started", format); long t1 = System.currentTimeMillis(); + String originalTemplate = template; template = expandRepeats(ctx, template, 1); @@ -416,7 +428,6 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { 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); @@ -439,7 +450,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { } 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); @@ -542,7 +553,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { 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)); @@ -612,6 +623,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); @@ -623,14 +636,16 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { 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"; + } + String contentType = p.contentType; + if(contentType == null) { + contentType = accept + ";charset=UTF-8"; } - Invocation.Builder invocationBuilder = webTarget.request(tt1).accept(tt); + Invocation.Builder invocationBuilder = webTarget.request(contentType).accept(accept); if (p.format == Format.NONE) { invocationBuilder.header("", ""); @@ -650,9 +665,9 @@ 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("Exception while posting http request to client " + + throw new SvcLogicException(requestPostingException + e.getLocalizedMessage(), e); } @@ -668,8 +683,8 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { } 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); @@ -730,7 +745,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { 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); } @@ -746,8 +761,8 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { 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); @@ -787,7 +802,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { 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); } @@ -824,7 +839,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { 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); } @@ -855,7 +870,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { 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); } @@ -871,8 +886,8 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { } 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); @@ -885,8 +900,8 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { 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; } @@ -953,7 +968,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { 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(); @@ -964,8 +979,8 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { } 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); @@ -1004,4 +1019,4 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { public String responsePrefix; public boolean skipSending; } -} \ No newline at end of file +}