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