1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.taglib.util;
24  
25  import com.liferay.portal.kernel.servlet.StringServletResponse;
26  import com.liferay.portal.util.WebKeys;
27  import com.liferay.util.servlet.DynamicServletRequest;
28  
29  import java.io.IOException;
30  
31  import java.util.LinkedHashMap;
32  import java.util.Map;
33  
34  import javax.servlet.RequestDispatcher;
35  import javax.servlet.ServletContext;
36  import javax.servlet.ServletException;
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  import javax.servlet.jsp.tagext.BodyTagSupport;
40  
41  /**
42   * <a href="ParamAndPropertyAncestorTagImpl.java.html"><b><i>View Source</i></b>
43   * </a>
44   *
45   * @author Brian Wing Shun Chan
46   */
47  public class ParamAndPropertyAncestorTagImpl
48      extends BodyTagSupport implements ParamAncestorTag, PropertyAncestorTag {
49  
50      public void addParam(String name, String value) {
51          if (_params == null) {
52              _params = new LinkedHashMap<String, String[]>();
53          }
54  
55          String[] values = _params.get(name);
56  
57          if (values == null) {
58              values = new String[] {value};
59          }
60          else {
61              String[] newValues = new String[values.length + 1];
62  
63              System.arraycopy(values, 0, newValues, 0, values.length);
64  
65              newValues[newValues.length - 1] = value;
66  
67              values = newValues;
68          }
69  
70          _params.put(name, values);
71      }
72  
73      public void addProperty(String name, String value) {
74          if (_properties == null) {
75              _properties = new LinkedHashMap<String, String[]>();
76          }
77  
78          String[] values = _properties.get(name);
79  
80          if (values == null) {
81              values = new String[] {value};
82          }
83          else {
84              String[] newValues = new String[values.length + 1];
85  
86              System.arraycopy(values, 0, newValues, 0, values.length);
87  
88              newValues[newValues.length - 1] = value;
89  
90              values = newValues;
91          }
92  
93          _properties.put(name, values);
94      }
95  
96      public void clearParams() {
97          if (_params != null) {
98              _params.clear();
99          }
100     }
101 
102     public void clearProperties() {
103         if (_properties != null) {
104             _properties.clear();
105         }
106     }
107 
108     public Map<String, String[]> getParams() {
109         return _params;
110     }
111 
112     public Map<String, String[]> getProperties() {
113         return _properties;
114     }
115 
116     public ServletContext getServletContext() {
117         HttpServletRequest request =
118             (HttpServletRequest)pageContext.getRequest();
119 
120         return (ServletContext)request.getAttribute(WebKeys.CTX);
121     }
122 
123     public HttpServletRequest getServletRequest() {
124         HttpServletRequest request =
125             (HttpServletRequest)pageContext.getRequest();
126 
127         if (_params != null) {
128             request = new DynamicServletRequest(request, _params);
129         }
130 
131         return request;
132     }
133 
134     public StringServletResponse getServletResponse() {
135         return new StringServletResponse(
136             (HttpServletResponse)pageContext.getResponse());
137     }
138 
139     public void include(String path) throws IOException, ServletException {
140         ServletContext servletContext = getServletContext();
141         HttpServletRequest request = getServletRequest();
142         StringServletResponse stringResponse = getServletResponse();
143 
144         RequestDispatcher requestDispatcher =
145             servletContext.getRequestDispatcher(path);
146 
147         requestDispatcher.include(request, stringResponse);
148 
149         pageContext.getOut().print(stringResponse.getString());
150     }
151 
152     private Map<String, String[]> _params;
153     private Map<String, String[]> _properties;
154 
155 }