View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.access.dummy;
15  
16  import ch.qos.logback.access.AccessConstants;
17  
18  import javax.servlet.*;
19  import javax.servlet.http.*;
20  import java.io.BufferedReader;
21  import java.io.IOException;
22  import java.io.UnsupportedEncodingException;
23  import java.security.Principal;
24  import java.util.*;
25  
26  public class DummyRequest implements HttpServletRequest {
27  
28      public final static String DUMMY_CONTENT_STRING = "request contents";
29      public final static byte[] DUMMY_CONTENT_BYTES = DUMMY_CONTENT_STRING.getBytes();
30  
31      public static final Map<String, Object> DUMMY_DEFAULT_ATTR_MAP = new HashMap<String, Object>();
32  
33      public static final String DUMMY_RESPONSE_CONTENT_STRING = "response contents";
34      public static final byte[] DUMMY_RESPONSE_CONTENT_BYTES = DUMMY_RESPONSE_CONTENT_STRING.getBytes();
35  
36      Hashtable<String, String> headerMap;
37      Hashtable<String, String[]> parameterMap;
38      
39      String uri;
40      Map<String, Object> attributes;
41  
42      static {
43          DUMMY_DEFAULT_ATTR_MAP.put("testKey", "testKey");
44          DUMMY_DEFAULT_ATTR_MAP.put(AccessConstants.LB_INPUT_BUFFER, DUMMY_CONTENT_BYTES);
45          DUMMY_DEFAULT_ATTR_MAP.put(AccessConstants.LB_OUTPUT_BUFFER, DUMMY_RESPONSE_CONTENT_BYTES);
46      }
47  
48      public DummyRequest() {
49          headerMap = new Hashtable<String, String>();
50          headerMap.put("headerName1", "headerValue1");
51          headerMap.put("headerName2", "headerValue2");
52  
53          parameterMap =  new Hashtable<String, String[]>();
54          parameterMap.put("param1", new String[] {"value1"});
55          
56          attributes = new HashMap<String, Object>(DUMMY_DEFAULT_ATTR_MAP);
57      }
58  
59      public String getAuthType() {
60          return null;
61      }
62  
63      public String getContextPath() {
64          return null;
65      }
66  
67      public Cookie[] getCookies() {
68          Cookie cookie = new Cookie("testName", "testCookie");
69          return new Cookie[] { cookie };
70      }
71  
72      public long getDateHeader(String arg0) {
73          return 0;
74      }
75  
76      public String getHeader(String key) {
77          return headerMap.get(key);
78      }
79  
80  
81      @Override
82      public Enumeration<String>  getHeaderNames() {
83          return headerMap.keys();
84      }
85  
86      @Override
87      public Enumeration<String> getHeaders(String arg) {
88          return null;
89      }
90  
91      public Map<String, String> getHeaders() {
92          return headerMap;
93      }
94  
95      
96      public int getIntHeader(String arg0) {
97          return 0;
98      }
99  
100     public String getMethod() {
101         return "testMethod";
102     }
103 
104     public String getPathInfo() {
105         return null;
106     }
107 
108     public String getPathTranslated() {
109         return null;
110     }
111 
112     public String getQueryString() {
113         return null;
114     }
115 
116     public String getRemoteUser() {
117         return "testUser";
118     }
119 
120     public String getRequestURI() {
121         return uri;
122     }
123 
124     public StringBuffer getRequestURL() {
125         return new StringBuffer(uri);
126     }
127 
128     public String getRequestedSessionId() {
129         return null;
130     }
131 
132     public String getServletPath() {
133         return null;
134     }
135 
136     public HttpSession getSession() {
137         return null;
138     }
139 
140     public HttpSession getSession(boolean arg0) {
141         return null;
142     }
143 
144     public Principal getUserPrincipal() {
145         return null;
146     }
147 
148     public boolean isRequestedSessionIdFromCookie() {
149         return false;
150     }
151 
152     public boolean isRequestedSessionIdFromURL() {
153         return false;
154     }
155 
156     public boolean isRequestedSessionIdFromUrl() {
157         return false;
158     }
159 
160     public boolean authenticate(HttpServletResponse response) throws IOException, ServletException {
161         return false; // To change body of implemented methods use File | Settings | File Templates.
162     }
163 
164     public void login(String username, String password) throws ServletException {
165         // To change body of implemented methods use File | Settings | File Templates.
166     }
167 
168     public void logout() throws ServletException {
169         // To change body of implemented methods use File | Settings | File Templates.
170     }
171 
172     public Collection<Part> getParts() throws IOException, IllegalStateException, ServletException {
173         return null; // To change body of implemented methods use File | Settings | File Templates.
174     }
175 
176     public Part getPart(String name) throws IOException, IllegalStateException, ServletException {
177         return null; // To change body of implemented methods use File | Settings | File Templates.
178     }
179 
180     public boolean isRequestedSessionIdValid() {
181         return false;
182     }
183 
184     public boolean isUserInRole(String arg0) {
185         return false;
186     }
187 
188     public Object getAttribute(String key) {
189         return attributes.get(key);
190     }
191 
192     public Enumeration<String> getAttributeNames() {
193         return Collections.enumeration(attributes.keySet());
194     }
195 
196     public String getCharacterEncoding() {
197         return null;
198     }
199 
200     public int getContentLength() {
201         return 0;
202     }
203 
204     public String getContentType() {
205         return null;
206     }
207 
208     public ServletInputStream getInputStream() throws IOException {
209         return null;
210     }
211 
212     public String getLocalAddr() {
213         return null;
214     }
215 
216     public String getLocalName() {
217         return null;
218     }
219 
220     public int getLocalPort() {
221         return 11;
222     }
223 
224     public ServletContext getServletContext() {
225         return null; // To change body of implemented methods use File | Settings | File Templates.
226     }
227 
228     public AsyncContext startAsync() {
229         return null; // To change body of implemented methods use File | Settings | File Templates.
230     }
231 
232     public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) {
233         return null; // To change body of implemented methods use File | Settings | File Templates.
234     }
235 
236     public boolean isAsyncStarted() {
237         return false; // To change body of implemented methods use File | Settings | File Templates.
238     }
239 
240     public boolean isAsyncSupported() {
241         return false; // To change body of implemented methods use File | Settings | File Templates.
242     }
243 
244     public AsyncContext getAsyncContext() {
245         return null; // To change body of implemented methods use File | Settings | File Templates.
246     }
247 
248     public DispatcherType getDispatcherType() {
249         return null; // To change body of implemented methods use File | Settings | File Templates.
250     }
251 
252     public Locale getLocale() {
253         return null;
254     }
255 
256     @Override
257     public Enumeration<Locale> getLocales() {
258         return null;
259     }
260 
261     public String getParameter(String arg) {
262         String[] stringArray = parameterMap.get(arg);
263         if(stringArray == null || stringArray.length == 0)
264             return null;
265         else
266             return stringArray[0];
267     }
268 
269     @Override
270     public Map<String, String[]> getParameterMap() {
271         return parameterMap;
272     }
273 
274     public Enumeration<String> getParameterNames() {
275         return parameterMap.keys();
276         //eturn Collections.enumeration(parameterMap.keySet());
277     }
278 
279     public String[] getParameterValues(String arg) {
280         return parameterMap.get(arg);
281     }
282 
283     public String getProtocol() {
284         return "testProtocol";
285     }
286 
287     public BufferedReader getReader() throws IOException {
288         return null;
289     }
290 
291     public String getRealPath(String arg0) {
292         return null;
293     }
294 
295     public String getRemoteAddr() {
296         return "testRemoteAddress";
297     }
298 
299     public String getRemoteHost() {
300         return "testHost";
301     }
302 
303     public int getRemotePort() {
304         return 0;
305     }
306 
307     public RequestDispatcher getRequestDispatcher(String arg0) {
308         return null;
309     }
310 
311     public String getScheme() {
312         return null;
313     }
314 
315     public String getServerName() {
316         return "testServerName";
317     }
318 
319     public int getServerPort() {
320         return 0;
321     }
322 
323     public boolean isSecure() {
324         return false;
325     }
326 
327     public void removeAttribute(String arg0) {
328     }
329 
330     public void setAttribute(String name, Object value) {
331         attributes.put(name, value);
332     }
333 
334     public void setCharacterEncoding(String arg0) throws UnsupportedEncodingException {
335     }
336 
337     public void setRequestUri(String uri) {
338         this.uri = uri;
339     }
340 
341     @Override
342     public long getContentLengthLong() {
343         // TODO Auto-generated method stub
344         return 0;
345     }
346 
347     @Override
348     public String changeSessionId() {
349         return null;
350     }
351 
352     @Override
353     public <T extends HttpUpgradeHandler> T upgrade(Class<T> httpUpgradeHandlerClass) throws IOException, ServletException {
354         return null;
355     }
356 }