Clover coverage report - Cactus 1.5 for J2EE API 1.2
Coverage timestamp: Wed Feb 18 2004 09:04:33 EST
file stats: LOC: 574   Methods: 44
NCLOC: 255   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AutoReadHttpURLConnection.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  * ====================================================================
 3   
  *
 4   
  * The Apache Software License, Version 1.1
 5   
  *
 6   
  * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
 7   
  * reserved.
 8   
  *
 9   
  * Redistribution and use in source and binary forms, with or without
 10   
  * modification, are permitted provided that the following conditions
 11   
  * are met:
 12   
  *
 13   
  * 1. Redistributions of source code must retain the above copyright
 14   
  *    notice, this list of conditions and the following disclaimer.
 15   
  *
 16   
  * 2. Redistributions in binary form must reproduce the above copyright
 17   
  *    notice, this list of conditions and the following disclaimer in
 18   
  *    the documentation and/or other materials provided with the
 19   
  *    distribution.
 20   
  *
 21   
  * 3. The end-user documentation included with the redistribution, if
 22   
  *    any, must include the following acknowlegement:
 23   
  *       "This product includes software developed by the
 24   
  *        Apache Software Foundation (http://www.apache.org/)."
 25   
  *    Alternately, this acknowlegement may appear in the software itself,
 26   
  *    if and wherever such third-party acknowlegements normally appear.
 27   
  *
 28   
  * 4. The names "The Jakarta Project", "Cactus" and "Apache Software
 29   
  *    Foundation" must not be used to endorse or promote products
 30   
  *    derived from this software without prior written permission. For
 31   
  *    written permission, please contact apache@apache.org.
 32   
  *
 33   
  * 5. Products derived from this software may not be called "Apache"
 34   
  *    nor may "Apache" appear in their names without prior written
 35   
  *    permission of the Apache Group.
 36   
  *
 37   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 38   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 39   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 40   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 41   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 42   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 43   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 44   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 45   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 46   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 47   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 48   
  * SUCH DAMAGE.
 49   
  * ====================================================================
 50   
  *
 51   
  * This software consists of voluntary contributions made by many
 52   
  * individuals on behalf of the Apache Software Foundation.  For more
 53   
  * information on the Apache Software Foundation, please see
 54   
  * <http://www.apache.org/>.
 55   
  *
 56   
  */
 57   
 package org.apache.cactus.client.connector.http;
 58   
 
 59   
 import java.io.BufferedReader;
 60   
 import java.io.ByteArrayInputStream;
 61   
 import java.io.ByteArrayOutputStream;
 62   
 import java.io.IOException;
 63   
 import java.io.InputStream;
 64   
 import java.io.InputStreamReader;
 65   
 import java.io.OutputStream;
 66   
 
 67   
 import java.net.HttpURLConnection;
 68   
 import java.net.ProtocolException;
 69   
 import java.net.URL;
 70   
 
 71   
 import java.security.Permission;
 72   
 
 73   
 import org.apache.commons.logging.Log;
 74   
 import org.apache.commons.logging.LogFactory;
 75   
 
 76   
 /**
 77   
  * Wrapper class for the real <code>HttpURLConnection</code> to the test servlet
 78   
  * that reads the complete input stream into an internal buffer on
 79   
  * the first call to getInputStream(). This is to ensure that the test servlet
 80   
  * is not blocked on i/o when the test caller asks for the results.
 81   
  * <p>
 82   
  * The wrapper returns the buffered input stream from getInputStream and
 83   
  * delegates the rest of the calls.
 84   
  * <p>
 85   
  * This class is final so we don't have to provide access to protected instance
 86   
  * variables and methods of the wrapped connection.
 87   
  *
 88   
  * @author <a href="mailto:Bob.Davison@reuters.com">Bob Davison</a>
 89   
  * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
 90   
  *
 91   
  * @version $Id: AutoReadHttpURLConnection.java,v 1.2 2003/05/26 11:45:26 cmlenz Exp $
 92   
  */
 93   
 final class AutoReadHttpURLConnection extends HttpURLConnection
 94   
 {
 95   
     /**
 96   
      * The logger
 97   
      */
 98   
     private static final Log LOGGER = 
 99   
         LogFactory.getLog(AutoReadHttpURLConnection.class);
 100   
 
 101   
     /**
 102   
      * Default size of array for copying data.
 103   
      */
 104   
     private static final int DEFAULT_CHUNK_SIZE = 16384;
 105   
 
 106   
     /**
 107   
      * The wrapped connection.
 108   
      */
 109   
     private HttpURLConnection delegate;
 110   
 
 111   
     /**
 112   
      * The read input stream.
 113   
      */
 114   
     private InputStream streamBuffer;
 115   
 
 116   
     /**
 117   
      * Constructs a an <code>AutoReadHttpURLConnection</code> object from an
 118   
      * <code>HttpURLConnection</code>.
 119   
      *
 120   
      * @param theConnection the original connection to wrap
 121   
      */
 122  0
     AutoReadHttpURLConnection(HttpURLConnection theConnection)
 123   
     {
 124  0
         super(null);
 125  0
         this.delegate = theConnection;
 126   
     }
 127   
 
 128   
     /**
 129   
      * Returns an input stream containing the fully read contents of
 130   
      * the wrapped connection's input stream
 131   
      *
 132   
      * @return the input stream
 133   
      * @exception IOException if an error occurs when reading the input stream
 134   
      */
 135  0
     public synchronized InputStream getInputStream() throws IOException
 136   
     {
 137   
         // Catch IOException to log the content of the error stream
 138  0
         try
 139   
         {
 140  0
             if (this.streamBuffer == null)
 141   
             {
 142  0
                 LOGGER.debug("Original connection = " + this.delegate);
 143   
 
 144  0
                 InputStream is = this.delegate.getInputStream();
 145   
 
 146  0
                 this.streamBuffer = getBufferedInputStream(is);
 147   
             }
 148   
         }
 149   
         catch (IOException e)
 150   
         {
 151  0
             logErrorStream(this.delegate.getErrorStream());
 152  0
             throw e;
 153   
         }
 154   
 
 155  0
         return this.streamBuffer;
 156   
     }
 157   
 
 158   
     /**
 159   
      * Logs the HTTP error stream (used to get more information when we fail
 160   
      * to read from the HTTP URL connection).
 161   
      *
 162   
      * @param theErrorStream the error stream containing the error description
 163   
      * @exception IOException if an error occurs when reading the input stream
 164   
      */
 165  0
     private void logErrorStream(InputStream theErrorStream) throws IOException
 166   
     {
 167  0
         if (theErrorStream != null)
 168   
         {
 169   
             // Log content of error stream
 170  0
             BufferedReader errorStream = 
 171   
                 new BufferedReader(new InputStreamReader(theErrorStream));
 172  0
             String buffer;
 173   
 
 174  0
             while ((buffer = errorStream.readLine()) != null)
 175   
             {
 176  0
                 LOGGER.debug("ErrorStream [" + buffer + "]");
 177   
             }
 178   
         }
 179   
     }
 180   
 
 181   
     /**
 182   
      * Fully read the HTTP Connection response stream until there is no
 183   
      * more bytes to read.
 184   
      *
 185   
      * @param theInputStream the input stream to fully read
 186   
      * @return the data read as a buffered input stream
 187   
      * @exception IOException if an error occurs when reading the input stream
 188   
      */
 189  0
     private InputStream getBufferedInputStream(InputStream theInputStream)
 190   
         throws IOException
 191   
     {
 192  0
         ByteArrayOutputStream os = 
 193   
             new ByteArrayOutputStream(DEFAULT_CHUNK_SIZE);
 194   
 
 195  0
         copy(theInputStream, os);
 196   
 
 197  0
         ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray());
 198   
 
 199  0
         return bais;
 200   
     }
 201   
 
 202   
     /**
 203   
      * Copies the input stream passed as parameter to the output stream also
 204   
      * passed as parameter. The full stream is read until there is no more
 205   
      * bytes to read.
 206   
      *
 207   
      * @param theInputStream the input stream to read from
 208   
      * @param theOutputStream the output stream to write to
 209   
      * @exception IOException if an error occurs when reading the input stream
 210   
      */
 211  0
     private void copy(InputStream theInputStream, OutputStream theOutputStream)
 212   
         throws IOException
 213   
     {
 214   
         // Only copy if there are data to copy ... The problem is that not
 215   
         // all servers return a content-length header. If there is no header
 216   
         // getContentLength() returns -1. It seems to work and it seems
 217   
         // that all servers that return no content-length header also do
 218   
         // not block on read() operations !
 219  0
         LOGGER.debug("Content-Length : [" + this.delegate.getContentLength()
 220   
             + "]");
 221   
 
 222  0
         if (this.delegate.getContentLength() != 0)
 223   
         {
 224  0
             byte[] buf = new byte[DEFAULT_CHUNK_SIZE];
 225  0
             int count;
 226   
 
 227  0
             while (-1 != (count = theInputStream.read(buf)))
 228   
             {
 229   
                 // log read data
 230  0
                 printReadLogs(count, buf);
 231  0
                 theOutputStream.write(buf, 0, count);
 232   
             }
 233   
         }
 234   
     }
 235   
 
 236   
     /**
 237   
      * Format log data read from socket for pretty printing (replaces
 238   
      * asc char 10 by "\r", asc char 13 by "\n").
 239   
      *
 240   
      * @param theCount the number of bytes read in the buffer
 241   
      * @param theBuffer the buffer containing the data to print
 242   
      */
 243  0
     private void printReadLogs(int theCount, byte[] theBuffer)
 244   
     {
 245   
         // Log portion of read data and replace asc 10 by \r and asc
 246   
         // 13 by /n
 247  0
         StringBuffer prefix = new StringBuffer();
 248   
 
 249  0
         for (int i = 0; i < theCount; i++)
 250   
         {
 251  0
             if (theBuffer[i] == 10)
 252   
             {
 253  0
                 prefix.append("\\r");
 254   
             }
 255  0
             else if (theBuffer[i] == 13)
 256   
             {
 257  0
                 prefix.append("\\n");
 258   
             }
 259   
             else
 260   
             {
 261  0
                 prefix.append((char) theBuffer[i]);
 262   
             }
 263   
         }
 264   
 
 265  0
         LOGGER.debug("Read [" + theCount + "]: [" + prefix + "]");
 266   
     }
 267   
 
 268   
     // Delegated methods
 269   
 
 270   
     /**
 271   
      * @see java.net.HttpURLConnection#connect()
 272   
      */
 273  0
     public void connect() throws IOException
 274   
     {
 275  0
         this.delegate.connect();
 276   
     }
 277   
 
 278   
     /**
 279   
      * @see java.net.HttpURLConnection#getAllowUserInteraction()
 280   
      */
 281  0
     public boolean getAllowUserInteraction()
 282   
     {
 283  0
         return this.delegate.getAllowUserInteraction();
 284   
     }
 285   
 
 286   
     /**
 287   
      * @see java.net.HttpURLConnection#getContent()
 288   
      */
 289  0
     public Object getContent() throws IOException
 290   
     {
 291  0
         return this.delegate.getContent();
 292   
     }
 293   
 
 294   
     /**
 295   
      * @see java.net.HttpURLConnection#getContentEncoding()
 296   
      */
 297  0
     public String getContentEncoding()
 298   
     {
 299  0
         return this.delegate.getContentEncoding();
 300   
     }
 301   
 
 302   
     /**
 303   
      * @see java.net.HttpURLConnection#getContentLength()
 304   
      */
 305  0
     public int getContentLength()
 306   
     {
 307  0
         return this.delegate.getContentLength();
 308   
     }
 309   
 
 310   
     /**
 311   
      * @see java.net.HttpURLConnection#getContentType()
 312   
      */
 313  0
     public String getContentType()
 314   
     {
 315  0
         return this.delegate.getContentType();
 316   
     }
 317   
 
 318   
     /**
 319   
      * @see java.net.HttpURLConnection#getDate()
 320   
      */
 321  0
     public long getDate()
 322   
     {
 323  0
         return this.delegate.getDate();
 324   
     }
 325   
 
 326   
     /**
 327   
      * @see java.net.HttpURLConnection#getDefaultUseCaches()
 328   
      */
 329  0
     public boolean getDefaultUseCaches()
 330   
     {
 331  0
         return this.delegate.getDefaultUseCaches();
 332   
     }
 333   
 
 334   
     /**
 335   
      * @see java.net.HttpURLConnection#getDoInput()
 336   
      */
 337  0
     public boolean getDoInput()
 338   
     {
 339  0
         return this.delegate.getDoInput();
 340   
     }
 341   
 
 342   
     /**
 343   
      * @see java.net.HttpURLConnection#getDoOutput()
 344   
      */
 345  0
     public boolean getDoOutput()
 346   
     {
 347  0
         return this.delegate.getDoOutput();
 348   
     }
 349   
 
 350   
     /**
 351   
      * @see java.net.HttpURLConnection#getExpiration()
 352   
      */
 353  0
     public long getExpiration()
 354   
     {
 355  0
         return this.delegate.getExpiration();
 356   
     }
 357   
 
 358   
     /**
 359   
      * @see java.net.HttpURLConnection#getHeaderField(int)
 360   
      */
 361  0
     public String getHeaderField(int thePosition)
 362   
     {
 363  0
         return this.delegate.getHeaderField(thePosition);
 364   
     }
 365   
 
 366   
     /**
 367   
      * @see java.net.HttpURLConnection#getHeaderField(String)
 368   
      */
 369  0
     public String getHeaderField(String theName)
 370   
     {
 371  0
         return this.delegate.getHeaderField(theName);
 372   
     }
 373   
 
 374   
     /**
 375   
      * @see java.net.HttpURLConnection#getHeaderFieldDate(String, long)
 376   
      */
 377  0
     public long getHeaderFieldDate(String theName, long theDefaultValue)
 378   
     {
 379  0
         return this.delegate.getHeaderFieldDate(theName, theDefaultValue);
 380   
     }
 381   
 
 382   
     /**
 383   
      * @see java.net.HttpURLConnection#getHeaderFieldInt(String, int)
 384   
      */
 385  0
     public int getHeaderFieldInt(String theName, int theDefaultValue)
 386   
     {
 387  0
         return this.delegate.getHeaderFieldInt(theName, theDefaultValue);
 388   
     }
 389   
 
 390   
     /**
 391   
      * @see java.net.HttpURLConnection#getHeaderFieldKey(int)
 392   
      */
 393  0
     public String getHeaderFieldKey(int thePosition)
 394   
     {
 395  0
         return this.delegate.getHeaderFieldKey(thePosition);
 396   
     }
 397   
 
 398   
     /**
 399   
      * @see java.net.HttpURLConnection#getIfModifiedSince()
 400   
      */
 401  0
     public long getIfModifiedSince()
 402   
     {
 403  0
         return this.delegate.getIfModifiedSince();
 404   
     }
 405   
 
 406   
     /**
 407   
      * @see java.net.HttpURLConnection#getLastModified()
 408   
      */
 409  0
     public long getLastModified()
 410   
     {
 411  0
         return this.delegate.getLastModified();
 412   
     }
 413   
 
 414   
     /**
 415   
      * @see java.net.HttpURLConnection#getOutputStream()
 416   
      */
 417  0
     public OutputStream getOutputStream() throws IOException
 418   
     {
 419  0
         return this.delegate.getOutputStream();
 420   
     }
 421   
 
 422   
     /**
 423   
      * @see java.net.HttpURLConnection#getPermission()
 424   
      */
 425  0
     public Permission getPermission() throws IOException
 426   
     {
 427  0
         return this.delegate.getPermission();
 428   
     }
 429   
 
 430   
     /**
 431   
      * @see java.net.HttpURLConnection#getRequestProperty(String)
 432   
      */
 433  0
     public String getRequestProperty(String theKey)
 434   
     {
 435  0
         return this.delegate.getRequestProperty(theKey);
 436   
     }
 437   
 
 438   
     /**
 439   
      * @see java.net.HttpURLConnection#getURL()
 440   
      */
 441  0
     public URL getURL()
 442   
     {
 443  0
         return this.delegate.getURL();
 444   
     }
 445   
 
 446   
     /**
 447   
      * @see java.net.HttpURLConnection#getUseCaches()
 448   
      */
 449  0
     public boolean getUseCaches()
 450   
     {
 451  0
         return this.delegate.getUseCaches();
 452   
     }
 453   
 
 454   
     /**
 455   
      * @see java.net.HttpURLConnection#setAllowUserInteraction(boolean)
 456   
      */
 457  0
     public void setAllowUserInteraction(boolean hasInteraction)
 458   
     {
 459  0
         this.delegate.setAllowUserInteraction(hasInteraction);
 460   
     }
 461   
 
 462   
     /**
 463   
      * @see java.net.HttpURLConnection#setDefaultUseCaches(boolean)
 464   
      */
 465  0
     public void setDefaultUseCaches(boolean isUsingDefaultCache)
 466   
     {
 467  0
         this.delegate.setDefaultUseCaches(isUsingDefaultCache);
 468   
     }
 469   
 
 470   
     /**
 471   
      * @see java.net.HttpURLConnection#setDoInput(boolean)
 472   
      */
 473  0
     public void setDoInput(boolean isInput)
 474   
     {
 475  0
         this.delegate.setDoInput(isInput);
 476   
     }
 477   
 
 478   
     /**
 479   
      * @see java.net.HttpURLConnection#setDoOutput(boolean)
 480   
      */
 481  0
     public void setDoOutput(boolean isOutput)
 482   
     {
 483  0
         this.delegate.setDoOutput(isOutput);
 484   
     }
 485   
 
 486   
     /**
 487   
      * @see java.net.HttpURLConnection#setIfModifiedSince(long)
 488   
      */
 489  0
     public void setIfModifiedSince(long isModifiedSince)
 490   
     {
 491  0
         this.delegate.setIfModifiedSince(isModifiedSince);
 492   
     }
 493   
 
 494   
     /**
 495   
      * @see java.net.HttpURLConnection#setRequestProperty(String, String)
 496   
      */
 497  0
     public void setRequestProperty(String theKey, String theValue)
 498   
     {
 499  0
         this.delegate.setRequestProperty(theKey, theValue);
 500   
     }
 501   
 
 502   
     /**
 503   
      * @see java.net.HttpURLConnection#setUseCaches(boolean)
 504   
      */
 505  0
     public void setUseCaches(boolean isUsingCaches)
 506   
     {
 507  0
         this.delegate.setUseCaches(isUsingCaches);
 508   
     }
 509   
 
 510   
     /**
 511   
      * @see java.net.HttpURLConnection#toString()
 512   
      */
 513  0
     public String toString()
 514   
     {
 515  0
         return this.delegate.toString();
 516   
     }
 517   
 
 518   
     /**
 519   
      * @see java.net.HttpURLConnection#disconnect()
 520   
      */
 521  0
     public void disconnect()
 522   
     {
 523  0
         this.delegate.disconnect();
 524   
     }
 525   
 
 526   
     /**
 527   
      * @see java.net.HttpURLConnection#getErrorStream()
 528   
      */
 529  0
     public InputStream getErrorStream()
 530   
     {
 531  0
         return this.delegate.getErrorStream();
 532   
     }
 533   
 
 534   
     /**
 535   
      * @see java.net.HttpURLConnection#getRequestMethod()
 536   
      */
 537  0
     public String getRequestMethod()
 538   
     {
 539  0
         return this.delegate.getRequestMethod();
 540   
     }
 541   
 
 542   
     /**
 543   
      * @see java.net.HttpURLConnection#getResponseCode()
 544   
      */
 545  0
     public int getResponseCode() throws IOException
 546   
     {
 547  0
         return this.delegate.getResponseCode();
 548   
     }
 549   
 
 550   
     /**
 551   
      * @see java.net.HttpURLConnection#getResponseMessage()
 552   
      */
 553  0
     public String getResponseMessage() throws IOException
 554   
     {
 555  0
         return this.delegate.getResponseMessage();
 556   
     }
 557   
 
 558   
     /**
 559   
      * @see java.net.HttpURLConnection#setRequestMethod(String)
 560   
      */
 561  0
     public void setRequestMethod(String theMethod) throws ProtocolException
 562   
     {
 563  0
         this.delegate.setRequestMethod(theMethod);
 564   
     }
 565   
 
 566   
     /**
 567   
      * @see java.net.HttpURLConnection#usingProxy()
 568   
      */
 569  0
     public boolean usingProxy()
 570   
     {
 571  0
         return this.delegate.usingProxy();
 572   
     }
 573   
 }
 574