001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.liveusers.messaging;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.messaging.Message;
022    import com.liferay.portal.kernel.messaging.MessageListener;
023    import com.liferay.portal.liveusers.LiveUsers;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class LiveUsersMessageListener implements MessageListener {
029    
030            public void receive(Message message) {
031                    try {
032                            doReceive(message);
033                    }
034                    catch (Exception e) {
035                            _log.error("Unable to process message " + message, e);
036                    }
037            }
038    
039            protected void doCommandSignIn(JSONObject jsonObject) throws Exception {
040                    long companyId = jsonObject.getLong("companyId");
041                    long userId = jsonObject.getLong("userId");
042                    String sessionId = jsonObject.getString("sessionId");
043                    String remoteAddr = jsonObject.getString("remoteAddr");
044                    String remoteHost = jsonObject.getString("remoteHost");
045                    String userAgent = jsonObject.getString("userAgent");
046    
047                    LiveUsers.signIn(
048                            companyId, userId, sessionId, remoteAddr, remoteHost, userAgent);
049            }
050    
051            protected void doCommandSignOut(JSONObject jsonObject) throws Exception {
052                    long companyId = jsonObject.getLong("companyId");
053                    long userId = jsonObject.getLong("userId");
054                    String sessionId = jsonObject.getString("sessionId");
055    
056                    LiveUsers.signOut(companyId, userId, sessionId);
057            }
058    
059            protected void doReceive(Message message) throws Exception {
060                    String payload = (String)message.getPayload();
061    
062                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(payload);
063    
064                    String command = jsonObject.getString("command");
065    
066                    if (command.equals("signIn")) {
067                            doCommandSignIn(jsonObject);
068                    }
069                    else if (command.equals("signOut")) {
070                            doCommandSignOut(jsonObject);
071                    }
072            }
073    
074            private static Log _log = LogFactoryUtil.getLog(
075                    LiveUsersMessageListener.class);
076    
077    }