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