1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.servlet.taglib.portlet;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.HttpUtil;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  
29  import java.util.StringTokenizer;
30  
31  import javax.portlet.PortletURL;
32  
33  import javax.servlet.jsp.JspException;
34  import javax.servlet.jsp.PageContext;
35  
36  /**
37   * <a href="RenderURLParamsTagUtil.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class RenderURLParamsTagUtil {
43  
44      public static String doEndTag(String varImpl, PageContext pageContext)
45          throws JspException {
46  
47          try {
48              PortletURL portletURL =
49                  (PortletURL)pageContext.getAttribute(varImpl);
50  
51              String params = StringPool.BLANK;
52  
53              if (portletURL != null) {
54                  params = _toParamsString(portletURL);
55  
56                  pageContext.getOut().print(params);
57              }
58  
59              return params;
60          }
61          catch (Exception e) {
62              _log.error(e, e);
63  
64              throw new JspException(e);
65          }
66      }
67  
68      private static String _toParamsString(PortletURL portletURL)
69          throws Exception {
70  
71          StringBuilder sb = new StringBuilder();
72  
73          String url = portletURL.toString();
74  
75          String queryString = HttpUtil.getQueryString(url);
76  
77          StringTokenizer st = new StringTokenizer(
78              queryString, StringPool.AMPERSAND);
79  
80          while (st.hasMoreTokens()) {
81              String token = st.nextToken();
82  
83              if (Validator.isNotNull(token)) {
84                  String[] kvp = StringUtil.split(token, StringPool.EQUAL);
85  
86                  if ((kvp != null) && (kvp.length > 0)) {
87                      String key = kvp[0];
88                      String value = StringPool.BLANK;
89  
90                      if (kvp.length > 1) {
91                          value = kvp[1];
92                      }
93  
94                      value = HttpUtil.decodeURL(value);
95  
96                      sb.append("<input name=\"");
97                      sb.append(key);
98                      sb.append("\" type=\"hidden\" value=\"");
99                      sb.append(value);
100                     sb.append("\" />");
101                 }
102             }
103         }
104 
105         return sb.toString();
106     }
107 
108     private static Log _log = LogFactoryUtil.getLog(ActionURLTagUtil.class);
109 
110 }