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