/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

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