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