View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.conn;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  import java.net.Socket;
34  import java.nio.charset.CharsetDecoder;
35  import java.nio.charset.CharsetEncoder;
36  
37  import org.apache.commons.logging.Log;
38  import org.apache.http.Header;
39  import org.apache.http.HttpRequest;
40  import org.apache.http.HttpResponse;
41  import org.apache.http.config.MessageConstraints;
42  import org.apache.http.entity.ContentLengthStrategy;
43  import org.apache.http.io.HttpMessageParserFactory;
44  import org.apache.http.io.HttpMessageWriterFactory;
45  
46  class LoggingManagedHttpClientConnection extends DefaultManagedHttpClientConnection {
47  
48      private final Log log;
49      private final Log headerlog;
50      private final Wire wire;
51  
52      public LoggingManagedHttpClientConnection(
53              final String id,
54              final Log log,
55              final Log headerlog,
56              final Log wirelog,
57              final int buffersize,
58              final int fragmentSizeHint,
59              final CharsetDecoder chardecoder,
60              final CharsetEncoder charencoder,
61              final MessageConstraints constraints,
62              final ContentLengthStrategy incomingContentStrategy,
63              final ContentLengthStrategy outgoingContentStrategy,
64              final HttpMessageWriterFactory<HttpRequest> requestWriterFactory,
65              final HttpMessageParserFactory<HttpResponse> responseParserFactory) {
66          super(id, buffersize, fragmentSizeHint, chardecoder, charencoder,
67                  constraints, incomingContentStrategy, outgoingContentStrategy,
68                  requestWriterFactory, responseParserFactory);
69          this.log = log;
70          this.headerlog = headerlog;
71          this.wire = new Wire(wirelog, id);
72      }
73  
74      @Override
75      public void close() throws IOException {
76  
77          if (super.isOpen()) {
78              if (this.log.isDebugEnabled()) {
79                  this.log.debug(getId() + ": Close connection");
80              }
81              super.close();
82          }
83      }
84  
85      @Override
86      public void setSocketTimeout(final int timeout) {
87          if (this.log.isDebugEnabled()) {
88              this.log.debug(getId() + ": set socket timeout to " + timeout);
89          }
90          super.setSocketTimeout(timeout);
91      }
92  
93      @Override
94      public void shutdown() throws IOException {
95          if (this.log.isDebugEnabled()) {
96              this.log.debug(getId() + ": Shutdown connection");
97          }
98          super.shutdown();
99      }
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 }