001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.portlet;
016    
017    import com.liferay.portal.kernel.util.CharPool;
018    import com.liferay.portal.kernel.util.HtmlUtil;
019    import com.liferay.portal.kernel.util.HttpUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    
025    import javax.portlet.PortletURL;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.jsp.JspException;
029    import javax.servlet.jsp.JspWriter;
030    import javax.servlet.jsp.PageContext;
031    import javax.servlet.jsp.tagext.TagSupport;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class RenderURLParamsTag extends TagSupport {
037    
038            public static String doTag(String varImpl, PageContext pageContext)
039                    throws Exception {
040    
041                    PortletURL portletURL = (PortletURL)pageContext.getAttribute(varImpl);
042    
043                    String params = StringPool.BLANK;
044    
045                    if (portletURL != null) {
046                            params = _toParamsString(portletURL, pageContext);
047    
048                            JspWriter jspWriter = pageContext.getOut();
049    
050                            jspWriter.write(params);
051                    }
052    
053                    return params;
054            }
055    
056            @Override
057            public int doEndTag() throws JspException {
058                    try {
059                            doTag(_varImpl, pageContext);
060    
061                            return EVAL_PAGE;
062                    }
063                    catch (Exception e) {
064                            throw new JspException(e);
065                    }
066            }
067    
068            public void setVarImpl(String varImpl) {
069                    _varImpl = varImpl;
070            }
071    
072            private static String _toParamsString(
073                            PortletURL portletURL, PageContext pageContext)
074                    throws Exception {
075    
076                    StringBundler sb = new StringBundler();
077    
078                    String url = portletURL.toString();
079    
080                    HttpServletRequest request =
081                            (HttpServletRequest)pageContext.getRequest();
082    
083                    if (ParamUtil.getBoolean(request, "wsrp")) {
084                            int x = url.indexOf("/wsrp_rewrite");
085    
086                            url = url.substring(0, x);
087                    }
088    
089                    String queryString = HttpUtil.getQueryString(url);
090    
091                    String[] parameters = StringUtil.split(queryString, CharPool.AMPERSAND);
092    
093                    for (String parameter : parameters) {
094                            if (parameter.length() > 0) {
095                                    String[] kvp = StringUtil.split(parameter, CharPool.EQUAL);
096    
097                                    if ((kvp != null) && (kvp.length > 0)) {
098                                            String key = kvp[0];
099                                            String value = StringPool.BLANK;
100    
101                                            if (kvp.length > 1) {
102                                                    value = kvp[1];
103                                            }
104    
105                                            value = HttpUtil.decodeURL(value);
106    
107                                            sb.append("<input name=\"");
108                                            sb.append(key);
109                                            sb.append("\" type=\"hidden\" value=\"");
110                                            sb.append(HtmlUtil.escapeAttribute(value));
111                                            sb.append("\" />");
112                                    }
113                            }
114                    }
115    
116                    return sb.toString();
117            }
118    
119            private String _varImpl;
120    
121    }