001
014
015 package com.liferay.util.portlet;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.portlet.LiferayWindowState;
021 import com.liferay.portal.kernel.util.HttpUtil;
022 import com.liferay.portal.kernel.util.StreamUtil;
023 import com.liferay.portal.kernel.util.StringPool;
024 import com.liferay.portal.kernel.util.StringUtil;
025 import com.liferay.portal.kernel.util.Validator;
026 import com.liferay.portal.kernel.util.WebKeys;
027 import com.liferay.portal.kernel.xml.simple.Element;
028 import com.liferay.portal.theme.PortletDisplay;
029 import com.liferay.portal.theme.ThemeDisplay;
030
031 import java.io.InputStream;
032
033 import java.util.Collection;
034 import java.util.Enumeration;
035 import java.util.List;
036 import java.util.Map;
037
038 import javax.portlet.ActionRequest;
039 import javax.portlet.MimeResponse;
040 import javax.portlet.PortletRequest;
041 import javax.portlet.PortletResponse;
042 import javax.portlet.PortletSession;
043 import javax.portlet.PortletURL;
044 import javax.portlet.RenderRequest;
045 import javax.portlet.ResourceRequest;
046 import javax.portlet.ResourceURL;
047 import javax.portlet.WindowStateException;
048
049 import org.apache.commons.fileupload.disk.DiskFileItem;
050 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
051 import org.apache.commons.fileupload.portlet.PortletFileUpload;
052
053
057 public class PortletRequestUtil {
058
059 public static List<DiskFileItem> testMultipartWithCommonsFileUpload(
060 ActionRequest actionRequest)
061 throws Exception {
062
063
064
065 boolean multiPartContent = PortletFileUpload.isMultipartContent(
066 actionRequest);
067
068 if (multiPartContent) {
069 _log.info("The given request is a multipart request");
070 }
071 else {
072 _log.info("The given request is NOT a multipart request");
073 }
074
075 DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
076
077 PortletFileUpload portletFileUpload = new PortletFileUpload(
078 diskFileItemFactory);
079
080 List<DiskFileItem> diskFileItems = portletFileUpload.parseRequest(
081 actionRequest);
082
083 if (_log.isInfoEnabled()) {
084 _log.info(
085 "Apache commons upload was able to parse " +
086 diskFileItems.size() + " items");
087 }
088
089 for (int i = 0; i < diskFileItems.size(); i++) {
090 DiskFileItem diskFileItem = diskFileItems.get(i);
091
092 if (_log.isInfoEnabled()) {
093 _log.info("Item " + i + " " + diskFileItem);
094 }
095 }
096
097 return diskFileItems;
098 }
099
100 public static int testMultipartWithPortletInputStream(
101 ActionRequest actionRequest)
102 throws Exception {
103
104 InputStream inputStream = actionRequest.getPortletInputStream();
105
106 if (inputStream != null) {
107 UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
108 new UnsyncByteArrayOutputStream();
109
110 StreamUtil.transfer(inputStream, unsyncByteArrayOutputStream);
111
112 int size = unsyncByteArrayOutputStream.size();
113
114 if (_log.isInfoEnabled()) {
115 _log.info(
116 "Byte array size from the raw input stream is " + size);
117 }
118
119 return size;
120 }
121
122 return -1;
123 }
124
125 public static String toXML(
126 PortletRequest portletRequest, PortletResponse portletResponse) {
127
128 Element requestElement = new Element("request");
129
130 requestElement.addElement("container-type", "portlet");
131 requestElement.addElement("container-type", "portlet");
132 requestElement.addElement(
133 "container-namespace", portletRequest.getContextPath());
134 requestElement.addElement(
135 "content-type", portletRequest.getResponseContentType());
136 requestElement.addElement(
137 "server-name", portletRequest.getServerName());
138 requestElement.addElement(
139 "server-port", portletRequest.getServerPort());
140 requestElement.addElement("secure", portletRequest.isSecure());
141 requestElement.addElement("auth-type", portletRequest.getAuthType());
142 requestElement.addElement(
143 "remote-user", portletRequest.getRemoteUser());
144 requestElement.addElement(
145 "context-path", portletRequest.getContextPath());
146 requestElement.addElement("locale", portletRequest.getLocale());
147 requestElement.addElement(
148 "portlet-mode", portletRequest.getPortletMode());
149 requestElement.addElement(
150 "portlet-session-id", portletRequest.getRequestedSessionId());
151 requestElement.addElement("scheme", portletRequest.getScheme());
152 requestElement.addElement(
153 "window-state", portletRequest.getWindowState());
154
155 if (portletRequest instanceof ActionRequest) {
156 requestElement.addElement("lifecycle", RenderRequest.ACTION_PHASE);
157 }
158 else if (portletRequest instanceof RenderRequest) {
159 requestElement.addElement("lifecycle", RenderRequest.RENDER_PHASE);
160 }
161 else if (portletRequest instanceof ResourceRequest) {
162 requestElement.addElement(
163 "lifecycle", RenderRequest.RESOURCE_PHASE);
164 }
165
166 if (portletResponse instanceof MimeResponse) {
167 _mimeResponseToXML((MimeResponse)portletResponse, requestElement);
168 }
169
170 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
171 WebKeys.THEME_DISPLAY);
172
173 if (themeDisplay != null) {
174 Element themeDisplayElement = requestElement.addElement(
175 "theme-display");
176
177 _themeDisplayToXML(themeDisplay, themeDisplayElement);
178 }
179
180 Element parametersElement = requestElement.addElement("parameters");
181
182 Enumeration<String> enu = portletRequest.getParameterNames();
183
184 while (enu.hasMoreElements()) {
185 String name = enu.nextElement();
186
187 Element parameterElement = parametersElement.addElement(
188 "parameter");
189
190 parameterElement.addElement("name", name);
191
192 String[] values = portletRequest.getParameterValues(name);
193
194 for (int i = 0; i < values.length; i++) {
195 parameterElement.addElement("value", values[i]);
196 }
197 }
198
199 Element attributesElement = requestElement.addElement("attributes");
200
201 enu = portletRequest.getAttributeNames();
202
203 while (enu.hasMoreElements()) {
204 String name = enu.nextElement();
205
206 if (!_isValidAttributeName(name)) {
207 continue;
208 }
209
210 Object value = portletRequest.getAttribute(name);
211
212 if (!_isValidAttributeValue(value)) {
213 continue;
214 }
215
216 Element attributeElement = attributesElement.addElement(
217 "attribute");
218
219 attributeElement.addElement("name", name);
220 attributeElement.addElement("value", value);
221 }
222
223 Element portletSessionElement = requestElement.addElement(
224 "portlet-session");
225
226 attributesElement = portletSessionElement.addElement(
227 "portlet-attributes");
228
229 PortletSession portletSession = portletRequest.getPortletSession();
230
231 try {
232 enu = portletSession.getAttributeNames(
233 PortletSession.PORTLET_SCOPE);
234
235 while (enu.hasMoreElements()) {
236 String name = enu.nextElement();
237
238 if (!_isValidAttributeName(name)) {
239 continue;
240 }
241
242 Object value = portletSession.getAttribute(
243 name, PortletSession.PORTLET_SCOPE);
244
245 if (!_isValidAttributeValue(value)) {
246 continue;
247 }
248
249 Element attributeElement = attributesElement.addElement(
250 "attribute");
251
252 attributeElement.addElement("name", name);
253 attributeElement.addElement("value", value);
254 }
255
256 attributesElement = portletSessionElement.addElement(
257 "application-attributes");
258
259 enu = portletSession.getAttributeNames(
260 PortletSession.APPLICATION_SCOPE);
261
262 while (enu.hasMoreElements()) {
263 String name = enu.nextElement();
264
265 if (!_isValidAttributeName(name)) {
266 continue;
267 }
268
269 Object value = portletSession.getAttribute(
270 name, PortletSession.APPLICATION_SCOPE);
271
272 if (!_isValidAttributeValue(value)) {
273 continue;
274 }
275
276 Element attributeElement = attributesElement.addElement(
277 "attribute");
278
279 attributeElement.addElement("name", name);
280 attributeElement.addElement("value", value);
281 }
282 }
283 catch (IllegalStateException ise) {
284 if (_log.isWarnEnabled()) {
285 _log.warn(ise.getMessage());
286 }
287 }
288
289 return requestElement.toXMLString();
290 }
291
292 private static boolean _isValidAttributeName(String name) {
293 if (name.equalsIgnoreCase("j_password") ||
294 name.equalsIgnoreCase("LAYOUT_CONTENT") ||
295 name.equalsIgnoreCase("LAYOUTS") ||
296 name.equalsIgnoreCase("PORTLET_RENDER_PARAMETERS") ||
297 name.equalsIgnoreCase("USER_PASSWORD") ||
298 name.startsWith("javax.") ||
299 name.startsWith("liferay-ui:")) {
300
301 return false;
302 }
303 else {
304 return true;
305 }
306 }
307
308 private static boolean _isValidAttributeValue(Object obj) {
309 if (obj == null) {
310 return false;
311 }
312 else if (obj instanceof Collection<?>) {
313 Collection<?> col = (Collection<?>)obj;
314
315 if (col.size() == 0) {
316 return false;
317 }
318 else {
319 return true;
320 }
321 }
322 else if (obj instanceof Map<?, ?>) {
323 Map<?, ?> map = (Map<?, ?>)obj;
324
325 if (map.size() == 0) {
326 return false;
327 }
328 else {
329 return true;
330 }
331 }
332 else {
333 String objString = String.valueOf(obj);
334
335 if (Validator.isNull(objString)) {
336 return false;
337 }
338
339 String hashCode = StringPool.AT.concat(
340 StringUtil.toHexString(obj.hashCode()));
341
342 if (objString.endsWith(hashCode)) {
343 return false;
344 }
345
346 return true;
347 }
348 }
349
350 private static void _mimeResponseToXML(
351 MimeResponse mimeResponse, Element requestElement) {
352
353 String namespace = mimeResponse.getNamespace();
354
355 requestElement.addElement("portlet-namespace", namespace);
356
357 try {
358 PortletURL actionURL = mimeResponse.createActionURL();
359
360 requestElement.addElement("action-url", actionURL);
361 }
362 catch (IllegalStateException ise) {
363 if (_log.isWarnEnabled()) {
364 _log.warn(ise.getMessage());
365 }
366 }
367
368 try {
369 PortletURL renderURL = mimeResponse.createRenderURL();
370
371 requestElement.addElement("render-url", renderURL);
372
373 try {
374 renderURL.setWindowState(LiferayWindowState.EXCLUSIVE);
375
376 requestElement.addElement("render-url-exclusive", renderURL);
377 }
378 catch (WindowStateException wse) {
379 }
380
381 try {
382 renderURL.setWindowState(LiferayWindowState.MAXIMIZED);
383
384 requestElement.addElement("render-url-maximized", renderURL);
385 }
386 catch (WindowStateException wse) {
387 }
388
389 try {
390 renderURL.setWindowState(LiferayWindowState.MINIMIZED);
391
392 requestElement.addElement("render-url-minimized", renderURL);
393 }
394 catch (WindowStateException wse) {
395 }
396
397 try {
398 renderURL.setWindowState(LiferayWindowState.NORMAL);
399
400 requestElement.addElement("render-url-normal", renderURL);
401 }
402 catch (WindowStateException wse) {
403 }
404
405 try {
406 renderURL.setWindowState(LiferayWindowState.POP_UP);
407
408 requestElement.addElement("render-url-pop-up", renderURL);
409 }
410 catch (WindowStateException wse) {
411 }
412 }
413 catch (IllegalStateException ise) {
414 if (_log.isWarnEnabled()) {
415 _log.warn(ise.getMessage());
416 }
417 }
418
419 ResourceURL resourceURL = mimeResponse.createResourceURL();
420
421 String resourceURLString = HttpUtil.removeParameter(
422 resourceURL.toString(), namespace + "struts_action");
423
424 resourceURLString = HttpUtil.removeParameter(
425 resourceURLString, namespace + "redirect");
426
427 requestElement.addElement("resource-url", resourceURLString);
428 }
429
430 private static void _portletDisplayToXML(
431 PortletDisplay portletDisplay, Element portletDisplayElement) {
432
433 portletDisplayElement.addElement("id", portletDisplay.getId());
434 portletDisplayElement.addElement(
435 "instance-id", portletDisplay.getInstanceId());
436 portletDisplayElement.addElement(
437 "portlet-name", portletDisplay.getPortletName());
438 portletDisplayElement.addElement(
439 "resource-pk", portletDisplay.getResourcePK());
440 portletDisplayElement.addElement(
441 "root-portlet-id", portletDisplay.getRootPortletId());
442 portletDisplayElement.addElement("title", portletDisplay.getTitle());
443 }
444
445 private static void _themeDisplayToXML(
446 ThemeDisplay themeDisplay, Element themeDisplayElement) {
447
448 themeDisplayElement.addElement("cdn-host", themeDisplay.getCDNHost());
449 themeDisplayElement.addElement(
450 "company-id", themeDisplay.getCompanyId());
451 themeDisplayElement.addElement(
452 "do-as-user-id", themeDisplay.getDoAsUserId());
453 themeDisplayElement.addElement(
454 "i18n-language-id", themeDisplay.getI18nLanguageId());
455 themeDisplayElement.addElement("i18n-path", themeDisplay.getI18nPath());
456 themeDisplayElement.addElement(
457 "language-id", themeDisplay.getLanguageId());
458 themeDisplayElement.addElement("locale", themeDisplay.getLocale());
459 themeDisplayElement.addElement(
460 "path-context", themeDisplay.getPathContext());
461 themeDisplayElement.addElement(
462 "path-friendly-url-private-group",
463 themeDisplay.getPathFriendlyURLPrivateGroup());
464 themeDisplayElement.addElement(
465 "path-friendly-url-private-user",
466 themeDisplay.getPathFriendlyURLPrivateUser());
467 themeDisplayElement.addElement(
468 "path-friendly-url-public",
469 themeDisplay.getPathFriendlyURLPublic());
470 themeDisplayElement.addElement(
471 "path-image", themeDisplay.getPathImage());
472 themeDisplayElement.addElement("path-main", themeDisplay.getPathMain());
473 themeDisplayElement.addElement(
474 "path-theme-images", themeDisplay.getPathThemeImages());
475 themeDisplayElement.addElement("plid", themeDisplay.getPlid());
476 themeDisplayElement.addElement(
477 "portal-url", HttpUtil.removeProtocol(themeDisplay.getPortalURL()));
478 themeDisplayElement.addElement(
479 "real-user-id", themeDisplay.getRealUserId());
480 themeDisplayElement.addElement(
481 "scope-group-id", themeDisplay.getScopeGroupId());
482 themeDisplayElement.addElement("secure", themeDisplay.isSecure());
483 themeDisplayElement.addElement(
484 "server-name", themeDisplay.getServerName());
485 themeDisplayElement.addElement(
486 "server-port", themeDisplay.getServerPort());
487 themeDisplayElement.addElement(
488 "time-zone", themeDisplay.getTimeZone().getID());
489 themeDisplayElement.addElement(
490 "url-portal", HttpUtil.removeProtocol(themeDisplay.getURLPortal()));
491 themeDisplayElement.addElement("user-id", themeDisplay.getUserId());
492
493 if (themeDisplay.getPortletDisplay() != null) {
494 Element portletDisplayElement = themeDisplayElement.addElement(
495 "portlet-display");
496
497 _portletDisplayToXML(
498 themeDisplay.getPortletDisplay(), portletDisplayElement);
499 }
500 }
501
502 private static Log _log = LogFactoryUtil.getLog(PortletRequestUtil.class);
503
504 }