You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Peter Bryant <p....@pingability.com> on 2005/06/23 06:50:58 UTC

HttpConnection IOWrapper Patch

Hi.  I needed to count the bytes sent/received for each http connection.

The approach I wanted to take was to grab the input and output streams 
of the http connection when they were created and wrap them in byte 
counting input/output streams.

I did not see a mechanism to do this, so I added a new interface and an 
extra parameter object to the httpconnectionparamters class.

The same approach may be useful, e.g., for someone to plug in i/o 
streams that debug output what is going out/in on the wire.

I submit the following code/idea to be included in the httpclient code. 
  (As then I won't have to maintain my own codebase with this change in it).


diff -Naur 
../commons-httpclient-3.0-rc2/src/java/org/apache/commons/httpclient/HttpConnection.java 
commons-httpclient-3.0-rc2/src/java/org/apache/commons/httpclient/HttpConnection.java
--- 
../commons-httpclient-3.0-rc2/src/java/org/apache/commons/httpclient/HttpConnection.java 
    2005-06-23 15:38:01.000000000 +1200
+++ 
commons-httpclient-3.0-rc2/src/java/org/apache/commons/httpclient/HttpConnection.java 
       2005-04-10 15:04:09.000000000 +1200
@@ -741,11 +741,6 @@
              }
              inputStream = new 
BufferedInputStream(socket.getInputStream(), inbuffersize);
              outputStream = new 
BufferedOutputStream(socket.getOutputStream(), outbuffersize);
-            IOWrapper ioWrapper = 
(IOWrapper)this.params.getParameter("iowrapper");
-            if(ioWrapper!=null) {
-                inputStream = ioWrapper.wrapInput(inputStream);
-                outputStream = ioWrapper.wrapOutput(outputStream);
-            }
              isOpen = true;
          } catch (IOException e) {
              // Connection wasn't opened properly
@@ -804,11 +799,6 @@
          }
          inputStream = new BufferedInputStream(socket.getInputStream(), 
inbuffersize);
          outputStream = new 
BufferedOutputStream(socket.getOutputStream(), outbuffersize);
-        IOWrapper ioWrapper = 
(IOWrapper)this.params.getParameter("iowrapper");
-        if(ioWrapper!=null) {
-            inputStream = ioWrapper.wrapInput(inputStream);
-            outputStream = ioWrapper.wrapOutput(outputStream);
-        }
          usingSecureSocket = true;
          tunnelEstablished = true;
      }
@@ -1372,8 +1362,4 @@

      /** The local interface on which the connection is created, or 
null for the default */
      private InetAddress localAddress;
-    public interface IOWrapper {
-        InputStream wrapInput(InputStream is);
-        OutputStream wrapOutput(OutputStream os);
-    }
  }


Sample code to use it:
     public static class ByteCountingWrapper implements 
HttpConnection.IOWrapper {
         int read;
         int sent;
         private IOUtil.CountInputStream cis;
         private IOUtil.CountOutputStream cos;
         public InputStream wrapInput(InputStream is) {
             if(cis!=null) {
                 read+=cis.getBytesRead();
             }
             return cis = new IOUtil.CountInputStream(is);
         }
         public OutputStream wrapOutput(OutputStream out) {
             if(cos!=null) {
                 sent+=cos.getCount();
             }
             return cos = new IOUtil.CountOutputStream(out);
         }
     }
         HttpClient client = new HttpClient();
         HttpConnectionManager mgr = client.getHttpConnectionManager();
         HttpConnectionManagerParams params = mgr.getParams();
         ByteCountingWrapper bcw = new ByteCountingWrapper();
         params.setParameter("iowrapper", bcw);
...

Regards, Peter
http://pingability.com - Web Site Monitoring Service

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org


Re: HttpConnection IOWrapper Patch

Posted by Michael Becke <mb...@gmail.com>.
It's also possible to implement a custom HttpConnectionManager that
just returns wrapped connections that count bytes.

Mike

On 6/25/05, Oleg Kalnichevski <ol...@apache.org> wrote:
> Peter,
> 
> I am very reluctant to accept any changes that extend the API so late in
> the release process, especially since there's a solution to this problem
> that does not require any changes in HttpClient at all, albeit much less
> elegant. One can simply wrap the input stream returned by
> HttpMethod#getResponseAsStream to measure the incoming data and provide
> a custom RequestEntity to measure the outgoing data
> 
> HttpClient 4.0 will provide a better architecture allowing custom
> extensions to be injected without braking the existing components.
> 
> Oleg
> 
> 
> On Thu, 2005-06-23 at 16:50 +1200, Peter Bryant wrote:
> > Hi.  I needed to count the bytes sent/received for each http connection.
> >
> > The approach I wanted to take was to grab the input and output streams
> > of the http connection when they were created and wrap them in byte
> > counting input/output streams.
> >
> > I did not see a mechanism to do this, so I added a new interface and an
> > extra parameter object to the httpconnectionparamters class.
> >
> > The same approach may be useful, e.g., for someone to plug in i/o
> > streams that debug output what is going out/in on the wire.
> >
> > I submit the following code/idea to be included in the httpclient code.
> >   (As then I won't have to maintain my own codebase with this change in it).
> >
> >
> > diff -Naur
> > ../commons-httpclient-3.0-rc2/src/java/org/apache/commons/httpclient/HttpConnection.java
> > commons-httpclient-3.0-rc2/src/java/org/apache/commons/httpclient/HttpConnection.java
> > ---
> > ../commons-httpclient-3.0-rc2/src/java/org/apache/commons/httpclient/HttpConnection.java
> >     2005-06-23 15:38:01.000000000 +1200
> > +++
> > commons-httpclient-3.0-rc2/src/java/org/apache/commons/httpclient/HttpConnection.java
> >        2005-04-10 15:04:09.000000000 +1200
> > @@ -741,11 +741,6 @@
> >               }
> >               inputStream = new
> > BufferedInputStream(socket.getInputStream(), inbuffersize);
> >               outputStream = new
> > BufferedOutputStream(socket.getOutputStream(), outbuffersize);
> > -            IOWrapper ioWrapper =
> > (IOWrapper)this.params.getParameter("iowrapper");
> > -            if(ioWrapper!=null) {
> > -                inputStream = ioWrapper.wrapInput(inputStream);
> > -                outputStream = ioWrapper.wrapOutput(outputStream);
> > -            }
> >               isOpen = true;
> >           } catch (IOException e) {
> >               // Connection wasn't opened properly
> > @@ -804,11 +799,6 @@
> >           }
> >           inputStream = new BufferedInputStream(socket.getInputStream(),
> > inbuffersize);
> >           outputStream = new
> > BufferedOutputStream(socket.getOutputStream(), outbuffersize);
> > -        IOWrapper ioWrapper =
> > (IOWrapper)this.params.getParameter("iowrapper");
> > -        if(ioWrapper!=null) {
> > -            inputStream = ioWrapper.wrapInput(inputStream);
> > -            outputStream = ioWrapper.wrapOutput(outputStream);
> > -        }
> >           usingSecureSocket = true;
> >           tunnelEstablished = true;
> >       }
> > @@ -1372,8 +1362,4 @@
> >
> >       /** The local interface on which the connection is created, or
> > null for the default */
> >       private InetAddress localAddress;
> > -    public interface IOWrapper {
> > -        InputStream wrapInput(InputStream is);
> > -        OutputStream wrapOutput(OutputStream os);
> > -    }
> >   }
> >
> >
> > Sample code to use it:
> >      public static class ByteCountingWrapper implements
> > HttpConnection.IOWrapper {
> >          int read;
> >          int sent;
> >          private IOUtil.CountInputStream cis;
> >          private IOUtil.CountOutputStream cos;
> >          public InputStream wrapInput(InputStream is) {
> >              if(cis!=null) {
> >                  read+=cis.getBytesRead();
> >              }
> >              return cis = new IOUtil.CountInputStream(is);
> >          }
> >          public OutputStream wrapOutput(OutputStream out) {
> >              if(cos!=null) {
> >                  sent+=cos.getCount();
> >              }
> >              return cos = new IOUtil.CountOutputStream(out);
> >          }
> >      }
> >          HttpClient client = new HttpClient();
> >          HttpConnectionManager mgr = client.getHttpConnectionManager();
> >          HttpConnectionManagerParams params = mgr.getParams();
> >          ByteCountingWrapper bcw = new ByteCountingWrapper();
> >          params.setParameter("iowrapper", bcw);
> > ...
> >
> > Regards, Peter
> > http://pingability.com - Web Site Monitoring Service
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> >
> >
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org


Re: HttpConnection IOWrapper Patch

Posted by Oleg Kalnichevski <ol...@apache.org>.
Peter, 

I am very reluctant to accept any changes that extend the API so late in
the release process, especially since there's a solution to this problem
that does not require any changes in HttpClient at all, albeit much less
elegant. One can simply wrap the input stream returned by
HttpMethod#getResponseAsStream to measure the incoming data and provide
a custom RequestEntity to measure the outgoing data

HttpClient 4.0 will provide a better architecture allowing custom
extensions to be injected without braking the existing components.

Oleg


On Thu, 2005-06-23 at 16:50 +1200, Peter Bryant wrote:
> Hi.  I needed to count the bytes sent/received for each http connection.
> 
> The approach I wanted to take was to grab the input and output streams 
> of the http connection when they were created and wrap them in byte 
> counting input/output streams.
> 
> I did not see a mechanism to do this, so I added a new interface and an 
> extra parameter object to the httpconnectionparamters class.
> 
> The same approach may be useful, e.g., for someone to plug in i/o 
> streams that debug output what is going out/in on the wire.
> 
> I submit the following code/idea to be included in the httpclient code. 
>   (As then I won't have to maintain my own codebase with this change in it).
> 
> 
> diff -Naur 
> ../commons-httpclient-3.0-rc2/src/java/org/apache/commons/httpclient/HttpConnection.java 
> commons-httpclient-3.0-rc2/src/java/org/apache/commons/httpclient/HttpConnection.java
> --- 
> ../commons-httpclient-3.0-rc2/src/java/org/apache/commons/httpclient/HttpConnection.java 
>     2005-06-23 15:38:01.000000000 +1200
> +++ 
> commons-httpclient-3.0-rc2/src/java/org/apache/commons/httpclient/HttpConnection.java 
>        2005-04-10 15:04:09.000000000 +1200
> @@ -741,11 +741,6 @@
>               }
>               inputStream = new 
> BufferedInputStream(socket.getInputStream(), inbuffersize);
>               outputStream = new 
> BufferedOutputStream(socket.getOutputStream(), outbuffersize);
> -            IOWrapper ioWrapper = 
> (IOWrapper)this.params.getParameter("iowrapper");
> -            if(ioWrapper!=null) {
> -                inputStream = ioWrapper.wrapInput(inputStream);
> -                outputStream = ioWrapper.wrapOutput(outputStream);
> -            }
>               isOpen = true;
>           } catch (IOException e) {
>               // Connection wasn't opened properly
> @@ -804,11 +799,6 @@
>           }
>           inputStream = new BufferedInputStream(socket.getInputStream(), 
> inbuffersize);
>           outputStream = new 
> BufferedOutputStream(socket.getOutputStream(), outbuffersize);
> -        IOWrapper ioWrapper = 
> (IOWrapper)this.params.getParameter("iowrapper");
> -        if(ioWrapper!=null) {
> -            inputStream = ioWrapper.wrapInput(inputStream);
> -            outputStream = ioWrapper.wrapOutput(outputStream);
> -        }
>           usingSecureSocket = true;
>           tunnelEstablished = true;
>       }
> @@ -1372,8 +1362,4 @@
> 
>       /** The local interface on which the connection is created, or 
> null for the default */
>       private InetAddress localAddress;
> -    public interface IOWrapper {
> -        InputStream wrapInput(InputStream is);
> -        OutputStream wrapOutput(OutputStream os);
> -    }
>   }
> 
> 
> Sample code to use it:
>      public static class ByteCountingWrapper implements 
> HttpConnection.IOWrapper {
>          int read;
>          int sent;
>          private IOUtil.CountInputStream cis;
>          private IOUtil.CountOutputStream cos;
>          public InputStream wrapInput(InputStream is) {
>              if(cis!=null) {
>                  read+=cis.getBytesRead();
>              }
>              return cis = new IOUtil.CountInputStream(is);
>          }
>          public OutputStream wrapOutput(OutputStream out) {
>              if(cos!=null) {
>                  sent+=cos.getCount();
>              }
>              return cos = new IOUtil.CountOutputStream(out);
>          }
>      }
>          HttpClient client = new HttpClient();
>          HttpConnectionManager mgr = client.getHttpConnectionManager();
>          HttpConnectionManagerParams params = mgr.getParams();
>          ByteCountingWrapper bcw = new ByteCountingWrapper();
>          params.setParameter("iowrapper", bcw);
> ...
> 
> Regards, Peter
> http://pingability.com - Web Site Monitoring Service
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org