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          if (this.log.isDebugEnabled()) {
79              this.log.debug(getId() + ": Close connection");
80          }
81          super.close();
82      }
83  
84      @Override
85      public void shutdown() throws IOException {
86          if (this.log.isDebugEnabled()) {
87              this.log.debug(getId() + ": Shutdown connection");
88          }
89          super.shutdown();
90      }
91  
92      @Override
93      protected InputStream getSocketInputStream(final Socket socket) throws IOException {
94          InputStream in = super.getSocketInputStream(socket);
95          if (this.wire.enabled()) {
96              in = new LoggingInputStream(in, this.wire);
97          }
98          return in;
99      }
100 
101     @Override
102     protected OutputStream getSocketOutputStream(final Socket socket) throws IOException {
103         OutputStream out = super.getSocketOutputStream(socket);
104         if (this.wire.enabled()) {
105             out = new LoggingOutputStream(out, this.wire);
106         }
107         return out;
108     }
109 
110     @Override
111     protected void onResponseReceived(final HttpResponse response) {
112         if (response != null && this.headerlog.isDebugEnabled()) {
113             this.headerlog.debug(getId() + " << " + response.getStatusLine().toString());
114             final Header[] headers = response.getAllHeaders();
115             for (final Header header : headers) {
116                 this.headerlog.debug(getId() + " << " + header.toString());
117             }
118         }
119     }
120 
121     @Override
122     protected void onRequestSubmitted(final HttpRequest request) {
123         if (request != null && this.headerlog.isDebugEnabled()) {
124             this.headerlog.debug(getId() + " >> " + request.getRequestLine().toString());
125             final Header[] headers = request.getAllHeaders();
126             for (final Header header : headers) {
127                 this.headerlog.debug(getId() + " >> " + header.toString());
128             }
129         }
130     }
131 
132 }