1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.fileupload;
18
19 import java.io.BufferedReader;
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.UnsupportedEncodingException;
24 import java.security.Principal;
25 import java.util.Enumeration;
26 import java.util.Locale;
27 import java.util.Map;
28
29 import javax.servlet.RequestDispatcher;
30 import javax.servlet.ServletInputStream;
31 import javax.servlet.http.Cookie;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpSession;
34
35 public class MockHttpServletRequest implements HttpServletRequest {
36
37 private final InputStream m_requestData;
38
39 private long length;
40
41 private String m_strContentType;
42
43 private int readLimit = -1;
44
45 private final Map<String, String> m_headers = new java.util.HashMap<String, String>();
46
47
48
49
50
51 public MockHttpServletRequest(
52 final byte[] requestData,
53 final String strContentType) {
54 this(new ByteArrayInputStream(requestData),
55 requestData.length, strContentType);
56 }
57
58
59
60
61
62 public MockHttpServletRequest(
63 final InputStream requestData,
64 final long requestLength,
65 final String strContentType) {
66 m_requestData = requestData;
67 length = requestLength;
68 m_strContentType = strContentType;
69 m_headers.put(FileUploadBase.CONTENT_TYPE, strContentType);
70 }
71
72
73
74
75 public String getAuthType() {
76 return null;
77 }
78
79
80
81
82 public Cookie[] getCookies() {
83 return null;
84 }
85
86
87
88
89 public long getDateHeader(String arg0) {
90 return 0;
91 }
92
93
94
95
96 public String getHeader(String headerName) {
97 return m_headers.get(headerName);
98 }
99
100
101
102
103 public Enumeration<String> getHeaders(String arg0) {
104
105 return null;
106 }
107
108
109
110
111 public Enumeration<String> getHeaderNames() {
112
113 return null;
114 }
115
116
117
118
119 public int getIntHeader(String arg0) {
120 return 0;
121 }
122
123
124
125
126 public String getMethod() {
127 return null;
128 }
129
130
131
132
133 public String getPathInfo() {
134 return null;
135 }
136
137
138
139
140 public String getPathTranslated() {
141 return null;
142 }
143
144
145
146
147 public String getContextPath() {
148 return null;
149 }
150
151
152
153
154 public String getQueryString() {
155 return null;
156 }
157
158
159
160
161 public String getRemoteUser() {
162 return null;
163 }
164
165
166
167
168 public boolean isUserInRole(String arg0) {
169 return false;
170 }
171
172
173
174
175 public Principal getUserPrincipal() {
176 return null;
177 }
178
179
180
181
182 public String getRequestedSessionId() {
183 return null;
184 }
185
186
187
188
189 public String getRequestURI() {
190 return null;
191 }
192
193
194
195
196 public StringBuffer getRequestURL() {
197 return null;
198 }
199
200
201
202
203 public String getServletPath() {
204 return null;
205 }
206
207
208
209
210 public HttpSession getSession(boolean arg0) {
211 return null;
212 }
213
214
215
216
217 public HttpSession getSession() {
218 return null;
219 }
220
221
222
223
224 public boolean isRequestedSessionIdValid() {
225 return false;
226 }
227
228
229
230
231 public boolean isRequestedSessionIdFromCookie() {
232 return false;
233 }
234
235
236
237
238 public boolean isRequestedSessionIdFromURL() {
239 return false;
240 }
241
242
243
244
245
246 @Deprecated
247 public boolean isRequestedSessionIdFromUrl() {
248 return false;
249 }
250
251
252
253
254 public Object getAttribute(String arg0) {
255 return null;
256 }
257
258
259
260
261 public Enumeration<String> getAttributeNames() {
262 return null;
263 }
264
265
266
267
268 public String getCharacterEncoding() {
269 return null;
270 }
271
272
273
274
275 public void setCharacterEncoding(String arg0)
276 throws UnsupportedEncodingException {
277 }
278
279
280
281
282 public int getContentLength() {
283 int iLength = 0;
284
285 if (null == m_requestData) {
286 iLength = -1;
287 } else {
288 if (length > Integer.MAX_VALUE) {
289 throw new RuntimeException("Value '" + length + "' is too large to be converted to int");
290 }
291 iLength = (int) length;
292 }
293 return iLength;
294 }
295
296
297
298
299 public void setContentLength(long length) {
300 this.length = length;
301 }
302
303
304
305
306 public String getContentType() {
307 return m_strContentType;
308 }
309
310
311
312
313 public ServletInputStream getInputStream() throws IOException {
314 ServletInputStream sis = new MyServletInputStream(m_requestData, readLimit);
315 return sis;
316 }
317
318
319
320
321
322
323 public void setReadLimit(int readLimit) {
324 this.readLimit = readLimit;
325 }
326
327
328
329
330 public String getParameter(String arg0) {
331 return null;
332 }
333
334
335
336
337 public Enumeration<String> getParameterNames() {
338 return null;
339 }
340
341
342
343
344 public String[] getParameterValues(String arg0) {
345 return null;
346 }
347
348
349
350
351 public Map<String, String[]> getParameterMap() {
352 return null;
353 }
354
355
356
357
358 public String getProtocol() {
359 return null;
360 }
361
362
363
364
365 public String getScheme() {
366 return null;
367 }
368
369
370
371
372 public String getServerName() {
373 return null;
374 }
375
376
377
378
379 @SuppressWarnings("javadoc")
380 public String getLocalName() {
381 return null;
382 }
383
384
385
386
387 public int getServerPort() {
388 return 0;
389 }
390
391
392
393
394 @SuppressWarnings("javadoc")
395 public int getLocalPort() {
396 return 0;
397 }
398
399
400
401
402 @SuppressWarnings("javadoc")
403 public int getRemotePort() {
404 return 0;
405 }
406
407
408
409
410 public BufferedReader getReader() throws IOException {
411 return null;
412 }
413
414
415
416
417 public String getRemoteAddr() {
418 return null;
419 }
420
421
422
423
424 @SuppressWarnings("javadoc")
425 public String getLocalAddr() {
426 return null;
427 }
428
429
430
431
432 public String getRemoteHost() {
433 return null;
434 }
435
436
437
438
439 public void setAttribute(String arg0, Object arg1) {
440 }
441
442
443
444
445 public void removeAttribute(String arg0) {
446 }
447
448
449
450
451 public Locale getLocale() {
452 return null;
453 }
454
455
456
457
458 public Enumeration<Locale> getLocales() {
459 return null;
460 }
461
462
463
464
465 public boolean isSecure() {
466 return false;
467 }
468
469
470
471
472 public RequestDispatcher getRequestDispatcher(String arg0) {
473 return null;
474 }
475
476
477
478
479
480 @Deprecated
481 public String getRealPath(String arg0) {
482 return null;
483 }
484
485 private static class MyServletInputStream
486 extends javax.servlet.ServletInputStream {
487
488 private final InputStream in;
489 private final int readLimit;
490
491
492
493
494
495 public MyServletInputStream(InputStream pStream, int readLimit) {
496 in = pStream;
497 this.readLimit = readLimit;
498 }
499
500 @Override
501 public int read() throws IOException {
502 return in.read();
503 }
504
505 @Override
506 public int read(byte b[], int off, int len) throws IOException {
507 if (readLimit > 0) {
508 return in.read(b, off, Math.min(readLimit, len));
509 }
510 return in.read(b, off, len);
511 }
512
513 }
514
515 }