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