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.HtmlUtil;
25  import com.liferay.portal.kernel.util.HttpUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  
30  import java.util.StringTokenizer;
31  
32  import javax.portlet.PortletURL;
33  
34  import javax.servlet.jsp.JspException;
35  import javax.servlet.jsp.PageContext;
36  
37  /**
38   * <a href="RenderURLParamsTagUtil.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class RenderURLParamsTagUtil {
44  
45      public static String doEndTag(String varImpl, PageContext pageContext)
46          throws JspException {
47  
48          try {
49              PortletURL portletURL =
50                  (PortletURL)pageContext.getAttribute(varImpl);
51  
52              String params = StringPool.BLANK;
53  
54              if (portletURL != null) {
55                  params = _toParamsString(portletURL);
56  
57                  pageContext.getOut().print(params);
58              }
59  
60              return params;
61          }
62          catch (Exception e) {
63              _log.error(e, e);
64  
65              throw new JspException(e);
66          }
67      }
68  
69      private static String _toParamsString(PortletURL portletURL)
70          throws Exception {
71  
72          StringBuilder sb = new StringBuilder();
73  
74          String url = portletURL.toString();
75  
76          String queryString = HttpUtil.getQueryString(url);
77  
78          StringTokenizer st = new StringTokenizer(
79              queryString, StringPool.AMPERSAND);
80  
81          while (st.hasMoreTokens()) {
82              String token = st.nextToken();
83  
84              if (Validator.isNotNull(token)) {
85                  String[] kvp = StringUtil.split(token, StringPool.EQUAL);
86  
87                  if ((kvp != null) && (kvp.length > 0)) {
88                      String key = kvp[0];
89                      String value = StringPool.BLANK;
90  
91                      if (kvp.length > 1) {
92                          value = kvp[1];
93                      }
94  
95                      value = HttpUtil.decodeURL(value);
96  
97                      sb.append("<input name=\"");
98                      sb.append(key);
99                      sb.append("\" type=\"hidden\" value=\"");
100                     sb.append(HtmlUtil.escape(value));
101                     sb.append("\" />");
102                 }
103             }
104         }
105 
106         return sb.toString();
107     }
108 
109     private static Log _log = LogFactoryUtil.getLog(ActionURLTagUtil.class);
110 
111 }