1
1
/*
2
- * Copyright 2002-2011 the original author or authors.
2
+ * Copyright 2002-2013 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
35
35
import java .util .Locale ;
36
36
import java .util .Map ;
37
37
import java .util .Set ;
38
- import java .util .Vector ;
39
38
import javax .servlet .RequestDispatcher ;
40
39
import javax .servlet .ServletContext ;
41
40
import javax .servlet .ServletException ;
52
51
* Mock implementation of the {@link javax.servlet.http.HttpServletRequest} interface.
53
52
*
54
53
* <p>Compatible with Servlet 2.5 and partially with Servlet 3.0 (notable exceptions:
55
- * the < code> getPart(s)</code> and < code> startAsync</code> families of methods).
54
+ * the {@ code getPart(s)} and {@ code startAsync} families of methods).
56
55
*
57
56
* @author Juergen Hoeller
58
57
* @author Rod Johnson
@@ -93,7 +92,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
93
92
public static final String DEFAULT_REMOTE_HOST = "localhost" ;
94
93
95
94
private static final String CONTENT_TYPE_HEADER = "Content-Type" ;
96
-
95
+
97
96
private static final String CHARSET_PREFIX = "charset=" ;
98
97
99
98
@@ -186,43 +185,45 @@ public class MockHttpServletRequest implements HttpServletRequest {
186
185
// ---------------------------------------------------------------------
187
186
188
187
/**
189
- * Create a new MockHttpServletRequest with a default
188
+ * Create a new {@code MockHttpServletRequest} with a default
190
189
* {@link MockServletContext}.
191
- * @see MockServletContext
190
+ * @see #MockHttpServletRequest(ServletContext, String, String)
192
191
*/
193
192
public MockHttpServletRequest () {
194
193
this (null , "" , "" );
195
194
}
196
195
197
196
/**
198
- * Create a new MockHttpServletRequest with a default
197
+ * Create a new {@code MockHttpServletRequest} with a default
199
198
* {@link MockServletContext}.
200
- * @param method the request method (may be < code> null</code> )
201
- * @param requestURI the request URI (may be < code> null</code> )
199
+ * @param method the request method (may be {@ code null} )
200
+ * @param requestURI the request URI (may be {@ code null} )
202
201
* @see #setMethod
203
202
* @see #setRequestURI
204
- * @see MockServletContext
203
+ * @see #MockHttpServletRequest(ServletContext, String, String)
205
204
*/
206
205
public MockHttpServletRequest (String method , String requestURI ) {
207
206
this (null , method , requestURI );
208
207
}
209
208
210
209
/**
211
- * Create a new MockHttpServletRequest.
210
+ * Create a new {@code MockHttpServletRequest} with the supplied {@link ServletContext} .
212
211
* @param servletContext the ServletContext that the request runs in (may be
213
- * < code> null</code> to use a default MockServletContext)
214
- * @see MockServletContext
212
+ * {@ code null} to use a default {@link MockServletContext} )
213
+ * @see #MockHttpServletRequest(ServletContext, String, String)
215
214
*/
216
215
public MockHttpServletRequest (ServletContext servletContext ) {
217
216
this (servletContext , "" , "" );
218
217
}
219
218
220
219
/**
221
- * Create a new MockHttpServletRequest.
220
+ * Create a new {@code MockHttpServletRequest} with the supplied {@link ServletContext},
221
+ * {@code method}, and {@code requestURI}.
222
+ * <p>The preferred locale will be set to {@link Locale#ENGLISH}.
222
223
* @param servletContext the ServletContext that the request runs in (may be
223
- * < code> null</code> to use a default MockServletContext)
224
- * @param method the request method (may be < code> null</code> )
225
- * @param requestURI the request URI (may be < code> null</code> )
224
+ * {@ code null} to use a default {@link MockServletContext} )
225
+ * @param method the request method (may be {@ code null} )
226
+ * @param requestURI the request URI (may be {@ code null} )
226
227
* @see #setMethod
227
228
* @see #setRequestURI
228
229
* @see MockServletContext
@@ -291,7 +292,7 @@ public Object getAttribute(String name) {
291
292
292
293
public Enumeration <String > getAttributeNames () {
293
294
checkActive ();
294
- return new Vector < String > (this .attributes .keySet ()). elements ( );
295
+ return Collections . enumeration (this .attributes .keySet ());
295
296
}
296
297
297
298
public String getCharacterEncoding () {
@@ -302,7 +303,7 @@ public void setCharacterEncoding(String characterEncoding) {
302
303
this .characterEncoding = characterEncoding ;
303
304
updateContentTypeHeader ();
304
305
}
305
-
306
+
306
307
private void updateContentTypeHeader () {
307
308
if (this .contentType != null ) {
308
309
StringBuilder sb = new StringBuilder (this .contentType );
@@ -348,18 +349,16 @@ public ServletInputStream getInputStream() {
348
349
349
350
/**
350
351
* Set a single value for the specified HTTP parameter.
351
- * <p>
352
- * If there are already one or more values registered for the given
352
+ * <p>If there are already one or more values registered for the given
353
353
* parameter name, they will be replaced.
354
354
*/
355
355
public void setParameter (String name , String value ) {
356
- setParameter (name , new String [] { value });
356
+ setParameter (name , new String [] {value });
357
357
}
358
358
359
359
/**
360
360
* Set an array of values for the specified HTTP parameter.
361
- * <p>
362
- * If there are already one or more values registered for the given
361
+ * <p>If there are already one or more values registered for the given
363
362
* parameter name, they will be replaced.
364
363
*/
365
364
public void setParameter (String name , String [] values ) {
@@ -368,15 +367,16 @@ public void setParameter(String name, String[] values) {
368
367
}
369
368
370
369
/**
371
- * Sets all provided parameters <emphasis >replacing</emphasis > any existing
370
+ * Sets all provided parameters <strong >replacing</strong > any existing
372
371
* values for the provided parameter names. To add without replacing
373
372
* existing values, use {@link #addParameters(java.util.Map)}.
374
373
*/
375
374
@ SuppressWarnings ("rawtypes" )
376
375
public void setParameters (Map params ) {
377
376
Assert .notNull (params , "Parameter map must not be null" );
378
377
for (Object key : params .keySet ()) {
379
- Assert .isInstanceOf (String .class , key , "Parameter map key must be of type [" + String .class .getName () + "]" );
378
+ Assert .isInstanceOf (String .class , key ,
379
+ "Parameter map key must be of type [" + String .class .getName () + "]" );
380
380
Object value = params .get (key );
381
381
if (value instanceof String ) {
382
382
this .setParameter ((String ) key , (String ) value );
@@ -385,26 +385,24 @@ else if (value instanceof String[]) {
385
385
this .setParameter ((String ) key , (String []) value );
386
386
}
387
387
else {
388
- throw new IllegalArgumentException ("Parameter map value must be single value " + " or array of type ["
389
- + String .class .getName () + "]" );
388
+ throw new IllegalArgumentException (
389
+ "Parameter map value must be single value " + " or array of type [" + String .class .getName () + "]" );
390
390
}
391
391
}
392
392
}
393
393
394
394
/**
395
395
* Add a single value for the specified HTTP parameter.
396
- * <p>
397
- * If there are already one or more values registered for the given
396
+ * <p>If there are already one or more values registered for the given
398
397
* parameter name, the given value will be added to the end of the list.
399
398
*/
400
399
public void addParameter (String name , String value ) {
401
- addParameter (name , new String [] { value });
400
+ addParameter (name , new String [] {value });
402
401
}
403
402
404
403
/**
405
404
* Add an array of values for the specified HTTP parameter.
406
- * <p>
407
- * If there are already one or more values registered for the given
405
+ * <p>If there are already one or more values registered for the given
408
406
* parameter name, the given values will be added to the end of the list.
409
407
*/
410
408
public void addParameter (String name , String [] values ) {
@@ -422,15 +420,16 @@ public void addParameter(String name, String[] values) {
422
420
}
423
421
424
422
/**
425
- * Adds all provided parameters <emphasis >without</emphasis > replacing any
423
+ * Adds all provided parameters <strong >without</strong > replacing any
426
424
* existing values. To replace existing values, use
427
425
* {@link #setParameters(java.util.Map)}.
428
426
*/
429
427
@ SuppressWarnings ("rawtypes" )
430
428
public void addParameters (Map params ) {
431
429
Assert .notNull (params , "Parameter map must not be null" );
432
430
for (Object key : params .keySet ()) {
433
- Assert .isInstanceOf (String .class , key , "Parameter map key must be of type [" + String .class .getName () + "]" );
431
+ Assert .isInstanceOf (String .class , key ,
432
+ "Parameter map key must be of type [" + String .class .getName () + "]" );
434
433
Object value = params .get (key );
435
434
if (value instanceof String ) {
436
435
this .addParameter ((String ) key , (String ) value );
@@ -439,15 +438,14 @@ else if (value instanceof String[]) {
439
438
this .addParameter ((String ) key , (String []) value );
440
439
}
441
440
else {
442
- throw new IllegalArgumentException ("Parameter map value must be single value " + " or array of type ["
443
- + String .class .getName () + "]" );
441
+ throw new IllegalArgumentException ("Parameter map value must be single value " +
442
+ " or array of type [" + String .class .getName () + "]" );
444
443
}
445
444
}
446
445
}
447
446
448
447
/**
449
- * Remove already registered values for the specified HTTP parameter, if
450
- * any.
448
+ * Remove already registered values for the specified HTTP parameter, if any.
451
449
*/
452
450
public void removeParameter (String name ) {
453
451
Assert .notNull (name , "Parameter name must not be null" );
@@ -462,8 +460,7 @@ public void removeAllParameters() {
462
460
}
463
461
464
462
public String getParameter (String name ) {
465
- Assert .notNull (name , "Parameter name must not be null" );
466
- String [] arr = this .parameters .get (name );
463
+ String [] arr = (name != null ? this .parameters .get (name ) : null );
467
464
return (arr != null && arr .length > 0 ? arr [0 ] : null );
468
465
}
469
466
@@ -472,8 +469,7 @@ public Enumeration<String> getParameterNames() {
472
469
}
473
470
474
471
public String [] getParameterValues (String name ) {
475
- Assert .notNull (name , "Parameter name must not be null" );
476
- return this .parameters .get (name );
472
+ return (name != null ? this .parameters .get (name ) : null );
477
473
}
478
474
479
475
public Map <String , String []> getParameterMap () {
@@ -515,8 +511,8 @@ public int getServerPort() {
515
511
public BufferedReader getReader () throws UnsupportedEncodingException {
516
512
if (this .content != null ) {
517
513
InputStream sourceStream = new ByteArrayInputStream (this .content );
518
- Reader sourceReader = (this .characterEncoding != null ) ? new InputStreamReader ( sourceStream ,
519
- this .characterEncoding ) : new InputStreamReader (sourceStream );
514
+ Reader sourceReader = (this .characterEncoding != null ) ?
515
+ new InputStreamReader ( sourceStream , this .characterEncoding ) : new InputStreamReader (sourceStream );
520
516
return new BufferedReader (sourceReader );
521
517
}
522
518
else {
@@ -656,8 +652,8 @@ public Cookie[] getCookies() {
656
652
* adding the given value (more specifically, its toString representation)
657
653
* as further element.
658
654
* <p>Multiple values can only be stored as list of Strings, following the
659
- * Servlet spec (see < code> getHeaders</code> accessor). As alternative to
660
- * repeated < code> addHeader</code> calls for individual elements, you can
655
+ * Servlet spec (see {@ code getHeaders} accessor). As alternative to
656
+ * repeated {@ code addHeader} calls for individual elements, you can
661
657
* use a single call with an entire array or Collection of values as
662
658
* parameter.
663
659
* @see #getHeaderNames
@@ -673,7 +669,7 @@ public void addHeader(String name, Object value) {
673
669
}
674
670
doAddHeaderValue (name , value , false );
675
671
}
676
-
672
+
677
673
@ SuppressWarnings ("rawtypes" )
678
674
private void doAddHeaderValue (String name , Object value , boolean replace ) {
679
675
HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
@@ -711,6 +707,20 @@ else if (value != null) {
711
707
}
712
708
}
713
709
710
+ public String getHeader (String name ) {
711
+ HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
712
+ return (header != null ? header .getStringValue () : null );
713
+ }
714
+
715
+ public Enumeration <String > getHeaders (String name ) {
716
+ HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
717
+ return Collections .enumeration (header != null ? header .getStringValues () : new LinkedList <String >());
718
+ }
719
+
720
+ public Enumeration <String > getHeaderNames () {
721
+ return Collections .enumeration (this .headers .keySet ());
722
+ }
723
+
714
724
public int getIntHeader (String name ) {
715
725
HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
716
726
Object value = (header != null ? header .getValue () : null );
@@ -728,20 +738,6 @@ else if (value != null) {
728
738
}
729
739
}
730
740
731
- public String getHeader (String name ) {
732
- HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
733
- return (header != null ? header .getStringValue () : null );
734
- }
735
-
736
- public Enumeration <String > getHeaders (String name ) {
737
- HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
738
- return Collections .enumeration (header != null ? header .getStringValues () : new LinkedList <String >());
739
- }
740
-
741
- public Enumeration <String > getHeaderNames () {
742
- return Collections .enumeration (this .headers .keySet ());
743
- }
744
-
745
741
public void setMethod (String method ) {
746
742
this .method = method ;
747
743
}
0 commit comments