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.portlet;
022:
023: import com.liferay.portal.kernel.portlet.LiferayPortlet;
024: import com.liferay.portal.kernel.util.GetterUtil;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.portal.security.permission.PermissionCheckerImpl;
027: import com.liferay.portal.security.permission.PermissionThreadLocal;
028: import com.liferay.portal.struts.PortletRequestProcessor;
029: import com.liferay.portal.struts.StrutsUtil;
030: import com.liferay.portal.util.PortalUtil;
031: import com.liferay.portal.util.WebKeys;
032:
033: import java.io.IOException;
034:
035: import java.util.Map;
036:
037: import javax.portlet.ActionRequest;
038: import javax.portlet.ActionResponse;
039: import javax.portlet.PortletConfig;
040: import javax.portlet.PortletException;
041: import javax.portlet.PortletRequest;
042: import javax.portlet.RenderRequest;
043: import javax.portlet.RenderResponse;
044:
045: import javax.servlet.ServletException;
046:
047: /**
048: * <a href="StrutsPortlet.java.html"><b><i>View Source</i></b></a>
049: *
050: * @author Brian Wing Shun Chan
051: *
052: */
053: public class StrutsPortlet extends LiferayPortlet {
054:
055: public void init(PortletConfig config) throws PortletException {
056: super .init(config);
057:
058: aboutAction = getInitParameter("about-action");
059: configAction = getInitParameter("config-action");
060: editAction = getInitParameter("edit-action");
061: editDefaultsAction = getInitParameter("edit-defaults-action");
062: editGuestAction = getInitParameter("edit-guest-action");
063: helpAction = getInitParameter("help-action");
064: previewAction = getInitParameter("preview-action");
065: printAction = getInitParameter("print-action");
066: viewAction = getInitParameter("view-action");
067:
068: copyRequestParameters = GetterUtil.getBoolean(
069: getInitParameter("copy-request-parameters"), true);
070:
071: _portletConfig = (PortletConfigImpl) config;
072: }
073:
074: public void processAction(ActionRequest req, ActionResponse res)
075: throws IOException, PortletException {
076:
077: String path = req.getParameter("struts_action");
078:
079: if (Validator.isNotNull(path)) {
080:
081: // Call processAction of com.liferay.portal.struts.PortletAction
082:
083: PermissionCheckerImpl permissionChecker = (PermissionCheckerImpl) PermissionThreadLocal
084: .getPermissionChecker();
085:
086: try {
087: permissionChecker.setValues(req);
088:
089: // Process action
090:
091: PortletRequestProcessor processor = _getPortletRequestProcessor(req);
092:
093: processor.process(req, res, path);
094: } catch (ServletException se) {
095: throw new PortletException(se);
096: } finally {
097: permissionChecker.resetValues();
098: }
099: }
100:
101: if (copyRequestParameters) {
102: PortalUtil.copyRequestParameters(req, res);
103: }
104: }
105:
106: public void doAbout(RenderRequest req, RenderResponse res)
107: throws IOException, PortletException {
108:
109: req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, aboutAction);
110:
111: include(req, res);
112: }
113:
114: public void doConfig(RenderRequest req, RenderResponse res)
115: throws IOException, PortletException {
116:
117: req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, configAction);
118:
119: include(req, res);
120: }
121:
122: public void doEdit(RenderRequest req, RenderResponse res)
123: throws IOException, PortletException {
124:
125: if (req.getPreferences() == null) {
126: super .doEdit(req, res);
127: } else {
128: req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, editAction);
129:
130: include(req, res);
131: }
132: }
133:
134: public void doEditDefaults(RenderRequest req, RenderResponse res)
135: throws IOException, PortletException {
136:
137: if (req.getPreferences() == null) {
138: super .doEdit(req, res);
139: } else {
140: req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION,
141: editDefaultsAction);
142:
143: include(req, res);
144: }
145: }
146:
147: public void doEditGuest(RenderRequest req, RenderResponse res)
148: throws IOException, PortletException {
149:
150: if (req.getPreferences() == null) {
151: super .doEdit(req, res);
152: } else {
153: req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION,
154: editGuestAction);
155:
156: include(req, res);
157: }
158: }
159:
160: public void doHelp(RenderRequest req, RenderResponse res)
161: throws IOException, PortletException {
162:
163: req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, helpAction);
164:
165: include(req, res);
166: }
167:
168: public void doPreview(RenderRequest req, RenderResponse res)
169: throws IOException, PortletException {
170:
171: req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, previewAction);
172:
173: include(req, res);
174: }
175:
176: public void doPrint(RenderRequest req, RenderResponse res)
177: throws IOException, PortletException {
178:
179: req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, printAction);
180:
181: include(req, res);
182: }
183:
184: public void doView(RenderRequest req, RenderResponse res)
185: throws IOException, PortletException {
186:
187: req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, viewAction);
188:
189: include(req, res);
190: }
191:
192: protected void include(RenderRequest req, RenderResponse res)
193: throws IOException, PortletException {
194:
195: // Call render of com.liferay.portal.struts.PortletAction
196:
197: Map strutsAttributes = null;
198:
199: if (_portletConfig.isWARFile()) {
200:
201: // Remove any Struts request attributes
202:
203: strutsAttributes = StrutsUtil.removeStrutsAttributes(
204: getPortletContext(), req);
205: }
206:
207: // Process render
208:
209: PermissionCheckerImpl permissionChecker = (PermissionCheckerImpl) PermissionThreadLocal
210: .getPermissionChecker();
211:
212: try {
213: permissionChecker.setValues(req);
214:
215: PortletRequestProcessor processor = _getPortletRequestProcessor(req);
216:
217: processor.process(req, res);
218: } catch (IOException ioe) {
219: throw ioe;
220: } catch (ServletException se) {
221: throw new PortletException(se);
222: } finally {
223: permissionChecker.resetValues();
224:
225: if (_portletConfig.isWARFile()) {
226:
227: // Set the Struts request attributes
228:
229: StrutsUtil.setStrutsAttributes(req, strutsAttributes);
230: }
231: }
232:
233: if (copyRequestParameters) {
234: PortalUtil.clearRequestParameters(req);
235: }
236: }
237:
238: private PortletRequestProcessor _getPortletRequestProcessor(
239: PortletRequest req) {
240:
241: return (PortletRequestProcessor) getPortletContext()
242: .getAttribute(WebKeys.PORTLET_STRUTS_PROCESSOR);
243: }
244:
245: protected String aboutAction;
246: protected String configAction;
247: protected String editAction;
248: protected String editDefaultsAction;
249: protected String editGuestAction;
250: protected String helpAction;
251: protected String previewAction;
252: protected String printAction;
253: protected String viewAction;
254: protected boolean copyRequestParameters;
255:
256: private PortletConfigImpl _portletConfig;
257:
258: }
|