Merge "Refinements and test additions to XmlParser"
authorDan Timoney <dtimoney@att.com>
Thu, 21 Sep 2017 14:45:41 +0000 (14:45 +0000)
committerGerrit Code Review <gerrit@onap.org>
Thu, 21 Sep 2017 14:45:41 +0000 (14:45 +0000)
restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java
restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/XmlJsonUtil.java
restapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/restapicall/TestRestapiCallNode.java

index 20c13da..c4ad4c5 100644 (file)
@@ -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<String> getListNameList(Map<String, String> paramMap) {
         Set<String> ll = new HashSet<>();
         for (Map.Entry<String,String> 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<String, String> 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) {
index b94f0a6..965d3da 100644 (file)
@@ -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);
                 }
             }
 
@@ -132,23 +132,24 @@ public final class XmlJsonUtil {
         if (o instanceof Map) {
             StringBuilder ss = new StringBuilder();
             Map<String, Object> mm = (Map<String, Object>) o;
-            for (String k : mm.keySet()) {
-                Object v = mm.get(k);
+            for (Map.Entry<String, Object> entry: mm.entrySet()) {
+                Object v = entry.getValue();
+                String key = entry.getKey();
                 if (v instanceof String) {
                     String s = escape ? escapeXml((String) v) : (String) v;
-                    ss.append(pad(indent)).append('<').append(k).append('>');
+                    ss.append(pad(indent)).append('<').append(key).append('>');
                     ss.append(s);
-                    ss.append("</").append(k).append('>').append('\n');
+                    ss.append("</").append(key).append('>').append('\n');
                 } else if (v instanceof Map) {
-                    ss.append(pad(indent)).append('<').append(k).append('>').append('\n');
+                    ss.append(pad(indent)).append('<').append(key).append('>').append('\n');
                     ss.append(generateXml(v, indent + 1, escape));
-                    ss.append(pad(indent)).append("</").append(k).append('>').append('\n');
+                    ss.append(pad(indent)).append("</").append(key).append('>').append('\n');
                 } else if (v instanceof List) {
                     List<Object> ll = (List<Object>) v;
                     for (Object o1 : ll) {
-                        ss.append(pad(indent)).append('<').append(k).append('>').append('\n');
+                        ss.append(pad(indent)).append('<').append(key).append('>').append('\n');
                         ss.append(generateXml(o1, indent + 1, escape));
-                        ss.append(pad(indent)).append("</").append(k).append('>').append('\n');
+                        ss.append(pad(indent)).append("</").append(key).append('>').append('\n');
                     }
                 }
             }
@@ -185,13 +186,13 @@ public final class XmlJsonUtil {
             ss.append("{\n");
 
             boolean first = true;
-            for (String k : mm.keySet()) {
+            for (Map.Entry<String, Object> entry : mm.entrySet()) {
                 if (!first)
                     ss.append(",\n");
                 first = false;
-
-                Object v = mm.get(k);
-                ss.append(pad(indent + 1)).append('"').append(k).append("\": ");
+                Object v = entry.getValue();
+                String key = entry.getKey();
+                ss.append(pad(indent + 1)).append('"').append(key).append("\": ");
                 generateJson(ss, v, indent + 1, false, escape);
             }
 
index 51c3947..118d97d 100644 (file)
@@ -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<String, String> p = new HashMap<String, String>();
+        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);
+    }
 }