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