1 18 19 package org.apache.beehive.netui.tools.testrecorder.shared; 20 21 import java.io.UnsupportedEncodingException ; 22 import java.util.Map ; 23 import java.util.Enumeration ; 24 import java.util.List ; 25 import java.util.ArrayList ; 26 import java.util.Collections ; 27 28 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.Cookie ; 31 32 33 36 public class RequestData { 37 38 private static final Logger log = Logger.getInstance( RequestData.class ); 39 40 private String protocol; 41 private String protocolVersion; 42 private String host; 43 private int port; 44 private String method; 45 private String path; 46 private NVPair[] headers; 47 private NVPair[] parameters; 48 private Cookie [] cookies; 49 50 public RequestData() { 51 } 52 53 58 public String getUri() { 59 return getUri( getHost(), getPort() ); 60 } 61 62 69 public String getUri( String host, int port ) { 70 return genUri( protocol, host, port, getPath() ); 71 } 72 73 public void setProtocol( String protocol ) { 74 this.protocol = protocol; 75 } 76 77 public String getProtocol() { 78 return protocol; 79 } 80 81 public void setProtocolVersion( String protocolVersion ) { 82 this.protocolVersion = protocolVersion; 83 } 84 85 public String getProtocolVersion() { 86 return protocolVersion; 87 } 88 89 public void setHost( String host ) { 90 this.host = host; 91 } 92 93 public String getHost() { 94 return host; 95 } 96 97 public void setMethod( String method ) { 98 this.method = method; 99 } 100 101 public String getMethod() { 102 return method; 103 } 104 105 public void setPath( String path ) { 106 this.path = path; 107 } 108 109 public String 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 getParamName( int index ) { 137 return ( (NVPair) parameters[index] ).getName(); 138 } 139 140 public String 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 getHeaderName( int index ) { 160 return ( (NVPair) headers[index] ).getName(); 161 } 162 163 public String getHeaderValue( int index ) { 164 return ( (NVPair) headers[index] ).getValue(); 165 } 166 167 public void setCookies( Cookie [] cookies ) { 168 this.cookies = cookies; 169 } 170 171 public Cookie [] 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 getCookieName( int index ) { 183 return ( (Cookie ) cookies[index] ).getName(); 184 } 185 186 public String getCookieValue( int index ) { 187 return ( (Cookie ) cookies[index] ).getValue(); 188 } 189 190 public static String genUri( String protocol, String host, int port, String path ) { 191 return protocol + "://" + host + ":" + port + path; 192 } 193 194 public static RequestData populate( HttpServletRequest request, RequestData data ) { 195 String protocol = request.getProtocol(); 196 int index = protocol.indexOf( "/" ); 197 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 request ) { 213 Map map = request.getParameterMap(); 214 Enumeration e = request.getParameterNames(); 215 List list = new ArrayList (); 216 String param = null; 217 while ( e.hasMoreElements() ) { 218 param = (String ) e.nextElement(); 219 list.add( param ); 223 } 224 Collections.sort( list ); 225 List pairs = new ArrayList ( list.size() ); 226 for ( int i = 0; i < list.size(); i++ ) { 227 String [] vals = (String []) 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 String value = new String ( bytes, "UTF-8" ); 241 242 pairs.add( new NVPair( (String ) list.get( i ), value ) ); 243 } catch ( UnsupportedEncodingException uee ) { 244 } 246 } 247 } 248 return (NVPair[]) pairs.toArray( (Object []) new NVPair[pairs.size()] ); 249 } 250 251 public static NVPair[] getHeaders( HttpServletRequest request ) { 252 Enumeration e = request.getHeaderNames(); 253 List list = new ArrayList (); 254 String name = null; 255 while ( e.hasMoreElements() ) { 256 name = (String ) e.nextElement(); 257 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 ) list.get( i ), 266 request.getHeader( (String ) list.get( i ) ) ); 267 } 271 return pairs; 272 } 273 274 public String toString() { 275 StringBuffer sb = new StringBuffer ( 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 |