Ported wire logging from Commons HttpClient

git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@526418 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2007-04-07 11:18:37 +00:00
parent 9be91137d5
commit b94236fa2e
10 changed files with 478 additions and 77 deletions

View File

@ -33,15 +33,23 @@ package org.apache.http.examples.client;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.PlainSocketFactory;
import org.apache.http.conn.Scheme;
import org.apache.http.conn.SchemeRegistry;
import org.apache.http.conn.SocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.ThreadSafeClientConnManager;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.RequestConnControl;
import org.apache.http.protocol.RequestContent;
@ -50,16 +58,6 @@ import org.apache.http.protocol.RequestTargetHost;
import org.apache.http.protocol.RequestUserAgent;
import org.apache.http.util.EntityUtils;
import org.apache.http.conn.Scheme;
import org.apache.http.conn.SchemeRegistry;
import org.apache.http.conn.SocketFactory;
import org.apache.http.conn.PlainSocketFactory;
import org.apache.http.conn.ClientConnectionManager;
//import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.impl.conn.ThreadSafeClientConnManager;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
/**

View File

@ -33,15 +33,26 @@ package org.apache.http.examples.client;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.RoutedRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.HttpRoute;
import org.apache.http.conn.PlainSocketFactory;
import org.apache.http.conn.Scheme;
import org.apache.http.conn.SchemeRegistry;
import org.apache.http.conn.SocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.ThreadSafeClientConnManager;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.RequestConnControl;
import org.apache.http.protocol.RequestContent;
@ -50,19 +61,6 @@ import org.apache.http.protocol.RequestTargetHost;
import org.apache.http.protocol.RequestUserAgent;
import org.apache.http.util.EntityUtils;
import org.apache.http.conn.Scheme;
import org.apache.http.conn.SchemeRegistry;
import org.apache.http.conn.SocketFactory;
import org.apache.http.conn.PlainSocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.HttpRoute;
//import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.impl.conn.ThreadSafeClientConnManager;
import org.apache.http.client.HttpClient;
import org.apache.http.client.RoutedRequest;
import org.apache.http.impl.client.DefaultHttpClient;
/**

View File

@ -32,28 +32,25 @@
package org.apache.http.examples.conn;
import org.apache.http.HttpHost;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpExecutionContext;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.HttpRoute;
import org.apache.http.conn.ManagedClientConnection;
import org.apache.http.conn.PlainSocketFactory;
import org.apache.http.conn.Scheme;
import org.apache.http.conn.SchemeRegistry;
import org.apache.http.conn.SocketFactory;
import org.apache.http.conn.PlainSocketFactory;
import org.apache.http.conn.HttpRoute;
import org.apache.http.conn.ManagedClientConnection;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.ClientConnectionOperator;
import org.apache.http.impl.conn.ThreadSafeClientConnManager;
import org.apache.http.impl.conn.DefaultClientConnectionOperator;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpExecutionContext;

View File

@ -32,29 +32,26 @@
package org.apache.http.examples.conn;
import org.apache.http.HttpHost;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpExecutionContext;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.HttpRoute;
import org.apache.http.conn.ManagedClientConnection;
import org.apache.http.conn.PlainSocketFactory;
import org.apache.http.conn.Scheme;
import org.apache.http.conn.SchemeRegistry;
import org.apache.http.conn.SocketFactory;
import org.apache.http.conn.PlainSocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.HttpRoute;
import org.apache.http.conn.ManagedClientConnection;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.ClientConnectionOperator;
import org.apache.http.impl.conn.ThreadSafeClientConnManager;
import org.apache.http.impl.conn.DefaultClientConnectionOperator;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpExecutionContext;

View File

@ -32,25 +32,24 @@
package org.apache.http.examples.conn;
import org.apache.http.HttpHost;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpExecutionContext;
import org.apache.http.conn.ClientConnectionOperator;
import org.apache.http.conn.OperatedClientConnection;
import org.apache.http.conn.PlainSocketFactory;
import org.apache.http.conn.Scheme;
import org.apache.http.conn.SchemeRegistry;
import org.apache.http.conn.SocketFactory;
import org.apache.http.conn.PlainSocketFactory;
import org.apache.http.conn.OperatedClientConnection;
import org.apache.http.conn.ClientConnectionOperator;
import org.apache.http.impl.conn.DefaultClientConnectionOperator;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpExecutionContext;

View File

@ -32,26 +32,25 @@
package org.apache.http.examples.conn;
import org.apache.http.HttpHost;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpExecutionContext;
import org.apache.http.conn.ClientConnectionOperator;
import org.apache.http.conn.OperatedClientConnection;
import org.apache.http.conn.PlainSocketFactory;
import org.apache.http.conn.Scheme;
import org.apache.http.conn.SchemeRegistry;
import org.apache.http.conn.SocketFactory;
import org.apache.http.conn.PlainSocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.OperatedClientConnection;
import org.apache.http.conn.ClientConnectionOperator;
import org.apache.http.impl.conn.DefaultClientConnectionOperator;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpExecutionContext;

View File

@ -38,6 +38,8 @@ import java.net.Socket;
import org.apache.http.HttpHost;
import org.apache.http.params.HttpParams;
import org.apache.http.impl.SocketHttpClientConnection;
import org.apache.http.io.HttpDataReceiver;
import org.apache.http.io.HttpDataTransmitter;
import org.apache.http.conn.OperatedClientConnection;
@ -117,6 +119,26 @@ public class DefaultClientConnection extends SocketHttpClientConnection
} // shutdown
protected HttpDataReceiver createHttpDataReceiver(
final HttpParams params) throws IOException {
HttpDataReceiver receiver = super.createHttpDataReceiver(params);
if (Wire.WIRE_LOG.enabled()) {
receiver = new LoggingHttpDataReceiverDecorator(receiver, Wire.WIRE_LOG);
}
return receiver;
}
protected HttpDataTransmitter createHttpDataTransmitter(
final HttpParams params) throws IOException {
HttpDataTransmitter transmitter = super.createHttpDataTransmitter(params);
if (Wire.WIRE_LOG.enabled()) {
transmitter = new LoggingHttpDataTransmitterDecorator(transmitter, Wire.WIRE_LOG);
}
return transmitter;
}
// non-javadoc, see interface OperatedClientConnection
public void open(Socket sock, HttpHost target,
boolean secure, HttpParams params)
@ -137,6 +159,7 @@ public class DefaultClientConnection extends SocketHttpClientConnection
}
bind(sock, params);
targetHost = target;
connSecure = secure;

View File

@ -0,0 +1,117 @@
/*
* $HeadURL$
* $Revision$
* $Date$
*
* ====================================================================
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.impl.conn;
import java.io.IOException;
import org.apache.http.io.HttpDataReceiver;
import org.apache.http.params.HttpParams;
import org.apache.http.util.CharArrayBuffer;
/**
* Logs all data read to the wire LOG.
*
* @author Ortwin Glueck
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
* @since 2.0
*/
class LoggingHttpDataReceiverDecorator implements HttpDataReceiver {
/** Original data receiver. */
private final HttpDataReceiver in;
/** The wire log to use for writing. */
private final Wire wire;
/**
* Create an instance that wraps the specified HTTP data receiver.
* @param in The input stream.
* @param wire The wire log to use.
*/
public LoggingHttpDataReceiverDecorator(final HttpDataReceiver in, final Wire wire) {
super();
this.in = in;
this.wire = wire;
}
public void reset(final HttpParams params) {
this.in.reset(params);
}
public boolean isDataAvailable(int timeout) throws IOException {
return this.in.isDataAvailable(timeout);
}
public int read(byte[] b, int off, int len) throws IOException {
int l = this.in.read(b, off, len);
if (this.wire.enabled() && l > 0) {
this.wire.input(b, off, l);
}
return l;
}
public int read() throws IOException {
int l = this.in.read();
if (this.wire.enabled() && l > 0) {
this.wire.input(l);
}
return l;
}
public int read(byte[] b) throws IOException {
int l = this.in.read(b);
if (this.wire.enabled() && l > 0) {
this.wire.input(b, 0, l);
}
return l;
}
public String readLine() throws IOException {
String s = this.in.readLine();
if (this.wire.enabled() && s != null) {
this.wire.input(s + "[EOL]");
}
return s;
}
public int readLine(final CharArrayBuffer buffer) throws IOException {
int l = this.in.readLine(buffer);
if (this.wire.enabled() && l > 0) {
int pos = buffer.length() - l;
String s = new String(buffer.buffer(), pos, l);
this.wire.input(s + "[EOL]");
}
return l;
}
}

View File

@ -0,0 +1,109 @@
/*
* $HeadURL$
* $Revision$
* $Date$
*
* ====================================================================
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.impl.conn;
import java.io.IOException;
import org.apache.http.io.HttpDataTransmitter;
import org.apache.http.params.HttpParams;
import org.apache.http.util.CharArrayBuffer;
/**
* Logs all data written to the wire LOG.
*
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
* @since 2.0beta1
*/
class LoggingHttpDataTransmitterDecorator implements HttpDataTransmitter {
/** Original data transmitter. */
private final HttpDataTransmitter out;
/** The wire log to use. */
private final Wire wire;
/**
* Create an instance that wraps the specified output stream.
* @param out The output stream.
* @param wire The Wire log to use.
*/
public LoggingHttpDataTransmitterDecorator(final HttpDataTransmitter out, final Wire wire) {
super();
this.out = out;
this.wire = wire;
}
public void reset(final HttpParams params) {
this.out.reset(params);
}
public void write(byte[] b, int off, int len) throws IOException {
this.out.write(b, off, len);
if (this.wire.enabled()) {
this.wire.output(b, off, len);
}
}
public void write(int b) throws IOException {
this.out.write(b);
if (this.wire.enabled()) {
this.wire.output(b);
}
}
public void write(byte[] b) throws IOException {
this.out.write(b);
if (this.wire.enabled()) {
this.wire.output(b);
}
}
public void flush() throws IOException {
this.out.flush();
}
public void writeLine(final CharArrayBuffer buffer) throws IOException {
this.out.writeLine(buffer);
if (this.wire.enabled()) {
String s = new String(buffer.buffer(), 0, buffer.length());
this.wire.output(s + "[EOL]");
}
}
public void writeLine(final String s) throws IOException {
this.out.writeLine(s);
if (this.wire.enabled()) {
this.wire.output(s + "[EOL]");
}
}
}

View File

@ -0,0 +1,164 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/Wire.java,v 1.9 2004/06/24 21:39:52 mbecke Exp $
* $Revision$
* $Date$
*
* ====================================================================
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.impl.conn;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Logs data to the wire LOG.
*
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
* @since 2.0beta1
*/
class Wire {
public static Wire WIRE_LOG = new Wire(LogFactory.getLog("org.apache.http.wire"));
/** Log for any wire messages. */
private Log log;
private Wire(Log log) {
this.log = log;
}
private void wire(String header, InputStream instream)
throws IOException {
StringBuffer buffer = new StringBuffer();
int ch;
while ((ch = instream.read()) != -1) {
if (ch == 13) {
buffer.append("[\\r]");
} else if (ch == 10) {
buffer.append("[\\n]\"");
buffer.insert(0, "\"");
buffer.insert(0, header);
log.debug(buffer.toString());
buffer.setLength(0);
} else if ((ch < 32) || (ch > 127)) {
buffer.append("[0x");
buffer.append(Integer.toHexString(ch));
buffer.append("]");
} else {
buffer.append((char) ch);
}
}
if (buffer.length() > 0) {
buffer.append("\"");
buffer.insert(0, "\"");
buffer.insert(0, header);
log.debug(buffer.toString());
}
}
public boolean enabled() {
return log.isDebugEnabled();
}
public void output(InputStream outstream)
throws IOException {
if (outstream == null) {
throw new IllegalArgumentException("Output may not be null");
}
wire(">> ", outstream);
}
public void input(InputStream instream)
throws IOException {
if (instream == null) {
throw new IllegalArgumentException("Input may not be null");
}
wire("<< ", instream);
}
public void output(byte[] b, int off, int len)
throws IOException {
if (b == null) {
throw new IllegalArgumentException("Output may not be null");
}
wire(">> ", new ByteArrayInputStream(b, off, len));
}
public void input(byte[] b, int off, int len)
throws IOException {
if (b == null) {
throw new IllegalArgumentException("Input may not be null");
}
wire("<< ", new ByteArrayInputStream(b, off, len));
}
public void output(byte[] b)
throws IOException {
if (b == null) {
throw new IllegalArgumentException("Output may not be null");
}
wire(">> ", new ByteArrayInputStream(b));
}
public void input(byte[] b)
throws IOException {
if (b == null) {
throw new IllegalArgumentException("Input may not be null");
}
wire("<< ", new ByteArrayInputStream(b));
}
public void output(int b)
throws IOException {
output(new byte[] {(byte) b});
}
public void input(int b)
throws IOException {
input(new byte[] {(byte) b});
}
public void output(final String s)
throws IOException {
if (s == null) {
throw new IllegalArgumentException("Output may not be null");
}
output(s.getBytes());
}
public void input(final String s)
throws IOException {
if (s == null) {
throw new IllegalArgumentException("Input may not be null");
}
input(s.getBytes());
}
}