Refactor Prov DB handling
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / LogServlet.java
index 0dbb3f5..9cde480 100644 (file)
@@ -7,9 +7,9 @@
  * * Licensed under the Apache License, Version 2.0 (the "License");\r
  * * you may not use this file except in compliance with the License.\r
  * * You may obtain a copy of the License at\r
- * * \r
+ * *\r
  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
+ * *\r
  *  * Unless required by applicable law or agreed to in writing, software\r
  * * distributed under the License is distributed on an "AS IS" BASIS,\r
  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
 \r
 package org.onap.dmaap.datarouter.provisioning;\r
 \r
+import static org.onap.dmaap.datarouter.provisioning.utils.HttpServletUtils.sendResponseError;\r
+\r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
 import java.io.IOException;\r
 import java.sql.Connection;\r
+import java.sql.PreparedStatement;\r
 import java.sql.ResultSet;\r
 import java.sql.SQLException;\r
-import java.sql.Statement;\r
 import java.text.ParseException;\r
 import java.text.SimpleDateFormat;\r
 import java.util.Date;\r
 import java.util.HashMap;\r
 import java.util.Map;\r
-\r
 import javax.servlet.ServletOutputStream;\r
 import javax.servlet.http.HttpServletRequest;\r
 import javax.servlet.http.HttpServletResponse;\r
-\r
 import org.onap.dmaap.datarouter.provisioning.beans.DeliveryRecord;\r
 import org.onap.dmaap.datarouter.provisioning.beans.EventLogRecord;\r
 import org.onap.dmaap.datarouter.provisioning.beans.ExpiryRecord;\r
@@ -46,11 +48,9 @@ import org.onap.dmaap.datarouter.provisioning.beans.LOGJSONable;
 import org.onap.dmaap.datarouter.provisioning.beans.PublishRecord;\r
 import org.onap.dmaap.datarouter.provisioning.beans.Subscription;\r
 import org.onap.dmaap.datarouter.provisioning.eelf.EelfMsgs;\r
-import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
 import org.onap.dmaap.datarouter.provisioning.utils.LOGJSONObject;\r
+import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;\r
 \r
-import com.att.eelf.configuration.EELFLogger;\r
-import com.att.eelf.configuration.EELFManager;\r
 \r
 /**\r
  * This servlet handles requests to the <feedLogURL> and  <subLogURL>,\r
@@ -60,374 +60,440 @@ import com.att.eelf.configuration.EELFManager;
  * @version $Id: LogServlet.java,v 1.11 2014/03/28 17:27:02 eby Exp $\r
  */\r
 @SuppressWarnings("serial")\r
+\r
 public class LogServlet extends BaseServlet {\r
-       //Adding EELF Logger Rally:US664892  \r
-    private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("org.onap.dmaap.datarouter.provisioning.LogServlet");\r
-\r
-       private static final long TWENTYFOUR_HOURS = (24 * 60 * 60 * 1000L);\r
-       private static final String fmt1 = "yyyy-MM-dd'T'HH:mm:ss'Z'";\r
-       private static final String fmt2 = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";\r
-\r
-       private boolean isfeedlog;\r
-\r
-       public abstract class RowHandler {\r
-               private final ServletOutputStream out;\r
-               private final String[] fields;\r
-               public boolean firstrow;\r
-\r
-               public RowHandler(ServletOutputStream out, String fieldparam, boolean b) {\r
-                       this.out = out;\r
-                       this.firstrow = b;\r
-                       this.fields = (fieldparam != null) ? fieldparam.split(":") : null;\r
-               }\r
-               public void handleRow(ResultSet rs) {\r
-                       try {\r
-                               LOGJSONable js = buildJSONable(rs);\r
-                               LOGJSONObject jo = js.asJSONObject();\r
-                               if (fields != null) {\r
-                                       // filter out unwanted fields\r
-                                       LOGJSONObject j2 = new LOGJSONObject();\r
-                                       for (String key : fields) {\r
-                                               Object v = jo.opt(key);\r
-                                               if (v != null)\r
-                                                       j2.put(key, v);\r
-                                       }\r
-                                       jo = j2;\r
-                               }\r
-                               String t = firstrow ? "\n" : ",\n";\r
-                               t += jo.toString();\r
-                               out.print(t);\r
-                               firstrow = false;\r
-                       } catch (Exception e) {\r
-                               // ignore\r
-                       }\r
-               }\r
-               public abstract LOGJSONable buildJSONable(ResultSet rs) throws SQLException;\r
-       }\r
-       public class PublishRecordRowHandler extends RowHandler {\r
-               public PublishRecordRowHandler(ServletOutputStream out, String fields, boolean b) {\r
-                       super(out, fields, b);\r
-               }\r
-               @Override\r
-               public LOGJSONable buildJSONable(ResultSet rs) throws SQLException {\r
-                       return new PublishRecord(rs);\r
-               }\r
-       }\r
-       public class DeliveryRecordRowHandler extends RowHandler {\r
-               public DeliveryRecordRowHandler(ServletOutputStream out, String fields, boolean b) {\r
-                       super(out, fields, b);\r
-               }\r
-               @Override\r
-               public LOGJSONable buildJSONable(ResultSet rs) throws SQLException {\r
-                       return new DeliveryRecord(rs);\r
-               }\r
-       }\r
-       public class ExpiryRecordRowHandler extends RowHandler {\r
-               public ExpiryRecordRowHandler(ServletOutputStream out, String fields, boolean b) {\r
-                       super(out, fields, b);\r
-               }\r
-               @Override\r
-               public LOGJSONable buildJSONable(ResultSet rs) throws SQLException {\r
-                       return new ExpiryRecord(rs);\r
-               }\r
-       }\r
-\r
-       /**\r
-        * This class must be created from either a {@link FeedLogServlet} or a {@link SubLogServlet}.\r
-        * @param isFeedLog boolean to handle those places where a feedlog request is different from\r
-        * a sublog request\r
-        */\r
-       protected LogServlet(boolean isFeedLog) {\r
-               this.isfeedlog = isFeedLog;\r
-       }\r
-\r
-       /**\r
-        * DELETE a logging URL -- not supported.\r
-        */\r
-       @Override\r
-       public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {\r
-               setIpAndFqdnForEelf("doDelete");\r
-               eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");\r
-               String message = "DELETE not allowed for the logURL.";\r
-               EventLogRecord elr = new EventLogRecord(req);\r
-               elr.setMessage(message);\r
-               elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);\r
-               eventlogger.info(elr);\r
-               resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message);\r
-       }\r
-       /**\r
-        * GET a logging URL -- retrieve logging data for a feed or subscription.\r
-        * See the <b>Logging API</b> document for details on how this method should be invoked.\r
-        */\r
-       @Override\r
-       public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {\r
-               setIpAndFqdnForEelf("doGet");\r
-               eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");\r
-               int id = getIdFromPath(req);\r
-               if (id < 0) {\r
-                       resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing or bad feed/subscription number.");\r
-                       return;\r
-               }\r
-               Map<String, String> map = buildMapFromRequest(req);\r
-               if (map.get("err") != null) {\r
-                       resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid arguments: "+map.get("err"));\r
-                       return;\r
-               }\r
-               // check Accept: header??\r
-\r
-               resp.setStatus(HttpServletResponse.SC_OK);\r
-               resp.setContentType(LOGLIST_CONTENT_TYPE);\r
-               @SuppressWarnings("resource")\r
-               ServletOutputStream out = resp.getOutputStream();\r
-               final String fields = req.getParameter("fields");\r
-\r
-               out.print("[");\r
-               if (isfeedlog) {\r
-                       // Handle /feedlog/feedid request\r
-                       boolean firstrow = true;\r
-\r
-                       // 1. Collect publish records for this feed\r
-                       RowHandler rh = new PublishRecordRowHandler(out, fields, firstrow);\r
-                       getPublishRecordsForFeed(id, rh, map);\r
-                       firstrow = rh.firstrow;\r
-\r
-                       // 2. Collect delivery records for subscriptions to this feed\r
-                       rh = new DeliveryRecordRowHandler(out, fields, firstrow);\r
-                       getDeliveryRecordsForFeed(id, rh, map);\r
-                       firstrow = rh.firstrow;\r
-\r
-                       // 3. Collect expiry records for subscriptions to this feed\r
-                       rh = new ExpiryRecordRowHandler(out, fields, firstrow);\r
-                       getExpiryRecordsForFeed(id, rh, map);\r
-               } else {\r
-                       // Handle /sublog/subid request\r
-                       Subscription sub = Subscription.getSubscriptionById(id);\r
-                       if (sub != null) {\r
-                               // 1. Collect publish records for the feed this subscription feeds\r
-                               RowHandler rh = new PublishRecordRowHandler(out, fields, true);\r
-                               getPublishRecordsForFeed(sub.getFeedid(), rh, map);\r
-\r
-                               // 2. Collect delivery records for this subscription\r
-                               rh = new DeliveryRecordRowHandler(out, fields, rh.firstrow);\r
-                               getDeliveryRecordsForSubscription(id, rh, map);\r
-\r
-                               // 3. Collect expiry records for this subscription\r
-                               rh = new ExpiryRecordRowHandler(out, fields, rh.firstrow);\r
-                               getExpiryRecordsForSubscription(id, rh, map);\r
-                       }\r
-               }\r
-               out.print("\n]");\r
-       }\r
-       /**\r
-        * PUT a logging URL -- not supported.\r
-        */\r
-       @Override\r
-       public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {\r
-               setIpAndFqdnForEelf("doPut");\r
-               eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");\r
-               String message = "PUT not allowed for the logURL.";\r
-               EventLogRecord elr = new EventLogRecord(req);\r
-               elr.setMessage(message);\r
-               elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);\r
-               eventlogger.info(elr);\r
-               resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message);\r
-       }\r
-       /**\r
-        * POST a logging URL -- not supported.\r
-        */\r
-       @Override\r
-       public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {\r
-               setIpAndFqdnForEelf("doPost");\r
-               eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF, req.getHeader(BEHALF_HEADER));\r
-               String message = "POST not allowed for the logURL.";\r
-               EventLogRecord elr = new EventLogRecord(req);\r
-               elr.setMessage(message);\r
-               elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);\r
-               eventlogger.info(elr);\r
-               resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message);\r
-       }\r
-\r
-       private Map<String, String> buildMapFromRequest(HttpServletRequest req) {\r
-               Map<String, String> map = new HashMap<String, String>();\r
-               String s = req.getParameter("type");\r
-               if (s != null) {\r
-                       if (s.equals("pub") || s.equals("del") || s.equals("exp")) {\r
-                               map.put("type", s);\r
-                       } else {\r
-                               map.put("err", "bad type");\r
-                               return map;\r
-                       }\r
-               } else\r
-                       map.put("type", "all");\r
-               map.put("publishSQL", "");\r
-               map.put("statusSQL", "");\r
-               map.put("resultSQL", "");\r
-               map.put("reasonSQL", "");\r
-\r
-               s = req.getParameter("publishId");\r
-               if (s != null) {\r
-                       if (s.indexOf("'") >= 0) {\r
-                               map.put("err", "bad publishId");\r
-                               return map;\r
-                       }\r
-                       map.put("publishSQL", " AND PUBLISH_ID = '"+s+"'");\r
-               }\r
-\r
-               s = req.getParameter("statusCode");\r
-               if (s != null) {\r
-                       String sql = null;\r
-                       if (s.equals("success")) {\r
-                               sql = " AND STATUS >= 200 AND STATUS < 300";\r
-                       } else if (s.equals("redirect")) {\r
-                               sql = " AND STATUS >= 300 AND STATUS < 400";\r
-                       } else if (s.equals("failure")) {\r
-                               sql = " AND STATUS >= 400";\r
-                       } else {\r
-                               try {\r
-                                       Integer n = Integer.parseInt(s);\r
-                                       if ((n >= 100 && n < 600) || (n == -1))\r
-                                               sql = " AND STATUS = " + n;\r
-                               } catch (NumberFormatException e) {\r
-                               }\r
-                       }\r
-                       if (sql == null) {\r
-                               map.put("err", "bad statusCode");\r
-                               return map;\r
-                       }\r
-                       map.put("statusSQL", sql);\r
-                       map.put("resultSQL", sql.replaceAll("STATUS", "RESULT"));\r
-               }\r
-\r
-               s = req.getParameter("expiryReason");\r
-               if (s != null) {\r
-                       map.put("type", "exp");\r
-                       if (s.equals("notRetryable")) {\r
-                               map.put("reasonSQL", " AND REASON = 'notRetryable'");\r
-                       } else if (s.equals("retriesExhausted")) {\r
-                               map.put("reasonSQL", " AND REASON = 'retriesExhausted'");\r
-                       } else if (s.equals("diskFull")) {\r
-                               map.put("reasonSQL", " AND REASON = 'diskFull'");\r
-                       } else if (s.equals("other")) {\r
-                               map.put("reasonSQL", " AND REASON = 'other'");\r
-                       } else {\r
-                               map.put("err", "bad expiryReason");\r
-                               return map;\r
-                       }\r
-               }\r
-\r
-               long stime = getTimeFromParam(req.getParameter("start"));\r
-               if (stime < 0) {\r
-                       map.put("err", "bad start");\r
-                       return map;\r
-               }\r
-               long etime = getTimeFromParam(req.getParameter("end"));\r
-               if (etime < 0) {\r
-                       map.put("err", "bad end");\r
-                       return map;\r
-               }\r
-               if (stime == 0 && etime == 0) {\r
-                       etime = System.currentTimeMillis();\r
-                       stime = etime - TWENTYFOUR_HOURS;\r
-               } else if (stime == 0) {\r
-                       stime = etime - TWENTYFOUR_HOURS;\r
-               } else if (etime == 0) {\r
-                       etime = stime + TWENTYFOUR_HOURS;\r
-               }\r
-               map.put("timeSQL", String.format(" AND EVENT_TIME >= %d AND EVENT_TIME <= %d", stime, etime));\r
-               return map;\r
-       }\r
-       private long getTimeFromParam(final String s) {\r
-               if (s == null)\r
-                       return 0;\r
-               try {\r
-                       // First, look for an RFC 3339 date\r
-                       String fmt = (s.indexOf('.') > 0) ? fmt2 : fmt1;\r
-                       SimpleDateFormat sdf = new SimpleDateFormat(fmt);\r
-                       Date d = sdf.parse(s);\r
-                       return d.getTime();\r
-               } catch (ParseException e) {\r
-               }\r
-               try {\r
-                       // Also allow a long (in ms); useful for testing\r
-                       long n = Long.parseLong(s);\r
-                       return n;\r
-               } catch (NumberFormatException e) {\r
-               }\r
-               intlogger.info("Error parsing time="+s);\r
-               return -1;\r
-       }\r
-\r
-       private void getPublishRecordsForFeed(int feedid, RowHandler rh, Map<String, String> map) {\r
-               String type = map.get("type");\r
-               if (type.equals("all") || type.equals("pub")) {\r
-                       String sql = "select * from LOG_RECORDS where FEEDID = "+feedid\r
-                               + " AND TYPE = 'pub'"\r
-                               + map.get("timeSQL") + map.get("publishSQL") + map.get("statusSQL");\r
-                       getRecordsForSQL(sql, rh);\r
-               }\r
-       }\r
-       private void getDeliveryRecordsForFeed(int feedid, RowHandler rh, Map<String, String> map) {\r
-               String type = map.get("type");\r
-               if (type.equals("all") || type.equals("del")) {\r
-                       String sql = "select * from LOG_RECORDS where FEEDID = "+feedid\r
-                               + " AND TYPE = 'del'"\r
-                               + map.get("timeSQL") + map.get("publishSQL") + map.get("resultSQL");\r
-                       getRecordsForSQL(sql, rh);\r
-               }\r
-       }\r
-       private void getDeliveryRecordsForSubscription(int subid, RowHandler rh, Map<String, String> map) {\r
-               String type = map.get("type");\r
-               if (type.equals("all") || type.equals("del")) {\r
-                       String sql = "select * from LOG_RECORDS where DELIVERY_SUBID = "+subid\r
-                               + " AND TYPE = 'del'"\r
-                               + map.get("timeSQL") + map.get("publishSQL") + map.get("resultSQL");\r
-                       getRecordsForSQL(sql, rh);\r
-               }\r
-       }\r
-       private void getExpiryRecordsForFeed(int feedid, RowHandler rh, Map<String, String> map) {\r
-               String type = map.get("type");\r
-               if (type.equals("all") || type.equals("exp")) {\r
-                       String st = map.get("statusSQL");\r
-                       if (st == null || st.length() == 0) {\r
-                               String sql = "select * from LOG_RECORDS where FEEDID = "+feedid\r
-                                       + " AND TYPE = 'exp'"\r
-                                       + map.get("timeSQL") + map.get("publishSQL") + map.get("reasonSQL");\r
-                               getRecordsForSQL(sql, rh);\r
-                       }\r
-               }\r
-       }\r
-       private void getExpiryRecordsForSubscription(int subid, RowHandler rh, Map<String, String> map) {\r
-               String type = map.get("type");\r
-               if (type.equals("all") || type.equals("exp")) {\r
-                       String st = map.get("statusSQL");\r
-                       if (st == null || st.length() == 0) {\r
-                               String sql = "select * from LOG_RECORDS where DELIVERY_SUBID = "+subid\r
-                                       + " AND TYPE = 'exp'"\r
-                                       + map.get("timeSQL") + map.get("publishSQL") + map.get("reasonSQL");\r
-                               getRecordsForSQL(sql, rh);\r
-                       }\r
-               }\r
-       }\r
-       private void getRecordsForSQL(String sql, RowHandler rh) {\r
-               intlogger.debug(sql);\r
-               long start = System.currentTimeMillis();\r
-               DB db = new DB();\r
-               Connection conn = null;\r
-               try {\r
-                       conn = db.getConnection();\r
-                       Statement  stmt = conn.createStatement();\r
-                       ResultSet rs = stmt.executeQuery(sql);\r
-                       while (rs.next()) {\r
-                               rh.handleRow(rs);\r
-                       }\r
-                       rs.close();\r
-                       stmt.close();\r
-               } catch (SQLException e) {\r
-                       e.printStackTrace();\r
-               } finally {\r
-                       if (conn != null)\r
-                               db.release(conn);\r
-               }\r
-               intlogger.debug("Time: " + (System.currentTimeMillis()-start) + " ms");\r
-       }\r
+    //Adding EELF Logger Rally:US664892\r
+    private static EELFLogger eelfLogger = EELFManager.getInstance().getLogger(LogServlet.class);\r
+    private static final long TWENTYFOUR_HOURS = (24 * 60 * 60 * 1000L);\r
+    private static final String FMT_1 = "yyyy-MM-dd'T'HH:mm:ss'Z'";\r
+    private static final String FMT_2 = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";\r
+    private static final String PUBLISHSQL = "publishSQL";\r
+    private static final String STATUSSQL = "statusSQL";\r
+    private static final String RESULTSQL = "resultSQL";\r
+    private static final String FILENAMESQL = "filenameSQL";\r
+    private static final String TIMESQL = "timeSQL";\r
+    private static final String LOG_RECORDSSQL = "select * from LOG_RECORDS where FEEDID = ";\r
+\r
+    private final boolean isfeedlog;\r
+\r
+    public abstract static class RowHandler {\r
+        private final ServletOutputStream out;\r
+        private final String[] fields;\r
+        private boolean firstrow;\r
+\r
+        /**\r
+         * Row setter.\r
+         * @param out ServletOutputStream\r
+         * @param fieldparam String field\r
+         * @param bool boolean\r
+         */\r
+        RowHandler(ServletOutputStream out, String fieldparam, boolean bool) {\r
+            this.out = out;\r
+            this.firstrow = bool;\r
+            this.fields = (fieldparam != null) ? fieldparam.split(":") : null;\r
+        }\r
+\r
+        /**\r
+         * Handling row from DB.\r
+         * @param rs DB Resultset\r
+         */\r
+        void handleRow(ResultSet rs) {\r
+            try {\r
+                LOGJSONable js = buildJSONable(rs);\r
+                LOGJSONObject jo = js.asJSONObject();\r
+                if (fields != null) {\r
+                    // filter out unwanted fields\r
+                    LOGJSONObject j2 = new LOGJSONObject();\r
+                    for (String key : fields) {\r
+                        Object val = jo.opt(key);\r
+                        if (val != null) {\r
+                            j2.put(key, val);\r
+                        }\r
+                    }\r
+                    jo = j2;\r
+                }\r
+                String str = firstrow ? "\n" : ",\n";\r
+                str += jo.toString();\r
+                out.print(str);\r
+                firstrow = false;\r
+            } catch (Exception exception) {\r
+                intlogger.info("Failed to handle row. Exception = " + exception.getMessage(),exception);\r
+            }\r
+        }\r
+\r
+        public abstract LOGJSONable buildJSONable(ResultSet rs) throws SQLException;\r
+    }\r
+\r
+    public static class PublishRecordRowHandler extends RowHandler {\r
+        PublishRecordRowHandler(ServletOutputStream out, String fields, boolean bool) {\r
+            super(out, fields, bool);\r
+        }\r
+\r
+        @Override\r
+        public LOGJSONable buildJSONable(ResultSet rs) throws SQLException {\r
+            return new PublishRecord(rs);\r
+        }\r
+    }\r
+\r
+    public static class DeliveryRecordRowHandler extends RowHandler {\r
+        DeliveryRecordRowHandler(ServletOutputStream out, String fields, boolean bool) {\r
+            super(out, fields, bool);\r
+        }\r
+\r
+        @Override\r
+        public LOGJSONable buildJSONable(ResultSet rs) throws SQLException {\r
+            return new DeliveryRecord(rs);\r
+        }\r
+    }\r
+\r
+    public static class ExpiryRecordRowHandler extends RowHandler {\r
+        ExpiryRecordRowHandler(ServletOutputStream out, String fields, boolean bool) {\r
+            super(out, fields, bool);\r
+        }\r
+\r
+        @Override\r
+        public LOGJSONable buildJSONable(ResultSet rs) throws SQLException {\r
+            return new ExpiryRecord(rs);\r
+        }\r
+    }\r
+\r
+    /**\r
+     * This class must be created from either a {@link FeedLogServlet} or a {@link SubLogServlet}.\r
+     * @param isFeedLog boolean to handle those places where a feedlog request is different from a sublog request\r
+     */\r
+    LogServlet(boolean isFeedLog) {\r
+        this.isfeedlog = isFeedLog;\r
+    }\r
+\r
+    /**\r
+     * DELETE a logging URL -- not supported.\r
+     */\r
+    @Override\r
+    public void doDelete(HttpServletRequest req, HttpServletResponse resp) {\r
+        setIpFqdnRequestIDandInvocationIDForEelf("doDelete", req);\r
+        eelfLogger.info(EelfMsgs.ENTRY);\r
+        try {\r
+            eelfLogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID,\r
+                req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");\r
+            String message = "DELETE not allowed for the logURL.";\r
+            EventLogRecord elr = new EventLogRecord(req);\r
+            elr.setMessage(message);\r
+            elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);\r
+            eventlogger.error(elr.toString());\r
+            sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger);\r
+        } finally {\r
+            eelfLogger.info(EelfMsgs.EXIT);\r
+        }\r
+    }\r
+\r
+    /**\r
+     * GET a logging URL -- retrieve logging data for a feed or subscription.\r
+     * See the <b>Logging API</b> document for details on how this method should be invoked.\r
+     */\r
+    @Override\r
+    public void doGet(HttpServletRequest req, HttpServletResponse resp) {\r
+        setIpFqdnRequestIDandInvocationIDForEelf("doGet", req);\r
+        eelfLogger.info(EelfMsgs.ENTRY);\r
+        try {\r
+            eelfLogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID,\r
+                req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");\r
+            int id = getIdFromPath(req);\r
+            if (id < 0) {\r
+                sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST,\r
+                    "Missing or bad feed/subscription number.", eventlogger);\r
+                return;\r
+            }\r
+            Map<String, String> map = buildMapFromRequest(req);\r
+            if (map.get("err") != null) {\r
+                sendResponseError(resp, HttpServletResponse.SC_BAD_REQUEST,\r
+                    "Invalid arguments: " + map.get("err"), eventlogger);\r
+                return;\r
+            }\r
+            // check Accept: header??\r
+            resp.setStatus(HttpServletResponse.SC_OK);\r
+            resp.setContentType(LOGLIST_CONTENT_TYPE);\r
+            try (ServletOutputStream out = resp.getOutputStream()) {\r
+                final String fields = req.getParameter("fields");\r
+                out.print("[");\r
+                if (isfeedlog) {\r
+                    // Handle /feedlog/feedid request\r
+                    boolean firstrow = true;\r
+                    // 1. Collect publish records for this feed\r
+                    RowHandler rh = new PublishRecordRowHandler(out, fields, firstrow);\r
+                    getPublishRecordsForFeed(id, rh, map);\r
+                    firstrow = rh.firstrow;\r
+                    // 2. Collect delivery records for subscriptions to this feed\r
+                    rh = new DeliveryRecordRowHandler(out, fields, firstrow);\r
+                    getDeliveryRecordsForFeed(id, rh, map);\r
+                    firstrow = rh.firstrow;\r
+                    // 3. Collect expiry records for subscriptions to this feed\r
+                    rh = new ExpiryRecordRowHandler(out, fields, firstrow);\r
+                    getExpiryRecordsForFeed(id, rh, map);\r
+                } else {\r
+                    // Handle /sublog/subid request\r
+                    Subscription sub = Subscription.getSubscriptionById(id);\r
+                    if (sub != null) {\r
+                        // 1. Collect publish records for the feed this subscription feeds\r
+                        RowHandler rh = new PublishRecordRowHandler(out, fields, true);\r
+                        getPublishRecordsForFeed(sub.getFeedid(), rh, map);\r
+                        // 2. Collect delivery records for this subscription\r
+                        rh = new DeliveryRecordRowHandler(out, fields, rh.firstrow);\r
+                        getDeliveryRecordsForSubscription(id, rh, map);\r
+                        // 3. Collect expiry records for this subscription\r
+                        rh = new ExpiryRecordRowHandler(out, fields, rh.firstrow);\r
+                        getExpiryRecordsForSubscription(id, rh, map);\r
+                    }\r
+                }\r
+                out.print("]");\r
+            } catch (IOException ioe) {\r
+                eventlogger.error("PROV0141 LogServlet.doGet: " + ioe.getMessage(), ioe);\r
+            }\r
+        } finally {\r
+            eelfLogger.info(EelfMsgs.EXIT);\r
+        }\r
+    }\r
+\r
+    /**\r
+     * PUT a logging URL -- not supported.\r
+     */\r
+    @Override\r
+    public void doPut(HttpServletRequest req, HttpServletResponse resp) {\r
+        setIpFqdnRequestIDandInvocationIDForEelf("doPut", req);\r
+        eelfLogger.info(EelfMsgs.ENTRY);\r
+        try {\r
+            eelfLogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID,\r
+                req.getHeader(BEHALF_HEADER),getIdFromPath(req) + "");\r
+            String message = "PUT not allowed for the logURL.";\r
+            EventLogRecord elr = new EventLogRecord(req);\r
+            elr.setMessage(message);\r
+            elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);\r
+            eventlogger.error(elr.toString());\r
+            sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger);\r
+        } finally {\r
+            eelfLogger.info(EelfMsgs.EXIT);\r
+        }\r
+    }\r
+\r
+    /**\r
+     * POST a logging URL -- not supported.\r
+     */\r
+    @Override\r
+    public void doPost(HttpServletRequest req, HttpServletResponse resp) {\r
+        setIpFqdnRequestIDandInvocationIDForEelf("doPost", req);\r
+        eelfLogger.info(EelfMsgs.ENTRY);\r
+        try {\r
+            eelfLogger.info(EelfMsgs.MESSAGE_WITH_BEHALF, req.getHeader(BEHALF_HEADER));\r
+            String message = "POST not allowed for the logURL.";\r
+            EventLogRecord elr = new EventLogRecord(req);\r
+            elr.setMessage(message);\r
+            elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);\r
+            eventlogger.error(elr.toString());\r
+            sendResponseError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, message, eventlogger);\r
+        } finally {\r
+            eelfLogger.info(EelfMsgs.EXIT);\r
+        }\r
+    }\r
+\r
+    private Map<String, String> buildMapFromRequest(HttpServletRequest req) {\r
+        Map<String, String> map = new HashMap<>();\r
+        String str = req.getParameter("type");\r
+        if (str != null) {\r
+            if ("pub".equals(str) || "del".equals(str) || "exp".equals(str)) {\r
+                map.put("type", str);\r
+            } else {\r
+                map.put("err", "bad type");\r
+                return map;\r
+            }\r
+        } else {\r
+            map.put("type", "all");\r
+        }\r
+        map.put(PUBLISHSQL, "");\r
+        map.put(STATUSSQL, "");\r
+        map.put(RESULTSQL, "");\r
+        map.put(REASON_SQL, "");\r
+        map.put(FILENAMESQL, "");\r
+\r
+        str = req.getParameter("publishId");\r
+        if (str != null) {\r
+            if (str.indexOf("'") >= 0) {\r
+                map.put("err", "bad publishId");\r
+                return map;\r
+            }\r
+            map.put(PUBLISHSQL, " AND PUBLISH_ID = '" + str + "'");\r
+        }\r
+\r
+        str = req.getParameter("filename");\r
+        if (str != null) {\r
+            map.put(FILENAMESQL, " AND FILENAME = '" + str + "'");\r
+        }\r
+\r
+        str = req.getParameter("statusCode");\r
+        if (str != null) {\r
+            String sql = null;\r
+            switch (str) {\r
+                case "success":\r
+                    sql = " AND STATUS >= 200 AND STATUS < 300";\r
+                    break;\r
+                case "redirect":\r
+                    sql = " AND STATUS >= 300 AND STATUS < 400";\r
+                    break;\r
+                case "failure":\r
+                    sql = " AND STATUS >= 400";\r
+                    break;\r
+                default:\r
+                    try {\r
+                        int statusCode = Integer.parseInt(str);\r
+                        if ((statusCode >= 100 && statusCode < 600) || (statusCode == -1)) {\r
+                            sql = " AND STATUS = " + statusCode;\r
+                        }\r
+                    } catch (NumberFormatException e) {\r
+                        intlogger.error("Failed to parse input", e);\r
+                    }\r
+                    break;\r
+            }\r
+            if (sql == null) {\r
+                map.put("err", "bad statusCode");\r
+                return map;\r
+            }\r
+            map.put(STATUSSQL, sql);\r
+            map.put(RESULTSQL, sql.replaceAll("STATUS", "RESULT"));\r
+        }\r
+\r
+        str = req.getParameter("expiryReason");\r
+        if (str != null) {\r
+            map.put("type", "exp");\r
+            switch (str) {\r
+                case "notRetryable":\r
+                    map.put(REASON_SQL, " AND REASON = 'notRetryable'");\r
+                    break;\r
+                case "retriesExhausted":\r
+                    map.put(REASON_SQL, " AND REASON = 'retriesExhausted'");\r
+                    break;\r
+                case "diskFull":\r
+                    map.put(REASON_SQL, " AND REASON = 'diskFull'");\r
+                    break;\r
+                case "other":\r
+                    map.put(REASON_SQL, " AND REASON = 'other'");\r
+                    break;\r
+                default:\r
+                    map.put("err", "bad expiryReason");\r
+                    return map;\r
+            }\r
+        }\r
+\r
+        long stime = getTimeFromParam(req.getParameter("start"));\r
+        if (stime < 0) {\r
+            map.put("err", "bad start");\r
+            return map;\r
+        }\r
+        long etime = getTimeFromParam(req.getParameter("end"));\r
+        if (etime < 0) {\r
+            map.put("err", "bad end");\r
+            return map;\r
+        }\r
+        if (stime == 0 && etime == 0) {\r
+            etime = System.currentTimeMillis();\r
+            stime = etime - TWENTYFOUR_HOURS;\r
+        } else if (stime == 0) {\r
+            stime = etime - TWENTYFOUR_HOURS;\r
+        } else if (etime == 0) {\r
+            etime = stime + TWENTYFOUR_HOURS;\r
+        }\r
+        map.put(TIMESQL, String.format(" AND EVENT_TIME >= %d AND EVENT_TIME <= %d", stime, etime));\r
+        return map;\r
+    }\r
+\r
+    private long getTimeFromParam(final String str) {\r
+        if (str == null) {\r
+            return 0;\r
+        }\r
+        try {\r
+            // First, look for an RFC 3339 date\r
+            String fmt = (str.indexOf('.') > 0) ? FMT_2 : FMT_1;\r
+            SimpleDateFormat sdf = new SimpleDateFormat(fmt);\r
+            Date date = sdf.parse(str);\r
+            return date.getTime();\r
+        } catch (ParseException parseException) {\r
+            intlogger.error("Exception in getting Time :- " + parseException.getMessage(),parseException);\r
+        }\r
+        try {\r
+            // Also allow a long (in ms); useful for testing\r
+            return Long.parseLong(str);\r
+        } catch (NumberFormatException numberFormatException) {\r
+            intlogger.error("Exception in getting Time :- " + numberFormatException.getMessage(),numberFormatException);\r
+        }\r
+        intlogger.info("Error parsing time=" + str);\r
+        return -1;\r
+    }\r
+\r
+    private void getPublishRecordsForFeed(int feedid, RowHandler rh, Map<String, String> map) {\r
+        String type = map.get("type");\r
+        if ("all".equals(type) || "pub".equals(type)) {\r
+            String sql = LOG_RECORDSSQL + feedid\r
+                + " AND TYPE = 'pub'"\r
+                + map.get(TIMESQL) + map.get(PUBLISHSQL) + map.get(STATUSSQL) + map.get(FILENAMESQL);\r
+            getRecordsForSQL(sql, rh);\r
+        }\r
+    }\r
+\r
+    private void getDeliveryRecordsForFeed(int feedid, RowHandler rh, Map<String, String> map) {\r
+        String type = map.get("type");\r
+        if ("all".equals(type) || "del".equals(type)) {\r
+            String sql = LOG_RECORDSSQL + feedid\r
+                + " AND TYPE = 'del'"\r
+                + map.get(TIMESQL) + map.get(PUBLISHSQL) + map.get(RESULTSQL);\r
+            getRecordsForSQL(sql, rh);\r
+        }\r
+    }\r
+\r
+    private void getDeliveryRecordsForSubscription(int subid, RowHandler rh, Map<String, String> map) {\r
+        String type = map.get("type");\r
+        if ("all".equals(type) || "del".equals(type)) {\r
+            String sql = "select * from LOG_RECORDS where DELIVERY_SUBID = " + subid\r
+                + " AND TYPE = 'del'"\r
+                + map.get(TIMESQL) + map.get(PUBLISHSQL) + map.get(RESULTSQL);\r
+            getRecordsForSQL(sql, rh);\r
+        }\r
+    }\r
+\r
+    private void getExpiryRecordsForFeed(int feedid, RowHandler rh, Map<String, String> map) {\r
+        String type = map.get("type");\r
+        if ("all".equals(type) || "exp".equals(type)) {\r
+            String st = map.get(STATUSSQL);\r
+            if (st == null || st.length() == 0) {\r
+                String sql = LOG_RECORDSSQL + feedid\r
+                    + " AND TYPE = 'exp'"\r
+                    + map.get(TIMESQL) + map.get(PUBLISHSQL) + map.get(REASON_SQL);\r
+                getRecordsForSQL(sql, rh);\r
+            }\r
+        }\r
+    }\r
+\r
+    private void getExpiryRecordsForSubscription(int subid, RowHandler rh, Map<String, String> map) {\r
+        String type = map.get("type");\r
+        if ("all".equals(type) || "exp".equals(type)) {\r
+            String st = map.get(STATUSSQL);\r
+            if (st == null || st.length() == 0) {\r
+                String sql = "select * from LOG_RECORDS where DELIVERY_SUBID = " + subid\r
+                    + " AND TYPE = 'exp'"\r
+                    + map.get(TIMESQL) + map.get(PUBLISHSQL) + map.get(REASON_SQL);\r
+                getRecordsForSQL(sql, rh);\r
+            }\r
+        }\r
+    }\r
+\r
+    private void getRecordsForSQL(String sql, RowHandler rh) {\r
+        intlogger.debug(sql);\r
+        long start = System.currentTimeMillis();\r
+        try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
+            PreparedStatement ps = conn.prepareStatement(sql);\r
+            ResultSet rs = ps.executeQuery()) {\r
+            while (rs.next()) {\r
+                rh.handleRow(rs);\r
+            }\r
+        } catch (SQLException sqlException) {\r
+            intlogger.info("Failed to get Records. Exception = " + sqlException.getMessage(),sqlException);\r
+        }\r
+        intlogger.debug("Time: " + (System.currentTimeMillis() - start) + " ms");\r
+    }\r
 }\r