1
22
23 package com.liferay.portlet;
24
25 import com.liferay.portal.kernel.portlet.LiferayPortlet;
26 import com.liferay.portal.kernel.util.GetterUtil;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portal.security.permission.PermissionCheckerImpl;
29 import com.liferay.portal.security.permission.PermissionThreadLocal;
30 import com.liferay.portal.struts.PortletRequestProcessor;
31 import com.liferay.portal.struts.StrutsUtil;
32 import com.liferay.portal.util.PortalUtil;
33 import com.liferay.portal.util.WebKeys;
34
35 import java.io.IOException;
36
37 import java.util.Map;
38
39 import javax.portlet.ActionRequest;
40 import javax.portlet.ActionResponse;
41 import javax.portlet.PortletConfig;
42 import javax.portlet.PortletException;
43 import javax.portlet.PortletRequest;
44 import javax.portlet.RenderRequest;
45 import javax.portlet.RenderResponse;
46
47 import javax.servlet.ServletException;
48
49
55 public class StrutsPortlet extends LiferayPortlet {
56
57 public void init(PortletConfig config) throws PortletException {
58 super.init(config);
59
60 aboutAction = getInitParameter("about-action");
61 configAction = getInitParameter("config-action");
62 editAction = getInitParameter("edit-action");
63 editDefaultsAction = getInitParameter("edit-defaults-action");
64 editGuestAction = getInitParameter("edit-guest-action");
65 helpAction = getInitParameter("help-action");
66 previewAction = getInitParameter("preview-action");
67 printAction = getInitParameter("print-action");
68 viewAction = getInitParameter("view-action");
69
70 copyRequestParameters = GetterUtil.getBoolean(
71 getInitParameter("copy-request-parameters"), true);
72
73 _portletConfig = (PortletConfigImpl)config;
74 }
75
76 public void processAction(ActionRequest req, ActionResponse res)
77 throws IOException, PortletException {
78
79 String path = req.getParameter("struts_action");
80
81 if (Validator.isNotNull(path)) {
82
83
85 PermissionCheckerImpl permissionChecker = (PermissionCheckerImpl)
86 PermissionThreadLocal.getPermissionChecker();
87
88 try {
89 permissionChecker.setValues(req);
90
91
93 PortletRequestProcessor processor =
94 _getPortletRequestProcessor(req);
95
96 processor.process(req, res, path);
97 }
98 catch (ServletException se) {
99 throw new PortletException(se);
100 }
101 finally {
102 permissionChecker.resetValues();
103 }
104 }
105
106 if (copyRequestParameters) {
107 PortalUtil.copyRequestParameters(req, res);
108 }
109 }
110
111 public void doAbout(RenderRequest req, RenderResponse res)
112 throws IOException, PortletException {
113
114 req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, aboutAction);
115
116 include(req, res);
117 }
118
119 public void doConfig(RenderRequest req, RenderResponse res)
120 throws IOException, PortletException {
121
122 req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, configAction);
123
124 include(req, res);
125 }
126
127 public void doEdit(RenderRequest req, RenderResponse res)
128 throws IOException, PortletException {
129
130 if (req.getPreferences() == null) {
131 super.doEdit(req, res);
132 }
133 else {
134 req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, editAction);
135
136 include(req, res);
137 }
138 }
139
140 public void doEditDefaults(RenderRequest req, RenderResponse res)
141 throws IOException, PortletException {
142
143 if (req.getPreferences() == null) {
144 super.doEdit(req, res);
145 }
146 else {
147 req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, editDefaultsAction);
148
149 include(req, res);
150 }
151 }
152
153 public void doEditGuest(RenderRequest req, RenderResponse res)
154 throws IOException, PortletException {
155
156 if (req.getPreferences() == null) {
157 super.doEdit(req, res);
158 }
159 else {
160 req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, editGuestAction);
161
162 include(req, res);
163 }
164 }
165
166 public void doHelp(RenderRequest req, RenderResponse res)
167 throws IOException, PortletException {
168
169 req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, helpAction);
170
171 include(req, res);
172 }
173
174 public void doPreview(RenderRequest req, RenderResponse res)
175 throws IOException, PortletException {
176
177 req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, previewAction);
178
179 include(req, res);
180 }
181
182 public void doPrint(RenderRequest req, RenderResponse res)
183 throws IOException, PortletException {
184
185 req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, printAction);
186
187 include(req, res);
188 }
189
190 public void doView(RenderRequest req, RenderResponse res)
191 throws IOException, PortletException {
192
193 req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, viewAction);
194
195 include(req, res);
196 }
197
198 protected void include(RenderRequest req, RenderResponse res)
199 throws IOException, PortletException {
200
201
203 Map strutsAttributes = null;
204
205 if (_portletConfig.isWARFile()) {
206
207
209 strutsAttributes =
210 StrutsUtil.removeStrutsAttributes(getPortletContext(), req);
211 }
212
213
215 PermissionCheckerImpl permissionChecker =
216 (PermissionCheckerImpl)PermissionThreadLocal.getPermissionChecker();
217
218 try {
219 permissionChecker.setValues(req);
220
221 PortletRequestProcessor processor =
222 _getPortletRequestProcessor(req);
223
224 processor.process(req, res);
225 }
226 catch (IOException ioe) {
227 throw ioe;
228 }
229 catch (ServletException se) {
230 throw new PortletException(se);
231 }
232 finally {
233 permissionChecker.resetValues();
234
235 if (_portletConfig.isWARFile()) {
236
237
239 StrutsUtil.setStrutsAttributes(req, strutsAttributes);
240 }
241 }
242
243 if (copyRequestParameters) {
244 PortalUtil.clearRequestParameters(req);
245 }
246 }
247
248 private PortletRequestProcessor _getPortletRequestProcessor(
249 PortletRequest req) {
250
251 return (PortletRequestProcessor)getPortletContext().getAttribute(
252 WebKeys.PORTLET_STRUTS_PROCESSOR);
253 }
254
255 protected String aboutAction;
256 protected String configAction;
257 protected String editAction;
258 protected String editDefaultsAction;
259 protected String editGuestAction;
260 protected String helpAction;
261 protected String previewAction;
262 protected String printAction;
263 protected String viewAction;
264 protected boolean copyRequestParameters;
265
266 private PortletConfigImpl _portletConfig;
267
268 }