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