[DMAAP-48] Initial code import
[dmaap/datarouter.git] / datarouter-prov / src / main / java / com / att / research / datarouter / provisioning / beans / BaseLogRecord.java
1 /*******************************************************************************\r
2  * ============LICENSE_START==================================================\r
3  * * org.onap.dmaap\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
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \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
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 \r
24 \r
25 package com.att.research.datarouter.provisioning.beans;\r
26 \r
27 import java.sql.PreparedStatement;\r
28 import java.sql.ResultSet;\r
29 import java.sql.SQLException;\r
30 import java.text.ParseException;\r
31 import java.text.SimpleDateFormat;\r
32 import java.util.Calendar;\r
33 import java.util.Date;\r
34 import java.util.GregorianCalendar;\r
35 import org.json.LOGJSONObject;\r
36 \r
37 /**\r
38  * Define the common fields used by the three types of records generated by DR nodes.\r
39  *\r
40  * @author Robert Eby\r
41  * @version $Id: BaseLogRecord.java,v 1.10 2013/10/29 16:57:57 eby Exp $\r
42  */\r
43 public class BaseLogRecord implements LOGJSONable, Loadable {\r
44         protected static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");\r
45 \r
46         private long eventTime;\r
47         private String publishId;\r
48         private int feedid;\r
49         private String requestUri;\r
50         private String method;\r
51         private String contentType;\r
52         private long contentLength;\r
53 \r
54         protected BaseLogRecord(String[] pp) throws ParseException {\r
55 //              This throws exceptions occasionally - don't know why.\r
56 //              Date d = null;\r
57 //              synchronized (sdf) {\r
58 //                      d = sdf.parse(pp[0]);\r
59 //              }\r
60                 Date d = parseDate(pp[0]);\r
61                 this.eventTime     = d.getTime();\r
62                 this.publishId     = pp[2];\r
63                 this.feedid        = Integer.parseInt(pp[3]);\r
64                 if (pp[1].equals("DLX")) {\r
65                         this.requestUri    = "";\r
66                         this.method        = "GET";     // Note: we need a valid value in this field, even though unused\r
67                         this.contentType   = "";\r
68                         this.contentLength = Long.parseLong(pp[5]);\r
69                 } else  if (pp[1].equals("PUB") || pp[1].equals("LOG") || pp[1].equals("PBF")) {\r
70                         this.requestUri    = pp[4];\r
71                         this.method        = pp[5];\r
72                         this.contentType   = pp[6];\r
73                         this.contentLength = Long.parseLong(pp[7]);\r
74                 } else {\r
75                         this.requestUri    = pp[5];\r
76                         this.method        = pp[6];\r
77                         this.contentType   = pp[7];\r
78                         this.contentLength = Long.parseLong(pp[8]);\r
79                 }\r
80         }\r
81         protected BaseLogRecord(ResultSet rs) throws SQLException {\r
82                 this.eventTime     = rs.getLong("EVENT_TIME");\r
83                 this.publishId     = rs.getString("PUBLISH_ID");\r
84                 this.feedid        = rs.getInt("FEEDID");\r
85                 this.requestUri    = rs.getString("REQURI");\r
86                 this.method        = rs.getString("METHOD");\r
87                 this.contentType   = rs.getString("CONTENT_TYPE");\r
88                 this.contentLength = rs.getLong("CONTENT_LENGTH");\r
89         }\r
90         protected Date parseDate(final String s) throws ParseException {\r
91                 int[] n = new int[7];\r
92                 int p = 0;\r
93                 for (int i = 0; i < s.length(); i++) {\r
94                         char c = s.charAt(i);\r
95                         if (c < '0' || c > '9') {\r
96                                 p++;\r
97                         } else {\r
98                                 if (p > n.length)\r
99                                         throw new ParseException("parseDate()", 0);\r
100                                 n[p] = (n[p] * 10) + (c - '0');\r
101                         }\r
102                 }\r
103                 if (p != 7)\r
104                         throw new ParseException("parseDate()", 1);\r
105                 Calendar cal = new GregorianCalendar();\r
106                 cal.set(Calendar.YEAR, n[0]);\r
107                 cal.set(Calendar.MONTH, n[1]-1);\r
108                 cal.set(Calendar.DAY_OF_MONTH, n[2]);\r
109                 cal.set(Calendar.HOUR_OF_DAY, n[3]);\r
110                 cal.set(Calendar.MINUTE, n[4]);\r
111                 cal.set(Calendar.SECOND, n[5]);\r
112                 cal.set(Calendar.MILLISECOND, n[6]);\r
113                 return cal.getTime();\r
114         }\r
115         public long getEventTime() {\r
116                 return eventTime;\r
117         }\r
118         public void setEventTime(long eventTime) {\r
119                 this.eventTime = eventTime;\r
120         }\r
121         public String getPublishId() {\r
122                 return publishId;\r
123         }\r
124         public void setPublishId(String publishId) {\r
125                 this.publishId = publishId;\r
126         }\r
127         public int getFeedid() {\r
128                 return feedid;\r
129         }\r
130         public void setFeedid(int feedid) {\r
131                 this.feedid = feedid;\r
132         }\r
133         public String getRequestUri() {\r
134                 return requestUri;\r
135         }\r
136         public void setRequestUri(String requestUri) {\r
137                 this.requestUri = requestUri;\r
138         }\r
139         public String getMethod() {\r
140                 return method;\r
141         }\r
142         public void setMethod(String method) {\r
143                 this.method = method;\r
144         }\r
145         public String getContentType() {\r
146                 return contentType;\r
147         }\r
148         public void setContentType(String contentType) {\r
149                 this.contentType = contentType;\r
150         }\r
151         public long getContentLength() {\r
152                 return contentLength;\r
153         }\r
154         public void setContentLength(long contentLength) {\r
155                 this.contentLength = contentLength;\r
156         }\r
157         @Override\r
158         public LOGJSONObject asJSONObject() {\r
159                 LOGJSONObject jo = new LOGJSONObject();\r
160                 String t = "";\r
161                 synchronized (sdf) {\r
162                         t = sdf.format(eventTime);\r
163                 }\r
164                 jo.put("date", t);\r
165                 jo.put("publishId", publishId);\r
166                 jo.put("requestURI", requestUri);\r
167                 jo.put("method", method);\r
168                 if (method.equals("PUT")) {\r
169                         jo.put("contentType", contentType);\r
170                         jo.put("contentLength", contentLength);\r
171                 }\r
172                 return jo;\r
173         }\r
174         @Override\r
175         public void load(PreparedStatement ps) throws SQLException {\r
176                 ps.setLong  (2, getEventTime());\r
177                 ps.setString(3, getPublishId());\r
178                 ps.setInt   (4, getFeedid());\r
179                 ps.setString(5, getRequestUri());\r
180                 ps.setString(6, getMethod());\r
181                 ps.setString(7, getContentType());\r
182                 ps.setLong  (8, getContentLength());\r
183         }\r
184 }\r