1
22
23 package com.liferay.portal.struts;
24
25 import com.liferay.portal.kernel.util.JavaConstants;
26 import com.liferay.portal.kernel.util.ParamUtil;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portal.security.auth.PrincipalException;
29 import com.liferay.portal.theme.ThemeDisplay;
30 import com.liferay.portal.util.PortalUtil;
31 import com.liferay.portal.util.WebKeys;
32 import com.liferay.portlet.PortletConfigImpl;
33 import com.liferay.util.servlet.SessionErrors;
34 import com.liferay.util.servlet.SessionMessages;
35
36 import java.io.IOException;
37
38 import javax.portlet.ActionRequest;
39 import javax.portlet.ActionResponse;
40 import javax.portlet.PortletConfig;
41 import javax.portlet.PortletRequest;
42 import javax.portlet.RenderRequest;
43 import javax.portlet.RenderResponse;
44 import javax.portlet.ResourceRequest;
45 import javax.portlet.ResourceResponse;
46
47 import javax.servlet.ServletContext;
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
50
51 import org.apache.commons.logging.Log;
52 import org.apache.commons.logging.LogFactory;
53 import org.apache.struts.Globals;
54 import org.apache.struts.action.Action;
55 import org.apache.struts.action.ActionForm;
56 import org.apache.struts.action.ActionForward;
57 import org.apache.struts.action.ActionMapping;
58 import org.apache.struts.config.ModuleConfig;
59 import org.apache.struts.util.MessageResources;
60
61
67 public class PortletAction extends Action {
68
69 public static String getForwardKey(HttpServletRequest req) {
70 PortletConfigImpl portletConfig = (PortletConfigImpl)req.getAttribute(
71 JavaConstants.JAVAX_PORTLET_CONFIG);
72
73 return PortalUtil.getPortletNamespace(portletConfig.getPortletId()) +
74 WebKeys.PORTLET_STRUTS_FORWARD;
75 }
76
77 public static String getForwardKey(PortletRequest req) {
78 PortletConfigImpl portletConfig = (PortletConfigImpl)req.getAttribute(
79 JavaConstants.JAVAX_PORTLET_CONFIG);
80
81 return PortalUtil.getPortletNamespace(portletConfig.getPortletId()) +
82 WebKeys.PORTLET_STRUTS_FORWARD;
83 }
84
85 public ActionForward execute(
86 ActionMapping mapping, ActionForm form, HttpServletRequest req,
87 HttpServletResponse res)
88 throws Exception {
89
90 PortletConfig portletConfig = (PortletConfig)req.getAttribute(
91 JavaConstants.JAVAX_PORTLET_CONFIG);
92
93 RenderRequest renderRequest = (RenderRequest)req.getAttribute(
94 JavaConstants.JAVAX_PORTLET_REQUEST);
95
96 RenderResponse renderResponse = (RenderResponse)req.getAttribute(
97 JavaConstants.JAVAX_PORTLET_RESPONSE);
98
99 Boolean strutsExecute = (Boolean)req.getAttribute(
100 WebKeys.PORTLET_STRUTS_EXECUTE);
101
102 if ((strutsExecute != null) && strutsExecute.booleanValue()) {
103 return strutsExecute(mapping, form, req, res);
104 }
105 else {
106 return render(
107 mapping, form, portletConfig, renderRequest, renderResponse);
108 }
109 }
110
111 public ActionForward strutsExecute(
112 ActionMapping mapping, ActionForm form, HttpServletRequest req,
113 HttpServletResponse res)
114 throws Exception {
115
116 return super.execute(mapping, form, req, res);
117 }
118
119 public void processAction(
120 ActionMapping mapping, ActionForm form, PortletConfig config,
121 ActionRequest req, ActionResponse res)
122 throws Exception {
123 }
124
125 public ActionForward render(
126 ActionMapping mapping, ActionForm form, PortletConfig config,
127 RenderRequest req, RenderResponse res)
128 throws Exception {
129
130 if (_log.isDebugEnabled()) {
131 _log.debug("Forward to " + getForward(req));
132 }
133
134 return mapping.findForward(getForward(req));
135 }
136
137 public ActionForward serveResource(
138 ActionMapping mapping, ActionForm form, PortletConfig config,
139 ResourceRequest req, ResourceResponse res)
140 throws Exception {
141
142 if (_log.isDebugEnabled()) {
143 _log.debug("Forward to " + getForward(req));
144 }
145
146 return mapping.findForward(getForward(req));
147 }
148
149 protected String getForward(PortletRequest req) {
150 return getForward(req, null);
151 }
152
153 protected String getForward(PortletRequest req, String defaultValue) {
154 String forward = (String)req.getAttribute(getForwardKey(req));
155
156 if (forward == null) {
157 return defaultValue;
158 }
159 else {
160 return forward;
161 }
162 }
163
164 protected void setForward(PortletRequest req, String forward) {
165 req.setAttribute(getForwardKey(req), forward);
166 }
167
168 protected ModuleConfig getModuleConfig(PortletRequest req) {
169 return (ModuleConfig)req.getAttribute(Globals.MODULE_KEY);
170 }
171
172 protected MessageResources getResources() {
173 ServletContext ctx = getServlet().getServletContext();
174
175 return (MessageResources)ctx.getAttribute(Globals.MESSAGES_KEY);
176 }
177
178 protected MessageResources getResources(HttpServletRequest req) {
179 return getResources();
180 }
181
182 protected MessageResources getResources(PortletRequest req) {
183 return getResources();
184 }
185
186 protected boolean isCheckMethodOnProcessAction() {
187 return _CHECK_METHOD_ON_PROCESS_ACTION;
188 }
189
190 protected void sendRedirect(ActionRequest req, ActionResponse res)
191 throws IOException {
192
193 sendRedirect(req, res, null);
194 }
195
196 protected void sendRedirect(
197 ActionRequest req, ActionResponse res, String redirect)
198 throws IOException {
199
200 if (SessionErrors.isEmpty(req)) {
201 SessionMessages.add(req, "request_processed");
202 }
203
204 if (redirect == null) {
205 redirect = ParamUtil.getString(req, "redirect");
206 }
207
208 if (Validator.isNotNull(redirect)) {
209 res.sendRedirect(redirect);
210 }
211 }
212
213 protected boolean redirectToLogin(ActionRequest req, ActionResponse res)
214 throws IOException {
215
216 if (req.getRemoteUser() == null) {
217 HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
218
219 SessionErrors.add(httpReq, PrincipalException.class.getName());
220
221 ThemeDisplay themeDisplay =
222 (ThemeDisplay)httpReq.getAttribute(WebKeys.THEME_DISPLAY);
223
224 res.sendRedirect(themeDisplay.getURLSignIn());
225
226 return true;
227 }
228 else {
229 return false;
230 }
231 }
232
233 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = true;
234
235 private static Log _log = LogFactory.getLog(PortletAction.class);
236
237 }