1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.taglib.portlet;
24  
25  import com.liferay.portal.kernel.util.BooleanWrapper;
26  import com.liferay.portal.kernel.util.LongWrapper;
27  import com.liferay.portal.kernel.util.MethodInvoker;
28  import com.liferay.portal.kernel.util.MethodWrapper;
29  import com.liferay.portal.kernel.util.NullWrapper;
30  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
33  
34  import java.util.Map;
35  
36  import javax.portlet.PortletRequest;
37  
38  import javax.servlet.jsp.JspException;
39  import javax.servlet.jsp.PageContext;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  /**
45   * <a href="ActionURLTag.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class ActionURLTag extends ParamAndPropertyAncestorTagImpl {
51  
52      public static String doTag(
53              String lifecycle, String windowState, String portletMode,
54              String var, String varImpl, Boolean secure,
55              Boolean copyCurrentRenderParameters, Boolean escapeXml, String name,
56              String resourceID, String cacheability, String portletName,
57              Boolean anchor, Boolean encrypt, long doAsUserId,
58              Boolean portletConfiguration, Map<String, String[]> params,
59              boolean writeOutput, PageContext pageContext)
60          throws Exception {
61  
62          Object returnObj = null;
63  
64          ClassLoader contextClassLoader =
65              Thread.currentThread().getContextClassLoader();
66  
67          try {
68              Thread.currentThread().setContextClassLoader(
69                  PortalClassLoaderUtil.getClassLoader());
70  
71              Object windowStateWrapper = windowState;
72  
73              if (windowStateWrapper == null) {
74                  windowStateWrapper = new NullWrapper(String.class.getName());
75              }
76  
77              Object portletModeWrapper = portletMode;
78  
79              if (portletModeWrapper == null) {
80                  portletModeWrapper = new NullWrapper(String.class.getName());
81              }
82  
83              Object varWrapper = var;
84  
85              if (varWrapper == null) {
86                  varWrapper = new NullWrapper(String.class.getName());
87              }
88  
89              Object varImplWrapper = varImpl;
90  
91              if (varImplWrapper == null) {
92                  varImplWrapper = new NullWrapper(String.class.getName());
93              }
94  
95              Object secureWrapper = secure;
96  
97              if (secureWrapper == null) {
98                  secureWrapper = new NullWrapper(Boolean.class.getName());
99              }
100 
101             Object copyCurrentRenderParametersWrapper =
102                 copyCurrentRenderParameters;
103 
104             if (copyCurrentRenderParametersWrapper == null) {
105                 copyCurrentRenderParametersWrapper = new NullWrapper(
106                     Boolean.class.getName());
107             }
108 
109             Object escapeXmlWrapper = escapeXml;
110 
111             if (escapeXmlWrapper == null) {
112                 escapeXmlWrapper = new NullWrapper(Boolean.class.getName());
113             }
114 
115             Object nameWrapper = name;
116 
117             if (nameWrapper == null) {
118                 nameWrapper = new NullWrapper(String.class.getName());
119             }
120 
121             Object resourceIDWrapper = resourceID;
122 
123             if (resourceIDWrapper == null) {
124                 resourceIDWrapper = new NullWrapper(String.class.getName());
125             }
126 
127             Object cacheabilityWrapper = cacheability;
128 
129             if (cacheabilityWrapper == null) {
130                 cacheabilityWrapper = new NullWrapper(String.class.getName());
131             }
132 
133             Object portletNameWrapper = portletName;
134 
135             if (portletNameWrapper == null) {
136                 portletNameWrapper = new NullWrapper(String.class.getName());
137             }
138 
139             Object anchorWrapper = anchor;
140 
141             if (anchorWrapper == null) {
142                 anchorWrapper = new NullWrapper(Boolean.class.getName());
143             }
144 
145             Object encryptWrapper = encrypt;
146 
147             if (encryptWrapper == null) {
148                 encryptWrapper = new NullWrapper(Boolean.class.getName());
149             }
150 
151             Object portletConfigurationWrapper = portletConfiguration;
152 
153             if (portletConfigurationWrapper == null) {
154                 portletConfigurationWrapper = new NullWrapper(
155                     Boolean.class.getName());
156             }
157 
158             Object paramsWrapper = params;
159 
160             if (paramsWrapper == null) {
161                 paramsWrapper = new NullWrapper(Map.class.getName());
162             }
163 
164             MethodWrapper methodWrapper = new MethodWrapper(
165                 _TAG_CLASS, _TAG_DO_END_METHOD,
166                 new Object[] {
167                     lifecycle, windowStateWrapper, portletModeWrapper,
168                     varWrapper, varImplWrapper, secureWrapper,
169                     copyCurrentRenderParametersWrapper, escapeXmlWrapper,
170                     nameWrapper, resourceIDWrapper, cacheabilityWrapper,
171                     portletNameWrapper, anchorWrapper, encryptWrapper,
172                     new LongWrapper(doAsUserId), portletConfigurationWrapper,
173                     paramsWrapper, new BooleanWrapper(writeOutput), pageContext
174                 });
175 
176             returnObj = MethodInvoker.invoke(methodWrapper);
177         }
178         catch (Exception e) {
179             _log.error(e, e);
180 
181             throw e;
182         }
183         finally {
184             Thread.currentThread().setContextClassLoader(contextClassLoader);
185         }
186 
187         if (returnObj != null) {
188             return returnObj.toString();
189         }
190         else {
191             return StringPool.BLANK;
192         }
193     }
194 
195     public int doEndTag() throws JspException {
196         try {
197             doTag(
198                 getLifecycle(), _windowState, _portletMode, _var, _varImpl,
199                 _secure, _copyCurrentRenderParameters, _escapeXml, _name,
200                 _resourceID, _cacheability, _portletName, _anchor, _encrypt,
201                 _doAsUserId, _portletConfiguration, getParams(), true,
202                 pageContext);
203         }
204         catch (Exception e) {
205             if (e instanceof JspException) {
206                 throw (JspException)e;
207             }
208             else {
209                 throw new JspException(e);
210             }
211         }
212         finally {
213             clearParams();
214             clearProperties();
215         }
216 
217         return EVAL_PAGE;
218     }
219 
220     public String getLifecycle() {
221         return PortletRequest.ACTION_PHASE;
222     }
223 
224     public void setWindowState(String windowState) {
225         _windowState = windowState;
226     }
227 
228     public void setPortletMode(String portletMode) {
229         _portletMode = portletMode;
230     }
231 
232     public void setVar(String var) {
233         _var = var;
234     }
235 
236     public void setVarImpl(String varImpl) {
237         _varImpl = varImpl;
238     }
239 
240     public void setSecure(boolean secure) {
241         _secure = Boolean.valueOf(secure);
242     }
243 
244     public void setCopyCurrentRenderParameters(
245         boolean copyCurrentRenderParameters) {
246 
247         _copyCurrentRenderParameters = Boolean.valueOf(
248             copyCurrentRenderParameters);
249     }
250 
251     public void setEscapeXml(boolean escapeXml) {
252         _escapeXml = Boolean.valueOf(escapeXml);
253     }
254 
255     public void setName(String name) {
256         _name = name;
257     }
258 
259     public void setId(String resourceID) {
260         _resourceID = resourceID;
261     }
262 
263     public void setCacheability(String cacheability) {
264         _cacheability = cacheability;
265     }
266 
267     public void setPortletName(String portletName) {
268         _portletName = portletName;
269     }
270 
271     public void setAnchor(boolean anchor) {
272         _anchor = Boolean.valueOf(anchor);
273     }
274 
275     public void setEncrypt(boolean encrypt) {
276         _encrypt = Boolean.valueOf(encrypt);
277     }
278 
279     public void setDoAsUserId(long doAsUserId) {
280         _doAsUserId = doAsUserId;
281     }
282 
283     public void setPortletConfiguration(boolean portletConfiguration) {
284         _portletConfiguration = Boolean.valueOf(portletConfiguration);
285     }
286 
287     private static final String _TAG_CLASS =
288         "com.liferay.portal.servlet.taglib.portlet.ActionURLTagUtil";
289 
290     private static final String _TAG_DO_END_METHOD = "doEndTag";
291 
292     private static Log _log = LogFactory.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 String _portletName;
305     private Boolean _anchor;
306     private Boolean _encrypt;
307     private long _doAsUserId;
308     private Boolean _portletConfiguration;
309 
310 }