1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messaging.action;
24  
25  import com.liferay.portal.kernel.servlet.HttpHeaders;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ContentTypes;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.service.UserLocalServiceUtil;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portlet.messaging.util.MessagingUtil;
34  
35  import java.io.PrintWriter;
36  
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  
40  import org.apache.struts.action.Action;
41  import org.apache.struts.action.ActionForm;
42  import org.apache.struts.action.ActionForward;
43  import org.apache.struts.action.ActionMapping;
44  
45  import org.json.JSONObject;
46  
47  /**
48   * <a href="MessagingAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Ming-Gih Lam
51   *
52   */
53  public class MessagingAction extends Action {
54  
55      public ActionForward execute(
56              ActionMapping mapping, ActionForm form, HttpServletRequest req,
57              HttpServletResponse res)
58          throws Exception {
59  
60          String ajaxId = req.getHeader("Ajax-ID");
61  
62          String cmd = ParamUtil.getString(req, "cmd");
63  
64          String json = null;
65  
66          res.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache");
67  
68          if (cmd.equals("chatbox")) {
69              return mapping.findForward("portlet.messaging.chatbox");
70          }
71          else {
72              if (ajaxId != null && !ajaxId.equals("")) {
73                  res.setHeader("Ajax-ID", ajaxId);
74              }
75  
76              try {
77                  json = getJSON(mapping, form, req, res);
78              }
79              catch (Exception e) {
80                  res.sendError(
81                      HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
82                      e.getMessage());
83  
84                  return null;
85              }
86  
87              if (Validator.isNotNull(json)) {
88                  res.setContentType(ContentTypes.TEXT_JAVASCRIPT);
89  
90                  PrintWriter pw = res.getWriter();
91  
92                  pw.write(json);
93  
94                  pw.close();
95              }
96  
97              return null;
98          }
99      }
100 
101     public String getJSON(ActionMapping mapping, ActionForm form,
102         HttpServletRequest req, HttpServletResponse res) throws Exception {
103 
104         String cmd = ParamUtil.getString(req, Constants.CMD);
105         JSONObject jo = new JSONObject();
106 
107         if ("getChats".equals(cmd)) {
108             jo = MessagingUtil.getChatMessages(req.getSession());
109         }
110         else if ("sendChat".equals(cmd)) {
111             jo = sendMessage(req);
112         }
113 
114         return jo.toString();
115     }
116 
117     protected JSONObject sendMessage(HttpServletRequest req) {
118         JSONObject jo = new JSONObject();
119 
120         try {
121             String bodyText = ParamUtil.getString(req, "text");
122             String tempId = ParamUtil.getString(req, "tempId", null);
123             long toId = ParamUtil.getLong(req, "toId");
124             String toAddr = ParamUtil.getString(req, "toAddr", null);
125             long companyId = PortalUtil.getCompanyId(req);
126             User fromUser = PortalUtil.getUser(req);
127             User toUser;
128 
129             if (toAddr != null) {
130                 toUser = UserLocalServiceUtil.getUserByEmailAddress(
131                     companyId, toAddr);
132 
133                 toId = toUser.getUserId();
134             }
135             else {
136                 toUser = UserLocalServiceUtil.getUserById(toId);
137             }
138 
139             MessagingUtil.sendMessage(
140                 req.getSession(), req.getRemoteUser(), fromUser.getFullName(),
141                 String.valueOf(toId), toUser.getFullName(), bodyText);
142 
143             jo.put("status", "success");
144             jo.put("toId", toId);
145             jo.put("toName", toUser.getFullName());
146             jo.put("fromId", PortalUtil.getUserId(req));
147             jo.put("fromName", fromUser.getFullName());
148 
149             if (tempId != null) {
150                 jo.put("tempId", tempId);
151             }
152         }
153         catch (Exception e) {
154             jo.put("status", "failure");
155         }
156 
157         return jo;
158     }
159 
160 }