001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.messaging.action;
022:
023: import com.liferay.portal.kernel.servlet.HttpHeaders;
024: import com.liferay.portal.kernel.util.Constants;
025: import com.liferay.portal.kernel.util.ContentTypes;
026: import com.liferay.portal.kernel.util.ParamUtil;
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.portal.model.User;
029: import com.liferay.portal.service.UserLocalServiceUtil;
030: import com.liferay.portal.util.PortalUtil;
031: import com.liferay.portlet.messaging.util.MessagingUtil;
032:
033: import java.io.PrintWriter;
034:
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpServletResponse;
037:
038: import org.apache.struts.action.Action;
039: import org.apache.struts.action.ActionForm;
040: import org.apache.struts.action.ActionForward;
041: import org.apache.struts.action.ActionMapping;
042:
043: import org.json.JSONObject;
044:
045: /**
046: * <a href="MessagingAction.java.html"><b><i>View Source</i></b></a>
047: *
048: * @author Ming-Gih Lam
049: *
050: */
051: public class MessagingAction extends Action {
052:
053: public ActionForward execute(ActionMapping mapping,
054: ActionForm form, HttpServletRequest req,
055: HttpServletResponse res) throws Exception {
056:
057: String ajaxId = req.getHeader("Ajax-ID");
058:
059: String cmd = ParamUtil.getString(req, "cmd");
060:
061: String json = null;
062:
063: res.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache");
064:
065: if (cmd.equals("chatbox")) {
066: return mapping.findForward("portlet.messaging.chatbox");
067: } else {
068: if (ajaxId != null && !ajaxId.equals("")) {
069: res.setHeader("Ajax-ID", ajaxId);
070: }
071:
072: try {
073: json = getJSON(mapping, form, req, res);
074: } catch (Exception e) {
075: PortalUtil.sendError(
076: HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
077: e, req, res);
078:
079: return null;
080: }
081:
082: if (Validator.isNotNull(json)) {
083: res.setContentType(ContentTypes.TEXT_JAVASCRIPT);
084:
085: PrintWriter pw = res.getWriter();
086:
087: pw.write(json);
088:
089: pw.close();
090: }
091:
092: return null;
093: }
094: }
095:
096: public String getJSON(ActionMapping mapping, ActionForm form,
097: HttpServletRequest req, HttpServletResponse res)
098: throws Exception {
099:
100: String cmd = ParamUtil.getString(req, Constants.CMD);
101: JSONObject jo = new JSONObject();
102:
103: if ("getChats".equals(cmd)) {
104: jo = MessagingUtil.getChatMessages(req.getSession());
105: } else if ("sendChat".equals(cmd)) {
106: jo = sendMessage(req);
107: }
108:
109: return jo.toString();
110: }
111:
112: protected JSONObject sendMessage(HttpServletRequest req) {
113: JSONObject jo = new JSONObject();
114:
115: try {
116: String bodyText = ParamUtil.getString(req, "text");
117: String tempId = ParamUtil.getString(req, "tempId", null);
118: long toId = ParamUtil.getLong(req, "toId");
119: String toAddr = ParamUtil.getString(req, "toAddr", null);
120: long companyId = PortalUtil.getCompanyId(req);
121: User fromUser = PortalUtil.getUser(req);
122: User toUser;
123:
124: if (toAddr != null) {
125: toUser = UserLocalServiceUtil.getUserByEmailAddress(
126: companyId, toAddr);
127:
128: toId = toUser.getUserId();
129: } else {
130: toUser = UserLocalServiceUtil.getUserById(toId);
131: }
132:
133: MessagingUtil.sendMessage(req.getSession(), req
134: .getRemoteUser(), fromUser.getFullName(), String
135: .valueOf(toId), toUser.getFullName(), bodyText);
136:
137: jo.put("status", "success");
138: jo.put("toId", toId);
139: jo.put("toName", toUser.getFullName());
140: jo.put("fromId", PortalUtil.getUserId(req));
141: jo.put("fromName", fromUser.getFullName());
142:
143: if (tempId != null) {
144: jo.put("tempId", tempId);
145: }
146: } catch (Exception e) {
147: jo.put("status", "failure");
148: }
149:
150: return jo;
151: }
152:
153: }
|