1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
48  public class ParamAndPropertyAncestorTagImpl
49      extends BodyTagSupport implements ParamAncestorTag, PropertyAncestorTag {
50  
51      public void addParam(String name, String value) {
52          if (_params == null) {
53              _params = new LinkedHashMap<String, String[]>();
54          }
55  
56          String[] values = _params.get(name);
57  
58          if (values == null) {
59              values = new String[] {value};
60          }
61          else {
62              String[] newValues = new String[values.length + 1];
63  
64              System.arraycopy(values, 0, newValues, 0, values.length);
65  
66              newValues[newValues.length - 1] = value;
67  
68              values = newValues;
69          }
70  
71          _params.put(name, values);
72      }
73  
74      public void addProperty(String name, String value) {
75          if (_properties == null) {
76              _properties = new LinkedHashMap<String, String[]>();
77          }
78  
79          String[] values = _properties.get(name);
80  
81          if (values == null) {
82              values = new String[] {value};
83          }
84          else {
85              String[] newValues = new String[values.length + 1];
86  
87              System.arraycopy(values, 0, newValues, 0, values.length);
88  
89              newValues[newValues.length - 1] = value;
90  
91              values = newValues;
92          }
93  
94          _properties.put(name, values);
95      }
96  
97      public void clearParams() {
98          if (_params != null) {
99              _params.clear();
100         }
101     }
102 
103     public void clearProperties() {
104         if (_properties != null) {
105             _properties.clear();
106         }
107     }
108 
109     public Map<String, String[]> getParams() {
110         return _params;
111     }
112 
113     public Map<String, String[]> getProperties() {
114         return _properties;
115     }
116 
117     public ServletContext getServletContext() {
118         HttpServletRequest request =
119             (HttpServletRequest)pageContext.getRequest();
120 
121         return (ServletContext)request.getAttribute(WebKeys.CTX);
122     }
123 
124     public HttpServletRequest getServletRequest() {
125         HttpServletRequest request =
126             (HttpServletRequest)pageContext.getRequest();
127 
128         if (_params != null) {
129             request = new DynamicServletRequest(request, _params);
130         }
131 
132         return request;
133     }
134 
135     public StringServletResponse getServletResponse() {
136         return new StringServletResponse(
137             (HttpServletResponse)pageContext.getResponse());
138     }
139 
140     public void include(String path) throws IOException, ServletException {
141         ServletContext servletContext = getServletContext();
142         HttpServletRequest request = getServletRequest();
143         StringServletResponse stringResponse = getServletResponse();
144 
145         RequestDispatcher requestDispatcher =
146             servletContext.getRequestDispatcher(path);
147 
148         requestDispatcher.include(request, stringResponse);
149 
150         pageContext.getOut().print(stringResponse.getString());
151     }
152 
153     private Map<String, String[]> _params;
154     private Map<String, String[]> _properties;
155 
156 }