PageRenderTime 35ms CodeModel.GetById 15ms app.highlight 10ms RepoModel.GetById 6ms app.codeStats 1ms

/test/src/solidstack/httpserver/Request.java

http://solidstack.googlecode.com/
Java | 110 lines | 93 code | 17 blank | 0 comment | 10 complexity | 859d30a8c56d81e0874601762ad9b5f7 MD5 | raw file
Possible License(s): Apache-2.0
  1package solidstack.httpserver;
  2
  3import java.util.ArrayList;
  4import java.util.HashMap;
  5import java.util.List;
  6import java.util.Map;
  7
  8import solidstack.lang.Assert;
  9
 10
 11public class Request
 12{
 13	protected String method;
 14	protected String url;
 15	protected String query;
 16	protected Map< String, List< String > > headers = new HashMap< String, List<String> >();
 17	protected Map< String, String > cookies = new HashMap< String, String >();
 18	protected Map< String, Object > parameters = new HashMap< String, Object >();
 19	protected String fragment;
 20
 21	public void setMethod( String method )
 22	{
 23		this.method = method;
 24	}
 25
 26	public String getMethod()
 27	{
 28		return this.method;
 29	}
 30
 31	public void setUrl( String url )
 32	{
 33		this.url = url;
 34	}
 35
 36	public String getUrl()
 37	{
 38		return this.url;
 39	}
 40
 41	public void setQuery( String query )
 42	{
 43		this.query = query;
 44	}
 45
 46	@SuppressWarnings( "unchecked" )
 47	public void addParameter( String name, String value )
 48	{
 49		Object elem = this.parameters.get( name );
 50		if( elem == null )
 51			this.parameters.put( name, value );
 52		else if( elem instanceof List )
 53			( (List<String>)elem ).add( value );
 54		else
 55		{
 56			List< String > values = new ArrayList<String>();
 57			values.add( (String)elem );
 58			values.add( value );
 59			this.parameters.put( name, values );
 60		}
 61	}
 62
 63	public void addHeader( String name, String value )
 64	{
 65		List<String> values = this.headers.get( name );
 66		if( values == null )
 67			this.headers.put( name, values = new ArrayList< String >() );
 68		values.add( value );
 69	}
 70
 71	public void addCookie( String name, String value )
 72	{
 73		this.cookies.put( name, value );
 74	}
 75
 76	@SuppressWarnings( "unchecked" )
 77	public String getParameter( String name )
 78	{
 79		Object elem = this.parameters.get( name );
 80		if( elem instanceof List )
 81			return ( (List< String >)elem ).get( 0 );
 82		return (String)elem;
 83	}
 84
 85	public Map< String, Object > getParameters()
 86	{
 87		return this.parameters;
 88	}
 89
 90	public String getHeader( String name )
 91	{
 92		List< String > values = this.headers.get( name );
 93		if( values == null )
 94			return null;
 95		Assert.isTrue( !values.isEmpty() );
 96		if( values.size() > 1 )
 97			throw new IllegalStateException( "Found more than 1 value for the header " + name );
 98		return values.get( 0 );
 99	}
100
101	public String getCookie( String name )
102	{
103		return this.cookies.get( name );
104	}
105
106	public boolean isConnectionClose()
107	{
108		return "close".equals( getHeader( "Connection" ) );
109	}
110}