1
14
15 package com.liferay.portal.kernel.audit;
16
17 import com.liferay.portal.kernel.json.JSONException;
18 import com.liferay.portal.kernel.json.JSONFactoryUtil;
19 import com.liferay.portal.kernel.json.JSONObject;
20 import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
21 import com.liferay.portal.kernel.util.GetterUtil;
22
23 import java.io.Serializable;
24
25 import java.text.DateFormat;
26
27 import java.util.Date;
28
29
36 public class AuditMessage implements Serializable {
37
38 public AuditMessage(String message) throws JSONException {
39 JSONObject jsonObj = JSONFactoryUtil.createJSONObject(message);
40
41 _eventType = jsonObj.getString(_EVENT_TYPE);
42 _companyId = jsonObj.getLong(_COMPANY_ID);
43 _userId = jsonObj.getLong(_USER_ID);
44 _userName = jsonObj.getString(_USER_NAME);
45 _className = jsonObj.getString(_CLASS_NAME);
46 _classPK = jsonObj.getString(_CLASS_PK);
47 _message = jsonObj.getString(_MESSAGE);
48
49 if (jsonObj.has(_CLIENT_HOST)) {
50 _clientHost = jsonObj.getString(_CLIENT_HOST);
51 }
52
53 if (jsonObj.has(_CLIENT_IP)) {
54 _clientIP = jsonObj.getString(_CLIENT_IP);
55 }
56
57 if (jsonObj.has(_SERVER_NAME)) {
58 _serverName = jsonObj.getString(_SERVER_NAME);
59 }
60
61 if (jsonObj.has(_SERVER_PORT)) {
62 _serverPort = jsonObj.getInt(_SERVER_PORT);
63 }
64
65 if (jsonObj.has(_SESSION_ID)) {
66 _sessionID = jsonObj.getString(_SESSION_ID);
67 }
68
69 _timestamp = GetterUtil.getDate(
70 jsonObj.getString(_TIMESTAMP), _getDateFormat());
71 _additionalInfo = jsonObj.getJSONObject(_ADDITIONAL_INFO);
72 }
73
74 public AuditMessage(
75 String eventType, long companyId, long userId, String userName) {
76
77 this(
78 eventType, companyId, userId, userName, null, null, null, null,
79 null);
80 }
81
82 public AuditMessage(
83 String eventType, long companyId, long userId, String userName,
84 String className, String classPK) {
85
86 this(
87 eventType, companyId, userId, userName, className, classPK, null,
88 null, null);
89 }
90
91 public AuditMessage(
92 String eventType, long companyId, long userId, String userName,
93 String className, String classPK, String message) {
94
95 this(
96 eventType, companyId, userId, userName, className, classPK, message,
97 null, null);
98 }
99
100 public AuditMessage(
101 String eventType, long companyId, long userId, String userName,
102 String className, String classPK, String message, Date timestamp,
103 JSONObject additionalInfo) {
104
105 _eventType = eventType;
106 _companyId = companyId;
107 _userId = userId;
108 _userName = userName;
109 _className = className;
110 _classPK = classPK;
111 _message = message;
112
113 AuditRequestThreadLocal auditRequestThreadLocal =
114 AuditRequestThreadLocal.getAuditThreadLocal();
115
116 _clientHost = auditRequestThreadLocal.getClientHost();
117 _clientIP = auditRequestThreadLocal.getClientIP();
118 _serverName = auditRequestThreadLocal.getServerName();
119 _serverPort = auditRequestThreadLocal.getServerPort();
120 _sessionID = auditRequestThreadLocal.getSessionID();
121
122 _timestamp = timestamp;
123
124 if (_timestamp == null) {
125 _timestamp = new Date();
126 }
127
128 _additionalInfo = additionalInfo;
129
130 if (_additionalInfo == null) {
131 JSONFactoryUtil.createJSONObject();
132 }
133 }
134
135 public AuditMessage(
136 String eventType, long companyId, long userId, String userName,
137 String className, String classPK, String message,
138 JSONObject additionalInfo) {
139
140 this(
141 eventType, companyId, userId, userName, className, classPK, message,
142 null, additionalInfo);
143 }
144
145 public JSONObject getAdditionalInfo() {
146 return _additionalInfo;
147 }
148
149 public String getClassName() {
150 return _className;
151 }
152
153 public String getClassPK() {
154 return _classPK;
155 }
156
157 public String getClientHost() {
158 return _clientHost;
159 }
160
161 public String getClientIP() {
162 return _clientIP;
163 }
164
165 public long getCompanyId() {
166 return _companyId;
167 }
168
169 public String getEventType() {
170 return _eventType;
171 }
172
173 public String getMessage() {
174 return _message;
175 }
176
177 public String getServerName() {
178 return _serverName;
179 }
180
181 public int getServerPort() {
182 return _serverPort;
183 }
184
185 public String getSessionID() {
186 return _sessionID;
187 }
188
189 public Date getTimestamp() {
190 return _timestamp;
191 }
192
193 public long getUserId() {
194 return _userId;
195 }
196
197 public String getUserName() {
198 return _userName;
199 }
200
201 public void setAdditionalInfo(JSONObject additionalInfo) {
202 _additionalInfo = additionalInfo;
203 }
204
205 public void setClassName(String className) {
206 _className = className;
207 }
208
209 public void setClassPK(long classPK) {
210 _classPK = String.valueOf(classPK);
211 }
212
213 public void setClassPK(String classPK) {
214 _classPK = classPK;
215 }
216
217 public void setClientHost(String clientHost) {
218 _clientHost = clientHost;
219 }
220
221 public void setClientIP(String clientIP) {
222 _clientIP = clientIP;
223 }
224
225 public void setCompanyId(long companyId) {
226 _companyId = companyId;
227 }
228
229 public void setEventType(String eventType) {
230 _eventType = eventType;
231 }
232
233 public void setMessage(String message) {
234 _message = message;
235 }
236
237 public void setServerName(String serverName) {
238 _serverName = serverName;
239 }
240
241 public void setServerPort(int serverPort) {
242 _serverPort = serverPort;
243 }
244
245 public void setSessionID(String sessionID) {
246 _sessionID = sessionID;
247 }
248
249 public void setTimestamp(Date timestamp) {
250 _timestamp = timestamp;
251 }
252
253 public void setUserId(long userId) {
254 _userId = userId;
255 }
256
257 public void setUserName(String userName) {
258 _userName = userName;
259 }
260
261 public JSONObject toJSONObject() {
262 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
263
264 jsonObj.put(_ADDITIONAL_INFO, _additionalInfo);
265 jsonObj.put(_COMPANY_ID, _companyId);
266 jsonObj.put(_CLASS_PK, _classPK);
267 jsonObj.put(_CLASS_NAME, _className);
268 jsonObj.put(_CLIENT_IP, _clientIP);
269 jsonObj.put(_clientHost, _clientHost);
270 jsonObj.put(_MESSAGE, _message);
271 jsonObj.put(_SERVER_PORT, _serverPort);
272 jsonObj.put(_SERVER_NAME, _serverName);
273 jsonObj.put(_SESSION_ID, _sessionID);
274 jsonObj.put(_TIMESTAMP, _getDateFormat().format(new Date()));
275 jsonObj.put(_EVENT_TYPE, _eventType);
276 jsonObj.put(_USER_ID, _userId);
277 jsonObj.put(_USER_NAME, _userName);
278
279 return jsonObj;
280 }
281
282 private DateFormat _getDateFormat() {
283 return DateFormatFactoryUtil.getSimpleDateFormat(_DATE_FORMAT);
284 }
285
286 private static final String _ADDITIONAL_INFO = "additionalInfo";
287
288 private static final String _CLASS_NAME = "className";
289
290 private static final String _CLASS_PK = "classPK";
291
292 private static final String _CLIENT_HOST = "clientHost";
293
294 private static final String _CLIENT_IP = "clientIP";
295
296 private static final String _COMPANY_ID = "companyId";
297
298 private static final String _DATE_FORMAT = "yyyyMMddkkmmssSSS";
299
300 private static final String _EVENT_TYPE = "eventType";
301
302 private static final String _MESSAGE = "message";
303
304 private static final String _SERVER_NAME = "serverName";
305
306 private static final String _SERVER_PORT = "serverPort";
307
308 private static final String _SESSION_ID = "sessionID";
309
310 private static final String _TIMESTAMP = "timestamp";
311
312 private static final String _USER_ID = "userId";
313
314 private static final String _USER_NAME = "userName";
315
316 private JSONObject _additionalInfo;
317 private String _className;
318 private String _classPK;
319 private String _clientHost;
320 private String _clientIP;
321 private long _companyId = -1;
322 private String _eventType;
323 private String _message;
324 private String _serverName;
325 private int _serverPort;
326 private String _sessionID;
327 private Date _timestamp;
328 private long _userId = -1;
329 private String _userName;
330
331 }