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