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