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.testserver;
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  import java.util.concurrent.atomic.AtomicLong;
37  
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  import org.apache.http.Header;
41  import org.apache.http.HttpRequest;
42  import org.apache.http.HttpResponse;
43  import org.apache.http.config.MessageConstraints;
44  import org.apache.http.entity.ContentLengthStrategy;
45  import org.apache.http.impl.DefaultBHttpServerConnection;
46  import org.apache.http.io.HttpMessageParserFactory;
47  import org.apache.http.io.HttpMessageWriterFactory;
48  
49  public class LoggingBHttpServerConnection extends DefaultBHttpServerConnection {
50  
51      private static final AtomicLong COUNT = new AtomicLong();
52  
53      private final String id;
54      private final Log log;
55      private final Log headerlog;
56      private final Wire wire;
57  
58      public LoggingBHttpServerConnection(
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 HttpMessageParserFactory<HttpRequest> requestParserFactory,
67              final HttpMessageWriterFactory<HttpResponse> responseWriterFactory) {
68          super(buffersize, fragmentSizeHint, chardecoder, charencoder, constraints,
69                  incomingContentStrategy, outgoingContentStrategy,
70                  requestParserFactory, responseWriterFactory);
71          this.id = "http-incoming-" + COUNT.incrementAndGet();
72          this.log = LogFactory.getLog(getClass());
73          this.headerlog = LogFactory.getLog("org.apache.http.headers");
74          this.wire = new Wire(LogFactory.getLog("org.apache.http.wire"), this.id);
75      }
76  
77      public LoggingBHttpServerConnection(final int buffersize) {
78          this(buffersize, buffersize, null, null, null, null, null, null, null);
79      }
80  
81      @Override
82      public void close() throws IOException {
83          if (this.log.isDebugEnabled()) {
84              this.log.debug(this.id + ": Close connection");
85          }
86          super.close();
87      }
88  
89      @Override
90      public void shutdown() throws IOException {
91          if (this.log.isDebugEnabled()) {
92              this.log.debug(this.id + ": Shutdown connection");
93          }
94          super.shutdown();
95      }
96  
97      @Override
98      protected InputStream getSocketInputStream(final Socket socket) throws IOException {
99          InputStream in = super.getSocketInputStream(socket);
100         if (wire.isEnabled()) {
101             in = new LoggingInputStream(in, wire);
102         }
103         return in;
104     }
105 
106     @Override
107     protected OutputStream getSocketOutputStream(final Socket socket) throws IOException {
108         OutputStream out = super.getSocketOutputStream(socket);
109         if (wire.isEnabled()) {
110             out = new LoggingOutputStream(out, wire);
111         }
112         return out;
113     }
114 
115     @Override
116     protected void onRequestReceived(final HttpRequest request) {
117         if (request != null && this.headerlog.isDebugEnabled()) {
118             this.headerlog.debug(this.id + " >> " + request.getRequestLine().toString());
119             final Header[] headers = request.getAllHeaders();
120             for (final Header header : headers) {
121                 this.headerlog.debug(this.id + " >> " + header.toString());
122             }
123         }
124     }
125 
126     @Override
127     protected void onResponseSubmitted(final HttpResponse response) {
128         if (response != null && this.headerlog.isDebugEnabled()) {
129             this.headerlog.debug(this.id + " << " + response.getStatusLine().toString());
130             final Header[] headers = response.getAllHeaders();
131             for (final Header header : headers) {
132                 this.headerlog.debug(this.id + " << " + header.toString());
133             }
134         }
135     }
136 
137 }