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 }
291
292 try {
293 xml = doc.formattedString();
294 }
295 catch (IOException ioe) {
296 }
297
298 return xml;
299 }
300
301 private static void _renderResponseToXML(
302 RenderResponse renderResponse, Element reqEl) {
303
304 DocUtil.add(reqEl, "portlet-namespace", renderResponse.getNamespace());
305
306 PortletURL url = null;
307
308 try {
309 url = renderResponse.createRenderURL();
310 }
311 catch (Exception e) {
312
313
315 return;
316 }
317
318 DocUtil.add(reqEl, "render-url", url);
319
320 try {
321 url.setWindowState(LiferayWindowState.EXCLUSIVE);
322
323 DocUtil.add(reqEl, "render-url-exclusive", url);
324 }
325 catch (WindowStateException wse) {
326 }
327
328 try {
329 url.setWindowState(LiferayWindowState.MAXIMIZED);
330
331 DocUtil.add(reqEl, "render-url-maximized", url);
332 }
333 catch (WindowStateException wse) {
334 }
335
336 try {
337 url.setWindowState(LiferayWindowState.MINIMIZED);
338
339 DocUtil.add(reqEl, "render-url-minimized", url);
340 }
341 catch (WindowStateException wse) {
342 }
343
344 try {
345 url.setWindowState(LiferayWindowState.NORMAL);
346
347 DocUtil.add(reqEl, "render-url-normal", url);
348 }
349 catch (WindowStateException wse) {
350 }
351
352 try {
353 url.setWindowState(LiferayWindowState.POP_UP);
354
355 DocUtil.add(reqEl, "render-url-pop-up", url);
356 }
357 catch (WindowStateException wse) {
358 }
359 }
360
361 private static void _themeDisplayToXML(
362 ThemeDisplay themeDisplay, Element themeDisplayEl) {
363
364 DocUtil.add(themeDisplayEl, "cdn-host", themeDisplay.getCDNHost());
365 DocUtil.add(themeDisplayEl, "company-id", themeDisplay.getCompanyId());
366 DocUtil.add(
367 themeDisplayEl, "do-as-user-id", themeDisplay.getDoAsUserId());
368 DocUtil.add(
369 themeDisplayEl, "i18n-language-id",
370 themeDisplay.getI18nLanguageId());
371 DocUtil.add(
372 themeDisplayEl, "language-id", themeDisplay.getLanguageId());
373 DocUtil.add(themeDisplayEl, "locale", themeDisplay.getLocale());
374 DocUtil.add(
375 themeDisplayEl, "path-context", themeDisplay.getPathContext());
376 DocUtil.add(
377 themeDisplayEl, "path-friendly-url-private-group",
378 themeDisplay.getPathFriendlyURLPrivateGroup());
379 DocUtil.add(
380 themeDisplayEl, "path-friendly-url-private-user",
381 themeDisplay.getPathFriendlyURLPrivateUser());
382 DocUtil.add(
383 themeDisplayEl, "path-friendly-url-public",
384 themeDisplay.getPathFriendlyURLPublic());
385 DocUtil.add(themeDisplayEl, "path-image", themeDisplay.getPathImage());
386 DocUtil.add(themeDisplayEl, "path-main", themeDisplay.getPathMain());
387 DocUtil.add(
388 themeDisplayEl, "path-theme-images",
389 themeDisplay.getPathThemeImages());
390 DocUtil.add(themeDisplayEl, "plid", themeDisplay.getPlid());
391 DocUtil.add(
392 themeDisplayEl, "portal-url",
393 HttpUtil.removeProtocol(themeDisplay.getPortalURL()));
394 DocUtil.add(
395 themeDisplayEl, "real-user-id", themeDisplay.getRealUserId());
396 DocUtil.add(
397 themeDisplayEl, "scope-group-id", themeDisplay.getScopeGroupId());
398 DocUtil.add(themeDisplayEl, "secure", themeDisplay.isSecure());
399 DocUtil.add(
400 themeDisplayEl, "server-name", themeDisplay.getServerName());
401 DocUtil.add(
402 themeDisplayEl, "server-port", themeDisplay.getServerPort());
403 DocUtil.add(
404 themeDisplayEl, "time-zone", themeDisplay.getTimeZone().getID());
405 DocUtil.add(
406 themeDisplayEl, "url-portal",
407 HttpUtil.removeProtocol(themeDisplay.getURLPortal()));
408 DocUtil.add(themeDisplayEl, "user-id", themeDisplay.getUserId());
409
410 if (themeDisplay.getPortletDisplay() != null) {
411 Element portletDisplayEl = themeDisplayEl.addElement(
412 "portlet-display");
413
414 _portletDisplayToXML(
415 themeDisplay.getPortletDisplay(), portletDisplayEl);
416 }
417 }
418
419 private static boolean _isValidAttributeName(String name) {
420 if (name.equalsIgnoreCase("j_password") ||
421 name.equalsIgnoreCase("LAYOUT_CONTENT") ||
422 name.equalsIgnoreCase("LAYOUTS") ||
423 name.equalsIgnoreCase("PORTLET_RENDER_PARAMETERS") ||
424 name.equalsIgnoreCase("USER_PASSWORD") ||
425 name.startsWith("javax.") ||
426 name.startsWith("liferay-ui:")) {
427
428 return false;
429 }
430 else {
431 return true;
432 }
433 }
434
435 private static boolean _isValidAttributeValue(Object obj) {
436 if (obj == null) {
437 return false;
438 }
439 else if (obj instanceof Collection) {
440 Collection<?> col = (Collection<?>)obj;
441
442 if (col.size() == 0) {
443 return false;
444 }
445 else {
446 return true;
447 }
448 }
449 else if (obj instanceof Map) {
450 Map<?, ?> map = (Map<?, ?>)obj;
451
452 if (map.size() == 0) {
453 return false;
454 }
455 else {
456 return true;
457 }
458 }
459 else {
460 String objString = String.valueOf(obj);
461
462 if (Validator.isNull(objString)) {
463 return false;
464 }
465
466 String hashCode =
467 StringPool.AT + Integer.toHexString(obj.hashCode());
468
469 if (objString.endsWith(hashCode)) {
470 return false;
471 }
472
473 return true;
474 }
475 }
476
477 private static void _portletDisplayToXML(
478 PortletDisplay portletDisplay, Element portletDisplayEl) {
479
480 DocUtil.add(portletDisplayEl, "id", portletDisplay.getId());
481 DocUtil.add(
482 portletDisplayEl, "instance-id", portletDisplay.getInstanceId());
483 DocUtil.add(
484 portletDisplayEl, "portlet-name", portletDisplay.getPortletName());
485 DocUtil.add(
486 portletDisplayEl, "resource-pk", portletDisplay.getResourcePK());
487 DocUtil.add(
488 portletDisplayEl, "root-portlet-id",
489 portletDisplay.getRootPortletId());
490 DocUtil.add(
491 portletDisplayEl, "title", portletDisplay.getTitle());
492 }
493
494 private static Log _log = LogFactoryUtil.getLog(PortletRequestUtil.class);
495
496 }