001    /**
002     * Copyright (c) 2000-2011 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.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    }