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.taglib.portlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.portlet.DummyPortletURL;
020    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
021    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
022    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
023    import com.liferay.portal.kernel.portlet.PortletModeFactory;
024    import com.liferay.portal.kernel.portlet.WindowStateFactory;
025    import com.liferay.portal.kernel.util.JavaConstants;
026    import com.liferay.portal.kernel.util.ParamUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.model.LayoutConstants;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portlet.PortletPreferencesFactoryConstants;
031    import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
032    
033    import java.util.Map;
034    import java.util.Map.Entry;
035    import java.util.Set;
036    
037    import javax.portlet.ActionRequest;
038    import javax.portlet.PortletRequest;
039    import javax.portlet.PortletResponse;
040    import javax.portlet.PortletURL;
041    
042    import javax.servlet.http.HttpServletRequest;
043    import javax.servlet.jsp.JspException;
044    import javax.servlet.jsp.JspWriter;
045    
046    /**
047     * @author Brian Wing Shun Chan
048     */
049    public class ActionURLTag extends ParamAndPropertyAncestorTagImpl {
050    
051            public static PortletURL doTag(
052                            String lifecycle, String windowState, String portletMode,
053                            Boolean secure, Boolean copyCurrentRenderParameters,
054                            Boolean escapeXml, String name, String resourceID,
055                            String cacheability, long plid, long refererPlid,
056                            String portletName, Boolean anchor, Boolean encrypt,
057                            long doAsGroupId, long doAsUserId, Boolean portletConfiguration,
058                            Map<String, String[]> parameterMap,
059                            Set<String> removedParameterNames, HttpServletRequest request)
060                    throws Exception {
061    
062                    if (portletName == null) {
063                            portletName = _getPortletName(request);
064                    }
065    
066                    LiferayPortletURL liferayPortletURL = _getLiferayPortletURL(
067                            request, plid, portletName, lifecycle);
068    
069                    if (liferayPortletURL == null) {
070                            _log.error(
071                                    "Render response is null because this tag is not being " +
072                                            "called within the context of a portlet");
073    
074                            return DummyPortletURL.getInstance();
075                    }
076    
077                    if (Validator.isNotNull(windowState)) {
078                            liferayPortletURL.setWindowState(
079                                    WindowStateFactory.getWindowState(windowState));
080                    }
081    
082                    if (Validator.isNotNull(portletMode)) {
083                            liferayPortletURL.setPortletMode(
084                                    PortletModeFactory.getPortletMode(portletMode));
085                    }
086    
087                    if (secure != null) {
088                            liferayPortletURL.setSecure(secure.booleanValue());
089                    }
090                    else {
091                            liferayPortletURL.setSecure(PortalUtil.isSecure(request));
092                    }
093    
094                    if (copyCurrentRenderParameters != null) {
095                            liferayPortletURL.setCopyCurrentRenderParameters(
096                                    copyCurrentRenderParameters.booleanValue());
097                    }
098    
099                    if (escapeXml != null) {
100                            liferayPortletURL.setEscapeXml(escapeXml.booleanValue());
101                    }
102    
103                    if (lifecycle.equals(PortletRequest.ACTION_PHASE) &&
104                            Validator.isNotNull(name)) {
105    
106                            liferayPortletURL.setParameter(ActionRequest.ACTION_NAME, name);
107                    }
108    
109                    if (resourceID != null) {
110                            liferayPortletURL.setResourceID(resourceID);
111                    }
112    
113                    if (cacheability != null) {
114                            liferayPortletURL.setCacheability(cacheability);
115                    }
116    
117                    if (refererPlid > LayoutConstants.DEFAULT_PLID) {
118                            liferayPortletURL.setRefererPlid(refererPlid);
119                    }
120    
121                    if (anchor != null) {
122                            liferayPortletURL.setAnchor(anchor.booleanValue());
123                    }
124    
125                    if (encrypt != null) {
126                            liferayPortletURL.setEncrypt(encrypt.booleanValue());
127                    }
128    
129                    if (doAsGroupId > 0) {
130                            liferayPortletURL.setDoAsGroupId(doAsGroupId);
131                    }
132    
133                    if (doAsUserId > 0) {
134                            liferayPortletURL.setDoAsUserId(doAsUserId);
135                    }
136    
137                    String settingsScope = null;
138    
139                    if ((portletConfiguration != null) &&
140                            portletConfiguration.booleanValue()) {
141    
142                            String returnToFullPageURL = ParamUtil.getString(
143                                    request, "returnToFullPageURL");
144                            String portletResource = ParamUtil.getString(
145                                    request, "portletResource");
146                            String previewWidth = ParamUtil.getString(request, "previewWidth");
147                            settingsScope = ParamUtil.getString(
148                                    request, "settingsScope",
149                                    PortletPreferencesFactoryConstants.
150                                            SETTINGS_SCOPE_PORTLET_INSTANCE);
151    
152                            if (Validator.isNull(name)) {
153                                    liferayPortletURL.setParameter(
154                                            ActionRequest.ACTION_NAME, "editConfiguration");
155                            }
156    
157                            liferayPortletURL.setParameter(
158                                    "mvcPath", "/edit_configuration.jsp");
159                            liferayPortletURL.setParameter(
160                                    "returnToFullPageURL", returnToFullPageURL);
161                            liferayPortletURL.setParameter(
162                                    "portletConfiguration", Boolean.TRUE.toString());
163                            liferayPortletURL.setParameter("portletResource", portletResource);
164                            liferayPortletURL.setParameter("previewWidth", previewWidth);
165                    }
166    
167                    if (parameterMap != null) {
168                            for (Entry<String, String[]> entry : parameterMap.entrySet()) {
169                                    liferayPortletURL.setParameter(
170                                            entry.getKey(), entry.getValue(), false);
171                            }
172                    }
173    
174                    if ((settingsScope != null) &&
175                            ((parameterMap == null) ||
176                             !parameterMap.containsKey("settingsScope"))) {
177    
178                            liferayPortletURL.setParameter("settingsScope", settingsScope);
179                    }
180    
181                    liferayPortletURL.setRemovedParameterNames(removedParameterNames);
182    
183                    return liferayPortletURL;
184            }
185    
186            @Override
187            public int doEndTag() throws JspException {
188                    try {
189                            PortletURL portletURL = doTag(
190                                    getLifecycle(), _windowState, _portletMode, _secure,
191                                    _copyCurrentRenderParameters, _escapeXml, _name, _resourceID,
192                                    _cacheability, _plid, _refererPlid, _portletName, _anchor,
193                                    _encrypt, _doAsGroupId, _doAsUserId, _portletConfiguration,
194                                    getParams(), getRemovedParameterNames(),
195                                    (HttpServletRequest)pageContext.getRequest());
196    
197                            if (Validator.isNotNull(_var)) {
198                                    pageContext.setAttribute(_var, portletURL.toString());
199                            }
200                            else if (Validator.isNotNull(_varImpl)) {
201                                    pageContext.setAttribute(_varImpl, portletURL);
202                            }
203                            else {
204                                    JspWriter jspWriter = pageContext.getOut();
205    
206                                    jspWriter.write(portletURL.toString());
207                            }
208    
209                            return EVAL_PAGE;
210                    }
211                    catch (Exception e) {
212                            throw new JspException(e);
213                    }
214                    finally {
215                            clearParams();
216                            clearProperties();
217    
218                            _plid = LayoutConstants.DEFAULT_PLID;
219                    }
220            }
221    
222            @Override
223            public int doStartTag() {
224                    return EVAL_BODY_INCLUDE;
225            }
226    
227            public String getLifecycle() {
228                    return PortletRequest.ACTION_PHASE;
229            }
230    
231            public void setAnchor(boolean anchor) {
232                    _anchor = Boolean.valueOf(anchor);
233            }
234    
235            public void setCacheability(String cacheability) {
236                    _cacheability = cacheability;
237            }
238    
239            @Override
240            public void setCopyCurrentRenderParameters(
241                    boolean copyCurrentRenderParameters) {
242    
243                    super.setCopyCurrentRenderParameters(copyCurrentRenderParameters);
244    
245                    _copyCurrentRenderParameters = Boolean.valueOf(
246                            copyCurrentRenderParameters);
247            }
248    
249            public void setDoAsGroupId(long doAsGroupId) {
250                    _doAsGroupId = doAsGroupId;
251            }
252    
253            public void setDoAsUserId(long doAsUserId) {
254                    _doAsUserId = doAsUserId;
255            }
256    
257            public void setEncrypt(boolean encrypt) {
258                    _encrypt = Boolean.valueOf(encrypt);
259            }
260    
261            public void setEscapeXml(boolean escapeXml) {
262                    _escapeXml = Boolean.valueOf(escapeXml);
263            }
264    
265            public void setId(String resourceID) {
266                    _resourceID = resourceID;
267            }
268    
269            public void setName(String name) {
270                    _name = name;
271            }
272    
273            public void setPlid(long plid) {
274                    _plid = plid;
275            }
276    
277            public void setPortletConfiguration(boolean portletConfiguration) {
278                    _portletConfiguration = Boolean.valueOf(portletConfiguration);
279            }
280    
281            public void setPortletMode(String portletMode) {
282                    _portletMode = portletMode;
283            }
284    
285            public void setPortletName(String portletName) {
286                    _portletName = portletName;
287            }
288    
289            public void setRefererPlid(long refererPlid) {
290                    _refererPlid = refererPlid;
291            }
292    
293            public void setSecure(boolean secure) {
294                    _secure = Boolean.valueOf(secure);
295            }
296    
297            public void setVar(String var) {
298                    _var = var;
299            }
300    
301            public void setVarImpl(String varImpl) {
302                    _varImpl = varImpl;
303            }
304    
305            public void setWindowState(String windowState) {
306                    _windowState = windowState;
307            }
308    
309            private static LiferayPortletURL _getLiferayPortletURL(
310                    HttpServletRequest request, long plid, String portletName,
311                    String lifecycle) {
312    
313                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
314                            JavaConstants.JAVAX_PORTLET_REQUEST);
315    
316                    if (portletRequest == null) {
317                            return null;
318                    }
319    
320                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
321                            JavaConstants.JAVAX_PORTLET_RESPONSE);
322    
323                    LiferayPortletResponse liferayPortletResponse =
324                            PortalUtil.getLiferayPortletResponse(portletResponse);
325    
326                    return liferayPortletResponse.createLiferayPortletURL(
327                            plid, portletName, lifecycle);
328            }
329    
330            private static String _getPortletName(HttpServletRequest request) {
331                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
332                            JavaConstants.JAVAX_PORTLET_REQUEST);
333    
334                    if (portletRequest == null) {
335                            return null;
336                    }
337    
338                    LiferayPortletConfig liferayPortletConfig =
339                            (LiferayPortletConfig)request.getAttribute(
340                                    JavaConstants.JAVAX_PORTLET_CONFIG);
341    
342                    return liferayPortletConfig.getPortletId();
343            }
344    
345            private static final Log _log = LogFactoryUtil.getLog(ActionURLTag.class);
346    
347            private Boolean _anchor;
348            private String _cacheability;
349            private Boolean _copyCurrentRenderParameters;
350            private long _doAsGroupId;
351            private long _doAsUserId;
352            private Boolean _encrypt;
353            private Boolean _escapeXml;
354            private String _name;
355            private long _plid = LayoutConstants.DEFAULT_PLID;
356            private Boolean _portletConfiguration;
357            private String _portletMode;
358            private String _portletName;
359            private long _refererPlid = LayoutConstants.DEFAULT_PLID;
360            private String _resourceID;
361            private Boolean _secure;
362            private String _var;
363            private String _varImpl;
364            private String _windowState;
365    
366    }