001
014
015 package com.liferay.util.bridges.mvc;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.portlet.LiferayPortlet;
020 import com.liferay.portal.kernel.util.GetterUtil;
021 import com.liferay.portal.kernel.util.ParamUtil;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.util.Validator;
024 import com.liferay.portal.kernel.util.WebKeys;
025 import com.liferay.portal.util.PortalUtil;
026
027 import java.io.IOException;
028
029 import java.util.List;
030
031 import javax.portlet.ActionRequest;
032 import javax.portlet.ActionResponse;
033 import javax.portlet.EventRequest;
034 import javax.portlet.EventResponse;
035 import javax.portlet.PortletConfig;
036 import javax.portlet.PortletContext;
037 import javax.portlet.PortletException;
038 import javax.portlet.PortletPreferences;
039 import javax.portlet.PortletRequest;
040 import javax.portlet.PortletRequestDispatcher;
041 import javax.portlet.PortletResponse;
042 import javax.portlet.RenderRequest;
043 import javax.portlet.RenderResponse;
044 import javax.portlet.ResourceRequest;
045 import javax.portlet.ResourceResponse;
046 import javax.portlet.WindowState;
047
048
052 public class MVCPortlet extends LiferayPortlet {
053
054 @Override
055 public void doAbout(
056 RenderRequest renderRequest, RenderResponse renderResponse)
057 throws IOException, PortletException {
058
059 include(aboutTemplate, renderRequest, renderResponse);
060 }
061
062 @Override
063 public void doConfig(
064 RenderRequest renderRequest, RenderResponse renderResponse)
065 throws IOException, PortletException {
066
067 include(configTemplate, renderRequest, renderResponse);
068 }
069
070 @Override
071 public void doEdit(
072 RenderRequest renderRequest, RenderResponse renderResponse)
073 throws IOException, PortletException {
074
075 PortletPreferences portletPreferences = renderRequest.getPreferences();
076
077 if (portletPreferences == null) {
078 super.doEdit(renderRequest, renderResponse);
079 }
080 else {
081 include(editTemplate, renderRequest, renderResponse);
082 }
083 }
084
085 @Override
086 public void doEditDefaults(
087 RenderRequest renderRequest, RenderResponse renderResponse)
088 throws IOException, PortletException {
089
090 PortletPreferences portletPreferences = renderRequest.getPreferences();
091
092 if (portletPreferences == null) {
093 super.doEdit(renderRequest, renderResponse);
094 }
095 else {
096 include(editDefaultsTemplate, renderRequest, renderResponse);
097 }
098 }
099
100 @Override
101 public void doEditGuest(
102 RenderRequest renderRequest, RenderResponse renderResponse)
103 throws IOException, PortletException {
104
105 PortletPreferences portletPreferences = renderRequest.getPreferences();
106
107 if (portletPreferences == null) {
108 super.doEdit(renderRequest, renderResponse);
109 }
110 else {
111 include(editGuestTemplate, renderRequest, renderResponse);
112 }
113 }
114
115 @Override
116 public void doHelp(
117 RenderRequest renderRequest, RenderResponse renderResponse)
118 throws IOException, PortletException {
119
120 include(helpTemplate, renderRequest, renderResponse);
121 }
122
123 @Override
124 public void doPreview(
125 RenderRequest renderRequest, RenderResponse renderResponse)
126 throws IOException, PortletException {
127
128 include(previewTemplate, renderRequest, renderResponse);
129 }
130
131 @Override
132 public void doPrint(
133 RenderRequest renderRequest, RenderResponse renderResponse)
134 throws IOException, PortletException {
135
136 include(printTemplate, renderRequest, renderResponse);
137 }
138
139 @Override
140 public void doView(
141 RenderRequest renderRequest, RenderResponse renderResponse)
142 throws IOException, PortletException {
143
144 include(viewTemplate, renderRequest, renderResponse);
145 }
146
147 @Override
148 public void init() throws PortletException {
149 super.init();
150
151 templatePath = _getInitParameter("template-path");
152
153 if (Validator.isNull(templatePath)) {
154 templatePath = StringPool.SLASH;
155 }
156 else if (templatePath.contains(StringPool.BACK_SLASH) ||
157 templatePath.contains(StringPool.DOUBLE_SLASH) ||
158 templatePath.contains(StringPool.PERIOD) ||
159 templatePath.contains(StringPool.SPACE)) {
160
161 throw new PortletException(
162 "template-path " + templatePath + " has invalid characters");
163 }
164 else if (!templatePath.startsWith(StringPool.SLASH) ||
165 !templatePath.endsWith(StringPool.SLASH)) {
166
167 throw new PortletException(
168 "template-path " + templatePath +
169 " must start and end with a /");
170 }
171
172 aboutTemplate = _getInitParameter("about-template");
173 configTemplate = _getInitParameter("config-template");
174 editTemplate = _getInitParameter("edit-template");
175 editDefaultsTemplate = _getInitParameter("edit-defaults-template");
176 editGuestTemplate = _getInitParameter("edit-guest-template");
177 helpTemplate = _getInitParameter("help-template");
178 previewTemplate = _getInitParameter("preview-template");
179 printTemplate = _getInitParameter("print-template");
180 viewTemplate = _getInitParameter("view-template");
181
182 clearRequestParameters = GetterUtil.getBoolean(
183 getInitParameter("clear-request-parameters"));
184 copyRequestParameters = GetterUtil.getBoolean(
185 getInitParameter("copy-request-parameters"));
186
187 String packagePrefix = getInitParameter(
188 ActionCommandCache.ACTION_PACKAGE_NAME);
189
190 if (Validator.isNotNull(packagePrefix)) {
191 _actionCommandCache = new ActionCommandCache(packagePrefix);
192 }
193 }
194
195 public void invokeTaglibDiscussion(
196 ActionRequest actionRequest, ActionResponse actionResponse)
197 throws Exception {
198
199 PortletConfig portletConfig = getPortletConfig();
200
201 PortalUtil.invokeTaglibDiscussion(
202 portletConfig, actionRequest, actionResponse);
203 }
204
205 @Override
206 public void processAction(
207 ActionRequest actionRequest, ActionResponse actionResponse)
208 throws IOException, PortletException {
209
210 super.processAction(actionRequest, actionResponse);
211
212 if (copyRequestParameters) {
213 PortalUtil.copyRequestParameters(actionRequest, actionResponse);
214 }
215 }
216
217 @Override
218 public void serveResource(
219 ResourceRequest resourceRequest, ResourceResponse resourceResponse)
220 throws IOException, PortletException {
221
222 String path = getPath(resourceRequest);
223
224 if (path != null) {
225 include(
226 path, resourceRequest, resourceResponse,
227 PortletRequest.RESOURCE_PHASE);
228 }
229 else {
230 super.serveResource(resourceRequest, resourceResponse);
231 }
232 }
233
234 @Override
235 protected boolean callActionMethod(
236 ActionRequest actionRequest, ActionResponse actionResponse)
237 throws PortletException {
238
239 try {
240 checkPermissions(actionRequest);
241 }
242 catch (Exception e) {
243 throw new PortletException(e);
244 }
245
246 if (_actionCommandCache == null) {
247 return super.callActionMethod(actionRequest, actionResponse);
248 }
249
250 String actionName = ParamUtil.getString(
251 actionRequest, ActionRequest.ACTION_NAME);
252
253 if (!actionName.contains(StringPool.COMMA)) {
254 ActionCommand actionCommand = _actionCommandCache.getActionCommand(
255 actionName);
256
257 if (actionCommand != ActionCommandCache.EMPTY) {
258 return actionCommand.processCommand(
259 actionRequest, actionResponse);
260 }
261 }
262 else {
263 List<ActionCommand> actionCommands =
264 _actionCommandCache.getActionCommandChain(actionName);
265
266 if (actionCommands.isEmpty()) {
267 return false;
268 }
269
270 for (ActionCommand actionCommand : actionCommands) {
271 if (!actionCommand.processCommand(
272 actionRequest, actionResponse)) {
273
274 return false;
275 }
276 }
277
278 return true;
279 }
280
281 return false;
282 }
283
284 protected void checkPath(String path) throws PortletException {
285 if (Validator.isNotNull(path) &&
286 (!path.startsWith(templatePath) ||
287 !PortalUtil.isValidResourceId(path) ||
288 !Validator.isFilePath(path, false))) {
289
290 throw new PortletException(
291 "Path " + path + " is not accessible by this portlet");
292 }
293 }
294
295 protected void checkPermissions(PortletRequest portletRequest)
296 throws Exception {
297 }
298
299 @Override
300 protected void doDispatch(
301 RenderRequest renderRequest, RenderResponse renderResponse)
302 throws IOException, PortletException {
303
304 String path = getPath(renderRequest);
305
306 if (path != null) {
307 if (!isProcessRenderRequest(renderRequest)) {
308 renderRequest.setAttribute(
309 WebKeys.PORTLET_DECORATE, Boolean.FALSE);
310
311 return;
312 }
313
314 WindowState windowState = renderRequest.getWindowState();
315
316 if (windowState.equals(WindowState.MINIMIZED)) {
317 return;
318 }
319
320 include(path, renderRequest, renderResponse);
321 }
322 else {
323 super.doDispatch(renderRequest, renderResponse);
324 }
325 }
326
327 protected String getPath(PortletRequest portletRequest) {
328 String mvcPath = portletRequest.getParameter("mvcPath");
329
330
331
332 if (mvcPath == null) {
333 mvcPath = portletRequest.getParameter("jspPage");
334 }
335
336 return mvcPath;
337 }
338
339 protected void include(
340 String path, ActionRequest actionRequest,
341 ActionResponse actionResponse)
342 throws IOException, PortletException {
343
344 include(
345 path, actionRequest, actionResponse, PortletRequest.ACTION_PHASE);
346 }
347
348 protected void include(
349 String path, EventRequest eventRequest, EventResponse eventResponse)
350 throws IOException, PortletException {
351
352 include(path, eventRequest, eventResponse, PortletRequest.EVENT_PHASE);
353 }
354
355 protected void include(
356 String path, PortletRequest portletRequest,
357 PortletResponse portletResponse, String lifecycle)
358 throws IOException, PortletException {
359
360 PortletContext portletContext = getPortletContext();
361
362 PortletRequestDispatcher portletRequestDispatcher =
363 portletContext.getRequestDispatcher(path);
364
365 if (portletRequestDispatcher == null) {
366 _log.error(path + " is not a valid include");
367 }
368 else {
369 checkPath(path);
370
371 portletRequestDispatcher.include(portletRequest, portletResponse);
372 }
373
374 if (clearRequestParameters) {
375 if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
376 portletResponse.setProperty(
377 "clear-request-parameters", Boolean.TRUE.toString());
378 }
379 }
380 }
381
382 protected void include(
383 String path, RenderRequest renderRequest,
384 RenderResponse renderResponse)
385 throws IOException, PortletException {
386
387 include(
388 path, renderRequest, renderResponse, PortletRequest.RENDER_PHASE);
389 }
390
391 protected void include(
392 String path, ResourceRequest resourceRequest,
393 ResourceResponse resourceResponse)
394 throws IOException, PortletException {
395
396 include(
397 path, resourceRequest, resourceResponse,
398 PortletRequest.RESOURCE_PHASE);
399 }
400
401 protected String aboutTemplate;
402 protected boolean clearRequestParameters;
403 protected String configTemplate;
404 protected boolean copyRequestParameters;
405 protected String editDefaultsTemplate;
406 protected String editGuestTemplate;
407 protected String editTemplate;
408 protected String helpTemplate;
409 protected String previewTemplate;
410 protected String printTemplate;
411 protected String templatePath;
412 protected String viewTemplate;
413
414 private String _getInitParameter(String name) {
415 String value = getInitParameter(name);
416
417 if (value != null) {
418 return value;
419 }
420
421
422
423 if (name.equals("template-path")) {
424 return getInitParameter("jsp-path");
425 }
426 else if (name.endsWith("-template")) {
427 name = name.substring(0, name.length() - 9) + "-jsp";
428
429 return getInitParameter(name);
430 }
431
432 return null;
433 }
434
435 private static Log _log = LogFactoryUtil.getLog(MVCPortlet.class);
436
437 private ActionCommandCache _actionCommandCache;
438
439 }