001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.taglib.portlet;
016    
017    import com.liferay.portal.kernel.util.HtmlUtil;
018    import com.liferay.portal.kernel.util.HttpUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    
025    import java.util.StringTokenizer;
026    
027    import javax.portlet.PortletURL;
028    
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.jsp.JspException;
031    import javax.servlet.jsp.PageContext;
032    import javax.servlet.jsp.tagext.TagSupport;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class RenderURLParamsTag extends TagSupport {
038    
039            public static String doTag(String varImpl, PageContext pageContext)
040                    throws Exception {
041    
042                    PortletURL portletURL = (PortletURL)pageContext.getAttribute(varImpl);
043    
044                    String params = StringPool.BLANK;
045    
046                    if (portletURL != null) {
047                            params = _toParamsString(portletURL, pageContext);
048    
049                            pageContext.getOut().print(params);
050                    }
051    
052                    return params;
053            }
054    
055            public int doEndTag() throws JspException {
056                    try {
057                            doTag(_varImpl, pageContext);
058    
059                            return EVAL_PAGE;
060                    }
061                    catch (Exception e) {
062                            throw new JspException(e);
063                    }
064            }
065    
066            public void setVarImpl(String varImpl) {
067                    _varImpl = varImpl;
068            }
069    
070            private static String _toParamsString(
071                            PortletURL portletURL, PageContext pageContext)
072                    throws Exception {
073    
074                    StringBundler sb = new StringBundler();
075    
076                    String url = portletURL.toString();
077    
078                    HttpServletRequest request =
079                            (HttpServletRequest)pageContext.getRequest();
080    
081                    if (ParamUtil.getBoolean(request, "wsrp")) {
082                            int x = url.indexOf("/wsrp_rewrite");
083    
084                            url = url.substring(0, x);
085                    }
086    
087                    String queryString = HttpUtil.getQueryString(url);
088    
089                    StringTokenizer st = new StringTokenizer(
090                            queryString, StringPool.AMPERSAND);
091    
092                    while (st.hasMoreTokens()) {
093                            String token = st.nextToken();
094    
095                            if (Validator.isNotNull(token)) {
096                                    String[] kvp = StringUtil.split(token, StringPool.EQUAL);
097    
098                                    if ((kvp != null) && (kvp.length > 0)) {
099                                            String key = kvp[0];
100                                            String value = StringPool.BLANK;
101    
102                                            if (kvp.length > 1) {
103                                                    value = kvp[1];
104                                            }
105    
106                                            value = HttpUtil.decodeURL(value);
107    
108                                            sb.append("<input name=\"");
109                                            sb.append(key);
110                                            sb.append("\" type=\"hidden\" value=\"");
111                                            sb.append(HtmlUtil.escapeAttribute(value));
112                                            sb.append("\" />");
113                                    }
114                            }
115                    }
116    
117                    return sb.toString();
118            }
119    
120            private String _varImpl;
121    
122    }