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