001
014
015 package com.liferay.portlet;
016
017 import com.liferay.portal.kernel.portlet.LiferayPortlet;
018 import com.liferay.portal.kernel.util.GetterUtil;
019 import com.liferay.portal.kernel.util.StringPool;
020 import com.liferay.portal.kernel.util.Validator;
021 import com.liferay.portal.struts.PortletRequestProcessor;
022 import com.liferay.portal.struts.StrutsUtil;
023 import com.liferay.portal.util.PortalUtil;
024 import com.liferay.portal.util.WebKeys;
025
026 import java.io.IOException;
027
028 import java.util.Map;
029
030 import javax.portlet.ActionRequest;
031 import javax.portlet.ActionResponse;
032 import javax.portlet.PortletConfig;
033 import javax.portlet.PortletContext;
034 import javax.portlet.PortletException;
035 import javax.portlet.RenderRequest;
036 import javax.portlet.RenderResponse;
037 import javax.portlet.ResourceRequest;
038 import javax.portlet.ResourceResponse;
039
040 import javax.servlet.ServletException;
041
042
045 public class StrutsPortlet extends LiferayPortlet {
046
047 @Override
048 public void doAbout(
049 RenderRequest renderRequest, RenderResponse renderResponse)
050 throws IOException, PortletException {
051
052 renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, aboutAction);
053
054 include(renderRequest, renderResponse);
055 }
056
057 @Override
058 public void doConfig(
059 RenderRequest renderRequest, RenderResponse renderResponse)
060 throws IOException, PortletException {
061
062 renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, configAction);
063
064 include(renderRequest, renderResponse);
065 }
066
067 @Override
068 public void doEdit(
069 RenderRequest renderRequest, RenderResponse renderResponse)
070 throws IOException, PortletException {
071
072 if (renderRequest.getPreferences() == null) {
073 super.doEdit(renderRequest, renderResponse);
074 }
075 else {
076 renderRequest.setAttribute(
077 WebKeys.PORTLET_STRUTS_ACTION, editAction);
078
079 include(renderRequest, renderResponse);
080 }
081 }
082
083 @Override
084 public void doEditDefaults(
085 RenderRequest renderRequest, RenderResponse renderResponse)
086 throws IOException, PortletException {
087
088 if (renderRequest.getPreferences() == null) {
089 super.doEdit(renderRequest, renderResponse);
090 }
091 else {
092 renderRequest.setAttribute(
093 WebKeys.PORTLET_STRUTS_ACTION, editDefaultsAction);
094
095 include(renderRequest, renderResponse);
096 }
097 }
098
099 @Override
100 public void doEditGuest(
101 RenderRequest renderRequest, RenderResponse renderResponse)
102 throws IOException, PortletException {
103
104 if (renderRequest.getPreferences() == null) {
105 super.doEdit(renderRequest, renderResponse);
106 }
107 else {
108 renderRequest.setAttribute(
109 WebKeys.PORTLET_STRUTS_ACTION, editGuestAction);
110
111 include(renderRequest, renderResponse);
112 }
113 }
114
115 @Override
116 public void doHelp(
117 RenderRequest renderRequest, RenderResponse renderResponse)
118 throws IOException, PortletException {
119
120 renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, helpAction);
121
122 include(renderRequest, renderResponse);
123 }
124
125 @Override
126 public void doPreview(
127 RenderRequest renderRequest, RenderResponse renderResponse)
128 throws IOException, PortletException {
129
130 renderRequest.setAttribute(
131 WebKeys.PORTLET_STRUTS_ACTION, previewAction);
132
133 include(renderRequest, renderResponse);
134 }
135
136 @Override
137 public void doPrint(
138 RenderRequest renderRequest, RenderResponse renderResponse)
139 throws IOException, PortletException {
140
141 renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, printAction);
142
143 include(renderRequest, renderResponse);
144 }
145
146 @Override
147 public void doView(
148 RenderRequest renderRequest, RenderResponse renderResponse)
149 throws IOException, PortletException {
150
151 renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, viewAction);
152
153 include(renderRequest, renderResponse);
154 }
155
156 @Override
157 public void init(PortletConfig portletConfig) throws PortletException {
158 super.init(portletConfig);
159
160 templatePath = getInitParameter("template-path");
161
162 if (Validator.isNull(templatePath)) {
163 templatePath = StringPool.SLASH;
164 }
165 else if (templatePath.contains(StringPool.BACK_SLASH) ||
166 templatePath.contains(StringPool.DOUBLE_SLASH) ||
167 templatePath.contains(StringPool.PERIOD) ||
168 templatePath.contains(StringPool.SPACE)) {
169
170 throw new PortletException(
171 "template-path " + templatePath + " has invalid characters");
172 }
173 else if (!templatePath.startsWith(StringPool.SLASH) ||
174 !templatePath.endsWith(StringPool.SLASH)) {
175
176 throw new PortletException(
177 "template-path " + templatePath +
178 " must start and end with a /");
179 }
180
181 aboutAction = getInitParameter("about-action");
182 configAction = getInitParameter("config-action");
183 editAction = getInitParameter("edit-action");
184 editDefaultsAction = getInitParameter("edit-defaults-action");
185 editGuestAction = getInitParameter("edit-guest-action");
186 helpAction = getInitParameter("help-action");
187 previewAction = getInitParameter("preview-action");
188 printAction = getInitParameter("print-action");
189 viewAction = getInitParameter("view-action");
190
191 copyRequestParameters = GetterUtil.getBoolean(
192 getInitParameter("copy-request-parameters"), true);
193
194 _portletConfig = (PortletConfigImpl)portletConfig;
195 }
196
197 @Override
198 public void processAction(
199 ActionRequest actionRequest, ActionResponse actionResponse)
200 throws IOException, PortletException {
201
202 String path = actionRequest.getParameter("struts_action");
203
204 if (Validator.isNotNull(path)) {
205
206
207
208 try {
209 PortletRequestProcessor processor =
210 _getPortletRequestProcessor();
211
212 processor.process(actionRequest, actionResponse, path);
213 }
214 catch (ServletException se) {
215 throw new PortletException(se);
216 }
217 }
218
219 if (copyRequestParameters) {
220 PortalUtil.copyRequestParameters(actionRequest, actionResponse);
221 }
222 }
223
224 @Override
225 public void serveResource(
226 ResourceRequest resourceRequest, ResourceResponse resourceResponse)
227 throws IOException, PortletException {
228
229 String resourceId = resourceRequest.getResourceID();
230
231 checkPath(resourceId);
232
233 resourceRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, viewAction);
234
235
236
237 try {
238 PortletRequestProcessor processor = _getPortletRequestProcessor();
239
240 processor.process(resourceRequest, resourceResponse);
241 }
242 catch (ServletException se) {
243 throw new PortletException(se);
244 }
245 }
246
247 protected void checkPath(String path) throws PortletException {
248 if (Validator.isNotNull(path) &&
249 (!path.startsWith(templatePath) ||
250 path.contains(StringPool.DOUBLE_PERIOD) ||
251 !PortalUtil.isValidResourceId(path))) {
252
253 throw new PortletException(
254 "Path " + path + " is not accessible by this portlet");
255 }
256 }
257
258 protected void include(
259 RenderRequest renderRequest, RenderResponse renderResponse)
260 throws IOException, PortletException {
261
262
263
264 Map<String, Object> strutsAttributes = null;
265
266 if (_portletConfig.isWARFile()) {
267 strutsAttributes = StrutsUtil.removeStrutsAttributes(
268 getPortletContext(), renderRequest);
269 }
270
271 try {
272 PortletRequestProcessor processor = _getPortletRequestProcessor();
273
274 processor.process(renderRequest, renderResponse);
275 }
276 catch (ServletException se) {
277 throw new PortletException(se);
278 }
279 finally {
280 if (_portletConfig.isWARFile()) {
281 StrutsUtil.setStrutsAttributes(renderRequest, strutsAttributes);
282 }
283 }
284
285 if (copyRequestParameters) {
286 PortalUtil.clearRequestParameters(renderRequest);
287 }
288 }
289
290 protected String aboutAction;
291 protected String configAction;
292 protected boolean copyRequestParameters;
293 protected String editAction;
294 protected String editDefaultsAction;
295 protected String editGuestAction;
296 protected String helpAction;
297 protected String previewAction;
298 protected String printAction;
299 protected String templatePath;
300 protected String viewAction;
301
302 private PortletRequestProcessor _getPortletRequestProcessor() {
303 PortletContext portletContext = getPortletContext();
304
305 return (PortletRequestProcessor)portletContext.getAttribute(
306 WebKeys.PORTLET_STRUTS_PROCESSOR);
307 }
308
309 private PortletConfigImpl _portletConfig;
310
311 }