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