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