001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.action;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
019    import com.liferay.portal.kernel.portlet.PortletModeFactory;
020    import com.liferay.portal.kernel.portlet.WindowStateFactory;
021    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portal.util.PropsValues;
027    import com.liferay.portal.util.WebKeys;
028    import com.liferay.portlet.PortletURLFactoryUtil;
029    
030    import java.util.Map;
031    
032    import javax.portlet.ActionRequest;
033    import javax.portlet.PortletRequest;
034    
035    import javax.servlet.http.HttpServletRequest;
036    import javax.servlet.http.HttpServletResponse;
037    
038    import org.apache.struts.action.Action;
039    import org.apache.struts.action.ActionForm;
040    import org.apache.struts.action.ActionForward;
041    import org.apache.struts.action.ActionMapping;
042    
043    /**
044     * @author     Eduardo Lundgren
045     * @deprecated As of 6.2.0, with no direct replacement
046     */
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                    LiferayPortletURL portletURL = PortletURLFactoryUtil.create(
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                                    "struts_action", "/portlet_configuration/edit_configuration");
140                            portletURL.setParameter("returnToFullPageURL", returnToFullPageURL);
141                            portletURL.setParameter("portletResource", portletResource);
142                            portletURL.setParameter("previewWidth", previewWidth);
143                    }
144    
145                    if (Validator.isNotNull(portletMode)) {
146                            portletURL.setPortletMode(
147                                    PortletModeFactory.getPortletMode(portletMode));
148                    }
149    
150                    if (Validator.isNotNull(resourceId)) {
151                            portletURL.setResourceID(resourceId);
152                    }
153    
154                    if (!themeDisplay.isStateMaximized()) {
155                            if (Validator.isNotNull(returnToFullPageURL)) {
156                                    portletURL.setParameter(
157                                            "returnToFullPageURL", returnToFullPageURL);
158                            }
159                    }
160    
161                    portletURL.setSecure(secure);
162    
163                    if (Validator.isNotNull(windowState)) {
164                            portletURL.setWindowState(
165                                    WindowStateFactory.getWindowState(windowState));
166                    }
167    
168                    String parameterMapString = ParamUtil.getString(
169                            request, "parameterMap");
170    
171                    if (Validator.isNotNull(parameterMapString)) {
172                            Map<String, String> parameterMap =
173                                    (Map<String, String>)JSONFactoryUtil.deserialize(
174                                            parameterMapString);
175    
176                            for (Map.Entry<String, String> entry : parameterMap.entrySet()) {
177                                    String key = entry.getKey();
178                                    String value = entry.getValue();
179    
180                                    if ((key != null) && (value != null)) {
181                                            portletURL.setParameter(key, value);
182                                    }
183                            }
184                    }
185    
186                    return portletURL.toString();
187            }
188    
189    }