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