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