X-Git-Url: https://gerrit.onap.org/r/gitweb?p=dmaap%2Fdatarouter.git;a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2FLogServlet.java;h=762ab4e5d2dee0dd10712b209ddfc45dac885b7a;hp=48a239214b92fbc713705c1ba8aaaeaecab39b0b;hb=9c6f3aa2db3c3bc632038f69ff680f48b5f28ec2;hpb=4261823d84c2b911b68cdf4cb4dc3be429ebe285 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 48a23921..762ab4e5 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 @@ -52,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. @@ -62,18 +64,23 @@ 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 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 boolean isfeedlog; + private final boolean isfeedlog; public abstract class RowHandler { private final ServletOutputStream out; private final String[] fields; - public boolean firstrow; + private boolean firstrow; public RowHandler(ServletOutputStream out, String fieldparam, boolean b) { this.out = out; @@ -98,8 +105,8 @@ public class LogServlet extends BaseServlet { t += jo.toString(); out.print(t); firstrow = false; - } catch (Exception e) { - // ignore + } catch (Exception exception) { + intlogger.info("Failed to handle row. Exception = " + exception.getMessage(),exception); } } public abstract LOGJSONable buildJSONable(ResultSet rs) throws SQLException; @@ -145,124 +152,149 @@ 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("PROV0141 LogServlet.doGet: " + ioe.getMessage(), ioe); } + } 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")) { + if ("pub".equals(s) || "del".equals(s) || "exp".equals(s)) { map.put("type", s); } else { 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(PUBLISHSQL, ""); + map.put(STATUSSQL, ""); + map.put(RESULTSQL, ""); + map.put(REASON_SQL, ""); + map.put(FILENAMESQL, ""); s = req.getParameter("publishId"); if (s != null) { @@ -270,17 +302,22 @@ 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+"'"); } s = req.getParameter("statusCode"); if (s != null) { String sql = null; - if (s.equals("success")) { + if ("success".equals(s)) { sql = " AND STATUS >= 200 AND STATUS < 300"; - } else if (s.equals("redirect")) { + } else if ("redirect".equals(s)) { sql = " AND STATUS >= 300 AND STATUS < 400"; - } else if (s.equals("failure")) { + } else if ("failure".equals(s)) { sql = " AND STATUS >= 400"; } else { try { @@ -294,21 +331,21 @@ public class LogServlet extends BaseServlet { 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; @@ -333,7 +370,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) { @@ -341,17 +378,18 @@ 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(); - } catch (ParseException e) { + } catch (ParseException 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; - } catch (NumberFormatException e) { + return Long.parseLong(s); + } catch (NumberFormatException numberFormatException) { + intlogger.error("Exception in getting Time :- "+numberFormatException.getMessage(),numberFormatException); } intlogger.info("Error parsing time="+s); return -1; @@ -359,51 +397,51 @@ public class LogServlet extends BaseServlet { 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(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")) { + 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 + " 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); } } @@ -415,15 +453,15 @@ public class LogServlet extends BaseServlet { Connection conn = null; try { conn = db.getConnection(); - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery(sql); - while (rs.next()) { - rh.handleRow(rs); - } - rs.close(); - stmt.close(); - } catch (SQLException e) { - e.printStackTrace(); + 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); } finally { if (conn != null) db.release(conn);