001    /**
002     * Copyright (c) 2000-2011 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.messaging.BaseMessageListener;
020    import com.liferay.portal.kernel.messaging.Message;
021    import com.liferay.portal.liveusers.LiveUsers;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class LiveUsersMessageListener extends BaseMessageListener {
027    
028            protected void doCommandSignIn(JSONObject jsonObject) throws Exception {
029                    long companyId = jsonObject.getLong("companyId");
030                    long userId = jsonObject.getLong("userId");
031                    String sessionId = jsonObject.getString("sessionId");
032                    String remoteAddr = jsonObject.getString("remoteAddr");
033                    String remoteHost = jsonObject.getString("remoteHost");
034                    String userAgent = jsonObject.getString("userAgent");
035    
036                    LiveUsers.signIn(
037                            companyId, userId, sessionId, remoteAddr, remoteHost, userAgent);
038            }
039    
040            protected void doCommandSignOut(JSONObject jsonObject) throws Exception {
041                    long companyId = jsonObject.getLong("companyId");
042                    long userId = jsonObject.getLong("userId");
043                    String sessionId = jsonObject.getString("sessionId");
044    
045                    LiveUsers.signOut(companyId, userId, sessionId);
046            }
047    
048            @Override
049            protected void doReceive(Message message) throws Exception {
050                    String payload = (String)message.getPayload();
051    
052                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(payload);
053    
054                    String command = jsonObject.getString("command");
055    
056                    if (command.equals("signIn")) {
057                            doCommandSignIn(jsonObject);
058                    }
059                    else if (command.equals("signOut")) {
060                            doCommandSignOut(jsonObject);
061                    }
062            }
063    
064    }