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.MapUtil;
027    import com.liferay.portal.kernel.util.ParamUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.model.LayoutConstants;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portlet.PortletPreferencesFactoryConstants;
032    import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
033    
034    import java.util.Map;
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",
159                                    "/html/portlet/portlet_configuration/edit_configuration.jsp");
160                            liferayPortletURL.setParameter(
161                                    "returnToFullPageURL", returnToFullPageURL);
162                            liferayPortletURL.setParameter("portletResource", portletResource);
163                            liferayPortletURL.setParameter("previewWidth", previewWidth);
164                    }
165    
166                    if (parameterMap != null) {
167                            MapUtil.merge(liferayPortletURL.getParameterMap(), parameterMap);
168    
169                            liferayPortletURL.setParameters(parameterMap);
170                    }
171    
172                    if ((settingsScope != null) &&
173                            ((parameterMap == null) ||
174                             !parameterMap.containsKey("settingsScope"))) {
175    
176                            liferayPortletURL.setParameter("settingsScope", settingsScope);
177                    }
178    
179                    liferayPortletURL.setRemovedParameterNames(removedParameterNames);
180    
181                    return liferayPortletURL;
182            }
183    
184            @Override
185            public int doEndTag() throws JspException {
186                    try {
187                            PortletURL portletURL = doTag(
188                                    getLifecycle(), _windowState, _portletMode, _secure,
189                                    _copyCurrentRenderParameters, _escapeXml, _name, _resourceID,
190                                    _cacheability, _plid, _refererPlid, _portletName, _anchor,
191                                    _encrypt, _doAsGroupId, _doAsUserId, _portletConfiguration,
192                                    getParams(), getRemovedParameterNames(),
193                                    (HttpServletRequest)pageContext.getRequest());
194    
195                            if (Validator.isNotNull(_var)) {
196                                    pageContext.setAttribute(_var, portletURL.toString());
197                            }
198                            else if (Validator.isNotNull(_varImpl)) {
199                                    pageContext.setAttribute(_varImpl, portletURL);
200                            }
201                            else {
202                                    JspWriter jspWriter = pageContext.getOut();
203    
204                                    jspWriter.write(portletURL.toString());
205                            }
206    
207                            return EVAL_PAGE;
208                    }
209                    catch (Exception e) {
210                            throw new JspException(e);
211                    }
212                    finally {
213                            clearParams();
214                            clearProperties();
215    
216                            _plid = LayoutConstants.DEFAULT_PLID;
217                    }
218            }
219    
220            @Override
221            public int doStartTag() {
222                    return EVAL_BODY_INCLUDE;
223            }
224    
225            public String getLifecycle() {
226                    return PortletRequest.ACTION_PHASE;
227            }
228    
229            public void setAnchor(boolean anchor) {
230                    _anchor = Boolean.valueOf(anchor);
231            }
232    
233            public void setCacheability(String cacheability) {
234                    _cacheability = cacheability;
235            }
236    
237            public void setCopyCurrentRenderParameters(
238                    boolean copyCurrentRenderParameters) {
239    
240                    _copyCurrentRenderParameters = Boolean.valueOf(
241                            copyCurrentRenderParameters);
242            }
243    
244            public void setDoAsGroupId(long doAsGroupId) {
245                    _doAsGroupId = doAsGroupId;
246            }
247    
248            public void setDoAsUserId(long doAsUserId) {
249                    _doAsUserId = doAsUserId;
250            }
251    
252            public void setEncrypt(boolean encrypt) {
253                    _encrypt = Boolean.valueOf(encrypt);
254            }
255    
256            public void setEscapeXml(boolean escapeXml) {
257                    _escapeXml = Boolean.valueOf(escapeXml);
258            }
259    
260            public void setId(String resourceID) {
261                    _resourceID = resourceID;
262            }
263    
264            public void setName(String name) {
265                    _name = name;
266            }
267    
268            public void setPlid(long plid) {
269                    _plid = plid;
270            }
271    
272            public void setPortletConfiguration(boolean portletConfiguration) {
273                    _portletConfiguration = Boolean.valueOf(portletConfiguration);
274            }
275    
276            public void setPortletMode(String portletMode) {
277                    _portletMode = portletMode;
278            }
279    
280            public void setPortletName(String portletName) {
281                    _portletName = portletName;
282            }
283    
284            public void setRefererPlid(long refererPlid) {
285                    _refererPlid = refererPlid;
286            }
287    
288            public void setSecure(boolean secure) {
289                    _secure = Boolean.valueOf(secure);
290            }
291    
292            public void setVar(String var) {
293                    _var = var;
294            }
295    
296            public void setVarImpl(String varImpl) {
297                    _varImpl = varImpl;
298            }
299    
300            public void setWindowState(String windowState) {
301                    _windowState = windowState;
302            }
303    
304            private static LiferayPortletURL _getLiferayPortletURL(
305                    HttpServletRequest request, long plid, String portletName,
306                    String lifecycle) {
307    
308                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
309                            JavaConstants.JAVAX_PORTLET_REQUEST);
310    
311                    if (portletRequest == null) {
312                            return null;
313                    }
314    
315                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
316                            JavaConstants.JAVAX_PORTLET_RESPONSE);
317    
318                    LiferayPortletResponse liferayPortletResponse =
319                            PortalUtil.getLiferayPortletResponse(portletResponse);
320    
321                    return liferayPortletResponse.createLiferayPortletURL(
322                            plid, portletName, lifecycle);
323            }
324    
325            private static String _getPortletName(HttpServletRequest request) {
326                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
327                            JavaConstants.JAVAX_PORTLET_REQUEST);
328    
329                    if (portletRequest == null) {
330                            return null;
331                    }
332    
333                    LiferayPortletConfig liferayPortletConfig =
334                            (LiferayPortletConfig)request.getAttribute(
335                                    JavaConstants.JAVAX_PORTLET_CONFIG);
336    
337                    return liferayPortletConfig.getPortletId();
338            }
339    
340            private static final Log _log = LogFactoryUtil.getLog(ActionURLTag.class);
341    
342            private Boolean _anchor;
343            private String _cacheability;
344            private Boolean _copyCurrentRenderParameters;
345            private long _doAsGroupId;
346            private long _doAsUserId;
347            private Boolean _encrypt;
348            private Boolean _escapeXml;
349            private String _name;
350            private long _plid = LayoutConstants.DEFAULT_PLID;
351            private Boolean _portletConfiguration;
352            private String _portletMode;
353            private String _portletName;
354            private long _refererPlid = LayoutConstants.DEFAULT_PLID;
355            private String _resourceID;
356            private Boolean _secure;
357            private String _var;
358            private String _varImpl;
359            private String _windowState;
360    
361    }