001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.audit;
016    
017    import com.liferay.portal.kernel.json.JSONException;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    
023    import java.io.Serializable;
024    
025    import java.text.DateFormat;
026    
027    import java.util.Date;
028    
029    /**
030     * @author Michael C. Han
031     * @author Mika Koivisto
032     * @author Bruno Farache
033     */
034    public class AuditMessage implements Serializable {
035    
036            public AuditMessage(String message) throws JSONException {
037                    JSONObject jsonObj = JSONFactoryUtil.createJSONObject(message);
038    
039                    _eventType = jsonObj.getString(_EVENT_TYPE);
040                    _companyId = jsonObj.getLong(_COMPANY_ID);
041                    _userId = jsonObj.getLong(_USER_ID);
042                    _userName = jsonObj.getString(_USER_NAME);
043                    _className = jsonObj.getString(_CLASS_NAME);
044                    _classPK = jsonObj.getString(_CLASS_PK);
045                    _message = jsonObj.getString(_MESSAGE);
046    
047                    if (jsonObj.has(_CLIENT_HOST)) {
048                            _clientHost = jsonObj.getString(_CLIENT_HOST);
049                    }
050    
051                    if (jsonObj.has(_CLIENT_IP)) {
052                            _clientIP = jsonObj.getString(_CLIENT_IP);
053                    }
054    
055                    if (jsonObj.has(_SERVER_NAME)) {
056                            _serverName = jsonObj.getString(_SERVER_NAME);
057                    }
058    
059                    if (jsonObj.has(_SERVER_PORT)) {
060                            _serverPort = jsonObj.getInt(_SERVER_PORT);
061                    }
062    
063                    if (jsonObj.has(_SESSION_ID)) {
064                            _sessionID = jsonObj.getString(_SESSION_ID);
065                    }
066    
067                    _timestamp = GetterUtil.getDate(
068                            jsonObj.getString(_TIMESTAMP), _getDateFormat());
069                    _additionalInfo = jsonObj.getJSONObject(_ADDITIONAL_INFO);
070            }
071    
072            public AuditMessage(
073                    String eventType, long companyId, long userId, String userName) {
074    
075                    this(
076                            eventType, companyId, userId, userName, null, null, null, null,
077                            null);
078            }
079    
080            public AuditMessage(
081                    String eventType, long companyId, long userId, String userName,
082                    String className, String classPK) {
083    
084                    this(
085                            eventType, companyId, userId, userName, className, classPK, null,
086                            null, null);
087            }
088    
089            public AuditMessage(
090                    String eventType, long companyId, long userId, String userName,
091                    String className, String classPK, String message) {
092    
093                    this(
094                            eventType, companyId, userId, userName, className, classPK, message,
095                            null, null);
096            }
097    
098            public AuditMessage(
099                    String eventType, long companyId, long userId, String userName,
100                    String className, String classPK, String message, Date timestamp,
101                    JSONObject additionalInfo) {
102    
103                    _eventType = eventType;
104                    _companyId = companyId;
105                    _userId = userId;
106                    _userName = userName;
107                    _className = className;
108                    _classPK = classPK;
109                    _message = message;
110    
111                    AuditRequestThreadLocal auditRequestThreadLocal =
112                            AuditRequestThreadLocal.getAuditThreadLocal();
113    
114                    _clientHost = auditRequestThreadLocal.getClientHost();
115                    _clientIP = auditRequestThreadLocal.getClientIP();
116                    _serverName = auditRequestThreadLocal.getServerName();
117                    _serverPort = auditRequestThreadLocal.getServerPort();
118                    _sessionID = auditRequestThreadLocal.getSessionID();
119    
120                    _timestamp = timestamp;
121    
122                    if (_timestamp == null) {
123                            _timestamp = new Date();
124                    }
125    
126                    _additionalInfo = additionalInfo;
127    
128                    if (_additionalInfo == null) {
129                            JSONFactoryUtil.createJSONObject();
130                    }
131            }
132    
133            public AuditMessage(
134                    String eventType, long companyId, long userId, String userName,
135                    String className, String classPK, String message,
136                    JSONObject additionalInfo) {
137    
138                    this(
139                            eventType, companyId, userId, userName, className, classPK, message,
140                            null, additionalInfo);
141            }
142    
143            public JSONObject getAdditionalInfo() {
144                    return _additionalInfo;
145            }
146    
147            public String getClassName() {
148                    return _className;
149            }
150    
151            public String getClassPK() {
152                    return _classPK;
153            }
154    
155            public String getClientHost() {
156                    return _clientHost;
157            }
158    
159            public String getClientIP() {
160                    return _clientIP;
161            }
162    
163            public long getCompanyId() {
164                    return _companyId;
165            }
166    
167            public String getEventType() {
168                    return _eventType;
169            }
170    
171            public String getMessage() {
172                    return _message;
173            }
174    
175            public String getServerName() {
176                    return _serverName;
177            }
178    
179            public int getServerPort() {
180                    return _serverPort;
181            }
182    
183            public String getSessionID() {
184                    return _sessionID;
185            }
186    
187            public Date getTimestamp() {
188                    return _timestamp;
189            }
190    
191            public long getUserId() {
192                    return _userId;
193            }
194    
195            public String getUserName() {
196                    return _userName;
197            }
198    
199            public void setAdditionalInfo(JSONObject additionalInfo) {
200                    _additionalInfo = additionalInfo;
201            }
202    
203            public void setClassName(String className) {
204                    _className = className;
205            }
206    
207            public void setClassPK(long classPK) {
208                    _classPK = String.valueOf(classPK);
209            }
210    
211            public void setClassPK(String classPK) {
212                    _classPK = classPK;
213            }
214    
215            public void setClientHost(String clientHost) {
216                    _clientHost = clientHost;
217            }
218    
219            public void setClientIP(String clientIP) {
220                    _clientIP = clientIP;
221            }
222    
223            public void setCompanyId(long companyId) {
224                    _companyId = companyId;
225            }
226    
227            public void setEventType(String eventType) {
228                    _eventType = eventType;
229            }
230    
231            public void setMessage(String message) {
232                    _message = message;
233            }
234    
235            public void setServerName(String serverName) {
236                    _serverName = serverName;
237            }
238    
239            public void setServerPort(int serverPort) {
240                    _serverPort = serverPort;
241            }
242    
243            public void setSessionID(String sessionID) {
244                    _sessionID = sessionID;
245            }
246    
247            public void setTimestamp(Date timestamp) {
248                    _timestamp = timestamp;
249            }
250    
251            public void setUserId(long userId) {
252                    _userId = userId;
253            }
254    
255            public void setUserName(String userName) {
256                    _userName = userName;
257            }
258    
259            public JSONObject toJSONObject() {
260                    JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
261    
262                    jsonObj.put(_ADDITIONAL_INFO, _additionalInfo);
263                    jsonObj.put(_COMPANY_ID, _companyId);
264                    jsonObj.put(_CLASS_PK, _classPK);
265                    jsonObj.put(_CLASS_NAME, _className);
266                    jsonObj.put(_CLIENT_HOST, _clientHost);
267                    jsonObj.put(_CLIENT_IP, _clientIP);
268                    jsonObj.put(_MESSAGE, _message);
269                    jsonObj.put(_SERVER_PORT, _serverPort);
270                    jsonObj.put(_SERVER_NAME, _serverName);
271                    jsonObj.put(_SESSION_ID, _sessionID);
272                    jsonObj.put(_TIMESTAMP, _getDateFormat().format(new Date()));
273                    jsonObj.put(_EVENT_TYPE, _eventType);
274                    jsonObj.put(_USER_ID, _userId);
275                    jsonObj.put(_USER_NAME, _userName);
276    
277                    return jsonObj;
278            }
279    
280            private DateFormat _getDateFormat() {
281                    return DateFormatFactoryUtil.getSimpleDateFormat(_DATE_FORMAT);
282            }
283    
284            private static final String _ADDITIONAL_INFO = "additionalInfo";
285    
286            private static final String _CLASS_NAME = "className";
287    
288            private static final String _CLASS_PK = "classPK";
289    
290            private static final String _CLIENT_HOST = "clientHost";
291    
292            private static final String _CLIENT_IP = "clientIP";
293    
294            private static final String _COMPANY_ID = "companyId";
295    
296            private static final String _DATE_FORMAT = "yyyyMMddkkmmssSSS";
297    
298            private static final String _EVENT_TYPE = "eventType";
299    
300            private static final String _MESSAGE = "message";
301    
302            private static final String _SERVER_NAME = "serverName";
303    
304            private static final String _SERVER_PORT = "serverPort";
305    
306            private static final String _SESSION_ID = "sessionID";
307    
308            private static final String _TIMESTAMP = "timestamp";
309    
310            private static final String _USER_ID = "userId";
311    
312            private static final String _USER_NAME = "userName";
313    
314            private JSONObject _additionalInfo;
315            private String _className;
316            private String _classPK;
317            private String _clientHost;
318            private String _clientIP;
319            private long _companyId = -1;
320            private String _eventType;
321            private String _message;
322            private String _serverName;
323            private int _serverPort;
324            private String _sessionID;
325            private Date _timestamp;
326            private long _userId = -1;
327            private String _userName;
328    
329    }