1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.taglib.portlet;
16  
17  import com.liferay.portal.kernel.util.HtmlUtil;
18  import com.liferay.portal.kernel.util.HttpUtil;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.StringBundler;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  
25  import java.util.StringTokenizer;
26  
27  import javax.portlet.PortletURL;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.jsp.JspException;
31  import javax.servlet.jsp.PageContext;
32  import javax.servlet.jsp.tagext.TagSupport;
33  
34  /**
35   * <a href="RenderURLParamsTag.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class RenderURLParamsTag extends TagSupport {
40  
41      public static String doTag(String varImpl, PageContext pageContext)
42          throws Exception {
43  
44          PortletURL portletURL = (PortletURL)pageContext.getAttribute(varImpl);
45  
46          String params = StringPool.BLANK;
47  
48          if (portletURL != null) {
49              params = _toParamsString(portletURL, pageContext);
50  
51              pageContext.getOut().print(params);
52          }
53  
54          return params;
55      }
56  
57      public int doEndTag() throws JspException {
58          try {
59              doTag(_varImpl, pageContext);
60  
61              return EVAL_PAGE;
62          }
63          catch (Exception e) {
64              throw new JspException(e);
65          }
66      }
67  
68      public void setVarImpl(String varImpl) {
69          _varImpl = varImpl;
70      }
71  
72      private static String _toParamsString(
73              PortletURL portletURL, PageContext pageContext)
74          throws Exception {
75  
76          StringBundler sb = new StringBundler();
77  
78          String url = portletURL.toString();
79  
80          HttpServletRequest request =
81              (HttpServletRequest)pageContext.getRequest();
82  
83          if (ParamUtil.getBoolean(request, "wsrp")) {
84              int x = url.indexOf("/wsrp_rewrite");
85  
86              url = url.substring(0, x);
87          }
88  
89          String queryString = HttpUtil.getQueryString(url);
90  
91          StringTokenizer st = new StringTokenizer(
92              queryString, StringPool.AMPERSAND);
93  
94          while (st.hasMoreTokens()) {
95              String token = st.nextToken();
96  
97              if (Validator.isNotNull(token)) {
98                  String[] kvp = StringUtil.split(token, StringPool.EQUAL);
99  
100                 if ((kvp != null) && (kvp.length > 0)) {
101                     String key = kvp[0];
102                     String value = StringPool.BLANK;
103 
104                     if (kvp.length > 1) {
105                         value = kvp[1];
106                     }
107 
108                     value = HttpUtil.decodeURL(value);
109 
110                     sb.append("<input name=\"");
111                     sb.append(key);
112                     sb.append("\" type=\"hidden\" value=\"");
113                     sb.append(HtmlUtil.escapeAttribute(value));
114                     sb.append("\" />");
115                 }
116             }
117         }
118 
119         return sb.toString();
120     }
121 
122     private String _varImpl;
123 
124 }