Reverting changes from DMAAP-775
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / NodeServlet.java
index 5f85139..a223b98 100644 (file)
 
 package org.onap.dmaap.datarouter.node;
 
+import static org.onap.dmaap.datarouter.node.NodeUtils.sendResponseError;
+
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.Writer;
-import java.net.Socket;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -63,10 +63,9 @@ public class NodeServlet extends HttpServlet {
     private static Logger logger = Logger.getLogger("org.onap.dmaap.datarouter.node.NodeServlet");
     private static NodeConfigManager config;
     private static Pattern MetaDataPattern;
-    private static SubnetMatcher internalsubnet = new SubnetMatcher("135.207.136.128/25");
     //Adding EELF Logger Rally:US664892
     private static EELFLogger eelflogger = EELFManager.getInstance()
-        .getLogger("org.onap.dmaap.datarouter.node.NodeServlet");
+            .getLogger("org.onap.dmaap.datarouter.node.NodeServlet");
 
     static {
         final String ws = "\\s*";
@@ -90,7 +89,7 @@ public class NodeServlet extends HttpServlet {
 
     private boolean down(HttpServletResponse resp) throws IOException {
         if (config.isShutdown() || !config.isConfigured()) {
-            resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
+            sendResponseError(resp, HttpServletResponse.SC_SERVICE_UNAVAILABLE, logger);
             logger.info("NODE0102 Rejecting request: Service is being quiesced");
             return (true);
         }
@@ -100,12 +99,17 @@ public class NodeServlet extends HttpServlet {
     /**
      * Handle a GET for /internal/fetchProv
      */
-    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
         NodeUtils.setIpAndFqdnForEelf("doGet");
         eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader("X-ATT-DR-ON-BEHALF-OF"),
-            getIdFromPath(req) + "");
-        if (down(resp)) {
-            return;
+                getIdFromPath(req) + "");
+        try {
+            if (down(resp)) {
+                return;
+            }
+
+        } catch (IOException ioe) {
+            logger.error("IOException" + ioe.getMessage());
         }
         String path = req.getPathInfo();
         String qs = req.getQueryString();
@@ -125,60 +129,25 @@ public class NodeServlet extends HttpServlet {
                 return;
             }
         }
-        if (internalsubnet.matches(NodeUtils.getInetAddress(ip))) {
-            if (path.startsWith("/internal/logs/")) {
-                String f = path.substring(15);
-                File fn = new File(config.getLogDir() + "/" + f);
-                if (f.indexOf('/') != -1 || !fn.isFile()) {
-                    logger.info("NODE0103 Rejecting invalid GET of " + path + " from " + ip);
-                    resp.sendError(HttpServletResponse.SC_NOT_FOUND);
-                    return;
-                }
-                byte[] buf = new byte[65536];
-                resp.setContentType("text/plain");
-                resp.setContentLength((int) fn.length());
-                resp.setStatus(200);
-                try (InputStream is = new FileInputStream(fn)) {
-                    OutputStream os = resp.getOutputStream();
-                    int i;
-                    while ((i = is.read(buf)) > 0) {
-                        os.write(buf, 0, i);
-                    }
-                }
-                return;
-            }
-            if (path.startsWith("/internal/rtt/")) {
-                String xip = path.substring(14);
-                long st = System.currentTimeMillis();
-                String status = " unknown";
-                try {
-                    Socket s = new Socket(xip, 443);
-                    s.close();
-                    status = " connected";
-                } catch (Exception e) {
-                    status = " error " + e.toString();
-                }
-                long dur = System.currentTimeMillis() - st;
-                resp.setContentType("text/plain");
-                resp.setStatus(200);
-                byte[] buf = (dur + status + "\n").getBytes();
-                resp.setContentLength(buf.length);
-                resp.getOutputStream().write(buf);
-                return;
-            }
-        }
+
         logger.info("NODE0103 Rejecting invalid GET of " + path + " from " + ip);
-        resp.sendError(HttpServletResponse.SC_NOT_FOUND);
+        sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, logger);
     }
 
     /**
      * Handle all PUT requests
      */
-    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+    protected void doPut(HttpServletRequest req, HttpServletResponse resp) {
         NodeUtils.setIpAndFqdnForEelf("doPut");
         eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader("X-ATT-DR-ON-BEHALF-OF"),
-            getIdFromPath(req) + "");
-        common(req, resp, true);
+                getIdFromPath(req) + "");
+        try {
+            common(req, resp, true);
+        } catch (IOException ioe) {
+            logger.error("IOException" + ioe.getMessage());
+        } catch (ServletException se) {
+            logger.error("ServletException" + se.getMessage());
+        }
     }
 
     /**
@@ -187,27 +156,34 @@ public class NodeServlet extends HttpServlet {
     protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         NodeUtils.setIpAndFqdnForEelf("doDelete");
         eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader("X-ATT-DR-ON-BEHALF-OF"),
-            getIdFromPath(req) + "");
-        common(req, resp, false);
+                getIdFromPath(req) + "");
+        try {
+            common(req, resp, false);
+        } catch (IOException ioe) {
+            logger.error("IOException" + ioe.getMessage());
+        } catch (ServletException se) {
+            logger.error("ServletException" + se.getMessage());
+        }
     }
 
     private void common(HttpServletRequest req, HttpServletResponse resp, boolean isput)
-        throws ServletException, IOException {
+            throws ServletException, IOException {
         if (down(resp)) {
             return;
         }
         if (!req.isSecure()) {
             logger.info(
-                "NODE0104 Rejecting insecure PUT or DELETE of " + req.getPathInfo() + " from " + req.getRemoteAddr());
+                    "NODE0104 Rejecting insecure PUT or DELETE of " + req.getPathInfo() + " from " + req
+                            .getRemoteAddr());
             resp.sendError(HttpServletResponse.SC_FORBIDDEN, "https required on publish requests");
             return;
         }
         String fileid = req.getPathInfo();
         if (fileid == null) {
             logger.info("NODE0105 Rejecting bad URI for PUT or DELETE of " + req.getPathInfo() + " from " + req
-                .getRemoteAddr());
+                    .getRemoteAddr());
             resp.sendError(HttpServletResponse.SC_NOT_FOUND,
-                "Invalid request URI.  Expecting <feed-publishing-url>/<fileid>.");
+                    "Invalid request URI.  Expecting <feed-publishing-url>/<fileid>.");
             return;
         }
         String feedid = null;
@@ -215,7 +191,7 @@ public class NodeServlet extends HttpServlet {
         String credentials = req.getHeader("Authorization");
         if (credentials == null) {
             logger.info("NODE0106 Rejecting unauthenticated PUT or DELETE of " + req.getPathInfo() + " from " + req
-                .getRemoteAddr());
+                    .getRemoteAddr());
             resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Authorization header required");
             return;
         }
@@ -230,9 +206,9 @@ public class NodeServlet extends HttpServlet {
             int i = fileid.indexOf('/');
             if (i == -1 || i == fileid.length() - 1) {
                 logger.info("NODE0105 Rejecting bad URI for PUT or DELETE of " + req.getPathInfo() + " from " + req
-                    .getRemoteAddr());
+                        .getRemoteAddr());
                 resp.sendError(HttpServletResponse.SC_NOT_FOUND,
-                    "Invalid request URI.  Expecting <feed-publishing-url>/<fileid>.  Possible missing fileid.");
+                        "Invalid request URI.  Expecting <feed-publishing-url>/<fileid>.  Possible missing fileid.");
                 return;
             }
             feedid = fileid.substring(0, i);
@@ -251,16 +227,16 @@ public class NodeServlet extends HttpServlet {
             targets = config.parseRouting(req.getHeader("X-ATT-DR-ROUTING"));
         } else {
             logger.info("NODE0105 Rejecting bad URI for PUT or DELETE of " + req.getPathInfo() + " from " + req
-                .getRemoteAddr());
+                    .getRemoteAddr());
             resp.sendError(HttpServletResponse.SC_NOT_FOUND,
-                "Invalid request URI.  Expecting <feed-publishing-url>/<fileid>.");
+                    "Invalid request URI.  Expecting <feed-publishing-url>/<fileid>.");
             return;
         }
         if (fileid.indexOf('/') != -1) {
             logger.info("NODE0105 Rejecting bad URI for PUT or DELETE of " + req.getPathInfo() + " from " + req
-                .getRemoteAddr());
+                    .getRemoteAddr());
             resp.sendError(HttpServletResponse.SC_NOT_FOUND,
-                "Invalid request URI.  Expecting <feed-publishing-url>/<fileid>.");
+                    "Invalid request URI.  Expecting <feed-publishing-url>/<fileid>.");
             return;
         }
         String qs = req.getQueryString();
@@ -278,8 +254,9 @@ public class NodeServlet extends HttpServlet {
             String reason = config.isPublishPermitted(feedid, credentials, ip);
             if (reason != null) {
                 logger.info(
-                    "NODE0111 Rejecting unauthorized publish attempt to feed " + feedid + " fileid " + fileid + " from "
-                        + ip + " reason " + reason);
+                        "NODE0111 Rejecting unauthorized publish attempt to feed " + feedid + " fileid " + fileid
+                                + " from "
+                                + ip + " reason " + reason);
                 resp.sendError(HttpServletResponse.SC_FORBIDDEN, reason);
                 return;
             }
@@ -293,8 +270,9 @@ public class NodeServlet extends HttpServlet {
                 }
                 String redirto = "https://" + newnode + port + "/publish/" + feedid + "/" + fileid;
                 logger.info(
-                    "NODE0108 Redirecting publish attempt for feed " + feedid + " user " + user + " ip " + ip + " to "
-                        + redirto);
+                        "NODE0108 Redirecting publish attempt for feed " + feedid + " user " + user + " ip " + ip
+                                + " to "
+                                + redirto);
                 resp.sendRedirect(redirto);
                 return;
             }
@@ -315,12 +293,12 @@ public class NodeServlet extends HttpServlet {
                 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-att-dr-meta".equals(hnlc) ||
-                    (feedid == null && "x-att-dr-received".equals(hnlc)) ||
-                    (hnlc.startsWith("x-") && !hnlc.startsWith("x-att-dr-"))) {
+                        "content-language".equals(hnlc) ||
+                        "content-md5".equals(hnlc) ||
+                        "content-range".equals(hnlc))) ||
+                        "x-att-dr-meta".equals(hnlc) ||
+                        (feedid == null && "x-att-dr-received".equals(hnlc)) ||
+                        (hnlc.startsWith("x-") && !hnlc.startsWith("x-att-dr-"))) {
                     Enumeration hvals = req.getHeaders(hn);
                     while (hvals.hasMoreElements()) {
                         String hv = (String) hvals.nextElement();
@@ -330,15 +308,15 @@ public class NodeServlet extends HttpServlet {
                         if ("x-att-dr-meta".equals(hnlc)) {
                             if (hv.length() > 4096) {
                                 logger.info(
-                                    "NODE0109 Rejecting publish attempt with metadata too long for feed " + feedid
-                                        + " user " + user + " ip " + ip);
+                                        "NODE0109 Rejecting publish attempt with metadata too long for feed " + feedid
+                                                + " user " + user + " ip " + ip);
                                 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Metadata too long");
                                 return;
                             }
                             if (!MetaDataPattern.matcher(hv.replaceAll("\\\\.", "X")).matches()) {
                                 logger.info(
-                                    "NODE0109 Rejecting publish attempt with malformed metadata for feed " + feedid
-                                        + " user " + user + " ip " + ip);
+                                        "NODE0109 Rejecting publish attempt with malformed metadata for feed " + feedid
+                                                + " user " + user + " ip " + ip);
                                 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Malformed metadata");
                                 return;
                             }
@@ -368,7 +346,7 @@ public class NodeServlet extends HttpServlet {
                 } catch (Exception e) {
                 }
                 StatusLog.logPubFail(pubid, feedid, logurl, req.getMethod(), ctype, exlen, data.length(), ip, user,
-                    ioe.getMessage());
+                        ioe.getMessage());
                 throw ioe;
             }
             Path dpath = Paths.get(fbase);
@@ -391,11 +369,11 @@ public class NodeServlet extends HttpServlet {
             resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
             resp.getOutputStream().close();
             StatusLog.logPub(pubid, feedid, logurl, req.getMethod(), ctype, data.length(), ip, user,
-                HttpServletResponse.SC_NO_CONTENT);
+                    HttpServletResponse.SC_NO_CONTENT);
         } catch (IOException ioe) {
             logger.info(
-                "NODE0110 IO Exception receiving publish attempt for feed " + feedid + " user " + user + " ip " + ip
-                    + " " + ioe.toString(), ioe);
+                    "NODE0110 IO Exception receiving publish attempt for feed " + feedid + " user " + user + " ip " + ip
+                            + " " + ioe.toString(), ioe);
             throw ioe;
         } finally {
             if (is != null) {