1   /**
2    * Copyright (c) 2000-2008 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.ServletRequest;
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  import javax.servlet.jsp.tagext.BodyTagSupport;
41  
42  /**
43   * <a href="ParamAndPropertyAncestorTagImpl.java.html"><b><i>View Source</i></b>
44   * </a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class ParamAndPropertyAncestorTagImpl
50      extends BodyTagSupport implements ParamAncestorTag, PropertyAncestorTag {
51  
52      public void addParam(String name, String value) {
53          if (_params == null) {
54              _params = new LinkedHashMap<String, String[]>();
55          }
56  
57          String[] values = _params.get(name);
58  
59          if (values == null) {
60              values = new String[] {value};
61          }
62          else {
63              String[] newValues = new String[values.length + 1];
64  
65              System.arraycopy(values, 0, newValues, 0, values.length);
66  
67              newValues[newValues.length - 1] = value;
68  
69              values = newValues;
70          }
71  
72          _params.put(name, values);
73      }
74  
75      public void addProperty(String name, String value) {
76          if (_properties == null) {
77              _properties = new LinkedHashMap<String, String[]>();
78          }
79  
80          String[] values = _properties.get(name);
81  
82          if (values == null) {
83              values = new String[] {value};
84          }
85          else {
86              String[] newValues = new String[values.length + 1];
87  
88              System.arraycopy(values, 0, newValues, 0, values.length);
89  
90              newValues[newValues.length - 1] = value;
91  
92              values = newValues;
93          }
94  
95          _properties.put(name, values);
96      }
97  
98      public void clearParams() {
99          if (_params != null) {
100             _params.clear();
101         }
102     }
103 
104     public void clearProperties() {
105         if (_properties != null) {
106             _properties.clear();
107         }
108     }
109 
110     public Map<String, String[]> getParams() {
111         return _params;
112     }
113 
114     public Map<String, String[]> getProperties() {
115         return _properties;
116     }
117 
118     public ServletContext getServletContext() {
119         ServletRequest req = pageContext.getRequest();
120 
121         return (ServletContext)req.getAttribute(WebKeys.CTX);
122     }
123 
124     public HttpServletRequest getServletRequest() {
125         HttpServletRequest req = (HttpServletRequest)pageContext.getRequest();
126 
127         if (_params != null) {
128             req = new DynamicServletRequest(req, _params);
129         }
130 
131         return req;
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 ctx = getServletContext();
141         HttpServletRequest req = getServletRequest();
142         StringServletResponse res = getServletResponse();
143 
144         RequestDispatcher rd = ctx.getRequestDispatcher(path);
145 
146         rd.include(req, res);
147 
148         pageContext.getOut().print(res.getString());
149     }
150 
151     private Map<String, String[]> _params;
152     private Map<String, String[]> _properties;
153 
154 }