001    /**
002     * Copyright (c) 2000-present 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(PortletURL portletURL, PageContext pageContext)
040                    throws Exception {
041    
042                    return _doTag(portletURL, null, pageContext);
043            }
044    
045            public static String doTag(String varImpl, PageContext pageContext)
046                    throws Exception {
047    
048                    return _doTag(null, varImpl, pageContext);
049            }
050    
051            @Override
052            public int doEndTag() throws JspException {
053                    try {
054                            _doTag(_portletURL, _varImpl, pageContext);
055    
056                            return EVAL_PAGE;
057                    }
058                    catch (Exception e) {
059                            throw new JspException(e);
060                    }
061            }
062    
063            public void setPortletURL(PortletURL portletURL) {
064                    _portletURL = portletURL;
065            }
066    
067            public void setVarImpl(String varImpl) {
068                    _varImpl = varImpl;
069            }
070    
071            private static String _doTag(
072                            PortletURL portletURL, String varImpl, PageContext pageContext)
073                    throws Exception {
074    
075                    if (portletURL == null) {
076                            portletURL = (PortletURL)pageContext.getAttribute(varImpl);
077                    }
078    
079                    String paramsString = StringPool.BLANK;
080    
081                    if (portletURL != null) {
082                            paramsString = _toParamsString(portletURL, pageContext);
083    
084                            JspWriter jspWriter = pageContext.getOut();
085    
086                            jspWriter.write(paramsString);
087                    }
088    
089                    return paramsString;
090            }
091    
092            private static String _toParamsString(
093                            PortletURL portletURL, PageContext pageContext)
094                    throws Exception {
095    
096                    StringBundler sb = new StringBundler();
097    
098                    String url = portletURL.toString();
099    
100                    HttpServletRequest request =
101                            (HttpServletRequest)pageContext.getRequest();
102    
103                    if (ParamUtil.getBoolean(request, "wsrp")) {
104                            int x = url.indexOf("/wsrp_rewrite");
105    
106                            url = url.substring(0, x);
107                    }
108    
109                    String queryString = HttpUtil.getQueryString(url);
110    
111                    String[] parameters = StringUtil.split(queryString, CharPool.AMPERSAND);
112    
113                    for (String parameter : parameters) {
114                            if (parameter.length() > 0) {
115                                    String[] kvp = StringUtil.split(parameter, CharPool.EQUAL);
116    
117                                    if (ArrayUtil.isNotEmpty(kvp)) {
118                                            String key = kvp[0];
119                                            String value = StringPool.BLANK;
120    
121                                            if (kvp.length > 1) {
122                                                    value = kvp[1];
123                                            }
124    
125                                            value = HttpUtil.decodeURL(value);
126    
127                                            sb.append("<input name=\"");
128                                            sb.append(key);
129                                            sb.append("\" type=\"hidden\" value=\"");
130                                            sb.append(HtmlUtil.escapeAttribute(value));
131                                            sb.append("\" />");
132                                    }
133                            }
134                    }
135    
136                    return sb.toString();
137            }
138    
139            private PortletURL _portletURL;
140            private String _varImpl;
141    
142    }