View Javadoc

1   package org.apache.velocity.tools.test.blackbox;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.reflect.InvocationHandler;
23  import java.lang.reflect.Method;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.Iterator;
27  import java.util.Map;
28  import org.apache.commons.collections.iterators.IteratorEnumeration;
29  import javax.servlet.ServletContext;
30  import javax.servlet.http.Cookie;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  
34  /**
35   * <p>Helper class for LinkToolTests class</p>
36   *
37   * @author Christopher Schultz
38   * @version $Id$
39   */
40  public class ServletAdaptor implements InvocationHandler
41  {
42      // the params now also serve as a cookie jar for CookieToolTests
43      private Map _params;
44      private String _contextPath;
45      private String _pathInfo;
46  
47      public ServletAdaptor(Map cookies)
48      {
49          this(null, null, cookies);
50      }
51  
52      public ServletAdaptor(String contextPath,
53                            Map params)
54      {
55          this(contextPath, "", params);
56      }
57  
58      public ServletAdaptor(String contextPath, String pathInfo, Map params)
59      {
60          _contextPath = contextPath;
61          if(null == _contextPath)
62          {
63              _contextPath = "";
64          }
65          _pathInfo = pathInfo;
66          if(null == _pathInfo)
67          {
68              _pathInfo = "";
69          }
70  
71          _params = params;
72  
73          if(null == _params)
74          {
75              _params = new HashMap();
76          }
77      }
78  
79      public Object invoke(Object proxy,
80                           Method method,
81                           Object[] args)
82      {
83          Class clazz = method.getDeclaringClass();
84  
85          if(clazz.isAssignableFrom(HttpServletRequest.class))
86          {
87              return request(proxy, method, args);
88          }
89          else if(clazz.isAssignableFrom(HttpServletResponse.class))
90          {
91              return response(proxy, method, args);
92          }
93          else if(clazz.isAssignableFrom(ServletContext.class))
94          {
95              return context(proxy, method, args);
96          }
97          else
98          {
99              throw new IllegalStateException("Unexpected proxy interface: "
100                                             + clazz.getName());
101         }
102     }
103 
104     protected Object response(Object proxy,
105                               Method method,
106                               Object[] args)
107     {
108         String methodName = method.getName();
109 
110         if("encodeURL".equals(methodName)
111            || "encodeUrl".equals(methodName))
112         {
113             // Don't worry about adding ";jsessionid" or anything.
114             return args[0];
115         }
116         else if ("addCookie".equals(methodName))
117         {
118             Cookie c = (Cookie)args[0];
119             if (c.getMaxAge() == 0)
120             {
121                 _params.remove(c.getName());
122             }
123             else
124             {
125                 _params.put(c.getName(), c);
126             }
127             return null;
128         }
129         else
130         {
131             throw new IllegalStateException("Unexpected method call: "
132                                             + method);
133         }
134     }
135 
136     protected Object request(Object proxy,
137                              Method method,
138                              Object[] args)
139     {
140         String methodName = method.getName();
141 
142         if("getContextPath".equals(methodName))
143         {
144             return _contextPath;
145         }
146         else if("getParameter".equals(methodName))
147         {
148             Object value = _params.get(args[0]);
149 
150             if(value instanceof String)
151             {
152                 return value;
153             }
154             else if (value instanceof String[])
155             {
156                 return ((String[])value)[0];
157             }
158             else
159             {
160                 throw new IllegalStateException("Parameter value must be either String or String[].");
161             }
162         }
163         else if("getParameterValues".equals(methodName))
164         {
165             Object value = _params.get(args[0]);
166 
167             if(value instanceof String)
168             {
169                 return new String[] { (String)value };
170             }
171             else if (value instanceof String[])
172             {
173                 return value;
174             }
175             else
176             {
177                 throw new IllegalStateException("Parameter value must be either String or String[].");
178             }
179         }
180         else if("getParameterMap".equals(methodName))
181         {
182             return Collections.unmodifiableMap(_params);
183         }
184         else if("getParameterNames".equals(methodName))
185         {
186             return new IteratorEnumeration(_params.keySet().iterator());
187         }
188         else if("getSession".equals(methodName))
189         {
190             return null;
191         }
192         else if("getAttribute".equals(methodName))
193         {
194             if(((String)args[0]).equals("XHTML"))
195             {
196                 return Boolean.TRUE; // xhtml = true
197             }
198             else
199             {
200                 return null;
201             }
202         }
203         else if ("getScheme".equals(methodName))
204         {
205             return "http";
206         }
207         else if ("getServerPort".equals(methodName))
208         {
209             return new Integer(8081);
210         }
211         else if ("getServerName".equals(methodName))
212         {
213             return "localhost";
214         }
215         else if ("getServletPath".equals(methodName))
216         {
217             return _contextPath;
218         }
219         else if ("getPathInfo".equals(methodName))
220         {
221             return _pathInfo;
222         }
223         else if("getCharacterEncoding".equals(methodName))
224         {
225             return "UTF-8";
226         }
227         else if ("getCookies".equals(methodName))
228         {
229             // just let params double as the cookie store
230             Cookie[] jar = new Cookie[_params.size()];
231             int i = 0;
232             for (Iterator iter = _params.keySet().iterator(); iter.hasNext(); i++)
233             {
234                 Object key = iter.next();
235                 Object val = _params.get(key);
236                 if (val instanceof Cookie)
237                 {
238                     jar[i] = (Cookie)val;
239                 }
240                 else
241                 {
242                     String name = String.valueOf(key);
243                     String value = String.valueOf(val);
244                     jar[i] = new Cookie(name, value);
245                     _params.put(name, jar[i]);
246                 }
247             }
248             return jar;
249         }
250         else
251         {
252             throw new IllegalStateException("Unexpected method call: "
253                                             + method);
254         }
255     }
256 
257     protected Object context(Object proxy,
258                              Method method,
259                              Object[] args)
260     {
261         String methodName = method.getName();
262 
263         if("getContextPath".equals(methodName))
264         {
265             return _contextPath;
266         }
267         else
268         {
269             throw new IllegalStateException("Unexpected method call: "
270                                             + methodName);
271         }
272     }
273 
274 }