001    /**
002     * Copyright (c) 2000-2013 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.ArrayUtil;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.HtmlUtil;
020    import com.liferay.portal.kernel.util.HttpUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    
026    import javax.portlet.PortletURL;
027    
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.jsp.JspException;
030    import javax.servlet.jsp.JspWriter;
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                            JspWriter jspWriter = pageContext.getOut();
050    
051                            jspWriter.write(params);
052                    }
053    
054                    return params;
055            }
056    
057            @Override
058            public int doEndTag() throws JspException {
059                    try {
060                            doTag(_varImpl, pageContext);
061    
062                            return EVAL_PAGE;
063                    }
064                    catch (Exception e) {
065                            throw new JspException(e);
066                    }
067            }
068    
069            public void setVarImpl(String varImpl) {
070                    _varImpl = varImpl;
071            }
072    
073            private static String _toParamsString(
074                            PortletURL portletURL, PageContext pageContext)
075                    throws Exception {
076    
077                    StringBundler sb = new StringBundler();
078    
079                    String url = portletURL.toString();
080    
081                    HttpServletRequest request =
082                            (HttpServletRequest)pageContext.getRequest();
083    
084                    if (ParamUtil.getBoolean(request, "wsrp")) {
085                            int x = url.indexOf("/wsrp_rewrite");
086    
087                            url = url.substring(0, x);
088                    }
089    
090                    String queryString = HttpUtil.getQueryString(url);
091    
092                    String[] parameters = StringUtil.split(queryString, CharPool.AMPERSAND);
093    
094                    for (String parameter : parameters) {
095                            if (parameter.length() > 0) {
096                                    String[] kvp = StringUtil.split(parameter, CharPool.EQUAL);
097    
098                                    if (ArrayUtil.isNotEmpty(kvp)) {
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    }