KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.UnsupportedEncodingException JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collections JavaDoc;
27
28 // J2EE dependencies
29
import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.Cookie JavaDoc;
31
32
33 /**
34  * This class has a dependency on javax.servlet.http
35  */

36 public class RequestData {
37
38     private static final Logger log = Logger.getInstance( RequestData.class );
39
40     private String JavaDoc protocol;
41     private String JavaDoc protocolVersion;
42     private String JavaDoc host;
43     private int port;
44     private String JavaDoc method;
45     private String JavaDoc path;
46     private NVPair[] headers;
47     private NVPair[] parameters;
48     private Cookie JavaDoc[] cookies;
49
50     public RequestData() {
51     }
52
53     /**
54      * returns the original request for this data
55      *
56      * @return
57      */

58     public String JavaDoc getUri() {
59         return getUri( getHost(), getPort() );
60     }
61
62     /**
63      * returns the Uri for this data but replaces the host and port with the provided values.
64      *
65      * @param host
66      * @param port
67      * @return
68      */

69     public String JavaDoc getUri( String JavaDoc host, int port ) {
70         return genUri( protocol, host, port, getPath() );
71     }
72
73     public void setProtocol( String JavaDoc protocol ) {
74         this.protocol = protocol;
75     }
76
77     public String JavaDoc getProtocol() {
78         return protocol;
79     }
80
81     public void setProtocolVersion( String JavaDoc protocolVersion ) {
82         this.protocolVersion = protocolVersion;
83     }
84
85     public String JavaDoc getProtocolVersion() {
86         return protocolVersion;
87     }
88
89     public void setHost( String JavaDoc host ) {
90         this.host = host;
91     }
92
93     public String JavaDoc getHost() {
94         return host;
95     }
96
97     public void setMethod( String JavaDoc method ) {
98         this.method = method;
99     }
100
101     public String JavaDoc getMethod() {
102         return method;
103     }
104
105     public void setPath( String JavaDoc path ) {
106         this.path = path;
107     }
108
109     public String JavaDoc getPath() {
110         return path;
111     }
112
113     public void setPort( int port ) {
114         this.port = port;
115     }
116
117     public int getPort() {
118         return port;
119     }
120
121     public void setParameters( NVPair[] parameters ) {
122         this.parameters = parameters;
123     }
124
125     public NVPair[] getParameters() {
126         return parameters;
127     }
128
129     public int getParamCount() {
130         if ( parameters == null ) {
131             return 0;
132         }
133         return parameters.length;
134     }
135
136     public String JavaDoc getParamName( int index ) {
137         return ( (NVPair) parameters[index] ).getName();
138     }
139
140     public String JavaDoc getParamValue( int index ) {
141         return ( (NVPair) parameters[index] ).getValue();
142     }
143
144     public void setHeaders( NVPair[] headers ) {
145         this.headers = headers;
146     }
147
148     public NVPair[] getHeaders() {
149         return headers;
150     }
151
152     public int getHeaderCount() {
153         if ( headers == null ) {
154             return 0;
155         }
156         return headers.length;
157     }
158
159     public String JavaDoc getHeaderName( int index ) {
160         return ( (NVPair) headers[index] ).getName();
161     }
162
163     public String JavaDoc getHeaderValue( int index ) {
164         return ( (NVPair) headers[index] ).getValue();
165     }
166
167     public void setCookies( Cookie JavaDoc[] cookies ) {
168         this.cookies = cookies;
169     }
170
171     public Cookie JavaDoc[] getCookies() {
172         return cookies;
173     }
174
175     public int getCookieCount() {
176         if ( cookies == null ) {
177             return 0;
178         }
179         return cookies.length;
180     }
181
182     public String JavaDoc getCookieName( int index ) {
183         return ( (Cookie JavaDoc) cookies[index] ).getName();
184     }
185
186     public String JavaDoc getCookieValue( int index ) {
187         return ( (Cookie JavaDoc) cookies[index] ).getValue();
188     }
189
190     public static String JavaDoc genUri( String JavaDoc protocol, String JavaDoc host, int port, String JavaDoc path ) {
191         return protocol + "://" + host + ":" + port + path;
192     }
193
194     public static RequestData populate( HttpServletRequest JavaDoc request, RequestData data ) {
195         String JavaDoc protocol = request.getProtocol();
196         int index = protocol.indexOf( "/" );
197         // make sure a slash exists and its not at the end of the string
198
if ( index > -1 && index < protocol.length() - 1 ) {
199             data.setProtocol( protocol.substring( 0, index ) );
200             data.setProtocolVersion( protocol.substring( index + 1 ) );
201         }
202         data.setHost( request.getServerName() );
203         data.setPort( request.getServerPort() );
204         data.setMethod( request.getMethod() );
205         data.setPath( request.getRequestURI() );
206         data.setParameters( getParams( request ) );
207         data.setHeaders( getHeaders( request ) );
208         data.setCookies( request.getCookies() );
209         return data;
210     }
211
212     public static NVPair[] getParams( HttpServletRequest JavaDoc request ) {
213         Map JavaDoc map = request.getParameterMap();
214         Enumeration JavaDoc e = request.getParameterNames();
215         List JavaDoc list = new ArrayList JavaDoc();
216         String JavaDoc param = null;
217         while ( e.hasMoreElements() ) {
218             param = (String JavaDoc) e.nextElement();
219 // if ( log.isDebugEnabled() ) {
220
// log.debug( "param name( " + param + " )" );
221
// }
222
list.add( param );
223         }
224         Collections.sort( list );
225         List JavaDoc pairs = new ArrayList JavaDoc( list.size() );
226         for ( int i = 0; i < list.size(); i++ ) {
227             String JavaDoc[] vals = (String JavaDoc[]) map.get( list.get( i ) );
228             for ( int j = 0; j < vals.length; j++ ) {
229                 try {
230                     byte[] bytes = vals[j].getBytes( "ISO-8859-1" );
231
232                     //
233
// todo: We should try to use a field defined in the
234
// test admin page, when recording a new test, that
235
// provides the expected character set encoding to use
236
// for the bytes of the values in the query parameters.
237
// The character set encoding used in the test would be
238
// stored in the recorded XML to be used during playback.
239
//
240
String JavaDoc value = new String JavaDoc( bytes, "UTF-8" );
241
242                     pairs.add( new NVPair( (String JavaDoc) list.get( i ), value ) );
243                 } catch ( UnsupportedEncodingException JavaDoc uee ) {
244                     // Should never hit this
245
}
246             }
247         }
248         return (NVPair[]) pairs.toArray( (Object JavaDoc[]) new NVPair[pairs.size()] );
249     }
250
251     public static NVPair[] getHeaders( HttpServletRequest JavaDoc request ) {
252         Enumeration JavaDoc e = request.getHeaderNames();
253         List JavaDoc list = new ArrayList JavaDoc();
254         String JavaDoc name = null;
255         while ( e.hasMoreElements() ) {
256             name = (String JavaDoc) e.nextElement();
257 // if ( log.isDebugEnabled() ) {
258
// log.debug( "header name( " + name + " )" );
259
// }
260
list.add( name );
261         }
262         Collections.sort( list );
263         NVPair[] pairs = new NVPair[list.size()];
264         for ( int i = 0; i < list.size(); i++ ) {
265             pairs[i] = new NVPair( (String JavaDoc) list.get( i ),
266                     request.getHeader( (String JavaDoc) list.get( i ) ) );
267 // if ( log.isDebugEnabled() ) {
268
// log.debug( "header: name( " + pairs[i].getName() + " ), value( " + pairs[i].getValue() + " )" );
269
// }
270
}
271         return pairs;
272     }
273
274     public String JavaDoc toString() {
275         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 256 );
276         sb.append( "[ " );
277         sb.append( "protocol( " + protocol + " )" );
278         sb.append( ", protocolVersion( " + protocolVersion + " )" );
279         sb.append( ", host( " + host + " )" );
280         sb.append( ", port( " + port + " )" );
281         sb.append( ", method( " + method + " )" );
282         sb.append( ", path( " + path + " )" );
283         sb.append( ", headers( " + Util.toString( headers ) + " )" );
284         sb.append( ", parameters( " + Util.toString( parameters ) + " )" );
285         sb.append( ", cookies( " + Util.toString( cookies ) + " )" );
286
287         sb.append( " ]" );
288         return sb.toString();
289     }
290
291 }
292
Popular Tags