1 17 18 package org.apache.coyote; 19 20 import java.io.IOException ; 21 import java.util.HashMap ; 22 23 import org.apache.tomcat.util.buf.ByteChunk; 24 import org.apache.tomcat.util.buf.MessageBytes; 25 import org.apache.tomcat.util.buf.UDecoder; 26 27 import org.apache.tomcat.util.http.MimeHeaders; 28 import org.apache.tomcat.util.http.Parameters; 29 import org.apache.tomcat.util.http.ContentType; 30 import org.apache.tomcat.util.http.Cookies; 31 32 66 public final class Request { 67 68 69 71 72 public Request() { 73 74 parameters.setQuery(queryMB); 75 parameters.setURLDecoder(urlDecoder); 76 parameters.setHeaders(headers); 77 78 } 79 80 81 83 84 private int serverPort = -1; 85 private MessageBytes serverNameMB = MessageBytes.newInstance(); 86 87 private int remotePort; 88 private int localPort; 89 90 private MessageBytes schemeMB = MessageBytes.newInstance(); 91 92 private MessageBytes methodMB = MessageBytes.newInstance(); 93 private MessageBytes unparsedURIMB = MessageBytes.newInstance(); 94 private MessageBytes uriMB = MessageBytes.newInstance(); 95 private MessageBytes decodedUriMB = MessageBytes.newInstance(); 96 private MessageBytes queryMB = MessageBytes.newInstance(); 97 private MessageBytes protoMB = MessageBytes.newInstance(); 98 99 private MessageBytes remoteAddrMB = MessageBytes.newInstance(); 101 private MessageBytes localNameMB = MessageBytes.newInstance(); 102 private MessageBytes remoteHostMB = MessageBytes.newInstance(); 103 private MessageBytes localAddrMB = MessageBytes.newInstance(); 104 105 private MimeHeaders headers = new MimeHeaders(); 106 107 private MessageBytes instanceId = MessageBytes.newInstance(); 108 109 112 private Object notes[] = new Object [Constants.MAX_NOTES]; 113 114 115 118 private InputBuffer inputBuffer = null; 119 120 121 124 private UDecoder urlDecoder = new UDecoder(); 125 126 127 130 private long contentLength = -1; 131 private MessageBytes contentTypeMB = null; 132 private String charEncoding = null; 133 private Cookies cookies = new Cookies(headers); 134 private Parameters parameters = new Parameters(); 135 136 private MessageBytes remoteUser=MessageBytes.newInstance(); 137 private MessageBytes authType=MessageBytes.newInstance(); 138 private HashMap attributes=new HashMap (); 139 140 private Response response; 141 private ActionHook hook; 142 143 private int bytesRead=0; 144 private long startTime = 0L; 146 147 private RequestInfo reqProcessorMX=new RequestInfo(this); 148 150 151 158 public MessageBytes instanceId() { 159 return instanceId; 160 } 161 162 163 public MimeHeaders getMimeHeaders() { 164 return headers; 165 } 166 167 168 public UDecoder getURLDecoder() { 169 return urlDecoder; 170 } 171 172 174 175 public MessageBytes scheme() { 176 return schemeMB; 177 } 178 179 public MessageBytes method() { 180 return methodMB; 181 } 182 183 public MessageBytes unparsedURI() { 184 return unparsedURIMB; 185 } 186 187 public MessageBytes requestURI() { 188 return uriMB; 189 } 190 191 public MessageBytes decodedURI() { 192 return decodedUriMB; 193 } 194 195 public MessageBytes query() { 196 return queryMB; 197 } 198 199 public MessageBytes queryString() { 200 return queryMB; 201 } 202 203 public MessageBytes protocol() { 204 return protoMB; 205 } 206 207 214 public MessageBytes serverName() { 215 return serverNameMB; 216 } 217 218 public int getServerPort() { 219 return serverPort; 220 } 221 222 public void setServerPort(int serverPort ) { 223 this.serverPort=serverPort; 224 } 225 226 public MessageBytes remoteAddr() { 227 return remoteAddrMB; 228 } 229 230 public MessageBytes remoteHost() { 231 return remoteHostMB; 232 } 233 234 public MessageBytes localName() { 235 return localNameMB; 236 } 237 238 public MessageBytes localAddr() { 239 return localAddrMB; 240 } 241 242 public int getRemotePort(){ 243 return remotePort; 244 } 245 246 public void setRemotePort(int port){ 247 this.remotePort = port; 248 } 249 250 public int getLocalPort(){ 251 return localPort; 252 } 253 254 public void setLocalPort(int port){ 255 this.localPort = port; 256 } 257 258 260 261 264 public String getCharacterEncoding() { 265 266 if (charEncoding != null) 267 return charEncoding; 268 269 charEncoding = ContentType.getCharsetFromContentType(getContentType()); 270 return charEncoding; 271 272 } 273 274 275 public void setCharacterEncoding(String enc) { 276 this.charEncoding = enc; 277 } 278 279 280 public void setContentLength(int len) { 281 this.contentLength = len; 282 } 283 284 285 public int getContentLength() { 286 long length = getContentLengthLong(); 287 288 if (length < Integer.MAX_VALUE) { 289 return (int) length; 290 } 291 return -1; 292 } 293 294 public long getContentLengthLong() { 295 if( contentLength > -1 ) return contentLength; 296 297 MessageBytes clB = headers.getValue("content-length"); 298 contentLength = (clB == null || clB.isNull()) ? -1 : clB.getLong(); 299 300 return contentLength; 301 } 302 303 public String getContentType() { 304 contentType(); 305 if ((contentTypeMB == null) || contentTypeMB.isNull()) 306 return null; 307 return contentTypeMB.toString(); 308 } 309 310 311 public void setContentType(String type) { 312 contentTypeMB.setString(type); 313 } 314 315 316 public MessageBytes contentType() { 317 if (contentTypeMB == null) 318 contentTypeMB = headers.getValue("content-type"); 319 return contentTypeMB; 320 } 321 322 323 public void setContentType(MessageBytes mb) { 324 contentTypeMB=mb; 325 } 326 327 328 public String getHeader(String name) { 329 return headers.getHeader(name); 330 } 331 332 334 public Response getResponse() { 335 return response; 336 } 337 338 public void setResponse( Response response ) { 339 this.response=response; 340 response.setRequest( this ); 341 } 342 343 public void action(ActionCode actionCode, Object param) { 344 if( hook==null && response!=null ) 345 hook=response.getHook(); 346 347 if (hook != null) { 348 if( param==null ) 349 hook.action(actionCode, this); 350 else 351 hook.action(actionCode, param); 352 } 353 } 354 355 356 358 359 public Cookies getCookies() { 360 return cookies; 361 } 362 363 364 366 367 public Parameters getParameters() { 368 return parameters; 369 } 370 371 372 375 public void setAttribute( String name, Object o ) { 376 attributes.put( name, o ); 377 } 378 379 public HashMap getAttributes() { 380 return attributes; 381 } 382 383 public Object getAttribute(String name ) { 384 return attributes.get(name); 385 } 386 387 public MessageBytes getRemoteUser() { 388 return remoteUser; 389 } 390 391 public MessageBytes getAuthType() { 392 return authType; 393 } 394 395 397 398 public InputBuffer getInputBuffer() { 399 return inputBuffer; 400 } 401 402 403 public void setInputBuffer(InputBuffer inputBuffer) { 404 this.inputBuffer = inputBuffer; 405 } 406 407 408 417 public int doRead(ByteChunk chunk) 418 throws IOException { 419 int n = inputBuffer.doRead(chunk, this); 420 if (n > 0) { 421 bytesRead+=n; 422 } 423 return n; 424 } 425 426 427 429 public String toString() { 430 return "R( " + requestURI().toString() + ")"; 431 } 432 433 public long getStartTime() { 434 return startTime; 435 } 436 437 public void setStartTime(long startTime) { 438 this.startTime = startTime; 439 } 440 441 443 444 463 public final void setNote(int pos, Object value) { 464 notes[pos] = value; 465 } 466 467 468 public final Object getNote(int pos) { 469 return notes[pos]; 470 } 471 472 473 475 476 public void recycle() { 477 bytesRead=0; 478 479 contentLength = -1; 480 contentTypeMB = null; 481 charEncoding = null; 482 headers.recycle(); 483 serverNameMB.recycle(); 484 serverPort=-1; 485 localPort = -1; 486 remotePort = -1; 487 488 cookies.recycle(); 489 parameters.recycle(); 490 491 unparsedURIMB.recycle(); 492 uriMB.recycle(); 493 decodedUriMB.recycle(); 494 queryMB.recycle(); 495 methodMB.recycle(); 496 protoMB.recycle(); 497 498 schemeMB.recycle(); 499 500 instanceId.recycle(); 501 remoteUser.recycle(); 502 authType.recycle(); 503 attributes.clear(); 504 } 505 506 public void updateCounters() { 508 reqProcessorMX.updateCounters(); 509 } 510 511 public RequestInfo getRequestProcessor() { 512 return reqProcessorMX; 513 } 514 515 public int getBytesRead() { 516 return bytesRead; 517 } 518 519 public void setBytesRead(int bytesRead) { 520 this.bytesRead = bytesRead; 521 } 522 } 523 | Popular Tags |