001
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
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 }