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.nio.testserver;
29  
30  import java.io.IOException;
31  import java.util.concurrent.atomic.AtomicLong;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.apache.http.Header;
36  import org.apache.http.HttpException;
37  import org.apache.http.HttpRequest;
38  import org.apache.http.HttpResponse;
39  import org.apache.http.impl.nio.DefaultNHttpClientConnection;
40  import org.apache.http.nio.NHttpClientEventHandler;
41  import org.apache.http.nio.reactor.IOSession;
42  
43  public class LoggingNHttpClientConnection extends DefaultNHttpClientConnection {
44  
45      private static final AtomicLong COUNT = new AtomicLong();
46  
47      private final Log log;
48      private final Log iolog;
49      private final Log headerlog;
50      private final Log wirelog;
51      private final String id;
52  
53      public LoggingNHttpClientConnection(final IOSession session) {
54          super(session, 8 * 1024);
55          this.log = LogFactory.getLog(getClass());
56          this.iolog = LogFactory.getLog(session.getClass());
57          this.headerlog = LogFactory.getLog("org.apache.http.headers");
58          this.wirelog = LogFactory.getLog("org.apache.http.wire");
59          this.id = "http-outgoing-" + COUNT.incrementAndGet();
60          if (this.iolog.isDebugEnabled() || this.wirelog.isDebugEnabled()) {
61              this.session = new LoggingIOSession(session, this.id, this.iolog, this.wirelog);
62          }
63      }
64  
65      @Override
66      public void close() throws IOException {
67          if (this.log.isDebugEnabled()) {
68              this.log.debug(this.id + ": Close connection");
69          }
70          super.close();
71      }
72  
73      @Override
74      public void shutdown() throws IOException {
75          if (this.log.isDebugEnabled()) {
76              this.log.debug(this.id + ": Shutdown connection");
77          }
78          super.shutdown();
79      }
80  
81      @Override
82      public void submitRequest(final HttpRequest request) throws IOException, HttpException {
83          if (this.log.isDebugEnabled()) {
84              this.log.debug(this.id + ": "  + request.getRequestLine().toString());
85          }
86          super.submitRequest(request);
87      }
88  
89      @Override
90      public void consumeInput(final NHttpClientEventHandler handler) {
91          if (this.log.isDebugEnabled()) {
92              this.log.debug(this.id + ": Consume input");
93          }
94          super.consumeInput(handler);
95      }
96  
97      @Override
98      public void produceOutput(final NHttpClientEventHandler handler) {
99          if (this.log.isDebugEnabled()) {
100             this.log.debug(this.id + ": Produce output");
101         }
102         super.produceOutput(handler);
103     }
104 
105     @Override
106     protected void onResponseReceived(final HttpResponse response) {
107         if (response != null && this.headerlog.isDebugEnabled()) {
108             this.headerlog.debug(this.id + " << " + response.getStatusLine().toString());
109             final Header[] headers = response.getAllHeaders();
110             for (final Header header : headers) {
111                 this.headerlog.debug(this.id + " << " + header.toString());
112             }
113         }
114     }
115 
116     @Override
117     protected void onRequestSubmitted(final HttpRequest request) {
118         if (request != null && this.headerlog.isDebugEnabled()) {
119             this.headerlog.debug(id + " >> " + request.getRequestLine().toString());
120             final Header[] headers = request.getAllHeaders();
121             for (final Header header : headers) {
122                 this.headerlog.debug(this.id + " >> " + header.toString());
123             }
124         }
125     }
126 
127     @Override
128     public String toString() {
129         return this.id;
130     }
131 
132 }