1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.taglib.util;
16  
17  import com.liferay.portal.kernel.servlet.StringServletResponse;
18  import com.liferay.portal.kernel.util.WebKeys;
19  import com.liferay.util.servlet.DynamicServletRequest;
20  
21  import java.io.IOException;
22  
23  import java.util.LinkedHashMap;
24  import java.util.Map;
25  
26  import javax.servlet.RequestDispatcher;
27  import javax.servlet.ServletContext;
28  import javax.servlet.ServletException;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import javax.servlet.jsp.tagext.BodyTagSupport;
32  
33  /**
34   * <a href="ParamAndPropertyAncestorTagImpl.java.html"><b><i>View Source</i></b>
35   * </a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class ParamAndPropertyAncestorTagImpl
40      extends BodyTagSupport implements ParamAncestorTag, PropertyAncestorTag {
41  
42      public void addParam(String name, String value) {
43          if (_params == null) {
44              _params = new LinkedHashMap<String, String[]>();
45          }
46  
47          String[] values = _params.get(name);
48  
49          if (values == null) {
50              values = new String[] {value};
51          }
52          else {
53              String[] newValues = new String[values.length + 1];
54  
55              System.arraycopy(values, 0, newValues, 0, values.length);
56  
57              newValues[newValues.length - 1] = value;
58  
59              values = newValues;
60          }
61  
62          _params.put(name, values);
63      }
64  
65      public void addProperty(String name, String value) {
66          if (_properties == null) {
67              _properties = new LinkedHashMap<String, String[]>();
68          }
69  
70          String[] values = _properties.get(name);
71  
72          if (values == null) {
73              values = new String[] {value};
74          }
75          else {
76              String[] newValues = new String[values.length + 1];
77  
78              System.arraycopy(values, 0, newValues, 0, values.length);
79  
80              newValues[newValues.length - 1] = value;
81  
82              values = newValues;
83          }
84  
85          _properties.put(name, values);
86      }
87  
88      public void clearParams() {
89          if (_params != null) {
90              _params.clear();
91          }
92      }
93  
94      public void clearProperties() {
95          if (_properties != null) {
96              _properties.clear();
97          }
98      }
99  
100     public Map<String, String[]> getParams() {
101         return _params;
102     }
103 
104     public Map<String, String[]> getProperties() {
105         return _properties;
106     }
107 
108     public ServletContext getServletContext() {
109         HttpServletRequest request =
110             (HttpServletRequest)pageContext.getRequest();
111 
112         ServletContext servletContext = (ServletContext)request.getAttribute(
113             WebKeys.CTX);
114 
115         if (servletContext == null) {
116             servletContext = pageContext.getServletContext();
117         }
118 
119         return servletContext;
120     }
121 
122     public HttpServletRequest getServletRequest() {
123         HttpServletRequest request =
124             (HttpServletRequest)pageContext.getRequest();
125 
126         if (_params != null) {
127             request = new DynamicServletRequest(request, _params);
128         }
129 
130         return request;
131     }
132 
133     public StringServletResponse getServletResponse() {
134         return new StringServletResponse(
135             (HttpServletResponse)pageContext.getResponse());
136     }
137 
138     public void include(String path) throws IOException, ServletException {
139         ServletContext servletContext = getServletContext();
140         HttpServletRequest request = getServletRequest();
141         StringServletResponse stringResponse = getServletResponse();
142 
143         RequestDispatcher requestDispatcher =
144             servletContext.getRequestDispatcher(path);
145 
146         requestDispatcher.include(request, stringResponse);
147 
148         pageContext.getOut().print(stringResponse.getString());
149     }
150 
151     private Map<String, String[]> _params;
152     private Map<String, String[]> _properties;
153 
154 }