1 18 19 package org.apache.beehive.netui.tools.testrecorder.shared; 20 21 import java.text.DecimalFormat ; 22 import java.text.NumberFormat ; 23 import java.util.Locale ; 24 import java.util.Map ; 25 import java.util.HashMap ; 26 import java.util.regex.Pattern ; 27 import java.util.regex.Matcher ; 28 29 30 public class ResponseData { 31 32 private static final Logger log = Logger.getInstance( ResponseData.class ); 33 34 public static final String NON_UNIQUE_SESSION_ID = ""; 35 public static final String NON_UNIQUE_HOST = "@NON_UNIQUE_HOST@"; 36 public static final String NON_UNIQUE_PORT = "@NON_UNIQUE_PORT@"; 37 private static final String COLON = ":"; 38 private static final String HTTP = "http://"; 39 private static final String HTTPS = "https://"; 40 41 private static final DecimalFormat format = (DecimalFormat ) 42 NumberFormat.getInstance( Locale.US ); 43 44 static { 45 format.setDecimalSeparatorAlwaysShown( false ); 47 format.setGroupingSize( 0 ); 48 } 49 50 private String host; 51 private int port; 52 private int statusCode; 53 private String reason; 54 private NVPair[] headers; 55 private Map headerMap; 56 private String body; 58 59 60 public ResponseData( String host, int port ) { 61 this.host = host; 62 this.port = port; 63 } 64 65 public void setStatusCode( int statusCode ) { 66 this.statusCode = statusCode; 67 } 68 69 public int getStatusCode() { 70 return statusCode; 71 } 72 73 public void setReason( String reason ) { 74 this.reason = reason; 75 } 76 77 public String getReason() { 78 return reason; 79 } 80 81 public void setHeaders( NVPair[] headers ) { 82 this.headers = headers; 83 } 84 85 public NVPair[] getHeaders() { 86 return headers; 87 } 88 89 94 public String getHeader( String name ) { 95 initializeHeaderMap(); 96 String value = (String ) headerMap.get( name ); 97 return value; 98 } 99 100 private void initializeHeaderMap() { 101 if ( headerMap != null ) { 102 return; 103 } 104 headerMap = new HashMap (); 105 NVPair header = null; 106 for ( int i = 0; i < headers.length; i++ ) { 107 header = headers[i]; 108 headerMap.put( header.getName(), header.getValue() ); 110 } 111 } 112 113 public void setBody( String body ) { 114 this.body = body.trim(); 115 } 116 117 123 public String getBody() { 124 return body; 125 } 126 127 132 public String getNormalizedBody() { 133 return replaceHostPort( getBody(), host, port ); 134 } 135 136 private static String replaceHostPort( String string, String host, int port ) { 139 Pattern pattern = Pattern.compile( HTTP + host + COLON + format.format( port ) ); 141 Matcher matcher = pattern.matcher( string ); 142 string = matcher.replaceAll( HTTP + NON_UNIQUE_HOST + COLON + NON_UNIQUE_PORT ); 143 144 pattern = Pattern.compile( HTTPS + host + COLON ); 146 matcher = pattern.matcher( string ); 147 string = matcher.replaceAll( HTTPS + NON_UNIQUE_HOST + COLON ); 148 return string; 149 } 150 151 public String toString() { 152 StringBuffer sb = new StringBuffer ( 256 ); 153 sb.append( "[ " ); 154 sb.append( "host( " + host + " )" ); 155 sb.append( ", port( " + port + " )" ); 156 sb.append( ", statusCode( " + statusCode + " )" ); 157 sb.append( ", reason( " + reason + " )" ); 158 sb.append( ", headers( " + headers + " )" ); 159 sb.append( ", body( " + body + " )" ); 160 sb.append( " ]" ); 161 return sb.toString(); 162 } 163 164 } 165 | Popular Tags |