11/*
2- * Copyright 2002-2011 the original author or authors.
2+ * Copyright 2002-2013 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
3535import java .util .Locale ;
3636import java .util .Map ;
3737import java .util .Set ;
38- import java .util .Vector ;
3938import javax .servlet .RequestDispatcher ;
4039import javax .servlet .ServletContext ;
4140import javax .servlet .ServletException ;
5251 * Mock implementation of the {@link javax.servlet.http.HttpServletRequest} interface.
5352 *
5453 * <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).
5655 *
5756 * @author Juergen Hoeller
5857 * @author Rod Johnson
@@ -93,7 +92,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
9392 public static final String DEFAULT_REMOTE_HOST = "localhost" ;
9493
9594 private static final String CONTENT_TYPE_HEADER = "Content-Type" ;
96-
95+
9796 private static final String CHARSET_PREFIX = "charset=" ;
9897
9998
@@ -186,43 +185,45 @@ public class MockHttpServletRequest implements HttpServletRequest {
186185 // ---------------------------------------------------------------------
187186
188187 /**
189- * Create a new MockHttpServletRequest with a default
188+ * Create a new {@code MockHttpServletRequest} with a default
190189 * {@link MockServletContext}.
191- * @see MockServletContext
190+ * @see #MockHttpServletRequest(ServletContext, String, String)
192191 */
193192 public MockHttpServletRequest () {
194193 this (null , "" , "" );
195194 }
196195
197196 /**
198- * Create a new MockHttpServletRequest with a default
197+ * Create a new {@code MockHttpServletRequest} with a default
199198 * {@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} )
202201 * @see #setMethod
203202 * @see #setRequestURI
204- * @see MockServletContext
203+ * @see #MockHttpServletRequest(ServletContext, String, String)
205204 */
206205 public MockHttpServletRequest (String method , String requestURI ) {
207206 this (null , method , requestURI );
208207 }
209208
210209 /**
211- * Create a new MockHttpServletRequest.
210+ * Create a new {@code MockHttpServletRequest} with the supplied {@link ServletContext} .
212211 * @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)
215214 */
216215 public MockHttpServletRequest (ServletContext servletContext ) {
217216 this (servletContext , "" , "" );
218217 }
219218
220219 /**
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}.
222223 * @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} )
226227 * @see #setMethod
227228 * @see #setRequestURI
228229 * @see MockServletContext
@@ -291,7 +292,7 @@ public Object getAttribute(String name) {
291292
292293 public Enumeration <String > getAttributeNames () {
293294 checkActive ();
294- return new Vector < String > (this .attributes .keySet ()). elements ( );
295+ return Collections . enumeration (this .attributes .keySet ());
295296 }
296297
297298 public String getCharacterEncoding () {
@@ -302,7 +303,7 @@ public void setCharacterEncoding(String characterEncoding) {
302303 this .characterEncoding = characterEncoding ;
303304 updateContentTypeHeader ();
304305 }
305-
306+
306307 private void updateContentTypeHeader () {
307308 if (this .contentType != null ) {
308309 StringBuilder sb = new StringBuilder (this .contentType );
@@ -348,18 +349,16 @@ public ServletInputStream getInputStream() {
348349
349350 /**
350351 * 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
353353 * parameter name, they will be replaced.
354354 */
355355 public void setParameter (String name , String value ) {
356- setParameter (name , new String [] { value });
356+ setParameter (name , new String [] {value });
357357 }
358358
359359 /**
360360 * 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
363362 * parameter name, they will be replaced.
364363 */
365364 public void setParameter (String name , String [] values ) {
@@ -368,15 +367,16 @@ public void setParameter(String name, String[] values) {
368367 }
369368
370369 /**
371- * Sets all provided parameters <emphasis >replacing</emphasis > any existing
370+ * Sets all provided parameters <strong >replacing</strong > any existing
372371 * values for the provided parameter names. To add without replacing
373372 * existing values, use {@link #addParameters(java.util.Map)}.
374373 */
375374 @ SuppressWarnings ("rawtypes" )
376375 public void setParameters (Map params ) {
377376 Assert .notNull (params , "Parameter map must not be null" );
378377 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 () + "]" );
380380 Object value = params .get (key );
381381 if (value instanceof String ) {
382382 this .setParameter ((String ) key , (String ) value );
@@ -385,26 +385,24 @@ else if (value instanceof String[]) {
385385 this .setParameter ((String ) key , (String []) value );
386386 }
387387 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 () + "]" );
390390 }
391391 }
392392 }
393393
394394 /**
395395 * 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
398397 * parameter name, the given value will be added to the end of the list.
399398 */
400399 public void addParameter (String name , String value ) {
401- addParameter (name , new String [] { value });
400+ addParameter (name , new String [] {value });
402401 }
403402
404403 /**
405404 * 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
408406 * parameter name, the given values will be added to the end of the list.
409407 */
410408 public void addParameter (String name , String [] values ) {
@@ -422,15 +420,16 @@ public void addParameter(String name, String[] values) {
422420 }
423421
424422 /**
425- * Adds all provided parameters <emphasis >without</emphasis > replacing any
423+ * Adds all provided parameters <strong >without</strong > replacing any
426424 * existing values. To replace existing values, use
427425 * {@link #setParameters(java.util.Map)}.
428426 */
429427 @ SuppressWarnings ("rawtypes" )
430428 public void addParameters (Map params ) {
431429 Assert .notNull (params , "Parameter map must not be null" );
432430 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 () + "]" );
434433 Object value = params .get (key );
435434 if (value instanceof String ) {
436435 this .addParameter ((String ) key , (String ) value );
@@ -439,15 +438,14 @@ else if (value instanceof String[]) {
439438 this .addParameter ((String ) key , (String []) value );
440439 }
441440 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 () + "]" );
444443 }
445444 }
446445 }
447446
448447 /**
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.
451449 */
452450 public void removeParameter (String name ) {
453451 Assert .notNull (name , "Parameter name must not be null" );
@@ -462,8 +460,7 @@ public void removeAllParameters() {
462460 }
463461
464462 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 );
467464 return (arr != null && arr .length > 0 ? arr [0 ] : null );
468465 }
469466
@@ -472,8 +469,7 @@ public Enumeration<String> getParameterNames() {
472469 }
473470
474471 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 );
477473 }
478474
479475 public Map <String , String []> getParameterMap () {
@@ -515,8 +511,8 @@ public int getServerPort() {
515511 public BufferedReader getReader () throws UnsupportedEncodingException {
516512 if (this .content != null ) {
517513 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 );
520516 return new BufferedReader (sourceReader );
521517 }
522518 else {
@@ -656,8 +652,8 @@ public Cookie[] getCookies() {
656652 * adding the given value (more specifically, its toString representation)
657653 * as further element.
658654 * <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
661657 * use a single call with an entire array or Collection of values as
662658 * parameter.
663659 * @see #getHeaderNames
@@ -673,7 +669,7 @@ public void addHeader(String name, Object value) {
673669 }
674670 doAddHeaderValue (name , value , false );
675671 }
676-
672+
677673 @ SuppressWarnings ("rawtypes" )
678674 private void doAddHeaderValue (String name , Object value , boolean replace ) {
679675 HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
@@ -711,6 +707,20 @@ else if (value != null) {
711707 }
712708 }
713709
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+
714724 public int getIntHeader (String name ) {
715725 HeaderValueHolder header = HeaderValueHolder .getByName (this .headers , name );
716726 Object value = (header != null ? header .getValue () : null );
@@ -728,20 +738,6 @@ else if (value != null) {
728738 }
729739 }
730740
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-
745741 public void setMethod (String method ) {
746742 this .method = method ;
747743 }
0 commit comments