1 20 package org.apache.cactus.internal.client.connector.http; 21 22 import java.io.BufferedReader ; 23 import java.io.ByteArrayInputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.InputStreamReader ; 28 import java.io.OutputStream ; 29 30 import java.net.HttpURLConnection ; 31 import java.net.ProtocolException ; 32 import java.net.URL ; 33 34 import java.security.Permission ; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 53 final class AutoReadHttpURLConnection extends HttpURLConnection 54 { 55 58 private static final Log LOGGER = 59 LogFactory.getLog(AutoReadHttpURLConnection.class); 60 61 64 private static final int DEFAULT_CHUNK_SIZE = 16384; 65 66 69 private HttpURLConnection delegate; 70 71 74 private InputStream streamBuffer; 75 76 82 AutoReadHttpURLConnection(HttpURLConnection theConnection) 83 { 84 super(null); 85 this.delegate = theConnection; 86 } 87 88 95 public synchronized InputStream getInputStream() throws IOException 96 { 97 try 99 { 100 if (this.streamBuffer == null) 101 { 102 LOGGER.debug("Original connection = " + this.delegate); 103 104 InputStream is = this.delegate.getInputStream(); 105 106 this.streamBuffer = getBufferedInputStream(is); 107 } 108 } 109 catch (IOException e) 110 { 111 logErrorStream(this.delegate.getErrorStream()); 112 throw e; 113 } 114 115 return this.streamBuffer; 116 } 117 118 125 private void logErrorStream(InputStream theErrorStream) throws IOException 126 { 127 if (theErrorStream != null) 128 { 129 BufferedReader errorStream = 131 new BufferedReader (new InputStreamReader (theErrorStream)); 132 String buffer; 133 134 while ((buffer = errorStream.readLine()) != null) 135 { 136 LOGGER.debug("ErrorStream [" + buffer + "]"); 137 } 138 } 139 } 140 141 149 private InputStream getBufferedInputStream(InputStream theInputStream) 150 throws IOException 151 { 152 ByteArrayOutputStream os = 153 new ByteArrayOutputStream (DEFAULT_CHUNK_SIZE); 154 155 copy(theInputStream, os); 156 157 ByteArrayInputStream bais = new ByteArrayInputStream (os.toByteArray()); 158 159 return bais; 160 } 161 162 171 private void copy(InputStream theInputStream, OutputStream theOutputStream) 172 throws IOException 173 { 174 LOGGER.debug("Content-Length : [" + this.delegate.getContentLength() 180 + "]"); 181 182 if (theInputStream != null && this.delegate.getContentLength() != 0) 183 { 184 byte[] buf = new byte[DEFAULT_CHUNK_SIZE]; 185 int count; 186 187 while (-1 != (count = theInputStream.read(buf))) 188 { 189 printReadLogs(count, buf); 191 theOutputStream.write(buf, 0, count); 192 } 193 } 194 } 195 196 203 private void printReadLogs(int theCount, byte[] theBuffer) 204 { 205 StringBuffer prefix = new StringBuffer (); 208 209 for (int i = 0; i < theCount; i++) 210 { 211 if (theBuffer[i] == 10) 212 { 213 prefix.append("\\r"); 214 } 215 else if (theBuffer[i] == 13) 216 { 217 prefix.append("\\n"); 218 } 219 else 220 { 221 prefix.append((char) theBuffer[i]); 222 } 223 } 224 225 LOGGER.debug("Read [" + theCount + "]: [" + prefix + "]"); 226 } 227 228 230 233 public void connect() throws IOException 234 { 235 this.delegate.connect(); 236 } 237 238 241 public boolean getAllowUserInteraction() 242 { 243 return this.delegate.getAllowUserInteraction(); 244 } 245 246 249 public Object getContent() throws IOException 250 { 251 return this.delegate.getContent(); 252 } 253 254 257 public String getContentEncoding() 258 { 259 return this.delegate.getContentEncoding(); 260 } 261 262 265 public int getContentLength() 266 { 267 return this.delegate.getContentLength(); 268 } 269 270 273 public String getContentType() 274 { 275 return this.delegate.getContentType(); 276 } 277 278 281 public long getDate() 282 { 283 return this.delegate.getDate(); 284 } 285 286 289 public boolean getDefaultUseCaches() 290 { 291 return this.delegate.getDefaultUseCaches(); 292 } 293 294 297 public boolean getDoInput() 298 { 299 return this.delegate.getDoInput(); 300 } 301 302 305 public boolean getDoOutput() 306 { 307 return this.delegate.getDoOutput(); 308 } 309 310 313 public long getExpiration() 314 { 315 return this.delegate.getExpiration(); 316 } 317 318 321 public String getHeaderField(int thePosition) 322 { 323 return this.delegate.getHeaderField(thePosition); 324 } 325 326 329 public String getHeaderField(String theName) 330 { 331 return this.delegate.getHeaderField(theName); 332 } 333 334 337 public long getHeaderFieldDate(String theName, long theDefaultValue) 338 { 339 return this.delegate.getHeaderFieldDate(theName, theDefaultValue); 340 } 341 342 345 public int getHeaderFieldInt(String theName, int theDefaultValue) 346 { 347 return this.delegate.getHeaderFieldInt(theName, theDefaultValue); 348 } 349 350 353 public String getHeaderFieldKey(int thePosition) 354 { 355 return this.delegate.getHeaderFieldKey(thePosition); 356 } 357 358 361 public long getIfModifiedSince() 362 { 363 return this.delegate.getIfModifiedSince(); 364 } 365 366 369 public long getLastModified() 370 { 371 return this.delegate.getLastModified(); 372 } 373 374 377 public OutputStream getOutputStream() throws IOException 378 { 379 return this.delegate.getOutputStream(); 380 } 381 382 385 public Permission getPermission() throws IOException 386 { 387 return this.delegate.getPermission(); 388 } 389 390 393 public String getRequestProperty(String theKey) 394 { 395 return this.delegate.getRequestProperty(theKey); 396 } 397 398 401 public URL getURL() 402 { 403 return this.delegate.getURL(); 404 } 405 406 409 public boolean getUseCaches() 410 { 411 return this.delegate.getUseCaches(); 412 } 413 414 417 public void setAllowUserInteraction(boolean hasInteraction) 418 { 419 this.delegate.setAllowUserInteraction(hasInteraction); 420 } 421 422 425 public void setDefaultUseCaches(boolean isUsingDefaultCache) 426 { 427 this.delegate.setDefaultUseCaches(isUsingDefaultCache); 428 } 429 430 433 public void setDoInput(boolean isInput) 434 { 435 this.delegate.setDoInput(isInput); 436 } 437 438 441 public void setDoOutput(boolean isOutput) 442 { 443 this.delegate.setDoOutput(isOutput); 444 } 445 446 449 public void setIfModifiedSince(long isModifiedSince) 450 { 451 this.delegate.setIfModifiedSince(isModifiedSince); 452 } 453 454 457 public void setRequestProperty(String theKey, String theValue) 458 { 459 this.delegate.setRequestProperty(theKey, theValue); 460 } 461 462 465 public void setUseCaches(boolean isUsingCaches) 466 { 467 this.delegate.setUseCaches(isUsingCaches); 468 } 469 470 473 public String toString() 474 { 475 return this.delegate.toString(); 476 } 477 478 481 public void disconnect() 482 { 483 this.delegate.disconnect(); 484 } 485 486 489 public InputStream getErrorStream() 490 { 491 return this.delegate.getErrorStream(); 492 } 493 494 497 public String getRequestMethod() 498 { 499 return this.delegate.getRequestMethod(); 500 } 501 502 505 public int getResponseCode() throws IOException 506 { 507 return this.delegate.getResponseCode(); 508 } 509 510 513 public String getResponseMessage() throws IOException 514 { 515 return this.delegate.getResponseMessage(); 516 } 517 518 521 public void setRequestMethod(String theMethod) throws ProtocolException 522 { 523 this.delegate.setRequestMethod(theMethod); 524 } 525 526 529 public boolean usingProxy() 530 { 531 return this.delegate.usingProxy(); 532 } 533 } 534 | Popular Tags |