X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2FLogServlet.java;h=77bcbddcc8485faa8215c77687dc8c5761901312;hb=b60213dc26540543f500b3442b061565907c3cf8;hp=101c9e6fc4c5e3647a4079a12b22328a9dcf1eeb;hpb=8b20afa47548717bcba098141b3ff76cf448410b;p=dmaap%2Fdatarouter.git diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/LogServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/LogServlet.java index 101c9e6f..77bcbddc 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/LogServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/LogServlet.java @@ -39,7 +39,6 @@ import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.apache.log4j.Logger; import org.onap.dmaap.datarouter.provisioning.beans.DeliveryRecord; import org.onap.dmaap.datarouter.provisioning.beans.EventLogRecord; import org.onap.dmaap.datarouter.provisioning.beans.ExpiryRecord; @@ -53,6 +52,8 @@ import org.onap.dmaap.datarouter.provisioning.utils.LOGJSONObject; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; +import static org.onap.dmaap.datarouter.provisioning.utils.HttpServletUtils.sendResponseError; + /** * This servlet handles requests to the <feedLogURL> and <subLogURL>, * which are generated by the provisioning server to handle the log query API. @@ -63,12 +64,12 @@ import com.att.eelf.configuration.EELFManager; @SuppressWarnings("serial") public class LogServlet extends BaseServlet { //Adding EELF Logger Rally:US664892 - private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("org.onap.dmaap.datarouter.provisioning.LogServlet"); + private static EELFLogger eelfLogger = EELFManager.getInstance().getLogger(LogServlet.class); private static final long TWENTYFOUR_HOURS = (24 * 60 * 60 * 1000L); - private static final String fmt1 = "yyyy-MM-dd'T'HH:mm:ss'Z'"; - private static final String fmt2 = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; + private static final String FMT_1 = "yyyy-MM-dd'T'HH:mm:ss'Z'"; + private static final String FMT_2 = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; - private static boolean isfeedlog; + private final boolean isfeedlog; public abstract class RowHandler { private final ServletOutputStream out; @@ -145,110 +146,133 @@ public class LogServlet extends BaseServlet { * DELETE a logging URL -- not supported. */ @Override - public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException { - setIpAndFqdnForEelf("doDelete"); - eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+""); - String message = "DELETE not allowed for the logURL."; - EventLogRecord elr = new EventLogRecord(req); - elr.setMessage(message); - elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED); - eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message); + public void doDelete(HttpServletRequest req, HttpServletResponse resp) { + setIpFqdnRequestIDandInvocationIDForEelf("doDelete", req); + eelfLogger.info(EelfMsgs.ENTRY); + try { + eelfLogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); + String message = "DELETE not allowed for the logURL."; + EventLogRecord elr = new EventLogRecord(req); + elr.setMessage(message); + elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED); + eventlogger.error(elr.toString()); + sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger); + } finally { + eelfLogger.info(EelfMsgs.EXIT); + } } /** * GET a logging URL -- retrieve logging data for a feed or subscription. * See the Logging API document for details on how this method should be invoked. */ @Override - public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { - setIpAndFqdnForEelf("doGet"); - eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+""); - int id = getIdFromPath(req); - if (id < 0) { - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing or bad feed/subscription number."); - return; - } - Map map = buildMapFromRequest(req); - if (map.get("err") != null) { - resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid arguments: "+map.get("err")); - return; - } - // check Accept: header?? + public void doGet(HttpServletRequest req, HttpServletResponse resp) { + setIpFqdnRequestIDandInvocationIDForEelf("doGet", req); + eelfLogger.info(EelfMsgs.ENTRY); + try { + eelfLogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + ""); + int id = getIdFromPath(req); + if (id < 0) { + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, "Missing or bad feed/subscription number.", eventlogger); + return; + } + Map map = buildMapFromRequest(req); + if (map.get("err") != null) { + sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST, "Invalid arguments: " + map.get("err"), eventlogger); + return; + } + // check Accept: header?? - resp.setStatus(HttpServletResponse.SC_OK); - resp.setContentType(LOGLIST_CONTENT_TYPE); - @SuppressWarnings("resource") - ServletOutputStream out = resp.getOutputStream(); - final String fields = req.getParameter("fields"); + resp.setStatus(HttpServletResponse.SC_OK); + resp.setContentType(LOGLIST_CONTENT_TYPE); - out.print("["); - if (isfeedlog) { - // Handle /feedlog/feedid request - boolean firstrow = true; + try (ServletOutputStream out = resp.getOutputStream()) { + final String fields = req.getParameter("fields"); - // 1. Collect publish records for this feed - RowHandler rh = new PublishRecordRowHandler(out, fields, firstrow); - getPublishRecordsForFeed(id, rh, map); - firstrow = rh.firstrow; + out.print("["); + if (isfeedlog) { + // Handle /feedlog/feedid request + boolean firstrow = true; - // 2. Collect delivery records for subscriptions to this feed - rh = new DeliveryRecordRowHandler(out, fields, firstrow); - getDeliveryRecordsForFeed(id, rh, map); - firstrow = rh.firstrow; + // 1. Collect publish records for this feed + RowHandler rh = new PublishRecordRowHandler(out, fields, firstrow); + getPublishRecordsForFeed(id, rh, map); + firstrow = rh.firstrow; - // 3. Collect expiry records for subscriptions to this feed - rh = new ExpiryRecordRowHandler(out, fields, firstrow); - getExpiryRecordsForFeed(id, rh, map); - } else { - // Handle /sublog/subid request - Subscription sub = Subscription.getSubscriptionById(id); - if (sub != null) { - // 1. Collect publish records for the feed this subscription feeds - RowHandler rh = new PublishRecordRowHandler(out, fields, true); - getPublishRecordsForFeed(sub.getFeedid(), rh, map); + // 2. Collect delivery records for subscriptions to this feed + rh = new DeliveryRecordRowHandler(out, fields, firstrow); + getDeliveryRecordsForFeed(id, rh, map); + firstrow = rh.firstrow; + + // 3. Collect expiry records for subscriptions to this feed + rh = new ExpiryRecordRowHandler(out, fields, firstrow); + getExpiryRecordsForFeed(id, rh, map); + } else { + // Handle /sublog/subid request + Subscription sub = Subscription.getSubscriptionById(id); + if (sub != null) { + // 1. Collect publish records for the feed this subscription feeds + RowHandler rh = new PublishRecordRowHandler(out, fields, true); + getPublishRecordsForFeed(sub.getFeedid(), rh, map); - // 2. Collect delivery records for this subscription - rh = new DeliveryRecordRowHandler(out, fields, rh.firstrow); - getDeliveryRecordsForSubscription(id, rh, map); + // 2. Collect delivery records for this subscription + rh = new DeliveryRecordRowHandler(out, fields, rh.firstrow); + getDeliveryRecordsForSubscription(id, rh, map); - // 3. Collect expiry records for this subscription - rh = new ExpiryRecordRowHandler(out, fields, rh.firstrow); - getExpiryRecordsForSubscription(id, rh, map); + // 3. Collect expiry records for this subscription + rh = new ExpiryRecordRowHandler(out, fields, rh.firstrow); + getExpiryRecordsForSubscription(id, rh, map); + } + } + out.print("]"); + } catch (IOException ioe) { + eventlogger.error("IOException: " + ioe.getMessage()); } + } finally { + eelfLogger.info(EelfMsgs.EXIT); } - out.print("\n]"); } /** * PUT a logging URL -- not supported. */ @Override - public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException { - setIpAndFqdnForEelf("doPut"); - eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+""); + public void doPut(HttpServletRequest req, HttpServletResponse resp) { + setIpFqdnRequestIDandInvocationIDForEelf("doPut", req); + eelfLogger.info(EelfMsgs.ENTRY); + try { + eelfLogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+""); String message = "PUT not allowed for the logURL."; EventLogRecord elr = new EventLogRecord(req); elr.setMessage(message); elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED); - eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message); + eventlogger.error(elr.toString()); + sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger); + } finally { + eelfLogger.info(EelfMsgs.EXIT); + } } /** * POST a logging URL -- not supported. */ @Override - public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { - setIpAndFqdnForEelf("doPost"); - eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF, req.getHeader(BEHALF_HEADER)); + public void doPost(HttpServletRequest req, HttpServletResponse resp) { + setIpFqdnRequestIDandInvocationIDForEelf("doPost", req); + eelfLogger.info(EelfMsgs.ENTRY); + try { + eelfLogger.info(EelfMsgs.MESSAGE_WITH_BEHALF, req.getHeader(BEHALF_HEADER)); String message = "POST not allowed for the logURL."; EventLogRecord elr = new EventLogRecord(req); elr.setMessage(message); elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED); - eventlogger.info(elr); - resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message); + eventlogger.error(elr.toString()); + sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger); + } finally { + eelfLogger.info(EelfMsgs.EXIT); + } } private Map buildMapFromRequest(HttpServletRequest req) { - Map map = new HashMap(); + Map map = new HashMap<>(); String s = req.getParameter("type"); if (s != null) { if (s.equals("pub") || s.equals("del") || s.equals("exp")) { @@ -257,12 +281,14 @@ public class LogServlet extends BaseServlet { map.put("err", "bad type"); return map; } - } else + } else { map.put("type", "all"); + } map.put("publishSQL", ""); map.put("statusSQL", ""); map.put("resultSQL", ""); map.put("reasonSQL", ""); + map.put("filenameSQL", ""); s = req.getParameter("publishId"); if (s != null) { @@ -273,6 +299,11 @@ public class LogServlet extends BaseServlet { map.put("publishSQL", " AND PUBLISH_ID = '"+s+"'"); } + s = req.getParameter("filename"); + if (s != null) { + map.put("filenameSQL", " AND FILENAME = '"+s+"'"); + } + s = req.getParameter("statusCode"); if (s != null) { String sql = null; @@ -341,7 +372,7 @@ public class LogServlet extends BaseServlet { return 0; try { // First, look for an RFC 3339 date - String fmt = (s.indexOf('.') > 0) ? fmt2 : fmt1; + String fmt = (s.indexOf('.') > 0) ? FMT_2 : FMT_1; SimpleDateFormat sdf = new SimpleDateFormat(fmt); Date d = sdf.parse(s); return d.getTime(); @@ -364,7 +395,7 @@ public class LogServlet extends BaseServlet { if (type.equals("all") || type.equals("pub")) { String sql = "select * from LOG_RECORDS where FEEDID = "+feedid + " AND TYPE = 'pub'" - + map.get("timeSQL") + map.get("publishSQL") + map.get("statusSQL"); + + map.get("timeSQL") + map.get("publishSQL") + map.get("statusSQL") + map.get("filenameSQL"); getRecordsForSQL(sql, rh); } }