001
014
015 package com.liferay.taglib.util;
016
017 import com.liferay.portal.kernel.servlet.DynamicServletRequest;
018 import com.liferay.portal.kernel.servlet.taglib.BaseBodyTagSupport;
019 import com.liferay.portal.kernel.util.WebKeys;
020
021 import java.util.HashSet;
022 import java.util.LinkedHashMap;
023 import java.util.Map;
024 import java.util.Set;
025
026 import javax.servlet.ServletContext;
027 import javax.servlet.http.HttpServletRequest;
028 import javax.servlet.jsp.PageContext;
029
030
034 public class ParamAndPropertyAncestorTagImpl
035 extends BaseBodyTagSupport
036 implements ParamAncestorTag, PropertyAncestorTag {
037
038 public void addParam(String name, String value) {
039 if (_dynamicServletRequest == null) {
040 _dynamicServletRequest = new DynamicServletRequest(request);
041
042 request = _dynamicServletRequest;
043 }
044
045 Map<String, String[]> params =
046 _dynamicServletRequest.getDynamicParameterMap();
047
048
049
050 if (!_allowEmptyParam && ((value == null) || (value.length() == 0))) {
051 params.remove(name);
052
053 if (_removedParameterNames == null) {
054 _removedParameterNames = new HashSet<String>();
055 }
056
057 _removedParameterNames.add(name);
058
059 return;
060 }
061
062 String[] values = params.get(name);
063
064 if (values == null) {
065 values = new String[] {value};
066 }
067 else {
068 String[] newValues = new String[values.length + 1];
069
070 System.arraycopy(values, 0, newValues, 0, values.length);
071
072 newValues[newValues.length - 1] = value;
073
074 values = newValues;
075 }
076
077 params.put(name, values);
078 }
079
080 public void addProperty(String name, String value) {
081 if (_properties == null) {
082 _properties = new LinkedHashMap<String, String[]>();
083 }
084
085 String[] values = _properties.get(name);
086
087 if (values == null) {
088 values = new String[] {value};
089 }
090 else {
091 String[] newValues = new String[values.length + 1];
092
093 System.arraycopy(values, 0, newValues, 0, values.length);
094
095 newValues[newValues.length - 1] = value;
096
097 values = newValues;
098 }
099
100 _properties.put(name, values);
101 }
102
103 public void clearParams() {
104 if (_dynamicServletRequest != null) {
105 Map<String, String[]> params =
106 _dynamicServletRequest.getDynamicParameterMap();
107
108 params.clear();
109
110 request = (HttpServletRequest)_dynamicServletRequest.getRequest();
111
112 _dynamicServletRequest = null;
113 }
114
115 if (_removedParameterNames != null) {
116 _removedParameterNames.clear();
117 }
118 }
119
120 public void clearProperties() {
121 if (_properties != null) {
122 _properties.clear();
123 }
124 }
125
126 public Map<String, String[]> getParams() {
127 if (_dynamicServletRequest != null) {
128 return _dynamicServletRequest.getDynamicParameterMap();
129 }
130 else {
131 return null;
132 }
133 }
134
135 public Map<String, String[]> getProperties() {
136 return _properties;
137 }
138
139 public Set<String> getRemovedParameterNames() {
140 return _removedParameterNames;
141 }
142
143 public boolean isAllowEmptyParam() {
144 return _allowEmptyParam;
145 }
146
147 @Override
148 public void release() {
149 super.release();
150
151 request = null;
152 servletContext = null;
153
154 _allowEmptyParam = false;
155 _properties = null;
156 _removedParameterNames = null;
157 }
158
159 public void setAllowEmptyParam(boolean allowEmptyParam) {
160 _allowEmptyParam = allowEmptyParam;
161 }
162
163 @Override
164 public void setPageContext(PageContext pageContext) {
165 super.setPageContext(pageContext);
166
167 request = (HttpServletRequest)pageContext.getRequest();
168
169 servletContext = (ServletContext)request.getAttribute(WebKeys.CTX);
170
171 if (servletContext == null) {
172 servletContext = pageContext.getServletContext();
173 }
174 }
175
176 public void setServletContext(ServletContext servletContext) {
177 this.servletContext = servletContext;
178 }
179
180 protected HttpServletRequest request;
181 protected ServletContext servletContext;
182
183 private boolean _allowEmptyParam;
184 private DynamicServletRequest _dynamicServletRequest;
185 private Map<String, String[]> _properties;
186 private Set<String> _removedParameterNames;
187
188 }