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=e3b9c57b5b4873bc64dc524c779cb72a76d1698b;hb=6c78b3e6a0a67c73f931337356a172cc69cee0e8;hp=77bcbddcc8485faa8215c77687dc8c5761901312;hpb=b60213dc26540543f500b3442b061565907c3cf8;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 77bcbddc..e3b9c57b 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 @@ -68,6 +68,12 @@ public class LogServlet extends BaseServlet { private static final long TWENTYFOUR_HOURS = (24 * 60 * 60 * 1000L); 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 final String PUBLISHSQL = "publishSQL"; + private static final String STATUSSQL = "statusSQL"; + private static final String RESULTSQL = "resultSQL"; + private static final String FILENAMESQL = "filenameSQL"; + private static final String TIMESQL = "timeSQL"; + private static final String LOG_RECORDSSQL = "select * from LOG_RECORDS where FEEDID = "; private final boolean isfeedlog; @@ -158,8 +164,8 @@ public class LogServlet extends BaseServlet { eventlogger.error(elr.toString()); sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger); } finally { - eelfLogger.info(EelfMsgs.EXIT); - } + eelfLogger.info(EelfMsgs.EXIT); + } } /** * GET a logging URL -- retrieve logging data for a feed or subscription. @@ -226,7 +232,7 @@ public class LogServlet extends BaseServlet { } out.print("]"); } catch (IOException ioe) { - eventlogger.error("IOException: " + ioe.getMessage()); + eventlogger.error("PROV0141 LogServlet.doGet: " + ioe.getMessage(), ioe); } } finally { eelfLogger.info(EelfMsgs.EXIT); @@ -240,13 +246,13 @@ public class LogServlet extends BaseServlet { 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.error(elr.toString()); - sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger); + 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.error(elr.toString()); + sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger); } finally { eelfLogger.info(EelfMsgs.EXIT); } @@ -259,13 +265,13 @@ public class LogServlet extends BaseServlet { 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.error(elr.toString()); - sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger); + 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.error(elr.toString()); + sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger); } finally { eelfLogger.info(EelfMsgs.EXIT); } @@ -275,7 +281,7 @@ public class LogServlet extends BaseServlet { Map map = new HashMap<>(); String s = req.getParameter("type"); if (s != null) { - if (s.equals("pub") || s.equals("del") || s.equals("exp")) { + if ("pub".equals(s) || "del".equals(s) || "exp".equals(s)) { map.put("type", s); } else { map.put("err", "bad type"); @@ -284,11 +290,11 @@ public class LogServlet extends BaseServlet { } else { map.put("type", "all"); } - map.put("publishSQL", ""); - map.put("statusSQL", ""); - map.put("resultSQL", ""); - map.put("reasonSQL", ""); - map.put("filenameSQL", ""); + map.put(PUBLISHSQL, ""); + map.put(STATUSSQL, ""); + map.put(RESULTSQL, ""); + map.put(REASON_SQL, ""); + map.put(FILENAMESQL, ""); s = req.getParameter("publishId"); if (s != null) { @@ -296,50 +302,57 @@ public class LogServlet extends BaseServlet { map.put("err", "bad publishId"); return map; } - map.put("publishSQL", " AND PUBLISH_ID = '"+s+"'"); + map.put(PUBLISHSQL, " AND PUBLISH_ID = '"+s+"'"); } s = req.getParameter("filename"); if (s != null) { - map.put("filenameSQL", " AND FILENAME = '"+s+"'"); + map.put(FILENAMESQL, " AND FILENAME = '" + s + "'"); } s = req.getParameter("statusCode"); if (s != null) { String sql = null; - if (s.equals("success")) { - sql = " AND STATUS >= 200 AND STATUS < 300"; - } else if (s.equals("redirect")) { - sql = " AND STATUS >= 300 AND STATUS < 400"; - } else if (s.equals("failure")) { - sql = " AND STATUS >= 400"; - } else { - try { - Integer n = Integer.parseInt(s); - if ((n >= 100 && n < 600) || (n == -1)) - sql = " AND STATUS = " + n; - } catch (NumberFormatException e) { - } + switch (s) { + case "success": + sql = " AND STATUS >= 200 AND STATUS < 300"; + break; + case "redirect": + sql = " AND STATUS >= 300 AND STATUS < 400"; + break; + case "failure": + sql = " AND STATUS >= 400"; + break; + default: + try { + int n = Integer.parseInt(s); + if ((n >= 100 && n < 600) || (n == -1)) { + sql = " AND STATUS = " + n; + } + } catch (NumberFormatException e) { + intlogger.error("Failed to parse input", e); + } + break; } if (sql == null) { map.put("err", "bad statusCode"); return map; } - map.put("statusSQL", sql); - map.put("resultSQL", sql.replaceAll("STATUS", "RESULT")); + map.put(STATUSSQL, sql); + map.put(RESULTSQL, sql.replaceAll("STATUS", "RESULT")); } s = req.getParameter("expiryReason"); if (s != null) { map.put("type", "exp"); - if (s.equals("notRetryable")) { - map.put("reasonSQL", " AND REASON = 'notRetryable'"); - } else if (s.equals("retriesExhausted")) { - map.put("reasonSQL", " AND REASON = 'retriesExhausted'"); - } else if (s.equals("diskFull")) { - map.put("reasonSQL", " AND REASON = 'diskFull'"); - } else if (s.equals("other")) { - map.put("reasonSQL", " AND REASON = 'other'"); + if ("notRetryable".equals(s)) { + map.put(REASON_SQL, " AND REASON = 'notRetryable'"); + } else if ("retriesExhausted".equals(s)) { + map.put(REASON_SQL, " AND REASON = 'retriesExhausted'"); + } else if ("diskFull".equals(s)) { + map.put(REASON_SQL, " AND REASON = 'diskFull'"); + } else if ("other".equals(s)) { + map.put(REASON_SQL, " AND REASON = 'other'"); } else { map.put("err", "bad expiryReason"); return map; @@ -364,7 +377,7 @@ public class LogServlet extends BaseServlet { } else if (etime == 0) { etime = stime + TWENTYFOUR_HOURS; } - map.put("timeSQL", String.format(" AND EVENT_TIME >= %d AND EVENT_TIME <= %d", stime, etime)); + map.put(TIMESQL, String.format(" AND EVENT_TIME >= %d AND EVENT_TIME <= %d", stime, etime)); return map; } private long getTimeFromParam(final String s) { @@ -377,70 +390,70 @@ public class LogServlet extends BaseServlet { Date d = sdf.parse(s); return d.getTime(); } catch (ParseException parseException) { - intlogger.error("Exception in getting Time :- "+parseException.getMessage(),parseException); + intlogger.error("Exception in getting Time :- " + parseException.getMessage(),parseException); } try { // Also allow a long (in ms); useful for testing - long n = Long.parseLong(s); - return n; + return Long.parseLong(s); } catch (NumberFormatException numberFormatException) { - intlogger.error("Exception in getting Time :- "+numberFormatException.getMessage(),numberFormatException); + intlogger.error("Exception in getting Time :- " + numberFormatException.getMessage(),numberFormatException); } - intlogger.info("Error parsing time="+s); + intlogger.info("Error parsing time=" + s); return -1; } private void getPublishRecordsForFeed(int feedid, RowHandler rh, Map map) { String type = map.get("type"); - if (type.equals("all") || type.equals("pub")) { - String sql = "select * from LOG_RECORDS where FEEDID = "+feedid + if ("all".equals(type) || "pub".equals(type)) { + String sql = LOG_RECORDSSQL + feedid + " AND TYPE = 'pub'" - + map.get("timeSQL") + map.get("publishSQL") + map.get("statusSQL") + map.get("filenameSQL"); + + map.get(TIMESQL) + map.get(PUBLISHSQL) + map.get(STATUSSQL) + map.get(FILENAMESQL); getRecordsForSQL(sql, rh); } } private void getDeliveryRecordsForFeed(int feedid, RowHandler rh, Map map) { String type = map.get("type"); - if (type.equals("all") || type.equals("del")) { - String sql = "select * from LOG_RECORDS where FEEDID = "+feedid + if ("all".equals(type) || "del".equals(type)) { + String sql = LOG_RECORDSSQL + feedid + " AND TYPE = 'del'" - + map.get("timeSQL") + map.get("publishSQL") + map.get("resultSQL"); + + map.get(TIMESQL) + map.get(PUBLISHSQL) + map.get(RESULTSQL); getRecordsForSQL(sql, rh); } } private void getDeliveryRecordsForSubscription(int subid, RowHandler rh, Map map) { String type = map.get("type"); - if (type.equals("all") || type.equals("del")) { - String sql = "select * from LOG_RECORDS where DELIVERY_SUBID = "+subid + if ("all".equals(type) || "del".equals(type)) { + String sql = "select * from LOG_RECORDS where DELIVERY_SUBID = " + subid + " AND TYPE = 'del'" - + map.get("timeSQL") + map.get("publishSQL") + map.get("resultSQL"); + + map.get(TIMESQL) + map.get(PUBLISHSQL) + map.get(RESULTSQL); getRecordsForSQL(sql, rh); } } private void getExpiryRecordsForFeed(int feedid, RowHandler rh, Map map) { String type = map.get("type"); - if (type.equals("all") || type.equals("exp")) { - String st = map.get("statusSQL"); + if ("all".equals(type) || "exp".equals(type)) { + String st = map.get(STATUSSQL); if (st == null || st.length() == 0) { - String sql = "select * from LOG_RECORDS where FEEDID = "+feedid + String sql = LOG_RECORDSSQL + feedid + " AND TYPE = 'exp'" - + map.get("timeSQL") + map.get("publishSQL") + map.get("reasonSQL"); + + map.get(TIMESQL) + map.get(PUBLISHSQL) + map.get(REASON_SQL); getRecordsForSQL(sql, rh); } } } private void getExpiryRecordsForSubscription(int subid, RowHandler rh, Map map) { String type = map.get("type"); - if (type.equals("all") || type.equals("exp")) { - String st = map.get("statusSQL"); + if ("all".equals(type) || "exp".equals(type)) { + String st = map.get(STATUSSQL); if (st == null || st.length() == 0) { - String sql = "select * from LOG_RECORDS where DELIVERY_SUBID = "+subid + String sql = "select * from LOG_RECORDS where DELIVERY_SUBID = " + subid + " AND TYPE = 'exp'" - + map.get("timeSQL") + map.get("publishSQL") + map.get("reasonSQL"); + + map.get(TIMESQL) + map.get(PUBLISHSQL) + map.get(REASON_SQL); getRecordsForSQL(sql, rh); } } } + private void getRecordsForSQL(String sql, RowHandler rh) { intlogger.debug(sql); long start = System.currentTimeMillis(); @@ -448,19 +461,19 @@ public class LogServlet extends BaseServlet { Connection conn = null; try { conn = db.getConnection(); - try( Statement stmt = conn.createStatement()){ - try(ResultSet rs = stmt.executeQuery(sql)){ - while (rs.next()) { - rh.handleRow(rs); - } - } - } + try (Statement stmt = conn.createStatement()) { + try (ResultSet rs = stmt.executeQuery(sql)) { + while (rs.next()) { + rh.handleRow(rs); + } + } + } } catch (SQLException sqlException) { - intlogger.info("Failed to get Records. Exception = " +sqlException.getMessage(),sqlException); + intlogger.info("Failed to get Records. Exception = " + sqlException.getMessage(),sqlException); } finally { if (conn != null) db.release(conn); } - intlogger.debug("Time: " + (System.currentTimeMillis()-start) + " ms"); + intlogger.debug("Time: " + (System.currentTimeMillis() - start) + " ms"); } }