X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-node%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fnode%2FNodeServlet.java;h=7e1d46d3ddba3a87be2f544e9b3e46f4ae38a0e9;hb=ee6fa61e2cd7df99891092709765235b6166a041;hp=9ddbc25af4d32c5f66e5ea40791729878e5f207b;hpb=9d8f44daae28d5a9c8113be7f280090b950b859e;p=dmaap%2Fdatarouter.git diff --git a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/NodeServlet.java b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/NodeServlet.java index 9ddbc25a..7e1d46d3 100644 --- a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/NodeServlet.java +++ b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/NodeServlet.java @@ -24,17 +24,17 @@ 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; @@ -46,6 +46,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; 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 @@ -63,24 +64,20 @@ 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(NodeServlet.class); static { - try { - final String ws = "\\s*"; - // assume that \\ and \" have been replaced by X - final String string = "\"[^\"]*\""; - //String string = "\"(?:[^\"\\\\]|\\\\.)*\""; - final String number = "[+-]?(?:\\.\\d+|(?:0|[1-9]\\d*)(?:\\.\\d*)?)(?:[eE][+-]?\\d+)?"; - final String value = "(?:" + string + "|" + number + "|null|true|false)"; - final String item = string + ws + ":" + ws + value + ws; - final String object = ws + "\\{" + ws + "(?:" + item + "(?:" + "," + ws + item + ")*)?\\}" + ws; - MetaDataPattern = Pattern.compile(object, Pattern.DOTALL); - } catch (Exception e) { - } + final String ws = "\\s*"; + // assume that \\ and \" have been replaced by X + final String string = "\"[^\"]*\""; + //String string = "\"(?:[^\"\\\\]|\\\\.)*\""; + final String number = "[+-]?(?:\\.\\d+|(?:0|[1-9]\\d*)(?:\\.\\d*)?)(?:[eE][+-]?\\d+)?"; + final String value = "(?:" + string + "|" + number + "|null|true|false)"; + final String item = string + ws + ":" + ws + value + ws; + final String object = ws + "\\{" + ws + "(?:" + item + "(?:" + "," + ws + item + ")*)?\\}" + ws; + MetaDataPattern = Pattern.compile(object, Pattern.DOTALL); } /** @@ -93,7 +90,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); } @@ -103,85 +100,65 @@ 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; - } - String path = req.getPathInfo(); - String qs = req.getQueryString(); - String ip = req.getRemoteAddr(); - if (qs != null) { - path = path + "?" + qs; - } - if ("/internal/fetchProv".equals(path)) { - config.gofetch(ip); - resp.setStatus(HttpServletResponse.SC_NO_CONTENT); - return; - } else if (path.startsWith("/internal/resetSubscription/")) { - String subid = path.substring(28); - if (subid.length() != 0 && subid.indexOf('/') == -1) { - NodeMain.resetQueue(subid, ip); - resp.setStatus(HttpServletResponse.SC_NO_CONTENT); - 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); + NodeUtils.setRequestIdAndInvocationId(req); + eelflogger.info(EelfMsgs.ENTRY); + try { + eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader("X-DMAAP-DR-ON-BEHALF-OF"), + getIdFromPath(req) + ""); + try { + if (down(resp)) { 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; + + } catch (IOException ioe) { + logger.error("IOException" + ioe.getMessage()); } - 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); + String path = req.getPathInfo(); + String qs = req.getQueryString(); + String ip = req.getRemoteAddr(); + if (qs != null) { + path = path + "?" + qs; + } + if ("/internal/fetchProv".equals(path)) { + config.gofetch(ip); + resp.setStatus(HttpServletResponse.SC_NO_CONTENT); return; + } else if (path.startsWith("/internal/resetSubscription/")) { + String subid = path.substring(28); + if (subid.length() != 0 && subid.indexOf('/') == -1) { + NodeMain.resetQueue(subid, ip); + resp.setStatus(HttpServletResponse.SC_NO_CONTENT); + return; + } } + + logger.info("NODE0103 Rejecting invalid GET of " + path + " from " + ip); + sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, logger); + } finally { + eelflogger.info(EelfMsgs.EXIT); } - logger.info("NODE0103 Rejecting invalid GET of " + path + " from " + ip); - resp.sendError(HttpServletResponse.SC_NOT_FOUND); } /** * 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); + NodeUtils.setRequestIdAndInvocationId(req); + eelflogger.info(EelfMsgs.ENTRY); + eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader("X-DMAAP-DR-ON-BEHALF-OF"), + getIdFromPath(req) + ""); + try { + common(req, resp, true); + } catch (IOException ioe) { + logger.error("IOException" + ioe.getMessage()); + eelflogger.info(EelfMsgs.EXIT); + } catch (ServletException se) { + logger.error("ServletException" + se.getMessage()); + eelflogger.info(EelfMsgs.EXIT); + } } /** @@ -189,28 +166,43 @@ 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); + NodeUtils.setRequestIdAndInvocationId(req); + eelflogger.info(EelfMsgs.ENTRY); + eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader("X-DMAAP-DR-ON-BEHALF-OF"), + getIdFromPath(req) + ""); + try { + common(req, resp, false); + } catch (IOException ioe) { + logger.error("IOException" + ioe.getMessage()); + eelflogger.info(EelfMsgs.EXIT); + } catch (ServletException se) { + logger.error("ServletException" + se.getMessage()); + eelflogger.info(EelfMsgs.EXIT); + } + } private void common(HttpServletRequest req, HttpServletResponse resp, boolean isput) - throws ServletException, IOException { + throws ServletException, IOException { if (down(resp)) { + eelflogger.info(EelfMsgs.EXIT); 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"); + eelflogger.info(EelfMsgs.EXIT); 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 /."); + "Invalid request URI. Expecting /."); + eelflogger.info(EelfMsgs.EXIT); return; } String feedid = null; @@ -218,8 +210,9 @@ 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"); + eelflogger.info(EelfMsgs.EXIT); return; } String ip = req.getRemoteAddr(); @@ -233,37 +226,41 @@ 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 /. Possible missing fileid."); + "Invalid request URI. Expecting /. Possible missing fileid."); + eelflogger.info(EelfMsgs.EXIT); return; } feedid = fileid.substring(0, i); fileid = fileid.substring(i + 1); pubid = config.getPublishId(); - xpubid = req.getHeader("X-ATT-DR-PUBLISH-ID"); + xpubid = req.getHeader("X-DMAAP-DR-PUBLISH-ID"); targets = config.getTargets(feedid); } else if (fileid.startsWith("/internal/publish/")) { if (!config.isAnotherNode(credentials, ip)) { logger.info("NODE0107 Rejecting unauthorized node-to-node transfer attempt from " + ip); resp.sendError(HttpServletResponse.SC_FORBIDDEN); + eelflogger.info(EelfMsgs.EXIT); return; } fileid = fileid.substring(18); - pubid = req.getHeader("X-ATT-DR-PUBLISH-ID"); - targets = config.parseRouting(req.getHeader("X-ATT-DR-ROUTING")); + pubid = req.getHeader("X-DMAAP-DR-PUBLISH-ID"); + targets = config.parseRouting(req.getHeader("X-DMAAP-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 /."); + "Invalid request URI. Expecting /."); + eelflogger.info(EelfMsgs.EXIT); 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 /."); + "Invalid request URI. Expecting /."); + eelflogger.info(EelfMsgs.EXIT); return; } String qs = req.getQueryString(); @@ -281,9 +278,11 @@ 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); + eelflogger.info(EelfMsgs.EXIT); return; } user = config.getAuthUser(feedid, credentials); @@ -296,12 +295,14 @@ 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); + eelflogger.info(EelfMsgs.EXIT); return; } - resp.setHeader("X-ATT-DR-PUBLISH-ID", pubid); + resp.setHeader("X-DMAAP-DR-PUBLISH-ID", pubid); } String fbase = config.getSpoolDir() + "/" + pubid; File data = new File(fbase); @@ -314,35 +315,45 @@ public class NodeServlet extends HttpServlet { mx.append(req.getMethod()).append('\t').append(fileid).append('\n'); Enumeration hnames = req.getHeaderNames(); String ctype = null; + Boolean hasRequestIdHeader = false; + Boolean hasInvocationIdHeader = false; 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-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-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(); if ("content-type".equals(hnlc)) { ctype = hv; } - if ("x-att-dr-meta".equals(hnlc)) { + if ("x-onap-requestid".equals(hnlc)) { + hasRequestIdHeader = true; + } + if ("x-invocationid".equals(hnlc)) { + hasInvocationIdHeader = true; + } + if ("x-dmaap-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"); + eelflogger.info(EelfMsgs.EXIT); 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"); + eelflogger.info(EelfMsgs.EXIT); return; } } @@ -350,7 +361,13 @@ public class NodeServlet extends HttpServlet { } } } - mx.append("X-ATT-DR-RECEIVED\t").append(rcvd).append('\n'); + if(!hasRequestIdHeader){ + mx.append("X-ONAP-RequestID\t").append(MDC.get("RequestId")).append('\n'); + } + if(!hasInvocationIdHeader){ + mx.append("X-InvocationID\t").append(MDC.get("InvocationId")).append('\n'); + } + mx.append("X-DMAAP-DR-RECEIVED\t").append(rcvd).append('\n'); String metadata = mx.toString(); byte[] buf = new byte[1024 * 1024]; int i; @@ -371,7 +388,8 @@ 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()); + eelflogger.info(EelfMsgs.EXIT); throw ioe; } Path dpath = Paths.get(fbase); @@ -386,7 +404,7 @@ public class NodeServlet extends HttpServlet { mw = new FileWriter(meta); mw.write(metadata); if (di.getSubId() == null) { - mw.write("X-ATT-DR-ROUTING\t" + t.getRouting() + "\n"); + mw.write("X-DMAAP-DR-ROUTING\t" + t.getRouting() + "\n"); } mw.close(); meta.renameTo(new File(dbase + ".M")); @@ -394,11 +412,12 @@ 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); + eelflogger.info(EelfMsgs.EXIT); throw ioe; } finally { if (is != null) {