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 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 }