1
22
23 package com.liferay.util.portlet;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.portlet.LiferayWindowState;
28 import com.liferay.portal.kernel.util.HttpUtil;
29 import com.liferay.portal.kernel.util.StringPool;
30 import com.liferay.portal.kernel.util.Validator;
31 import com.liferay.portal.kernel.util.WebKeys;
32 import com.liferay.portal.kernel.xml.Document;
33 import com.liferay.portal.kernel.xml.Element;
34 import com.liferay.portal.kernel.xml.SAXReaderUtil;
35 import com.liferay.portal.theme.PortletDisplay;
36 import com.liferay.portal.theme.ThemeDisplay;
37 import com.liferay.util.xml.DocUtil;
38
39 import java.io.ByteArrayOutputStream;
40 import java.io.IOException;
41 import java.io.InputStream;
42
43 import java.util.Collection;
44 import java.util.Enumeration;
45 import java.util.List;
46 import java.util.Map;
47
48 import javax.portlet.ActionRequest;
49 import javax.portlet.PortletRequest;
50 import javax.portlet.PortletResponse;
51 import javax.portlet.PortletSession;
52 import javax.portlet.PortletURL;
53 import javax.portlet.RenderRequest;
54 import javax.portlet.RenderResponse;
55 import javax.portlet.WindowStateException;
56
57 import org.apache.commons.fileupload.disk.DiskFileItem;
58 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
59 import org.apache.commons.fileupload.portlet.PortletFileUpload;
60
61
68 public class PortletRequestUtil {
69
70 public static List<DiskFileItem> testMultipartWithCommonsFileUpload(
71 ActionRequest actionRequest)
72 throws Exception {
73
74
76 boolean multiPartContent = PortletFileUpload.isMultipartContent(
77 actionRequest);
78
79 if (multiPartContent) {
80 _log.info("The given request is a multipart request");
81 }
82 else {
83 _log.info("The given request is NOT a multipart request");
84 }
85
86
88 DiskFileItemFactory factory = new DiskFileItemFactory();
89
90 PortletFileUpload upload = new PortletFileUpload(factory);
91
92 List<DiskFileItem> fileItems = upload.parseRequest(actionRequest);
93
94 if (_log.isInfoEnabled()) {
95 _log.info(
96 "Apache commons upload was able to parse " + fileItems.size() +
97 " items");
98 }
99
100 for (int i = 0; i < fileItems.size(); i++) {
101 DiskFileItem fileItem = fileItems.get(i);
102
103 if (_log.isInfoEnabled()) {
104 _log.info("Item " + i + " " + fileItem);
105 }
106 }
107
108 return fileItems;
109 }
110
111 public static int testMultipartWithPortletInputStream(
112 ActionRequest actionRequest)
113 throws Exception {
114
115
117 InputStream is = actionRequest.getPortletInputStream();
118
119 if (is != null) {
120 ByteArrayOutputStream baos = new ByteArrayOutputStream();
121
122 int c = -1;
123
124 try {
125 while ((c = is.read()) != -1) {
126 baos.write(c);
127 }
128 }
129 finally {
130 is.close();
131 }
132
133 byte[] bytes = baos.toByteArray();
134
135 if (_log.isInfoEnabled()) {
136 _log.info(
137 "Byte array size from the raw input stream is " +
138 bytes.length);
139 }
140
141 return bytes.length;
142 }
143
144 return -1;
145 }
146
147 public static String toXML(
148 PortletRequest portletRequest, PortletResponse portletResponse) {
149
150 String xml = null;
151
152 Document doc = SAXReaderUtil.createDocument();
153
154 Element reqEl = doc.addElement("request");
155
156 DocUtil.add(reqEl, "container-type", "portlet");
157 DocUtil.add(
158 reqEl, "container-namespace", portletRequest.getContextPath());
159 DocUtil.add(
160 reqEl, "content-type", portletRequest.getResponseContentType());
161 DocUtil.add(reqEl, "server-name", portletRequest.getServerName());
162 DocUtil.add(reqEl, "server-port", portletRequest.getServerPort());
163 DocUtil.add(reqEl, "secure", portletRequest.isSecure());
164 DocUtil.add(reqEl, "auth-type", portletRequest.getAuthType());
165 DocUtil.add(reqEl, "remote-user", portletRequest.getRemoteUser());
166 DocUtil.add(reqEl, "context-path", portletRequest.getContextPath());
167 DocUtil.add(reqEl, "locale", portletRequest.getLocale());
168 DocUtil.add(reqEl, "portlet-mode", portletRequest.getPortletMode());
169 DocUtil.add(
170 reqEl, "portlet-session-id",
171 portletRequest.getRequestedSessionId());
172 DocUtil.add(reqEl, "scheme", portletRequest.getScheme());
173 DocUtil.add(reqEl, "window-state", portletRequest.getWindowState());
174
175 if (portletRequest instanceof RenderRequest) {
176 DocUtil.add(reqEl, "action", Boolean.FALSE);
177 }
178 else if (portletRequest instanceof ActionRequest) {
179 DocUtil.add(reqEl, "action", Boolean.TRUE);
180 }
181
182 if (portletResponse instanceof RenderResponse) {
183 _renderResponseToXML((RenderResponse)portletResponse, reqEl);
184 }
185
186 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
187 WebKeys.THEME_DISPLAY);
188
189 if (themeDisplay != null) {
190 Element themeDisplayEl = reqEl.addElement("theme-display");
191
192 _themeDisplayToXML(themeDisplay, themeDisplayEl);
193 }
194
195 Element parametersEl = reqEl.addElement("parameters");
196
197 Enumeration<String> enu = portletRequest.getParameterNames();
198
199 while (enu.hasMoreElements()) {
200 String name = enu.nextElement();
201
202 Element parameterEl = parametersEl.addElement("parameter");
203
204 DocUtil.add(parameterEl, "name", name);
205
206 String[] values = portletRequest.getParameterValues(name);
207
208 for (int i = 0; i < values.length; i++) {
209 DocUtil.add(parameterEl, "value", values[i]);
210 }
211 }
212
213 Element attributesEl = reqEl.addElement("attributes");
214
215 enu = portletRequest.getAttributeNames();
216
217 while (enu.hasMoreElements()) {
218 String name = enu.nextElement();
219
220 if (!_isValidAttributeName(name)) {
221 continue;
222 }
223
224 Object value = portletRequest.getAttribute(name);
225
226 if (!_isValidAttributeValue(value)) {
227 continue;
228 }
229
230 Element attributeEl = attributesEl.addElement("attribute");
231
232 DocUtil.add(attributeEl, "name", name);
233 DocUtil.add(attributeEl, "value", String.valueOf(value));
234 }
235
236 Element portletSessionEl = reqEl.addElement("portlet-session");
237
238 attributesEl = portletSessionEl.addElement("portlet-attributes");
239
240 PortletSession portletSession = portletRequest.getPortletSession();
241
242 try {
243 enu = portletSession.getAttributeNames(
244 PortletSession.PORTLET_SCOPE);
245
246 while (enu.hasMoreElements()) {
247 String name = enu.nextElement();
248
249 if (!_isValidAttributeName(name)) {
250 continue;
251 }
252
253 Object value = portletSession.getAttribute(
254 name, PortletSession.PORTLET_SCOPE);
255
256 if (!_isValidAttributeValue(value)) {
257 continue;
258 }
259
260 Element attributeEl = attributesEl.addElement("attribute");
261
262 DocUtil.add(attributeEl, "name", name);
263 DocUtil.add(attributeEl, "value", String.valueOf(value));
264 }
265
266 attributesEl = portletSessionEl.addElement(
267 "application-attributes");
268
269 enu = portletSession.getAttributeNames(
270 PortletSession.APPLICATION_SCOPE);
271
272 while (enu.hasMoreElements()) {
273 String name = enu.nextElement();
274
275 if (!_isValidAttributeName(name)) {
276 continue;
277 }
278
279 Object value = portletSession.getAttribute(
280 name, PortletSession.APPLICATION_SCOPE);
281
282 if (!_isValidAttributeValue(value)) {
283 continue;
284 }
285
286 Element attributeEl = attributesEl.addElement("attribute");
287
288 DocUtil.add(attributeEl, "name", name);
289 DocUtil.add(attributeEl, "value", String.valueOf(value));
290 }
291 }
292 catch (IllegalStateException ise) {
293 if (_log.isWarnEnabled()) {
294 _log.warn(ise.getMessage());
295 }
296 }
297
298 try {
299 xml = doc.formattedString();
300 }
301 catch (IOException ioe) {
302 }
303
304 return xml;
305 }
306
307 private static void _renderResponseToXML(
308 RenderResponse renderResponse, Element reqEl) {
309
310 DocUtil.add(reqEl, "portlet-namespace", renderResponse.getNamespace());
311
312 PortletURL url = null;
313
314 try {
315 url = renderResponse.createRenderURL();
316 }
317 catch (Exception e) {
318
319
321 return;
322 }
323
324 DocUtil.add(reqEl, "render-url", url);
325
326 try {
327 url.setWindowState(LiferayWindowState.EXCLUSIVE);
328
329 DocUtil.add(reqEl, "render-url-exclusive", url);
330 }
331 catch (WindowStateException wse) {
332 }
333
334 try {
335 url.setWindowState(LiferayWindowState.MAXIMIZED);
336
337 DocUtil.add(reqEl, "render-url-maximized", url);
338 }
339 catch (WindowStateException wse) {
340 }
341
342 try {
343 url.setWindowState(LiferayWindowState.MINIMIZED);
344
345 DocUtil.add(reqEl, "render-url-minimized", url);
346 }
347 catch (WindowStateException wse) {
348 }
349
350 try {
351 url.setWindowState(LiferayWindowState.NORMAL);
352
353 DocUtil.add(reqEl, "render-url-normal", url);
354 }
355 catch (WindowStateException wse) {
356 }
357
358 try {
359 url.setWindowState(LiferayWindowState.POP_UP);
360
361 DocUtil.add(reqEl, "render-url-pop-up", url);
362 }
363 catch (WindowStateException wse) {
364 }
365 }
366
367 private static void _themeDisplayToXML(
368 ThemeDisplay themeDisplay, Element themeDisplayEl) {
369
370 DocUtil.add(themeDisplayEl, "cdn-host", themeDisplay.getCDNHost());
371 DocUtil.add(themeDisplayEl, "company-id", themeDisplay.getCompanyId());
372 DocUtil.add(
373 themeDisplayEl, "do-as-user-id", themeDisplay.getDoAsUserId());
374 DocUtil.add(
375 themeDisplayEl, "i18n-language-id",
376 themeDisplay.getI18nLanguageId());
377 DocUtil.add(themeDisplayEl, "i18n-path", themeDisplay.getI18nPath());
378 DocUtil.add(
379 themeDisplayEl, "language-id", themeDisplay.getLanguageId());
380 DocUtil.add(themeDisplayEl, "locale", themeDisplay.getLocale());
381 DocUtil.add(
382 themeDisplayEl, "path-context", themeDisplay.getPathContext());
383 DocUtil.add(
384 themeDisplayEl, "path-friendly-url-private-group",
385 themeDisplay.getPathFriendlyURLPrivateGroup());
386 DocUtil.add(
387 themeDisplayEl, "path-friendly-url-private-user",
388 themeDisplay.getPathFriendlyURLPrivateUser());
389 DocUtil.add(
390 themeDisplayEl, "path-friendly-url-public",
391 themeDisplay.getPathFriendlyURLPublic());
392 DocUtil.add(themeDisplayEl, "path-image", themeDisplay.getPathImage());
393 DocUtil.add(themeDisplayEl, "path-main", themeDisplay.getPathMain());
394 DocUtil.add(
395 themeDisplayEl, "path-theme-images",
396 themeDisplay.getPathThemeImages());
397 DocUtil.add(themeDisplayEl, "plid", themeDisplay.getPlid());
398 DocUtil.add(
399 themeDisplayEl, "portal-url",
400 HttpUtil.removeProtocol(themeDisplay.getPortalURL()));
401 DocUtil.add(
402 themeDisplayEl, "real-user-id", themeDisplay.getRealUserId());
403 DocUtil.add(
404 themeDisplayEl, "scope-group-id", themeDisplay.getScopeGroupId());
405 DocUtil.add(themeDisplayEl, "secure", themeDisplay.isSecure());
406 DocUtil.add(
407 themeDisplayEl, "server-name", themeDisplay.getServerName());
408 DocUtil.add(
409 themeDisplayEl, "server-port", themeDisplay.getServerPort());
410 DocUtil.add(
411 themeDisplayEl, "time-zone", themeDisplay.getTimeZone().getID());
412 DocUtil.add(
413 themeDisplayEl, "url-portal",
414 HttpUtil.removeProtocol(themeDisplay.getURLPortal()));
415 DocUtil.add(themeDisplayEl, "user-id", themeDisplay.getUserId());
416
417 if (themeDisplay.getPortletDisplay() != null) {
418 Element portletDisplayEl = themeDisplayEl.addElement(
419 "portlet-display");
420
421 _portletDisplayToXML(
422 themeDisplay.getPortletDisplay(), portletDisplayEl);
423 }
424 }
425
426 private static boolean _isValidAttributeName(String name) {
427 if (name.equalsIgnoreCase("j_password") ||
428 name.equalsIgnoreCase("LAYOUT_CONTENT") ||
429 name.equalsIgnoreCase("LAYOUTS") ||
430 name.equalsIgnoreCase("PORTLET_RENDER_PARAMETERS") ||
431 name.equalsIgnoreCase("USER_PASSWORD") ||
432 name.startsWith("javax.") ||
433 name.startsWith("liferay-ui:")) {
434
435 return false;
436 }
437 else {
438 return true;
439 }
440 }
441
442 private static boolean _isValidAttributeValue(Object obj) {
443 if (obj == null) {
444 return false;
445 }
446 else if (obj instanceof Collection) {
447 Collection<?> col = (Collection<?>)obj;
448
449 if (col.size() == 0) {
450 return false;
451 }
452 else {
453 return true;
454 }
455 }
456 else if (obj instanceof Map) {
457 Map<?, ?> map = (Map<?, ?>)obj;
458
459 if (map.size() == 0) {
460 return false;
461 }
462 else {
463 return true;
464 }
465 }
466 else {
467 String objString = String.valueOf(obj);
468
469 if (Validator.isNull(objString)) {
470 return false;
471 }
472
473 String hashCode =
474 StringPool.AT + Integer.toHexString(obj.hashCode());
475
476 if (objString.endsWith(hashCode)) {
477 return false;
478 }
479
480 return true;
481 }
482 }
483
484 private static void _portletDisplayToXML(
485 PortletDisplay portletDisplay, Element portletDisplayEl) {
486
487 DocUtil.add(portletDisplayEl, "id", portletDisplay.getId());
488 DocUtil.add(
489 portletDisplayEl, "instance-id", portletDisplay.getInstanceId());
490 DocUtil.add(
491 portletDisplayEl, "portlet-name", portletDisplay.getPortletName());
492 DocUtil.add(
493 portletDisplayEl, "resource-pk", portletDisplay.getResourcePK());
494 DocUtil.add(
495 portletDisplayEl, "root-portlet-id",
496 portletDisplay.getRootPortletId());
497 DocUtil.add(
498 portletDisplayEl, "title", portletDisplay.getTitle());
499 }
500
501 private static Log _log = LogFactoryUtil.getLog(PortletRequestUtil.class);
502
503 }