1
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
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 PortalUtil.sendError(
81 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, req, res);
82
83 return null;
84 }
85
86 if (Validator.isNotNull(json)) {
87 res.setContentType(ContentTypes.TEXT_JAVASCRIPT);
88
89 PrintWriter pw = res.getWriter();
90
91 pw.write(json);
92
93 pw.close();
94 }
95
96 return null;
97 }
98 }
99
100 public String getJSON(ActionMapping mapping, ActionForm form,
101 HttpServletRequest req, HttpServletResponse res) throws Exception {
102
103 String cmd = ParamUtil.getString(req, Constants.CMD);
104 JSONObject jo = new JSONObject();
105
106 if ("getChats".equals(cmd)) {
107 jo = MessagingUtil.getChatMessages(req.getSession());
108 }
109 else if ("sendChat".equals(cmd)) {
110 jo = sendMessage(req);
111 }
112
113 return jo.toString();
114 }
115
116 protected JSONObject sendMessage(HttpServletRequest req) {
117 JSONObject jo = new JSONObject();
118
119 try {
120 String bodyText = ParamUtil.getString(req, "text");
121 String tempId = ParamUtil.getString(req, "tempId", null);
122 long toId = ParamUtil.getLong(req, "toId");
123 String toAddr = ParamUtil.getString(req, "toAddr", null);
124 long companyId = PortalUtil.getCompanyId(req);
125 User fromUser = PortalUtil.getUser(req);
126 User toUser;
127
128 if (toAddr != null) {
129 toUser = UserLocalServiceUtil.getUserByEmailAddress(
130 companyId, toAddr);
131
132 toId = toUser.getUserId();
133 }
134 else {
135 toUser = UserLocalServiceUtil.getUserById(toId);
136 }
137
138 MessagingUtil.sendMessage(
139 req.getSession(), req.getRemoteUser(), fromUser.getFullName(),
140 String.valueOf(toId), toUser.getFullName(), bodyText);
141
142 jo.put("status", "success");
143 jo.put("toId", toId);
144 jo.put("toName", toUser.getFullName());
145 jo.put("fromId", PortalUtil.getUserId(req));
146 jo.put("fromName", fromUser.getFullName());
147
148 if (tempId != null) {
149 jo.put("tempId", tempId);
150 }
151 }
152 catch (Exception e) {
153 jo.put("status", "failure");
154 }
155
156 return jo;
157 }
158
159 }