From 33bfbaf4a7c2c33fc0b0b5ab18a5553dd8884cf7 Mon Sep 17 00:00:00 2001 From: Gaurav Agrawal Date: Wed, 20 Sep 2017 11:48:54 +0530 Subject: [PATCH] Changes to RestApiCall plugin Changes includes 1. Perform validation of URL syntax during parameter parsing phase. 2. StringBuilder to replace string concatenation within loop. 3. Testcase addition. 4. Sonar critical issue fix for XmlJsonUtil 5) Use logger built-in string formatting rather then string concatenation https://sonar.onap.org/component_issues/index?id=org.onap.ccsdk.sli.plugins%3Accsdk-sli-plugins#resolved=false|severities=CRITICAL Change-Id: I884af51023bbd4983c43707aca97c398d24fc9de Issue-Id: CCSDK-67 Signed-off-by: Gaurav Agrawal --- .../sli/plugins/restapicall/RestapiCallNode.java | 73 ++++++++++++---------- .../ccsdk/sli/plugins/restapicall/XmlJsonUtil.java | 2 +- .../plugins/restapicall/TestRestapiCallNode.java | 27 ++++++++ 3 files changed, 69 insertions(+), 33 deletions(-) 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 20c13daa..c4ad4c55 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 @@ -183,10 +183,10 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { URI retryUri = UriBuilder.fromUri(uri).host(uriTwo.getHost()).port(uriTwo.getPort()).scheme( uriTwo.getScheme()).build(); paramMap.put("restapiUrl", retryUri.toString()); - log.debug("URL was set to " + retryUri.toString()); - log.debug("Failed to communicate with host " + hostname + - ". Request will be re-attempted using the host " + retryString + "."); - log.debug("This is retry attempt " + retryCount + " out of " + retryPolicy.getMaximumRetries()); + 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); + log.debug("This is retry attempt {} out of {}", retryCount, retryPolicy.getMaximumRetries()); sendRequest(paramMap, ctx, retryCount); } else { log.debug("Maximum retries reached, calling setFailureResponseStatus."); @@ -210,6 +210,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { Parameters p = new Parameters(); p.templateFileName = parseParam(paramMap, "templateFileName", false, null); p.restapiUrl = parseParam(paramMap, "restapiUrl", true, null); + validateUrl(p.restapiUrl); p.restapiUser = parseParam(paramMap, "restapiUser", false, null); p.restapiPassword = parseParam(paramMap, "restapiPassword", false, null); p.contentType = parseParam(paramMap, "contentType", false, null); @@ -232,6 +233,14 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { return p; } + private void validateUrl(String restapiUrl) throws SvcLogicException { + try { + URI.create(restapiUrl); + } catch (IllegalArgumentException e) { + throw new SvcLogicException("Invalid input of url " + e.getLocalizedMessage(), e); + } + } + protected Set getListNameList(Map paramMap) { Set ll = new HashSet<>(); for (Map.Entry entry : paramMap.entrySet()) @@ -251,7 +260,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { } s = s.trim(); - String value = ""; + StringBuilder value = new StringBuilder(); int i = 0; int i1 = s.indexOf('%'); while (i1 >= 0) { @@ -264,21 +273,21 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { if (varValue == null) varValue = "%" + varName + "%"; - value += s.substring(i, i1); - value += varValue; + value.append(s.substring(i, i1)); + value.append(varValue); i = i2 + 1; i1 = s.indexOf('%', i); } - value += s.substring(i); + value.append(s.substring(i)); - log.info("Parameter " + name + ": [" + value + "]"); - return value; + log.info("Parameter {}: [{}]", name, value); + return value.toString(); } protected String buildXmlJsonRequest(SvcLogicContext ctx, String template, Format format) throws SvcLogicException { - log.info("Building " + format + " started"); + log.info("Building {} started", format); long t1 = System.currentTimeMillis(); template = expandRepeats(ctx, template, 1); @@ -328,7 +337,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { req = XmlJsonUtil.removeLastCommaJson(req); long t2 = System.currentTimeMillis(); - log.info("Building " + format + " completed. Time: " + (t2 - t1)); + log.info("Building {} completed. Time: {}", format, (t2 - t1)); return req; } @@ -368,7 +377,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { String var1 = template.substring(i1 + 9, i2); String value1 = ctx.getAttribute(var1); - log.info(" " + var1 + ": " + value1); + log.info(" {}:{}", var1, value1); int n = 0; try { n = Integer.parseInt(value1); @@ -477,11 +486,11 @@ 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("HTTP response message: " + r.message); + log.info("Response received. Time: {}", (t2 - t1)); + log.info("HTTP response code: {}", r.code); + log.info("HTTP response message: {}", r.message); logHeaders(r.headers); - log.info("HTTP response: " + r.body); + log.info("HTTP response: {}", r.body); return r; } @@ -504,7 +513,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { ctx.init(kmf.getKeyManagers(), null, null); return ctx; } catch (Exception e) { - log.error("Error creating SSLContext: " + e.getMessage(), e); + log.error("Error creating SSLContext: {}", e.getMessage(), e); } return null; } @@ -534,7 +543,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { setResponseStatus(ctx, p.responsePrefix, r); } catch (SvcLogicException | IOException e) { - log.error("Error sending the request: " + e.getMessage(), e); + log.error("Error sending the request: {}", e.getMessage(), e); r = new HttpResponse(); r.code = 500; @@ -612,7 +621,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { if (r.code == 301) { String newUrl = response.getHeaders().getFirst("Location"); - log.info("Got response code 301. Sending same request to URL: " + newUrl); + log.info("Got response code 301. Sending same request to URL: {}", newUrl); webResource = client.resource(newUrl); @@ -638,11 +647,11 @@ 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("HTTP response message: " + r.message); + log.info("Response received. Time: {}", (t2 - t1)); + log.info("HTTP response code: {}", r.code); + log.info("HTTP response message: {}", r.message); logHeaders(r.headers); - log.info("HTTP response: " + r.body); + log.info("HTTP response: {}", r.body); return r; } @@ -657,7 +666,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { String req; if (p.templateFileName == null) { - log.info("No template file name specified. Using default UEB template: " + defaultUebTemplateFileName); + log.info("No template file name specified. Using default UEB template: {}", defaultUebTemplateFileName); p.templateFileName = defaultUebTemplateFileName; } @@ -671,7 +680,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { ctx.setAttribute(pp + "httpResponse", r.body); } catch (SvcLogicException e) { - log.error("Error sending the request: " + e.getMessage(), e); + log.error("Error sending the request: {}", e.getMessage(), e); r = new HttpResponse(); r.code = 500; @@ -716,7 +725,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { client.setConnectTimeout(5000); WebResource webResource = client.resource(urls[0]); - log.info("UEB URL: " + urls[0]); + log.info("UEB URL: {}", urls[0]); log.info("Sending request:"); log.info(request); long t1 = System.currentTimeMillis(); @@ -744,10 +753,10 @@ 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("Response received. Time: {}", (t2 - t1)); + log.info("HTTP response code: {}", r.code); logHeaders(r.headers); - log.info("HTTP response:\n" + r.body); + log.info("HTTP response:\n {}", r.body); return r; } @@ -760,7 +769,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { log.info("Properties:"); for (String name : ll) - log.info("--- " + name + ": " + String.valueOf(mm.get(name))); + log.info("--- {}:{}", name, String.valueOf(mm.get(name))); } protected void logHeaders(MultivaluedMap mm) { @@ -775,7 +784,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { Collections.sort(ll); for (String name : ll) - log.info("--- " + name + ": " + String.valueOf(mm.get(name))); + log.info("--- {}:{}", name, String.valueOf(mm.get(name))); } public void setUebServers(String uebServers) { diff --git a/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/XmlJsonUtil.java b/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/XmlJsonUtil.java index b94f0a63..359501fc 100644 --- a/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/XmlJsonUtil.java +++ b/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/XmlJsonUtil.java @@ -103,7 +103,7 @@ public final class XmlJsonUtil { try { length = Integer.parseInt(lengthStr); } catch (Exception e) { - log.warn("Invalid number for {}_length:{}", var, lengthStr); + log.warn("Invalid number for {}_length:{}", var, lengthStr, e); } } diff --git a/restapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/restapicall/TestRestapiCallNode.java b/restapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/restapicall/TestRestapiCallNode.java index 51c39478..118d97d3 100644 --- a/restapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/restapicall/TestRestapiCallNode.java +++ b/restapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/restapicall/TestRestapiCallNode.java @@ -168,4 +168,31 @@ public class TestRestapiCallNode { RestapiCallNode rcn = new RestapiCallNode(); rcn.sendRequest(p, ctx); } + + + @Test(expected = SvcLogicException.class) + public void testWithInvalidURI() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("tmp.sdn-circuit-req-row_length", "1"); + ctx.setAttribute("tmp.sdn-circuit-req-row[0].source-uid", "APIDOC-123"); + ctx.setAttribute("tmp.sdn-circuit-req-row[0].action", "delete"); + ctx.setAttribute("tmp.sdn-circuit-req-row[0].request-timestamp", "2016-09-09 16:30:35.0"); + ctx.setAttribute("tmp.sdn-circuit-req-row[0].request-status", "New"); + ctx.setAttribute("tmp.sdn-circuit-req-row[0].processing-status", "New"); + ctx.setAttribute("tmp.sdn-circuit-req-row[0].service-clfi", "testClfi1"); + ctx.setAttribute("tmp.sdn-circuit-req-row[0].clci", "clci"); + + Map p = new HashMap(); + p.put("templateFileName", "src/test/resources/test-template.json"); + p.put("restapiUrl", "http://echo. getpostman.com"); + p.put("restapiUser", "user1"); + p.put("restapiPassword", "abc123"); + p.put("format", "json"); + p.put("httpMethod", "post"); + p.put("responsePrefix", "response"); + p.put("skipSending", "false"); + + RestapiCallNode rcn = new RestapiCallNode(); + rcn.sendRequest(p, ctx); + } } -- 2.16.6