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 org.apache.commons.logging.Log;
31  import org.apache.http.Header;
32  import org.apache.http.HttpRequest;
33  import org.apache.http.HttpResponse;
34  import org.apache.http.annotation.NotThreadSafe;
35  import org.apache.http.config.MessageConstraints;
36  import org.apache.http.entity.ContentLengthStrategy;
37  import org.apache.http.io.HttpMessageParserFactory;
38  import org.apache.http.io.HttpMessageWriterFactory;
39  
40  import java.io.IOException;
41  import java.io.InputStream;
42  import java.io.OutputStream;
43  import java.net.Socket;
44  import java.nio.charset.CharsetDecoder;
45  import java.nio.charset.CharsetEncoder;
46  
47  @NotThreadSafe
48  class LoggingManagedHttpClientConnection extends DefaultManagedHttpClientConnection {
49  
50      private final Log log;
51      private final Log headerlog;
52      private final Wire wire;
53  
54      public LoggingManagedHttpClientConnection(
55              final String id,
56              final Log log,
57              final Log headerlog,
58              final Log wirelog,
59              final int buffersize,
60              final int fragmentSizeHint,
61              final CharsetDecoder chardecoder,
62              final CharsetEncoder charencoder,
63              final MessageConstraints constraints,
64              final ContentLengthStrategy incomingContentStrategy,
65              final ContentLengthStrategy outgoingContentStrategy,
66              final HttpMessageWriterFactory<HttpRequest> requestWriterFactory,
67              final HttpMessageParserFactory<HttpResponse> responseParserFactory) {
68          super(id, buffersize, fragmentSizeHint, chardecoder, charencoder,
69                  constraints, incomingContentStrategy, outgoingContentStrategy,
70                  requestWriterFactory, responseParserFactory);
71          this.log = log;
72          this.headerlog = headerlog;
73          this.wire = new Wire(wirelog, id);
74      }
75  
76      @Override
77      public void close() throws IOException {
78  
79          if (super.isOpen()) {
80              if (this.log.isDebugEnabled()) {
81                  this.log.debug(getId() + ": Close connection");
82              }
83              super.close();
84          }
85      }
86  
87      @Override
88      public void setSocketTimeout(final int timeout) {
89          if (this.log.isDebugEnabled()) {
90              this.log.debug(getId() + ": set socket timeout to " + timeout);
91          }
92          super.setSocketTimeout(timeout);
93      }
94  
95      @Override
96      public void shutdown() throws IOException {
97          if (this.log.isDebugEnabled()) {
98              this.log.debug(getId() + ": Shutdown connection");
99          }
100         super.shutdown();
101     }
102 
103     @Override
104     protected InputStream getSocketInputStream(final Socket socket) throws IOException {
105         InputStream in = super.getSocketInputStream(socket);
106         if (this.wire.enabled()) {
107             in = new LoggingInputStream(in, this.wire);
108         }
109         return in;
110     }
111 
112     @Override
113     protected OutputStream getSocketOutputStream(final Socket socket) throws IOException {
114         OutputStream out = super.getSocketOutputStream(socket);
115         if (this.wire.enabled()) {
116             out = new LoggingOutputStream(out, this.wire);
117         }
118         return out;
119     }
120 
121     @Override
122     protected void onResponseReceived(final HttpResponse response) {
123         if (response != null && this.headerlog.isDebugEnabled()) {
124             this.headerlog.debug(getId() + " << " + response.getStatusLine().toString());
125             final Header[] headers = response.getAllHeaders();
126             for (final Header header : headers) {
127                 this.headerlog.debug(getId() + " << " + header.toString());
128             }
129         }
130     }
131 
132     @Override
133     protected void onRequestSubmitted(final HttpRequest request) {
134         if (request != null && this.headerlog.isDebugEnabled()) {
135             this.headerlog.debug(getId() + " >> " + request.getRequestLine().toString());
136             final Header[] headers = request.getAllHeaders();
137             for (final Header header : headers) {
138                 this.headerlog.debug(getId() + " >> " + header.toString());
139             }
140         }
141     }
142 
143 }