001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.struts;
022:
023: import com.liferay.portal.kernel.util.JavaConstants;
024: import com.liferay.portal.kernel.util.ParamUtil;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.portal.security.auth.PrincipalException;
027: import com.liferay.portal.theme.ThemeDisplay;
028: import com.liferay.portal.util.PortalUtil;
029: import com.liferay.portal.util.WebKeys;
030: import com.liferay.portlet.PortletConfigImpl;
031: import com.liferay.util.servlet.SessionErrors;
032: import com.liferay.util.servlet.SessionMessages;
033:
034: import java.io.IOException;
035:
036: import javax.portlet.ActionRequest;
037: import javax.portlet.ActionResponse;
038: import javax.portlet.PortletConfig;
039: import javax.portlet.PortletRequest;
040: import javax.portlet.RenderRequest;
041: import javax.portlet.RenderResponse;
042:
043: import javax.servlet.ServletContext;
044: import javax.servlet.http.HttpServletRequest;
045: import javax.servlet.http.HttpServletResponse;
046:
047: import org.apache.commons.logging.Log;
048: import org.apache.commons.logging.LogFactory;
049: import org.apache.struts.Globals;
050: import org.apache.struts.action.Action;
051: import org.apache.struts.action.ActionForm;
052: import org.apache.struts.action.ActionForward;
053: import org.apache.struts.action.ActionMapping;
054: import org.apache.struts.config.ModuleConfig;
055: import org.apache.struts.util.MessageResources;
056:
057: /**
058: * <a href="PortletAction.java.html"><b><i>View Source</i></b></a>
059: *
060: * @author Brian Wing Shun Chan
061: *
062: */
063: public class PortletAction extends Action {
064:
065: public static String getForwardKey(HttpServletRequest req) {
066: PortletConfigImpl portletConfig = (PortletConfigImpl) req
067: .getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
068:
069: return PortalUtil.getPortletNamespace(portletConfig
070: .getPortletId())
071: + WebKeys.PORTLET_STRUTS_FORWARD;
072: }
073:
074: public static String getForwardKey(PortletRequest req) {
075: PortletConfigImpl portletConfig = (PortletConfigImpl) req
076: .getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
077:
078: return PortalUtil.getPortletNamespace(portletConfig
079: .getPortletId())
080: + WebKeys.PORTLET_STRUTS_FORWARD;
081: }
082:
083: public ActionForward execute(ActionMapping mapping,
084: ActionForm form, HttpServletRequest req,
085: HttpServletResponse res) throws Exception {
086:
087: PortletConfig portletConfig = (PortletConfig) req
088: .getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
089:
090: RenderRequest renderRequest = (RenderRequest) req
091: .getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);
092:
093: RenderResponse renderResponse = (RenderResponse) req
094: .getAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE);
095:
096: Boolean strutsExecute = (Boolean) req
097: .getAttribute(WebKeys.PORTLET_STRUTS_EXECUTE);
098:
099: if ((strutsExecute != null) && strutsExecute.booleanValue()) {
100: return strutsExecute(mapping, form, req, res);
101: } else {
102: return render(mapping, form, portletConfig, renderRequest,
103: renderResponse);
104: }
105: }
106:
107: public ActionForward strutsExecute(ActionMapping mapping,
108: ActionForm form, HttpServletRequest req,
109: HttpServletResponse res) throws Exception {
110:
111: return super .execute(mapping, form, req, res);
112: }
113:
114: public void processAction(ActionMapping mapping, ActionForm form,
115: PortletConfig config, ActionRequest req, ActionResponse res)
116: throws Exception {
117: }
118:
119: public ActionForward render(ActionMapping mapping, ActionForm form,
120: PortletConfig config, RenderRequest req, RenderResponse res)
121: throws Exception {
122:
123: if (_log.isDebugEnabled()) {
124: _log.debug("Forward to " + getForward(req));
125: }
126:
127: return mapping.findForward(getForward(req));
128: }
129:
130: protected String getForward(PortletRequest req) {
131: return getForward(req, null);
132: }
133:
134: protected String getForward(PortletRequest req, String defaultValue) {
135: String forward = (String) req.getAttribute(getForwardKey(req));
136:
137: if (forward == null) {
138: return defaultValue;
139: } else {
140: return forward;
141: }
142: }
143:
144: protected void setForward(PortletRequest req, String forward) {
145: req.setAttribute(getForwardKey(req), forward);
146: }
147:
148: protected ModuleConfig getModuleConfig(PortletRequest req) {
149: return (ModuleConfig) req.getAttribute(Globals.MODULE_KEY);
150: }
151:
152: protected MessageResources getResources() {
153: ServletContext ctx = getServlet().getServletContext();
154:
155: return (MessageResources) ctx
156: .getAttribute(Globals.MESSAGES_KEY);
157: }
158:
159: protected MessageResources getResources(HttpServletRequest req) {
160: return getResources();
161: }
162:
163: protected MessageResources getResources(PortletRequest req) {
164: return getResources();
165: }
166:
167: protected boolean isCheckMethodOnProcessAction() {
168: return _CHECK_METHOD_ON_PROCESS_ACTION;
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(ActionRequest req, ActionResponse res,
178: String redirect) throws IOException {
179:
180: if (SessionErrors.isEmpty(req)) {
181: SessionMessages.add(req, "request_processed");
182: }
183:
184: if (redirect == null) {
185: redirect = ParamUtil.getString(req, "redirect");
186: }
187:
188: if (Validator.isNotNull(redirect)) {
189: res.sendRedirect(redirect);
190: }
191: }
192:
193: protected boolean redirectToLogin(ActionRequest req,
194: ActionResponse res) throws IOException {
195:
196: if (req.getRemoteUser() == null) {
197: HttpServletRequest httpReq = PortalUtil
198: .getHttpServletRequest(req);
199:
200: SessionErrors.add(httpReq, PrincipalException.class
201: .getName());
202:
203: ThemeDisplay themeDisplay = (ThemeDisplay) httpReq
204: .getAttribute(WebKeys.THEME_DISPLAY);
205:
206: res.sendRedirect(themeDisplay.getPathMain()
207: + "/portal/login");
208:
209: return true;
210: } else {
211: return false;
212: }
213: }
214:
215: private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = true;
216:
217: private static Log _log = LogFactory.getLog(PortletAction.class);
218:
219: }
|