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