PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/httpunit-1.7/src/com/meterware/httpunit/HttpHeader.java

#
Java | 124 lines | 77 code | 18 blank | 29 comment | 26 complexity | c29292fa1286fd90357c66c99f318b04 MD5 | raw file
  1. package com.meterware.httpunit;
  2. /********************************************************************************************************************
  3. * $Id: HttpHeader.java 908 2008-04-05 08:24:51Z wolfgang_fahl $
  4. *
  5. * Copyright (c) 2006, Russell Gold
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  8. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  10. * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all copies or substantial portions
  13. * of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  16. * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  18. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. * DEALINGS IN THE SOFTWARE.
  20. *
  21. *******************************************************************************************************************/
  22. import java.util.*;
  23. /**
  24. * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
  25. */
  26. public class HttpHeader {
  27. private String _label;
  28. private Map _properties;
  29. public HttpHeader( String headerString ) {
  30. this( headerString, null );
  31. }
  32. public HttpHeader( String headerString, String defaultLabel ) {
  33. if (headerString != null) {
  34. final int index = headerString.indexOf( ' ' );
  35. if (index < 0) { // non-conforming header
  36. _label = defaultLabel;
  37. _properties = loadProperties( headerString );
  38. } else {
  39. _label = headerString.substring( 0, index );
  40. _properties = loadProperties( headerString.substring( index+1 ) );
  41. }
  42. }
  43. }
  44. public boolean equals( Object obj ) {
  45. if (!getClass().equals( obj.getClass() ) ) return false;
  46. return getLabel().equals( ((HttpHeader) obj).getLabel() ) &&
  47. getProperties().equals( ((HttpHeader) obj).getProperties() );
  48. }
  49. public String toString() {
  50. return getLabel() + " " + getProperties();
  51. }
  52. protected String getProperty( String key ) {
  53. return unQuote( (String) getProperties().get( key ) );
  54. }
  55. private String unQuote( String value ) {
  56. if (value == null || value.length() <= 1 || !value.startsWith( "\"" ) || !value.endsWith( "\"")) return value;
  57. return value.substring( 1, value.length()-1 );
  58. }
  59. // Headers have the general format (ignoring unquoted white space):
  60. // header ::= property-def | property-def ',' header
  61. // property-def ::= name '=' value
  62. // name ::= ID
  63. // value ::= ID | QUOTED-STRING
  64. //
  65. static private Map loadProperties( String parameterString ) {
  66. Properties properties = new Properties();
  67. char[] chars = parameterString.toCharArray();
  68. int i = 0;
  69. StringBuffer sb = new StringBuffer();
  70. while (i < chars.length) {
  71. while (i < chars.length && Character.isWhitespace( chars[i] ) ) i++;
  72. while (i < chars.length && Character.isJavaIdentifierPart( chars[i] ) ) sb.append( chars[i++] );
  73. String name = sb.toString();
  74. sb.setLength( 0 );
  75. while (i < chars.length && chars[i] != '=' ) i++;
  76. if (i == chars.length) break;
  77. i++; // skip '='
  78. while (i < chars.length && Character.isWhitespace( chars[i] ) ) i++;
  79. if (i == chars.length) break;
  80. if (chars[i] == '"') {
  81. sb.append( chars[i++] );
  82. while (i < chars.length && chars[i] != '"' ) sb.append( chars[i++] );
  83. sb.append( '"' );
  84. if (i < chars.length) i++; // skip close quote
  85. } else {
  86. while (i < chars.length && Character.isJavaIdentifierPart( chars[i] ) ) sb.append( chars[i++] );
  87. }
  88. properties.setProperty( name, sb.toString() );
  89. sb.setLength( 0 );
  90. while (i < chars.length && chars[i] != ',' ) i++;
  91. if (i == chars.length) break;
  92. i++; // skip '='
  93. }
  94. return properties;
  95. }
  96. public String getLabel() {
  97. return _label;
  98. }
  99. public Map getProperties() {
  100. return _properties;
  101. }
  102. }