From 697556b9b0a94f0ec2f0d04da221d9d7476f2062 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Wed, 28 Nov 2012 13:35:01 +0000 Subject: [PATCH] DefaultClientConnection deprecated in favor of package private ClientConnectionImpl git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1414672 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/impl/conn/ClientConnectionImpl.java | 136 ++++++++++++++++++ .../impl/conn/DefaultClientConnection.java | 13 +- .../http/impl/conn/LoggingInputStream.java | 104 ++++++++++++++ .../http/impl/conn/LoggingOutputStream.java | 74 ++++++++++ .../impl/conn/LoggingSessionInputBuffer.java | 4 +- .../java/org/apache/http/impl/conn/Wire.java | 17 ++- 6 files changed, 333 insertions(+), 15 deletions(-) create mode 100644 httpclient/src/main/java/org/apache/http/impl/conn/ClientConnectionImpl.java create mode 100644 httpclient/src/main/java/org/apache/http/impl/conn/LoggingInputStream.java create mode 100644 httpclient/src/main/java/org/apache/http/impl/conn/LoggingOutputStream.java diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/ClientConnectionImpl.java b/httpclient/src/main/java/org/apache/http/impl/conn/ClientConnectionImpl.java new file mode 100644 index 000000000..c4957bdb7 --- /dev/null +++ b/httpclient/src/main/java/org/apache/http/impl/conn/ClientConnectionImpl.java @@ -0,0 +1,136 @@ +/* + * ==================================================================== + * 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 + * . + * + */ + +package org.apache.http.impl.conn; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CharsetEncoder; +import java.util.concurrent.atomic.AtomicLong; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.http.Header; +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; +import org.apache.http.config.MessageConstraints; +import org.apache.http.entity.ContentLengthStrategy; +import org.apache.http.impl.DefaultBHttpClientConnection; +import org.apache.http.io.HttpMessageParserFactory; +import org.apache.http.io.HttpMessageWriterFactory; + +class ClientConnectionImpl extends DefaultBHttpClientConnection { + + private static final AtomicLong COUNT = new AtomicLong(); + + private final String id; + private final Log log; + private final Log headerlog; + private final Wire wire; + + public ClientConnectionImpl( + int buffersize, + final CharsetDecoder chardecoder, + final CharsetEncoder charencoder, + final MessageConstraints constraints, + final ContentLengthStrategy incomingContentStrategy, + final ContentLengthStrategy outgoingContentStrategy, + final HttpMessageWriterFactory requestWriterFactory, + final HttpMessageParserFactory responseParserFactory) { + super(buffersize, chardecoder, charencoder, + constraints, incomingContentStrategy, outgoingContentStrategy, + requestWriterFactory, responseParserFactory); + this.id = "http-outgoing-" + COUNT.incrementAndGet(); + this.log = LogFactory.getLog(getClass()); + this.headerlog = LogFactory.getLog("org.apache.http.headers"); + this.wire = new Wire(LogFactory.getLog("org.apache.http.wire"), this.id); + } + + public ClientConnectionImpl(int buffersize) { + this(buffersize, null, null, null, null, null, null, null); + } + + @Override + public void close() throws IOException { + if (this.log.isDebugEnabled()) { + this.log.debug(this.id + ": Close connection"); + } + super.close(); + } + + @Override + public void shutdown() throws IOException { + if (this.log.isDebugEnabled()) { + this.log.debug(this.id + ": Shutdown connection"); + } + super.shutdown(); + } + + @Override + protected InputStream getSocketInputStream(final Socket socket) throws IOException { + InputStream in = super.getSocketInputStream(socket); + if (this.wire.enabled()) { + in = new LoggingInputStream(in, this.wire); + } + return in; + } + + @Override + protected OutputStream getSocketOutputStream(final Socket socket) throws IOException { + OutputStream out = super.getSocketOutputStream(socket); + if (this.wire.enabled()) { + out = new LoggingOutputStream(out, this.wire); + } + return out; + } + + @Override + protected void onResponseReceived(final HttpResponse response) { + if (response != null && this.headerlog.isDebugEnabled()) { + this.headerlog.debug(this.id + " << " + response.getStatusLine().toString()); + Header[] headers = response.getAllHeaders(); + for (int i = 0; i < headers.length; i++) { + this.headerlog.debug(this.id + " << " + headers[i].toString()); + } + } + } + + @Override + protected void onRequestSubmitted(final HttpRequest request) { + if (request != null && this.headerlog.isDebugEnabled()) { + this.headerlog.debug(id + " >> " + request.getRequestLine().toString()); + Header[] headers = request.getAllHeaders(); + for (int i = 0; i < headers.length; i++) { + this.headerlog.debug(this.id + " >> " + headers[i].toString()); + } + } + } + +} diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java b/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java index df5479004..8a27ead7e 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java @@ -59,21 +59,14 @@ import org.apache.http.conn.OperatedClientConnection; /** * Default implementation of an operated client connection. - *

- * The following parameters can be used to customize the behavior of this - * class: - *

    - *
  • {@link org.apache.http.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING}
  • - *
  • {@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}
  • - *
  • {@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}
  • - *
  • {@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}
  • - *
  • {@link org.apache.http.params.CoreConnectionPNames#MAX_HEADER_COUNT}
  • - *
* * @since 4.0 + * + * @deprecated (4.3) deprecated in favor of {@link ClientConnectionImpl}. */ @SuppressWarnings("deprecation") @NotThreadSafe // connSecure, targetHost +@Deprecated public class DefaultClientConnection extends SocketHttpClientConnection implements OperatedClientConnection, HttpSSLConnection, HttpContext { diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/LoggingInputStream.java b/httpclient/src/main/java/org/apache/http/impl/conn/LoggingInputStream.java new file mode 100644 index 000000000..45179fb9d --- /dev/null +++ b/httpclient/src/main/java/org/apache/http/impl/conn/LoggingInputStream.java @@ -0,0 +1,104 @@ +/* + * ==================================================================== + * 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 + * . + * + */ + +package org.apache.http.impl.conn; + +import java.io.IOException; +import java.io.InputStream; + +/** + * @since 4.3 + */ +public class LoggingInputStream extends InputStream { + + private final InputStream in; + private final Wire wire; + + public LoggingInputStream(final InputStream in, final Wire wire) { + super(); + this.in = in; + this.wire = wire; + } + + @Override + public int read() throws IOException { + int b = in.read(); + if (b != -1) { + wire.input(b); + } + return b; + } + + @Override + public int read(byte[] b) throws IOException { + int bytesRead = in.read(b); + if (bytesRead != -1) { + wire.input(b, 0, bytesRead); + } + return bytesRead; + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + int bytesRead = in.read(b, off, len); + if (bytesRead != -1) { + wire.input(b, off, bytesRead); + } + return bytesRead; + } + + @Override + public long skip(long n) throws IOException { + return super.skip(n); + } + + @Override + public int available() throws IOException { + return in.available(); + } + + @Override + public synchronized void mark(int readlimit) { + super.mark(readlimit); + } + + @Override + public synchronized void reset() throws IOException { + super.reset(); + } + + @Override + public boolean markSupported() { + return false; + } + + @Override + public void close() throws IOException { + in.close(); + } + +} diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/LoggingOutputStream.java b/httpclient/src/main/java/org/apache/http/impl/conn/LoggingOutputStream.java new file mode 100644 index 000000000..cc09b0044 --- /dev/null +++ b/httpclient/src/main/java/org/apache/http/impl/conn/LoggingOutputStream.java @@ -0,0 +1,74 @@ +/* + * ==================================================================== + * 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 + * . + * + */ + +package org.apache.http.impl.conn; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * @since 4.3 + */ +public class LoggingOutputStream extends OutputStream { + + private final OutputStream out; + private final Wire wire; + + public LoggingOutputStream(final OutputStream out, final Wire wire) { + super(); + this.out = out; + this.wire = wire; + } + + @Override + public void write(int b) throws IOException { + wire.output(b); + } + + @Override + public void write(byte[] b) throws IOException { + wire.output(b); + out.write(b); + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + wire.output(b, off, len); + out.write(b, off, len); + } + + @Override + public void flush() throws IOException { + out.flush(); + } + + @Override + public void close() throws IOException { + out.close(); + } + +} diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/LoggingSessionInputBuffer.java b/httpclient/src/main/java/org/apache/http/impl/conn/LoggingSessionInputBuffer.java index ff6cfa21e..afcbb1ff3 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/LoggingSessionInputBuffer.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/LoggingSessionInputBuffer.java @@ -39,10 +39,12 @@ import org.apache.http.util.CharArrayBuffer; /** * Logs all data read to the wire LOG. * - * * @since 4.0 + * + * @deprecated (4.3) no longer used. */ @Immutable +@Deprecated public class LoggingSessionInputBuffer implements SessionInputBuffer, EofSensor { /** Original session input buffer. */ diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/Wire.java b/httpclient/src/main/java/org/apache/http/impl/conn/Wire.java index 11cb49236..38b9d11e6 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/Wire.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/Wire.java @@ -36,7 +36,7 @@ import org.apache.commons.logging.Log; /** * Logs data to the wire LOG. - * + * TODO: make package private. Should not be part of the public API. * * @since 4.0 */ @@ -44,9 +44,18 @@ import org.apache.commons.logging.Log; public class Wire { private final Log log; + private final String id; + + /** + * @since 4.3 + */ + public Wire(Log log, String id) { + this.log = log; + this.id = id; + } public Wire(Log log) { - this.log = log; + this(log, ""); } private void wire(String header, InputStream instream) @@ -60,7 +69,7 @@ public class Wire { buffer.append("[\\n]\""); buffer.insert(0, "\""); buffer.insert(0, header); - log.debug(buffer.toString()); + log.debug(id + " " + buffer.toString()); buffer.setLength(0); } else if ((ch < 32) || (ch > 127)) { buffer.append("[0x"); @@ -74,7 +83,7 @@ public class Wire { buffer.append('\"'); buffer.insert(0, '\"'); buffer.insert(0, header); - log.debug(buffer.toString()); + log.debug(id + " " + buffer.toString()); } }