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