Logging changes and unit tests
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / NodeServlet.java
index 3f2fc09..163b59e 100644 (file)
@@ -48,9 +48,9 @@ import org.onap.dmaap.datarouter.node.eelf.EelfMsgs;
 import org.slf4j.MDC;
 
 /**
- * Servlet for handling all http and https requests to the data router node
- * <p>
- * Handled requests are:
+ * Servlet for handling all http and https requests to the data router node.
+ *
+ * <p>Handled requests are:
  * <br>
  * GET http://<i>node</i>/internal/fetchProv - fetch the provisioning data
  * <br>
@@ -86,25 +86,25 @@ public class NodeServlet extends HttpServlet {
     }
 
     /**
-     * Get the NodeConfigurationManager
+     * Get the NodeConfigurationManager.
      */
     @Override
     public void init() {
         config = NodeConfigManager.getInstance();
-        eelfLogger.info("NODE0101 Node Servlet Configured");
+        eelfLogger.debug("NODE0101 Node Servlet Configured");
     }
 
     private boolean down(HttpServletResponse resp) {
         if (config.isShutdown() || !config.isConfigured()) {
             sendResponseError(resp, HttpServletResponse.SC_SERVICE_UNAVAILABLE, eelfLogger);
-            eelfLogger.info("NODE0102 Rejecting request: Service is being quiesced");
+            eelfLogger.error("NODE0102 Rejecting request: Service is being quiesced");
             return true;
         }
         return false;
     }
 
     /**
-     * Handle a GET for /internal/fetchProv
+     * Handle a GET for /internal/fetchProv.
      */
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
@@ -112,7 +112,7 @@ public class NodeServlet extends HttpServlet {
         NodeUtils.setRequestIdAndInvocationId(req);
         eelfLogger.info(EelfMsgs.ENTRY);
         try {
-            eelfLogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(ON_BEHALF_OF),
+            eelfLogger.debug(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(ON_BEHALF_OF),
                     getIdFromPath(req) + "");
             if (down(resp)) {
                 return;
@@ -136,7 +136,7 @@ public class NodeServlet extends HttpServlet {
                 }
             }
 
-            eelfLogger.info("NODE0103 Rejecting invalid GET of " + path + FROM + ip);
+            eelfLogger.debug("NODE0103 Rejecting invalid GET of " + path + FROM + ip);
             sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, eelfLogger);
         } finally {
             eelfLogger.info(EelfMsgs.EXIT);
@@ -144,14 +144,14 @@ public class NodeServlet extends HttpServlet {
     }
 
     /**
-     * Handle all PUT requests
+     * Handle all PUT requests.
      */
     @Override
     protected void doPut(HttpServletRequest req, HttpServletResponse resp) {
         NodeUtils.setIpAndFqdnForEelf("doPut");
         NodeUtils.setRequestIdAndInvocationId(req);
         eelfLogger.info(EelfMsgs.ENTRY);
-        eelfLogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(ON_BEHALF_OF),
+        eelfLogger.debug(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(ON_BEHALF_OF),
                 getIdFromPath(req) + "");
         try {
             common(req, resp, true);
@@ -162,14 +162,14 @@ public class NodeServlet extends HttpServlet {
     }
 
     /**
-     * Handle all DELETE requests
+     * Handle all DELETE requests.
      */
     @Override
     protected void doDelete(HttpServletRequest req, HttpServletResponse resp) {
         NodeUtils.setIpAndFqdnForEelf("doDelete");
         NodeUtils.setRequestIdAndInvocationId(req);
         eelfLogger.info(EelfMsgs.ENTRY);
-        eelfLogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(ON_BEHALF_OF),
+        eelfLogger.debug(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(ON_BEHALF_OF),
                 getIdFromPath(req) + "");
         try {
             common(req, resp, false);
@@ -210,8 +210,8 @@ public class NodeServlet extends HttpServlet {
         }
         if (fileid.startsWith(PUBLISH)) {
             fileid = fileid.substring(9);
-            int i = fileid.indexOf('/');
-            if (i == -1 || i == fileid.length() - 1) {
+            int index = fileid.indexOf('/');
+            if (index == -1 || index == fileid.length() - 1) {
                 eelfLogger.error("NODE0205 Rejecting bad URI for PUT or DELETE of " + req.getPathInfo() + FROM + req
                         .getRemoteAddr());
                 resp.sendError(HttpServletResponse.SC_NOT_FOUND,
@@ -219,7 +219,7 @@ public class NodeServlet extends HttpServlet {
                 eelfLogger.info(EelfMsgs.EXIT);
                 return;
             }
-            feedid = fileid.substring(0, i);
+            feedid = fileid.substring(0, index);
 
             if (config.getCadiEnabled()) {
                 String path = req.getPathInfo();
@@ -228,7 +228,7 @@ public class NodeServlet extends HttpServlet {
                     if (!("legacy".equalsIgnoreCase(aafInstance))) {
                         isAAFFeed = true;
                         String permission = config.getPermission(aafInstance);
-                        eelfLogger.info("NodeServlet.common() permission string - " + permission);
+                        eelfLogger.debug("NodeServlet.common() permission string - " + permission);
                         //Check in CADI Framework API if user has AAF permission or not
                         if (!req.isUserInRole(permission)) {
                             String message = "AAF disallows access to permission string - " + permission;
@@ -242,7 +242,7 @@ public class NodeServlet extends HttpServlet {
                 }
             }
 
-            fileid = fileid.substring(i + 1);
+            fileid = fileid.substring(index + 1);
             pubid = config.getPublishId();
             targets = config.getTargets(feedid);
         } else if (fileid.startsWith(INTERNAL_PUBLISH)) {
@@ -303,7 +303,7 @@ public class NodeServlet extends HttpServlet {
                             .cleanString(feedid) + " fileid " + PathUtil.cleanString(fileid) + FROM + PathUtil
                             .cleanString(ip) + " reason   Invalid AAF user- " + PathUtil.cleanString(reason));
                     String message = "Invalid AAF user- " + PathUtil.cleanString(reason);
-                    eelfLogger.info("NODE0308 Rejecting unauthenticated PUT or DELETE of " + PathUtil
+                    eelfLogger.debug("NODE0308 Rejecting unauthenticated PUT or DELETE of " + PathUtil
                             .cleanString(req.getPathInfo()) + FROM + PathUtil.cleanString(req.getRemoteAddr()));
                     resp.sendError(HttpServletResponse.SC_FORBIDDEN, message);
                     return;
@@ -326,7 +326,7 @@ public class NodeServlet extends HttpServlet {
                 }
                 String redirto = HTTPS + newnode + port + PUBLISH + feedid + "/" + fileid;
                 eelfLogger
-                        .info("NODE0108 Redirecting publish attempt for feed " + PathUtil.cleanString(feedid) + USER
+                        .debug("NODE0108 Redirecting publish attempt for feed " + PathUtil.cleanString(feedid) + USER
                                 + PathUtil.cleanString(user) + " ip " + PathUtil.cleanString(ip) + " to " + PathUtil
                                 .cleanString(redirto));  //Fortify scan fixes - log forging
                 resp.sendRedirect(PathUtil.cleanString(redirto));         //Fortify scan fixes-open redirect - 2 issues
@@ -352,13 +352,13 @@ public class NodeServlet extends HttpServlet {
             while (hnames.hasMoreElements()) {
                 String hn = (String) hnames.nextElement();
                 String hnlc = hn.toLowerCase();
-                if ((isput && ("content-type".equals(hnlc) ||
-                        "content-language".equals(hnlc) ||
-                        "content-md5".equals(hnlc) ||
-                        "content-range".equals(hnlc))) ||
-                        "x-dmaap-dr-meta".equals(hnlc) ||
-                        (feedid == null && "x-dmaap-dr-received".equals(hnlc)) ||
-                        (hnlc.startsWith("x-") && !hnlc.startsWith("x-dmaap-dr-"))) {
+                if ((isput && ("content-type".equals(hnlc)
+                        || "content-language".equals(hnlc)
+                        || "content-md5".equals(hnlc)
+                        || "content-range".equals(hnlc)))
+                        || "x-dmaap-dr-meta".equals(hnlc)
+                        || (feedid == null && "x-dmaap-dr-received".equals(hnlc))
+                        || (hnlc.startsWith("x-") && !hnlc.startsWith("x-dmaap-dr-"))) {
                     Enumeration hvals = req.getHeaders(hn);
                     while (hvals.hasMoreElements()) {
                         String hv = (String) hvals.nextElement();
@@ -469,11 +469,11 @@ public class NodeServlet extends HttpServlet {
 
     private String writeInputStreamToFile(HttpServletRequest req, File data) {
         byte[] buf = new byte[1024 * 1024];
-        int i;
+        int bytesRead;
         try (OutputStream dos = new FileOutputStream(data);
                 InputStream is = req.getInputStream()) {
-            while ((i = is.read(buf)) > 0) {
-                dos.write(buf, 0, i);
+            while ((bytesRead = is.read(buf)) > 0) {
+                dos.write(buf, 0, bytesRead);
             }
         } catch (IOException ioe) {
             eelfLogger.error("NODE0530 Exception common: " + ioe, ioe);
@@ -497,8 +497,8 @@ public class NodeServlet extends HttpServlet {
         final String FROM_DR_MESSAGE = ".M) from DR Node: ";
         try {
             fileid = fileid.substring(8);
-            int i = fileid.indexOf('/');
-            if (i == -1 || i == fileid.length() - 1) {
+            int index = fileid.indexOf('/');
+            if (index == -1 || index == fileid.length() - 1) {
                 eelfLogger.error("NODE0112 Rejecting bad URI for DELETE of " + req.getPathInfo() + FROM + req
                         .getRemoteAddr());
                 resp.sendError(HttpServletResponse.SC_NOT_FOUND,
@@ -506,9 +506,9 @@ public class NodeServlet extends HttpServlet {
                 eelfLogger.info(EelfMsgs.EXIT);
                 return;
             }
-            String subscriptionId = fileid.substring(0, i);
+            String subscriptionId = fileid.substring(0, index);
             int subId = Integer.parseInt(subscriptionId);
-            pubid = fileid.substring(i + 1);
+            pubid = fileid.substring(index + 1);
             String errorMessage = "Unable to delete files (" + pubid + ", " + pubid + FROM_DR_MESSAGE
                     + config.getMyName() + ".";
             int subIdDir = subId - (subId % 100);
@@ -517,7 +517,7 @@ public class NodeServlet extends HttpServlet {
             }
             boolean result = delivery.markTaskSuccess(config.getSpoolBase() + "/s/" + subIdDir + "/" + subId, pubid);
             if (result) {
-                eelfLogger.info("NODE0115 Successfully deleted files (" + pubid + ", " + pubid + FROM_DR_MESSAGE
+                eelfLogger.debug("NODE0115 Successfully deleted files (" + pubid + ", " + pubid + FROM_DR_MESSAGE
                         + config.getMyName());
                 resp.setStatus(HttpServletResponse.SC_OK);
                 eelfLogger.info(EelfMsgs.EXIT);
@@ -571,8 +571,8 @@ public class NodeServlet extends HttpServlet {
                 return false;
             }
         } catch (NullPointerException npe) {
-            eelfLogger.error("NODE0114 " + errorMessage + " Error: Subscription " + subscriptionId +
-                    " does not exist", npe);
+            eelfLogger.error("NODE0114 " + errorMessage + " Error: Subscription " + subscriptionId
+                    " does not exist", npe);
             resp.sendError(HttpServletResponse.SC_NOT_FOUND);
             eelfLogger.info(EelfMsgs.EXIT);
             return false;