KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tools > testrecorder > shared > ResponseData


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18
19 package org.apache.beehive.netui.tools.testrecorder.shared;
20
21 import java.text.DecimalFormat JavaDoc;
22 import java.text.NumberFormat JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.regex.Pattern JavaDoc;
27 import java.util.regex.Matcher JavaDoc;
28
29
30 public class ResponseData {
31
32     private static final Logger log = Logger.getInstance( ResponseData.class );
33
34     public static final String JavaDoc NON_UNIQUE_SESSION_ID = "";
35     public static final String JavaDoc NON_UNIQUE_HOST = "@NON_UNIQUE_HOST@";
36     public static final String JavaDoc NON_UNIQUE_PORT = "@NON_UNIQUE_PORT@";
37     private static final String JavaDoc COLON = ":";
38     private static final String JavaDoc HTTP = "http://";
39     private static final String JavaDoc HTTPS = "https://";
40
41     private static final DecimalFormat JavaDoc format = (DecimalFormat JavaDoc)
42             NumberFormat.getInstance( Locale.US );
43
44     static {
45         // we just want the integer portion
46
format.setDecimalSeparatorAlwaysShown( false );
47         format.setGroupingSize( 0 );
48     }
49
50     private String JavaDoc host;
51     private int port;
52     private int statusCode;
53     private String JavaDoc reason;
54     private NVPair[] headers;
55     private Map JavaDoc headerMap;
56     // non-normalized
57
private String JavaDoc body;
58
59
60     public ResponseData( String JavaDoc 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 JavaDoc reason ) {
74         this.reason = reason;
75     }
76
77     public String JavaDoc 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     /**
90      * returns null if the header does not exist.
91      * @param name
92      * @return
93      */

94     public String JavaDoc getHeader( String JavaDoc name ) {
95         initializeHeaderMap();
96         String JavaDoc value = (String JavaDoc) headerMap.get( name );
97         return value;
98     }
99
100     private void initializeHeaderMap() {
101         if ( headerMap != null ) {
102             return;
103         }
104         headerMap = new HashMap JavaDoc();
105         NVPair header = null;
106         for ( int i = 0; i < headers.length; i++ ) {
107             header = headers[i];
108             // duplicates are over written
109
headerMap.put( header.getName(), header.getValue() );
110         }
111     }
112
113     public void setBody( String JavaDoc body ) {
114         this.body = body.trim();
115     }
116
117     /**
118      * Returns the non-normalized response body, that is, the response body contains the original
119      * host and port.
120      *
121      * @return
122      */

123     public String JavaDoc getBody() {
124         return body;
125     }
126
127     /**
128      * Returns the normalized response body, with the original port and host replaced with tokens.
129      *
130      * @return
131      */

132     public String JavaDoc getNormalizedBody() {
133         return replaceHostPort( getBody(), host, port );
134     }
135
136     // TODO needs to handle using the default port http://<host>/<path>
137
// TODO https needs to handle port and default port
138
private static String JavaDoc replaceHostPort( String JavaDoc string, String JavaDoc host, int port ) {
139         // replace http://<host>:<port> combination with a non-unique string
140
Pattern JavaDoc pattern = Pattern.compile( HTTP + host + COLON + format.format( port ) );
141         Matcher JavaDoc matcher = pattern.matcher( string );
142         string = matcher.replaceAll( HTTP + NON_UNIQUE_HOST + COLON + NON_UNIQUE_PORT );
143
144         // https
145
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 JavaDoc toString() {
152         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 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