1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.liveusers.messaging;
21  
22  import com.liferay.portal.kernel.json.JSONObject;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.messaging.Message;
26  import com.liferay.portal.kernel.messaging.MessageListener;
27  import com.liferay.portal.liveusers.LiveUsers;
28  
29  /**
30   * <a href="LiveUsersMessageListener.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   *
34   */
35  public class LiveUsersMessageListener implements MessageListener {
36  
37      public void receive(Message message) {
38          try {
39              doReceive(message);
40          }
41          catch (Exception e) {
42              _log.error("Unable to process message " + message, e);
43          }
44      }
45  
46      public void doReceive(Message message) throws Exception {
47          JSONObject jsonObj = (JSONObject)message.getPayload();
48  
49          String command = jsonObj.getString("command");
50  
51          if (command.equals("signIn")) {
52              doCommandSignIn(jsonObj);
53          }
54          else if (command.equals("signOut")) {
55              doCommandSignOut(jsonObj);
56          }
57      }
58  
59      protected void doCommandSignIn(JSONObject jsonObj) throws Exception {
60          long companyId = jsonObj.getLong("companyId");
61          long userId = jsonObj.getLong("userId");
62          String sessionId = jsonObj.getString("sessionId");
63          String remoteAddr = jsonObj.getString("remoteAddr");
64          String remoteHost = jsonObj.getString("remoteHost");
65          String userAgent = jsonObj.getString("userAgent");
66  
67          LiveUsers.signIn(
68              companyId, userId, sessionId, remoteAddr, remoteHost, userAgent);
69      }
70  
71      protected void doCommandSignOut(JSONObject jsonObj) throws Exception {
72          long companyId = jsonObj.getLong("companyId");
73          long userId = jsonObj.getLong("userId");
74          String sessionId = jsonObj.getString("sessionId");
75  
76          LiveUsers.signOut(companyId, userId, sessionId);
77      }
78  
79      private static Log _log =
80          LogFactoryUtil.getLog(LiveUsersMessageListener.class);
81  
82  }