001    /**
002     * Copyright (c) 2000-2012 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.portal.action;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.portlet.PortletModeFactory;
019    import com.liferay.portal.kernel.portlet.WindowStateFactory;
020    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.PortletURLImpl;
027    
028    import java.util.Map;
029    
030    import javax.portlet.ActionRequest;
031    import javax.portlet.PortletRequest;
032    
033    import javax.servlet.http.HttpServletRequest;
034    import javax.servlet.http.HttpServletResponse;
035    
036    import org.apache.struts.action.Action;
037    import org.apache.struts.action.ActionForm;
038    import org.apache.struts.action.ActionForward;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Eduardo Lundgren
043     */
044    public class PortletURLAction extends Action {
045    
046            @Override
047            public ActionForward execute(
048                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
049                            HttpServletResponse response)
050                    throws Exception {
051    
052                    try {
053                            String portletURL = getPortletURL(request);
054    
055                            ServletResponseUtil.write(response, portletURL);
056                    }
057                    catch (Exception e) {
058                            PortalUtil.sendError(e, request, response);
059                    }
060    
061                    return null;
062            }
063    
064            protected String getPortletURL(HttpServletRequest request)
065                    throws Exception {
066    
067                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
068                            WebKeys.THEME_DISPLAY);
069    
070                    String cacheability = ParamUtil.getString(request, "cacheability");
071                    boolean copyCurrentRenderParameters = ParamUtil.getBoolean(
072                            request, "copyCurrentRenderParameters");
073                    long doAsGroupId = ParamUtil.getLong(request, "doAsGroupId");
074                    long doAsUserId = ParamUtil.getLong(request, "doAsUserId");
075                    String doAsUserLanguageId = ParamUtil.getString(
076                            request, "doAsUserLanguageId");
077                    //boolean encrypt = ParamUtil.getBoolean(request, "encrypt");
078                    boolean escapeXml = ParamUtil.getBoolean(request, "escapeXml");
079                    String lifecycle = ParamUtil.getString(request, "lifecycle");
080                    String name = ParamUtil.getString(request, "name");
081                    boolean portletConfiguration = ParamUtil.getBoolean(
082                            request, "portletConfiguration");
083                    String portletId = ParamUtil.getString(request, "portletId");
084                    String portletMode = ParamUtil.getString(request, "portletMode");
085                    String resourceId = ParamUtil.getString(request, "resourceId");
086                    String returnToFullPageURL = ParamUtil.getString(
087                            request, "returnToFullPageURL");
088                    boolean secure = ParamUtil.getBoolean(
089                            request, "secure", request.isSecure());
090                    String windowState = ParamUtil.getString(request, "windowState");
091    
092                    PortletURLImpl portletURL = new PortletURLImpl(
093                            request, portletId, themeDisplay.getPlid(), lifecycle);
094    
095                    if (Validator.isNotNull(cacheability)) {
096                            portletURL.setCacheability(cacheability);
097                    }
098    
099                    portletURL.setCopyCurrentRenderParameters(copyCurrentRenderParameters);
100    
101                    if (doAsGroupId > 0) {
102                            portletURL.setDoAsGroupId(doAsGroupId);
103                    }
104    
105                    if (doAsUserId > 0) {
106                            //portletURL.setDoAsUserId(doAsUserId);
107                    }
108    
109                    if (Validator.isNotNull(doAsUserLanguageId)) {
110                            portletURL.setDoAsUserLanguageId(doAsUserLanguageId);
111                    }
112    
113                    //portletURL.setEncrypt(encrypt);
114                    portletURL.setEscapeXml(escapeXml);
115    
116                    if (lifecycle.equals(PortletRequest.ACTION_PHASE) &&
117                            Validator.isNotNull(name)) {
118    
119                            portletURL.setParameter(ActionRequest.ACTION_NAME, name);
120                    }
121    
122                    portletURL.setPortletId(portletId);
123    
124                    if (portletConfiguration) {
125                            String portletResource = ParamUtil.getString(
126                                    request, "portletResource");
127                            String previewWidth = ParamUtil.getString(request, "previewWidth");
128    
129                            portletURL.setParameter(
130                                    "struts_action", "/portlet_configuration/edit_configuration");
131                            portletURL.setParameter("returnToFullPageURL", returnToFullPageURL);
132                            portletURL.setParameter("portletResource", portletResource);
133                            portletURL.setParameter("previewWidth", previewWidth);
134                    }
135    
136                    if (Validator.isNotNull(portletMode)) {
137                            portletURL.setPortletMode(
138                                    PortletModeFactory.getPortletMode(portletMode));
139                    }
140    
141                    if (Validator.isNotNull(resourceId)) {
142                            portletURL.setResourceID(resourceId);
143                    }
144    
145                    if (!themeDisplay.isStateMaximized()) {
146                            if (Validator.isNotNull(returnToFullPageURL)) {
147                                    portletURL.setParameter(
148                                            "returnToFullPageURL", returnToFullPageURL);
149                            }
150                    }
151    
152                    portletURL.setSecure(secure);
153    
154                    if (Validator.isNotNull(windowState)) {
155                            portletURL.setWindowState(
156                                    WindowStateFactory.getWindowState(windowState));
157                    }
158    
159                    String parameterMapString = ParamUtil.getString(
160                            request, "parameterMap");
161    
162                    if (Validator.isNotNull(parameterMapString)) {
163                            Map<String, String> parameterMap =
164                                    (Map<String, String>)JSONFactoryUtil.deserialize(
165                                            parameterMapString);
166    
167                            for (Map.Entry<String, String> entry : parameterMap.entrySet()) {
168                                    String key = entry.getKey();
169                                    String value = entry.getValue();
170    
171                                    if ((key != null) && (value != null)) {
172                                            portletURL.setParameter(key, value);
173                                    }
174                            }
175                    }
176    
177                    return portletURL.toString();
178            }
179    
180    }