View Javadoc
1   /**
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    * 
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   * 
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  
18  package org.exoplatform.webservice.cs.rest;
19  
20  import java.io.BufferedReader;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.UnsupportedEncodingException;
24  import java.security.Principal;
25  import java.util.Collection;
26  import java.util.Enumeration;
27  import java.util.Iterator;
28  import java.util.Locale;
29  import java.util.Map;
30  
31  import javax.servlet.AsyncContext;
32  import javax.servlet.DispatcherType;
33  import javax.servlet.RequestDispatcher;
34  import javax.servlet.ServletContext;
35  import javax.servlet.ServletException;
36  import javax.servlet.ServletInputStream;
37  import javax.servlet.ServletRequest;
38  import javax.servlet.ServletResponse;
39  import javax.servlet.http.Cookie;
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  import javax.servlet.http.HttpSession;
43  import javax.servlet.http.Part;
44  import javax.ws.rs.core.MultivaluedMap;
45  
46  /**
47   * @author <a href="mailto:andrew00x@gmail.com">Andrey Parfonov</a>
48   * @version $Id: $
49   */
50  @SuppressWarnings("unchecked")
51  public class MockHttpServletRequest implements HttpServletRequest {
52  
53    private String                         method;
54  
55    private int                            length;
56  
57    private InputStream                    data;
58  
59    private MultivaluedMap<String, String> headers;
60  
61    public MockHttpServletRequest(InputStream data,
62                                  int length,
63                                  String method,
64                                  MultivaluedMap<String, String> headers) {
65      this.data = data;
66      this.length = length;
67      this.method = method;
68      this.headers = headers;
69    }
70  
71    public String getAuthType() {
72      return null;
73    }
74  
75    public String getContextPath() {
76      return "test";
77    }
78  
79    public Cookie[] getCookies() {
80      return null;
81    }
82  
83    public long getDateHeader(String arg0) {
84      return 0;
85    }
86  
87    public String getHeader(String arg0) {
88      return headers.getFirst(arg0);
89    }
90  
91    public Enumeration getHeaderNames() {
92      return new EnumerationImpl(headers.keySet().iterator());
93    }
94  
95    public Enumeration getHeaders(String arg0) {
96      return new EnumerationImpl(headers.get(arg0).iterator());
97    }
98  
99    public int getIntHeader(String arg0) {
100     return 0;
101   }
102 
103   public String getMethod() {
104     return method;
105   }
106 
107   public String getPathInfo() {
108     return null;
109   }
110 
111   public String getPathTranslated() {
112     return null;
113   }
114 
115   public String getQueryString() {
116     return null;
117   }
118 
119   public String getRemoteUser() {
120     return null;
121   }
122 
123   public String getRequestURI() {
124     return null;
125   }
126 
127   public StringBuffer getRequestURL() {
128     return null;
129   }
130 
131   public String getRequestedSessionId() {
132     return null;
133   }
134 
135   public String getServletPath() {
136     return null;
137   }
138 
139   public HttpSession getSession() {
140     return null;
141   }
142 
143   public HttpSession getSession(boolean arg0) {
144     return null;
145   }
146 
147   public Principal getUserPrincipal() {
148     return null;
149   }
150 
151   public boolean isRequestedSessionIdFromCookie() {
152     return false;
153   }
154 
155   public boolean isRequestedSessionIdFromURL() {
156     return false;
157   }
158 
159   public boolean isRequestedSessionIdFromUrl() {
160     return false;
161   }
162 
163   public boolean isRequestedSessionIdValid() {
164     return false;
165   }
166 
167   public boolean isUserInRole(String arg0) {
168     return false;
169   }
170 
171   public Object getAttribute(String arg0) {
172     return null;
173   }
174 
175   public Enumeration getAttributeNames() {
176     return null;
177   }
178 
179   public String getCharacterEncoding() {
180     return null;
181   }
182 
183   public int getContentLength() {
184     return length;
185   }
186 
187   public String getContentType() {
188     return headers.getFirst("content-type");
189   }
190 
191   public ServletInputStream getInputStream() throws IOException {
192     return new MockServletInputStream(data);
193   }
194 
195   public Locale getLocale() {
196     return null;
197   }
198 
199   public Enumeration getLocales() {
200     return null;
201   }
202 
203   public String getParameter(String arg0) {
204     return null;
205   }
206 
207   public Map getParameterMap() {
208     return null;
209   }
210 
211   public Enumeration getParameterNames() {
212     return null;
213   }
214 
215   public String[] getParameterValues(String arg0) {
216     return null;
217   }
218 
219   public String getProtocol() {
220     return null;
221   }
222 
223   public BufferedReader getReader() throws IOException {
224     return null;
225   }
226 
227   public String getRealPath(String arg0) {
228     return null;
229   }
230 
231   public String getRemoteAddr() {
232     return null;
233   }
234 
235   public String getRemoteHost() {
236     return null;
237   }
238 
239   public RequestDispatcher getRequestDispatcher(String arg0) {
240     return null;
241   }
242 
243   public String getScheme() {
244     return null;
245   }
246 
247   public String getServerName() {
248     return null;
249   }
250 
251   public int getServerPort() {
252     return 0;
253   }
254 
255   public boolean isSecure() {
256     return false;
257   }
258 
259   public void removeAttribute(String arg0) {
260   }
261 
262   public void setAttribute(String arg0, Object arg1) {
263   }
264 
265   public void setCharacterEncoding(String arg0) throws UnsupportedEncodingException {
266   }
267 
268 public String getLocalAddr() {
269   return null;
270 }
271 
272 public String getLocalName() {
273   return null;
274 }
275 
276 public int getLocalPort() {
277   return 0;
278 }
279 
280 public int getRemotePort() {
281   return 0;
282 }
283 
284 public Part getPart(String arg0) throws IOException, ServletException {
285   return null;
286 }
287 
288  
289 public AsyncContext getAsyncContext() {
290 	// TODO Auto-generated method stub
291 	return null;
292 }
293 
294  
295 public DispatcherType getDispatcherType() {
296 	// TODO Auto-generated method stub
297 	return null;
298 }
299 
300  
301 public ServletContext getServletContext() {
302 	// TODO Auto-generated method stub
303 	return null;
304 }
305 
306  
307 public boolean isAsyncStarted() {
308 	// TODO Auto-generated method stub
309 	return false;
310 }
311 
312  
313 public boolean isAsyncSupported() {
314 	// TODO Auto-generated method stub
315 	return false;
316 }
317 
318  
319 public AsyncContext startAsync() throws IllegalStateException {
320 	// TODO Auto-generated method stub
321 	return null;
322 }
323 
324  
325 public AsyncContext startAsync(ServletRequest arg0, ServletResponse arg1)
326 		throws IllegalStateException {
327 	// TODO Auto-generated method stub
328 	return null;
329 }
330 
331  
332 public boolean authenticate(HttpServletResponse arg0) throws IOException,
333 		ServletException {
334 	// TODO Auto-generated method stub
335 	return false;
336 }
337 
338  
339 public Collection<Part> getParts() throws IOException, ServletException {
340 	// TODO Auto-generated method stub
341 	return null;
342 }
343 
344  
345 public void login(String arg0, String arg1) throws ServletException {
346 	// TODO Auto-generated method stub
347 	
348 }
349 
350  
351 public void logout() throws ServletException {
352 	// TODO Auto-generated method stub
353 	
354 }
355 
356 }
357 
358 @SuppressWarnings("unchecked")
359 class EnumerationImpl implements Enumeration {
360 
361   private final Iterator iter;
362 
363   public EnumerationImpl(Iterator iter) {
364     this.iter = iter;
365   }
366 
367   public boolean hasMoreElements() {
368     return iter.hasNext();
369   }
370 
371   public Object nextElement() {
372     return iter.next();
373   }
374 }
375 
376 class MockServletInputStream extends ServletInputStream {
377 
378   private final InputStream data;
379 
380   public MockServletInputStream(InputStream data) {
381     this.data = data;
382   }
383 
384    
385   public int read() throws IOException {
386     return data.read();
387   }
388 
389 }