View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.mock.servlet;
21  
22  import org.apache.commons.collections.iterators.IteratorEnumeration;
23  
24  import javax.servlet.RequestDispatcher;
25  import javax.servlet.ServletInputStream;
26  import javax.servlet.http.Cookie;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpSession;
29  import java.io.BufferedReader;
30  import java.io.IOException;
31  import java.security.Principal;
32  import java.util.Enumeration;
33  import java.util.HashMap;
34  import java.util.Locale;
35  import java.util.Map;
36  
37  public class MockHttpServletRequest implements HttpServletRequest {
38  // ----------------------------------------------------------------- attributes
39  
40    private Map parameters = new HashMap();
41    private Map attributes = new HashMap();
42    private MockHttpSession httpSession = null;
43  
44    private byte[] body;
45    private String method;
46  
47  // --------------------------------------------------------------- constructors
48  
49    public MockHttpServletRequest() {
50    }
51  
52    public MockHttpServletRequest(final byte[] body) {
53      this.body = body;
54    }
55  
56  // ----------------------------------------------------------------- interfaces
57  
58  
59  // ---------------------------- interface HttpServletRequest
60  
61    public String getAuthType() {
62      return null;
63    }
64  
65    public Cookie[] getCookies() {
66      return new Cookie[0];
67    }
68  
69    public long getDateHeader(final String s) {
70      return 0;
71    }
72  
73    public String getHeader(final String reference) {
74      if (reference.equals("Content-type")) {
75        return "multipart/form-data; boundary=xxx";
76      }
77      return null;
78    }
79  
80    public Enumeration getHeaders(final String s) {
81      return null;
82    }
83  
84    public Enumeration getHeaderNames() {
85      return null;
86    }
87  
88    public int getIntHeader(final String s) {
89      return 0;
90    }
91  
92    public String getMethod() {
93      return method;
94    }
95  
96    public void setMethod(final String method) {
97      this.method = method;
98    }
99  
100   public String getPathInfo() {
101     return null;
102   }
103 
104   public String getPathTranslated() {
105     return null;
106   }
107 
108   public String getContextPath() {
109     return null;
110   }
111 
112   public String getQueryString() {
113     return null;
114   }
115 
116   public String getRemoteUser() {
117     return null;
118   }
119 
120   public boolean isUserInRole(final String s) {
121     return false;
122   }
123 
124   public Principal getUserPrincipal() {
125     return null;
126   }
127 
128   public String getRequestedSessionId() {
129     return null;
130   }
131 
132   public String getRequestURI() {
133     return null;
134   }
135 
136   public StringBuffer getRequestURL() {
137     return null;
138   }
139 
140   public String getServletPath() {
141     return null;
142   }
143 
144   public HttpSession getSession(final boolean flag) {
145     if (flag && httpSession == null) {
146       httpSession = new MockHttpSession();
147     }
148     return httpSession;
149   }
150 
151   public HttpSession getSession() {
152     return httpSession;
153   }
154 
155   public boolean isRequestedSessionIdValid() {
156     return false;
157   }
158 
159   public boolean isRequestedSessionIdFromCookie() {
160     return false;
161   }
162 
163   public boolean isRequestedSessionIdFromURL() {
164     return false;
165   }
166 
167   public boolean isRequestedSessionIdFromUrl() {
168     return false;
169   }
170 
171 // ---------------------------- interface ServletRequest
172 
173   public Object getAttribute(final String name) {
174     return attributes.get(name);
175   }
176 
177   public Enumeration getAttributeNames() {
178     return null;
179   }
180 
181   public String getCharacterEncoding() {
182     return null;
183   }
184 
185   public void setCharacterEncoding(final java.lang.String env) {
186   }
187 
188   public int getContentLength() {
189     return body.length;
190   }
191 
192   public String getContentType() {
193     return "multipart/form-data; boundary=xxx";
194   }
195 
196   public ServletInputStream getInputStream() throws IOException {
197     return new MockServletInputStream(body);
198   }
199 
200   public String getParameter(final String name) {
201     final String[] values = (String[]) parameters.get(name);
202     if (values != null) {
203       return (values[0]);
204     } else {
205       return (null);
206     }
207   }
208 
209   public Map getParameterMap() {
210     return parameters;
211   }
212 
213   public String getProtocol() {
214     return null;
215   }
216 
217   public String getScheme() {
218     return null;
219   }
220 
221   public String getServerName() {
222     return null;
223   }
224 
225   public int getServerPort() {
226     return 0;
227   }
228 
229   public BufferedReader getReader() throws IOException {
230     return null;
231   }
232 
233   public String getRemoteAddr() {
234     return null;
235   }
236 
237   public String getRemoteHost() {
238     return null;
239   }
240 
241   public void setAttribute(final String name, final Object o) {
242     attributes.put(name, o);
243   }
244 
245   public void removeAttribute(final String name) {
246     attributes.remove(name);
247   }
248 
249   public Locale getLocale() {
250     return null;
251   }
252 
253   public Enumeration getLocales() {
254     return null;
255   }
256 
257   public boolean isSecure() {
258     return false;
259   }
260 
261   public RequestDispatcher getRequestDispatcher(final String s) {
262     return null;
263   }
264 
265   /** @deprecated */
266   public String getRealPath(final String s) {
267     return null;
268   }
269 
270   public int getRemotePort() {
271     return 0;
272   }
273 
274   public String getLocalName() {
275     return null;
276   }
277 
278   public String getLocalAddr() {
279     return null;
280   }
281 
282   public int getLocalPort() {
283     return 0;
284   }
285 
286 // ----------------------------------------------------------- business methods
287 
288   public void addParameter(final String name, final String[] values) {
289     parameters.put(name, values);
290   }
291 
292 // ---------------------------------------------------------- canonical methods
293 
294   public String toString() {
295     final StringBuilder buffer = new StringBuilder();
296     final Enumeration enumeration = getParameterNames();
297     while (enumeration.hasMoreElements()) {
298       final String key = (String) enumeration.nextElement();
299       final String[] values = getParameterValues(key);
300       buffer.append("parameter:");
301       buffer.append(key);
302       buffer.append("=[");
303       for (int i = 0; i < values.length; i++) {
304         final String value = values[i];
305         buffer.append(value);
306         if (i < values.length - 1) {
307           buffer.append(",");
308         }
309       }
310       buffer.append("]\n");
311     }
312     return buffer.toString();
313   }
314 
315   public Enumeration getParameterNames() {
316     return (new IteratorEnumeration(parameters.keySet().iterator()));
317   }
318 
319   public String[] getParameterValues(final String name) {
320     return (String[]) parameters.get(name);
321   }
322 }