/** * e.g. usage: * ByteArrayOutputStream osCopy = new ByteArrayOutputStream(); * .. . * client.getHttpConnectionManager().getParams().setParameter("iowrapper", new WireWrapper(osCopy, osCopy)); * ... * client.executeMethod(gm); * .. * System.out.println("input/output: " + new String(osCopy.toByteArray())); * * Will output something like: * GET / HTTP/1.1 * User-Agent: Jakarta Commons-HttpClient/3.0 * Host: google.com * * HTTP/1.1 301 Moved Permanently * Location: http://www.google.com/ * Set-Cookie: PREF=ID=1f19f8bb9ac951a9:TM=1135227992:LM=1135227992:S=Ly6FRGzsKeYnIlWw; expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com * Content-Type: text/html * Server: GWS/2.1 * Content-Length: 222 * Date: Thu, 22 Dec 2005 05:06:32 GMT * ... */ public static class WireWrapper implements HttpConnection.IOWrapper { public WireWrapper(OutputStream _isCopy, OutputStream _osCopy) { isCopy = _isCopy; osCopy=_osCopy; } OutputStream isCopy; OutputStream osCopy; public InputStream wrapInput(InputStream is) { // this input stream will copy everything read from is to isCopy return new IOUtil.CopyInputStream(is, isCopy); } public OutputStream wrapOutput(OutputStream out) { // this outputstream will copy everything written to out to osCopy return new IOUtil.CopyOutputStream(out, osCopy); } } /** * This wrapper counts the bytes written/read in an http transaction. */ 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) { // cis will not be null, e.g. if the original connection is closed // and the httpclient does a redirect (which can require a new connection) read+=cis.getByteCount(); } return cis = new IOUtil.CountInputStream(is); } public OutputStream wrapOutput(OutputStream out) { if(cos!=null) { sent+=cos.getByteCount(); } return cos = new IOUtil.CountOutputStream(out); } }