1   /**
2    * Copyright (c) 2000-2007 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="ParamAncestorTagImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class ParamAncestorTagImpl
49      extends BodyTagSupport implements ParamAncestorTag {
50  
51      public void addParam(String name, String value) {
52          if (_params == null) {
53              _params = new LinkedHashMap();
54          }
55  
56          String[] values = (String[])_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 clearParams() {
75          if (_params != null) {
76              _params.clear();
77          }
78      }
79  
80      public Map getParams() {
81          return _params;
82      }
83  
84      public ServletContext getServletContext() {
85          ServletRequest req = pageContext.getRequest();
86  
87          return (ServletContext)req.getAttribute(WebKeys.CTX);
88      }
89  
90      public HttpServletRequest getServletRequest() {
91          HttpServletRequest req = (HttpServletRequest)pageContext.getRequest();
92  
93          if (_params != null) {
94              req = new DynamicServletRequest(req, _params);
95          }
96  
97          return req;
98      }
99  
100     public StringServletResponse getServletResponse() {
101         return new StringServletResponse(
102             (HttpServletResponse)pageContext.getResponse());
103     }
104 
105     public void include(String path) throws IOException, ServletException {
106         ServletContext ctx = getServletContext();
107         HttpServletRequest req = getServletRequest();
108         StringServletResponse res = getServletResponse();
109 
110         RequestDispatcher rd = ctx.getRequestDispatcher(path);
111 
112         rd.include(req, res);
113 
114         pageContext.getOut().print(res.getString());
115     }
116 
117     private Map _params;
118 
119 }