1   /**
2    * Copyright (c) 2000-2007 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.ParamAncestorTagImpl;
33  
34  import java.util.Map;
35  
36  import javax.servlet.jsp.JspException;
37  import javax.servlet.jsp.PageContext;
38  
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  
42  /**
43   * <a href="ActionURLTag.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class ActionURLTag extends ParamAncestorTagImpl {
49  
50      public static String doTag(
51              boolean action, String windowState, String portletMode, String var,
52              String varImpl, Boolean secure, String portletName, Boolean anchor,
53              Boolean encrypt, long doAsUserId, Boolean portletConfiguration,
54              Map params, boolean writeOutput, PageContext pageContext)
55          throws Exception {
56  
57          Object returnObj = null;
58  
59          ClassLoader contextClassLoader =
60              Thread.currentThread().getContextClassLoader();
61  
62          try {
63              Thread.currentThread().setContextClassLoader(
64                  PortalClassLoaderUtil.getClassLoader());
65  
66              Object windowStateWrapper = windowState;
67  
68              if (windowStateWrapper == null) {
69                  windowStateWrapper = new NullWrapper(String.class.getName());
70              }
71  
72              Object portletModeWrapper = portletMode;
73  
74              if (portletModeWrapper == null) {
75                  portletModeWrapper = new NullWrapper(String.class.getName());
76              }
77  
78              Object varWrapper = var;
79  
80              if (varWrapper == null) {
81                  varWrapper = new NullWrapper(String.class.getName());
82              }
83  
84              Object varImplWrapper = varImpl;
85  
86              if (varImplWrapper == null) {
87                  varImplWrapper = new NullWrapper(String.class.getName());
88              }
89  
90              Object secureWrapper = secure;
91  
92              if (secureWrapper == null) {
93                  secureWrapper = new NullWrapper(Boolean.class.getName());
94              }
95  
96              Object portletNameWrapper = portletName;
97  
98              if (portletNameWrapper == null) {
99                  portletNameWrapper = new NullWrapper(String.class.getName());
100             }
101 
102             Object anchorWrapper = anchor;
103 
104             if (anchorWrapper == null) {
105                 anchorWrapper = new NullWrapper(Boolean.class.getName());
106             }
107 
108             Object encryptWrapper = encrypt;
109 
110             if (encryptWrapper == null) {
111                 encryptWrapper = new NullWrapper(Boolean.class.getName());
112             }
113 
114             Object portletConfigurationWrapper = portletConfiguration;
115 
116             if (portletConfigurationWrapper == null) {
117                 portletConfigurationWrapper = new NullWrapper(
118                     Boolean.class.getName());
119             }
120 
121             Object paramsWrapper = params;
122 
123             if (paramsWrapper == null) {
124                 paramsWrapper = new NullWrapper(Map.class.getName());
125             }
126 
127             MethodWrapper methodWrapper = new MethodWrapper(
128                 _TAG_CLASS, _TAG_DO_END_METHOD,
129                 new Object[] {
130                     new BooleanWrapper(action), windowStateWrapper,
131                     portletModeWrapper, varWrapper, varImplWrapper,
132                     secureWrapper, portletNameWrapper, anchorWrapper,
133                     encryptWrapper, new LongWrapper(doAsUserId),
134                     portletConfigurationWrapper, paramsWrapper,
135                     new BooleanWrapper(writeOutput), pageContext
136                 });
137 
138             returnObj = MethodInvoker.invoke(methodWrapper);
139         }
140         catch (Exception e) {
141             _log.error(e, e);
142 
143             throw e;
144         }
145         finally {
146             Thread.currentThread().setContextClassLoader(contextClassLoader);
147         }
148 
149         if (returnObj != null) {
150             return returnObj.toString();
151         }
152         else {
153             return StringPool.BLANK;
154         }
155     }
156 
157     public int doEndTag() throws JspException {
158         try {
159             doTag(
160                 isAction(), _windowState, _portletMode, _var, _varImpl, _secure,
161                 _portletName, _anchor, _encrypt, _doAsUserId,
162                 _portletConfiguration, getParams(), true, pageContext);
163         }
164         catch (Exception e) {
165             if (e instanceof JspException) {
166                 throw (JspException)e;
167             }
168             else {
169                 throw new JspException(e);
170             }
171         }
172         finally {
173             clearParams();
174         }
175 
176         return EVAL_PAGE;
177     }
178 
179     public boolean isAction() {
180         return _ACTION;
181     }
182 
183     public void setWindowState(String windowState) {
184         _windowState = windowState;
185     }
186 
187     public void setPortletMode(String portletMode) {
188         _portletMode = portletMode;
189     }
190 
191     public void setVar(String var) {
192         _var = var;
193     }
194 
195     public void setVarImpl(String varImpl) {
196         _varImpl = varImpl;
197     }
198 
199     public void setSecure(boolean secure) {
200         _secure = Boolean.valueOf(secure);
201     }
202 
203     public void setPortletName(String portletName) {
204         _portletName = portletName;
205     }
206 
207     public void setAnchor(boolean anchor) {
208         _anchor = Boolean.valueOf(anchor);
209     }
210 
211     public void setEncrypt(boolean encrypt) {
212         _encrypt = Boolean.valueOf(encrypt);
213     }
214 
215     public void setDoAsUserId(long doAsUserId) {
216         _doAsUserId = doAsUserId;
217     }
218 
219     public void setPortletConfiguration(boolean portletConfiguration) {
220         _portletConfiguration = Boolean.valueOf(portletConfiguration);
221     }
222 
223     private static final String _TAG_CLASS =
224         "com.liferay.portal.servlet.taglib.portlet.ActionURLTagUtil";
225 
226     private static final String _TAG_DO_END_METHOD = "doEndTag";
227 
228     private static final boolean _ACTION = true;
229 
230     private static Log _log = LogFactory.getLog(ActionURLTag.class);
231 
232     private String _windowState;
233     private String _portletMode;
234     private String _var;
235     private String _varImpl;
236     private Boolean _secure;
237     private String _portletName;
238     private Boolean _anchor;
239     private Boolean _encrypt;
240     private long _doAsUserId;
241     private Boolean _portletConfiguration;
242 
243 }