1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
25 package com.att.research.datarouter.provisioning;
\r
27 import java.io.IOException;
\r
28 import java.sql.Connection;
\r
29 import java.sql.ResultSet;
\r
30 import java.sql.SQLException;
\r
31 import java.sql.Statement;
\r
32 import java.text.ParseException;
\r
33 import java.text.SimpleDateFormat;
\r
34 import java.util.Date;
\r
35 import java.util.HashMap;
\r
36 import java.util.Map;
\r
38 import javax.servlet.ServletOutputStream;
\r
39 import javax.servlet.http.HttpServletRequest;
\r
40 import javax.servlet.http.HttpServletResponse;
\r
42 import org.json.LOGJSONObject;
\r
44 import com.att.eelf.configuration.EELFLogger;
\r
45 import com.att.eelf.configuration.EELFManager;
\r
46 import com.att.research.datarouter.provisioning.beans.DeliveryRecord;
\r
47 import com.att.research.datarouter.provisioning.beans.EventLogRecord;
\r
48 import com.att.research.datarouter.provisioning.beans.ExpiryRecord;
\r
49 import com.att.research.datarouter.provisioning.beans.LOGJSONable;
\r
50 import com.att.research.datarouter.provisioning.beans.PublishRecord;
\r
51 import com.att.research.datarouter.provisioning.beans.Subscription;
\r
52 import com.att.research.datarouter.provisioning.eelf.EelfMsgs;
\r
53 import com.att.research.datarouter.provisioning.utils.DB;
\r
56 * This servlet handles requests to the <feedLogURL> and <subLogURL>,
\r
57 * which are generated by the provisioning server to handle the log query API.
\r
59 * @author Robert Eby
\r
60 * @version $Id: LogServlet.java,v 1.11 2014/03/28 17:27:02 eby Exp $
\r
62 @SuppressWarnings("serial")
\r
63 public class LogServlet extends BaseServlet {
\r
64 //Adding EELF Logger Rally:US664892
\r
65 private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("com.att.research.datarouter.provisioning.LogServlet");
\r
67 private static final long TWENTYFOUR_HOURS = (24 * 60 * 60 * 1000L);
\r
68 private static final String fmt1 = "yyyy-MM-dd'T'HH:mm:ss'Z'";
\r
69 private static final String fmt2 = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
\r
71 private boolean isfeedlog;
\r
73 public abstract class RowHandler {
\r
74 private final ServletOutputStream out;
\r
75 private final String[] fields;
\r
76 public boolean firstrow;
\r
78 public RowHandler(ServletOutputStream out, String fieldparam, boolean b) {
\r
81 this.fields = (fieldparam != null) ? fieldparam.split(":") : null;
\r
83 public void handleRow(ResultSet rs) {
\r
85 LOGJSONable js = buildJSONable(rs);
\r
86 LOGJSONObject jo = js.asJSONObject();
\r
87 if (fields != null) {
\r
88 // filter out unwanted fields
\r
89 LOGJSONObject j2 = new LOGJSONObject();
\r
90 for (String key : fields) {
\r
91 Object v = jo.opt(key);
\r
97 String t = firstrow ? "\n" : ",\n";
\r
101 } catch (Exception e) {
\r
105 public abstract LOGJSONable buildJSONable(ResultSet rs) throws SQLException;
\r
107 public class PublishRecordRowHandler extends RowHandler {
\r
108 public PublishRecordRowHandler(ServletOutputStream out, String fields, boolean b) {
\r
109 super(out, fields, b);
\r
112 public LOGJSONable buildJSONable(ResultSet rs) throws SQLException {
\r
113 return new PublishRecord(rs);
\r
116 public class DeliveryRecordRowHandler extends RowHandler {
\r
117 public DeliveryRecordRowHandler(ServletOutputStream out, String fields, boolean b) {
\r
118 super(out, fields, b);
\r
121 public LOGJSONable buildJSONable(ResultSet rs) throws SQLException {
\r
122 return new DeliveryRecord(rs);
\r
125 public class ExpiryRecordRowHandler extends RowHandler {
\r
126 public ExpiryRecordRowHandler(ServletOutputStream out, String fields, boolean b) {
\r
127 super(out, fields, b);
\r
130 public LOGJSONable buildJSONable(ResultSet rs) throws SQLException {
\r
131 return new ExpiryRecord(rs);
\r
136 * This class must be created from either a {@link FeedLogServlet} or a {@link SubLogServlet}.
\r
137 * @param isFeedLog boolean to handle those places where a feedlog request is different from
\r
140 protected LogServlet(boolean isFeedLog) {
\r
141 this.isfeedlog = isFeedLog;
\r
145 * DELETE a logging URL -- not supported.
\r
148 public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
\r
149 setIpAndFqdnForEelf("doDelete");
\r
150 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");
\r
151 String message = "DELETE not allowed for the logURL.";
\r
152 EventLogRecord elr = new EventLogRecord(req);
\r
153 elr.setMessage(message);
\r
154 elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
\r
155 eventlogger.info(elr);
\r
156 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message);
\r
159 * GET a logging URL -- retrieve logging data for a feed or subscription.
\r
160 * See the <b>Logging API</b> document for details on how this method should be invoked.
\r
163 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
\r
164 setIpAndFqdnForEelf("doGet");
\r
165 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");
\r
166 int id = getIdFromPath(req);
\r
168 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing or bad feed/subscription number.");
\r
171 Map<String, String> map = buildMapFromRequest(req);
\r
172 if (map.get("err") != null) {
\r
173 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid arguments: "+map.get("err"));
\r
176 // check Accept: header??
\r
178 resp.setStatus(HttpServletResponse.SC_OK);
\r
179 resp.setContentType(LOGLIST_CONTENT_TYPE);
\r
180 @SuppressWarnings("resource")
\r
181 ServletOutputStream out = resp.getOutputStream();
\r
182 final String fields = req.getParameter("fields");
\r
186 // Handle /feedlog/feedid request
\r
187 boolean firstrow = true;
\r
189 // 1. Collect publish records for this feed
\r
190 RowHandler rh = new PublishRecordRowHandler(out, fields, firstrow);
\r
191 getPublishRecordsForFeed(id, rh, map);
\r
192 firstrow = rh.firstrow;
\r
194 // 2. Collect delivery records for subscriptions to this feed
\r
195 rh = new DeliveryRecordRowHandler(out, fields, firstrow);
\r
196 getDeliveryRecordsForFeed(id, rh, map);
\r
197 firstrow = rh.firstrow;
\r
199 // 3. Collect expiry records for subscriptions to this feed
\r
200 rh = new ExpiryRecordRowHandler(out, fields, firstrow);
\r
201 getExpiryRecordsForFeed(id, rh, map);
\r
203 // Handle /sublog/subid request
\r
204 Subscription sub = Subscription.getSubscriptionById(id);
\r
206 // 1. Collect publish records for the feed this subscription feeds
\r
207 RowHandler rh = new PublishRecordRowHandler(out, fields, true);
\r
208 getPublishRecordsForFeed(sub.getFeedid(), rh, map);
\r
210 // 2. Collect delivery records for this subscription
\r
211 rh = new DeliveryRecordRowHandler(out, fields, rh.firstrow);
\r
212 getDeliveryRecordsForSubscription(id, rh, map);
\r
214 // 3. Collect expiry records for this subscription
\r
215 rh = new ExpiryRecordRowHandler(out, fields, rh.firstrow);
\r
216 getExpiryRecordsForSubscription(id, rh, map);
\r
222 * PUT a logging URL -- not supported.
\r
225 public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
\r
226 setIpAndFqdnForEelf("doPut");
\r
227 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");
\r
228 String message = "PUT not allowed for the logURL.";
\r
229 EventLogRecord elr = new EventLogRecord(req);
\r
230 elr.setMessage(message);
\r
231 elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
\r
232 eventlogger.info(elr);
\r
233 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message);
\r
236 * POST a logging URL -- not supported.
\r
239 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
\r
240 setIpAndFqdnForEelf("doPost");
\r
241 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF, req.getHeader(BEHALF_HEADER));
\r
242 String message = "POST not allowed for the logURL.";
\r
243 EventLogRecord elr = new EventLogRecord(req);
\r
244 elr.setMessage(message);
\r
245 elr.setResult(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
\r
246 eventlogger.info(elr);
\r
247 resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message);
\r
250 private Map<String, String> buildMapFromRequest(HttpServletRequest req) {
\r
251 Map<String, String> map = new HashMap<String, String>();
\r
252 String s = req.getParameter("type");
\r
254 if (s.equals("pub") || s.equals("del") || s.equals("exp")) {
\r
255 map.put("type", s);
\r
257 map.put("err", "bad type");
\r
261 map.put("type", "all");
\r
262 map.put("publishSQL", "");
\r
263 map.put("statusSQL", "");
\r
264 map.put("resultSQL", "");
\r
265 map.put("reasonSQL", "");
\r
267 s = req.getParameter("publishId");
\r
269 if (s.indexOf("'") >= 0) {
\r
270 map.put("err", "bad publishId");
\r
273 map.put("publishSQL", " AND PUBLISH_ID = '"+s+"'");
\r
276 s = req.getParameter("statusCode");
\r
279 if (s.equals("success")) {
\r
280 sql = " AND STATUS >= 200 AND STATUS < 300";
\r
281 } else if (s.equals("redirect")) {
\r
282 sql = " AND STATUS >= 300 AND STATUS < 400";
\r
283 } else if (s.equals("failure")) {
\r
284 sql = " AND STATUS >= 400";
\r
287 Integer n = Integer.parseInt(s);
\r
288 if ((n >= 100 && n < 600) || (n == -1))
\r
289 sql = " AND STATUS = " + n;
\r
290 } catch (NumberFormatException e) {
\r
294 map.put("err", "bad statusCode");
\r
297 map.put("statusSQL", sql);
\r
298 map.put("resultSQL", sql.replaceAll("STATUS", "RESULT"));
\r
301 s = req.getParameter("expiryReason");
\r
303 map.put("type", "exp");
\r
304 if (s.equals("notRetryable")) {
\r
305 map.put("reasonSQL", " AND REASON = 'notRetryable'");
\r
306 } else if (s.equals("retriesExhausted")) {
\r
307 map.put("reasonSQL", " AND REASON = 'retriesExhausted'");
\r
308 } else if (s.equals("diskFull")) {
\r
309 map.put("reasonSQL", " AND REASON = 'diskFull'");
\r
310 } else if (s.equals("other")) {
\r
311 map.put("reasonSQL", " AND REASON = 'other'");
\r
313 map.put("err", "bad expiryReason");
\r
318 long stime = getTimeFromParam(req.getParameter("start"));
\r
320 map.put("err", "bad start");
\r
323 long etime = getTimeFromParam(req.getParameter("end"));
\r
325 map.put("err", "bad end");
\r
328 if (stime == 0 && etime == 0) {
\r
329 etime = System.currentTimeMillis();
\r
330 stime = etime - TWENTYFOUR_HOURS;
\r
331 } else if (stime == 0) {
\r
332 stime = etime - TWENTYFOUR_HOURS;
\r
333 } else if (etime == 0) {
\r
334 etime = stime + TWENTYFOUR_HOURS;
\r
336 map.put("timeSQL", String.format(" AND EVENT_TIME >= %d AND EVENT_TIME <= %d", stime, etime));
\r
339 private long getTimeFromParam(final String s) {
\r
343 // First, look for an RFC 3339 date
\r
344 String fmt = (s.indexOf('.') > 0) ? fmt2 : fmt1;
\r
345 SimpleDateFormat sdf = new SimpleDateFormat(fmt);
\r
346 Date d = sdf.parse(s);
\r
347 return d.getTime();
\r
348 } catch (ParseException e) {
\r
351 // Also allow a long (in ms); useful for testing
\r
352 long n = Long.parseLong(s);
\r
354 } catch (NumberFormatException e) {
\r
356 intlogger.info("Error parsing time="+s);
\r
360 private void getPublishRecordsForFeed(int feedid, RowHandler rh, Map<String, String> map) {
\r
361 String type = map.get("type");
\r
362 if (type.equals("all") || type.equals("pub")) {
\r
363 String sql = "select * from LOG_RECORDS where FEEDID = "+feedid
\r
364 + " AND TYPE = 'pub'"
\r
365 + map.get("timeSQL") + map.get("publishSQL") + map.get("statusSQL");
\r
366 getRecordsForSQL(sql, rh);
\r
369 private void getDeliveryRecordsForFeed(int feedid, RowHandler rh, Map<String, String> map) {
\r
370 String type = map.get("type");
\r
371 if (type.equals("all") || type.equals("del")) {
\r
372 String sql = "select * from LOG_RECORDS where FEEDID = "+feedid
\r
373 + " AND TYPE = 'del'"
\r
374 + map.get("timeSQL") + map.get("publishSQL") + map.get("resultSQL");
\r
375 getRecordsForSQL(sql, rh);
\r
378 private void getDeliveryRecordsForSubscription(int subid, RowHandler rh, Map<String, String> map) {
\r
379 String type = map.get("type");
\r
380 if (type.equals("all") || type.equals("del")) {
\r
381 String sql = "select * from LOG_RECORDS where DELIVERY_SUBID = "+subid
\r
382 + " AND TYPE = 'del'"
\r
383 + map.get("timeSQL") + map.get("publishSQL") + map.get("resultSQL");
\r
384 getRecordsForSQL(sql, rh);
\r
387 private void getExpiryRecordsForFeed(int feedid, RowHandler rh, Map<String, String> map) {
\r
388 String type = map.get("type");
\r
389 if (type.equals("all") || type.equals("exp")) {
\r
390 String st = map.get("statusSQL");
\r
391 if (st == null || st.length() == 0) {
\r
392 String sql = "select * from LOG_RECORDS where FEEDID = "+feedid
\r
393 + " AND TYPE = 'exp'"
\r
394 + map.get("timeSQL") + map.get("publishSQL") + map.get("reasonSQL");
\r
395 getRecordsForSQL(sql, rh);
\r
399 private void getExpiryRecordsForSubscription(int subid, RowHandler rh, Map<String, String> map) {
\r
400 String type = map.get("type");
\r
401 if (type.equals("all") || type.equals("exp")) {
\r
402 String st = map.get("statusSQL");
\r
403 if (st == null || st.length() == 0) {
\r
404 String sql = "select * from LOG_RECORDS where DELIVERY_SUBID = "+subid
\r
405 + " AND TYPE = 'exp'"
\r
406 + map.get("timeSQL") + map.get("publishSQL") + map.get("reasonSQL");
\r
407 getRecordsForSQL(sql, rh);
\r
411 private void getRecordsForSQL(String sql, RowHandler rh) {
\r
412 intlogger.debug(sql);
\r
413 long start = System.currentTimeMillis();
\r
415 Connection conn = null;
\r
417 conn = db.getConnection();
\r
418 Statement stmt = conn.createStatement();
\r
419 ResultSet rs = stmt.executeQuery(sql);
\r
420 while (rs.next()) {
\r
425 } catch (SQLException e) {
\r
426 e.printStackTrace();
\r
431 intlogger.debug("Time: " + (System.currentTimeMillis()-start) + " ms");
\r