1
14
15 package com.liferay.util.bridges.mvc;
16
17 import com.liferay.portal.kernel.log.Log;
18 import com.liferay.portal.kernel.log.LogFactoryUtil;
19 import com.liferay.portal.kernel.portlet.LiferayPortlet;
20 import com.liferay.portal.kernel.util.GetterUtil;
21 import com.liferay.portal.kernel.util.ParamUtil;
22 import com.liferay.portal.kernel.util.StringPool;
23 import com.liferay.portal.kernel.util.Validator;
24 import com.liferay.portal.util.PortalUtil;
25
26 import java.io.IOException;
27
28 import java.util.List;
29
30 import javax.portlet.ActionRequest;
31 import javax.portlet.ActionResponse;
32 import javax.portlet.PortletException;
33 import javax.portlet.PortletRequest;
34 import javax.portlet.PortletRequestDispatcher;
35 import javax.portlet.PortletResponse;
36 import javax.portlet.RenderRequest;
37 import javax.portlet.RenderResponse;
38 import javax.portlet.ResourceRequest;
39 import javax.portlet.ResourceResponse;
40
41
46 public class MVCPortlet extends LiferayPortlet {
47
48 public void init() {
49 aboutJSP = getInitParameter("about-jsp");
50 configJSP = getInitParameter("config-jsp");
51 editJSP = getInitParameter("edit-jsp");
52 editDefaultsJSP = getInitParameter("edit-defaults-jsp");
53 editGuestJSP = getInitParameter("edit-guest-jsp");
54 helpJSP = getInitParameter("help-jsp");
55 previewJSP = getInitParameter("preview-jsp");
56 printJSP = getInitParameter("print-jsp");
57 viewJSP = getInitParameter("view-jsp");
58
59 clearRequestParameters = GetterUtil.getBoolean(
60 getInitParameter("clear-request-parameters"));
61 copyRequestParameters = GetterUtil.getBoolean(
62 getInitParameter("copy-request-parameters"));
63
64 String packagePrefix = getInitParameter(
65 ActionCommandCache.ACTION_PACKAGE_NAME);
66
67 if (Validator.isNotNull(packagePrefix)) {
68 _actionCommandCache = new ActionCommandCache(packagePrefix);
69 }
70 }
71
72 public void doAbout(
73 RenderRequest renderRequest, RenderResponse renderResponse)
74 throws IOException, PortletException {
75
76 include(aboutJSP, renderRequest, renderResponse);
77 }
78
79 public void doConfig(
80 RenderRequest renderRequest, RenderResponse renderResponse)
81 throws IOException, PortletException {
82
83 include(configJSP, renderRequest, renderResponse);
84 }
85
86 public void doEdit(
87 RenderRequest renderRequest, RenderResponse renderResponse)
88 throws IOException, PortletException {
89
90 if (renderRequest.getPreferences() == null) {
91 super.doEdit(renderRequest, renderResponse);
92 }
93 else {
94 include(editJSP, renderRequest, renderResponse);
95 }
96 }
97
98 public void doEditDefaults(
99 RenderRequest renderRequest, RenderResponse renderResponse)
100 throws IOException, PortletException {
101
102 if (renderRequest.getPreferences() == null) {
103 super.doEdit(renderRequest, renderResponse);
104 }
105 else {
106 include(editDefaultsJSP, renderRequest, renderResponse);
107 }
108 }
109
110 public void doEditGuest(
111 RenderRequest renderRequest, RenderResponse renderResponse)
112 throws IOException, PortletException {
113
114 if (renderRequest.getPreferences() == null) {
115 super.doEdit(renderRequest, renderResponse);
116 }
117 else {
118 include(editGuestJSP, renderRequest, renderResponse);
119 }
120 }
121
122 public void doHelp(
123 RenderRequest renderRequest, RenderResponse renderResponse)
124 throws IOException, PortletException {
125
126 include(helpJSP, renderRequest, renderResponse);
127 }
128
129 public void doPreview(
130 RenderRequest renderRequest, RenderResponse renderResponse)
131 throws IOException, PortletException {
132
133 include(previewJSP, renderRequest, renderResponse);
134 }
135
136 public void doPrint(
137 RenderRequest renderRequest, RenderResponse renderResponse)
138 throws IOException, PortletException {
139
140 include(printJSP, renderRequest, renderResponse);
141 }
142
143 public void doView(
144 RenderRequest renderRequest, RenderResponse renderResponse)
145 throws IOException, PortletException {
146
147 include(viewJSP, renderRequest, renderResponse);
148 }
149
150 public void processAction(
151 ActionRequest actionRequest, ActionResponse actionResponse)
152 throws IOException, PortletException {
153
154 super.processAction(actionRequest, actionResponse);
155
156 if (copyRequestParameters) {
157 PortalUtil.copyRequestParameters(actionRequest, actionResponse);
158 }
159 }
160
161 public void serveResource(
162 ResourceRequest resourceRequest, ResourceResponse resourceResponse)
163 throws IOException, PortletException {
164
165 String jspPage = resourceRequest.getParameter("jspPage");
166
167 if (jspPage != null) {
168 include(
169 jspPage, resourceRequest, resourceResponse,
170 PortletRequest.RESOURCE_PHASE);
171 }
172 else {
173 super.serveResource(resourceRequest, resourceResponse);
174 }
175 }
176
177 protected boolean callActionMethod(
178 ActionRequest request, ActionResponse response)
179 throws PortletException {
180
181 if ((_actionCommandCache == null) || _actionCommandCache.isEmpty()) {
182 return super.callActionMethod(request, response);
183 }
184
185 String actionName = ParamUtil.getString(
186 request, ActionRequest.ACTION_NAME);
187
188 if (!actionName.contains(StringPool.COMMA)) {
189 ActionCommand actionCommand = _actionCommandCache.getActionCommand(
190 actionName);
191
192 if (actionCommand != ActionCommandCache.EMPTY) {
193 return actionCommand.processCommand(request, response);
194 }
195 }
196 else {
197 List<ActionCommand> actionCommands =
198 _actionCommandCache.getActionCommandChain(actionName);
199
200 if (actionCommands.isEmpty()) {
201 return false;
202 }
203
204 for (ActionCommand actionCommand : actionCommands) {
205 if (!actionCommand.processCommand(request, response)) {
206 return false;
207 }
208 }
209
210 return true;
211 }
212
213 return false;
214 }
215
216 protected void doDispatch(
217 RenderRequest renderRequest, RenderResponse renderResponse)
218 throws IOException, PortletException {
219
220 String jspPage = renderRequest.getParameter("jspPage");
221
222 if (jspPage != null) {
223 include(jspPage, renderRequest, renderResponse);
224 }
225 else {
226 super.doDispatch(renderRequest, renderResponse);
227 }
228 }
229
230 protected void include(
231 String path, PortletRequest portletRequest,
232 PortletResponse portletResponse)
233 throws IOException, PortletException {
234
235 include(
236 path, portletRequest, portletResponse, PortletRequest.RENDER_PHASE);
237 }
238
239 protected void include(
240 String path, PortletRequest portletRequest,
241 PortletResponse portletResponse, String lifecycle)
242 throws IOException, PortletException {
243
244 PortletRequestDispatcher portletRequestDispatcher =
245 getPortletContext().getRequestDispatcher(path);
246
247 if (portletRequestDispatcher == null) {
248 _log.error(path + " is not a valid include");
249 }
250 else {
251 portletRequestDispatcher.include(portletRequest, portletResponse);
252 }
253
254 if (clearRequestParameters) {
255 if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
256 portletResponse.setProperty("clear-request-parameters", "true");
257 }
258 }
259 }
260
261 protected ActionCommandCache _actionCommandCache;
262 protected String aboutJSP;
263 protected String configJSP;
264 protected String editJSP;
265 protected String editDefaultsJSP;
266 protected String editGuestJSP;
267 protected String helpJSP;
268 protected String previewJSP;
269 protected String printJSP;
270 protected String viewJSP;
271 protected boolean clearRequestParameters;
272 protected boolean copyRequestParameters;
273
274 private static Log _log = LogFactoryUtil.getLog(MVCPortlet.class);
275
276 }