001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.impl.conn;
029
030import java.io.IOException;
031import java.io.InputStream;
032import java.io.OutputStream;
033import java.net.Socket;
034import java.nio.charset.CharsetDecoder;
035import java.nio.charset.CharsetEncoder;
036
037import org.apache.commons.logging.Log;
038import org.apache.http.Header;
039import org.apache.http.HttpRequest;
040import org.apache.http.HttpResponse;
041import org.apache.http.config.MessageConstraints;
042import org.apache.http.entity.ContentLengthStrategy;
043import org.apache.http.io.HttpMessageParserFactory;
044import org.apache.http.io.HttpMessageWriterFactory;
045
046class LoggingManagedHttpClientConnection extends DefaultManagedHttpClientConnection {
047
048    private final Log log;
049    private final Log headerlog;
050    private final Wire wire;
051
052    public LoggingManagedHttpClientConnection(
053            final String id,
054            final Log log,
055            final Log headerlog,
056            final Log wirelog,
057            final int buffersize,
058            final int fragmentSizeHint,
059            final CharsetDecoder chardecoder,
060            final CharsetEncoder charencoder,
061            final MessageConstraints constraints,
062            final ContentLengthStrategy incomingContentStrategy,
063            final ContentLengthStrategy outgoingContentStrategy,
064            final HttpMessageWriterFactory<HttpRequest> requestWriterFactory,
065            final HttpMessageParserFactory<HttpResponse> responseParserFactory) {
066        super(id, buffersize, fragmentSizeHint, chardecoder, charencoder,
067                constraints, incomingContentStrategy, outgoingContentStrategy,
068                requestWriterFactory, responseParserFactory);
069        this.log = log;
070        this.headerlog = headerlog;
071        this.wire = new Wire(wirelog, id);
072    }
073
074    @Override
075    public void close() throws IOException {
076
077        if (super.isOpen()) {
078            if (this.log.isDebugEnabled()) {
079                this.log.debug(getId() + ": Close connection");
080            }
081            super.close();
082        }
083    }
084
085    @Override
086    public void setSocketTimeout(final int timeout) {
087        if (this.log.isDebugEnabled()) {
088            this.log.debug(getId() + ": set socket timeout to " + timeout);
089        }
090        super.setSocketTimeout(timeout);
091    }
092
093    @Override
094    public void shutdown() throws IOException {
095        if (this.log.isDebugEnabled()) {
096            this.log.debug(getId() + ": Shutdown connection");
097        }
098        super.shutdown();
099    }
100
101    @Override
102    protected InputStream getSocketInputStream(final Socket socket) throws IOException {
103        InputStream in = super.getSocketInputStream(socket);
104        if (this.wire.enabled()) {
105            in = new LoggingInputStream(in, this.wire);
106        }
107        return in;
108    }
109
110    @Override
111    protected OutputStream getSocketOutputStream(final Socket socket) throws IOException {
112        OutputStream out = super.getSocketOutputStream(socket);
113        if (this.wire.enabled()) {
114            out = new LoggingOutputStream(out, this.wire);
115        }
116        return out;
117    }
118
119    @Override
120    protected void onResponseReceived(final HttpResponse response) {
121        if (response != null && this.headerlog.isDebugEnabled()) {
122            this.headerlog.debug(getId() + " << " + response.getStatusLine().toString());
123            final Header[] headers = response.getAllHeaders();
124            for (final Header header : headers) {
125                this.headerlog.debug(getId() + " << " + header.toString());
126            }
127        }
128    }
129
130    @Override
131    protected void onRequestSubmitted(final HttpRequest request) {
132        if (request != null && this.headerlog.isDebugEnabled()) {
133            this.headerlog.debug(getId() + " >> " + request.getRequestLine().toString());
134            final Header[] headers = request.getAllHeaders();
135            for (final Header header : headers) {
136                this.headerlog.debug(getId() + " >> " + header.toString());
137            }
138        }
139    }
140
141}