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  package org.apache.http.impl.nio.conn;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.http.Header;
32  import org.apache.http.HttpRequest;
33  import org.apache.http.HttpResponse;
34  import org.apache.http.HttpResponseFactory;
35  import org.apache.http.impl.nio.DefaultNHttpClientConnection;
36  import org.apache.http.nio.conn.ClientAsyncConnection;
37  import org.apache.http.nio.reactor.IOSession;
38  import org.apache.http.nio.util.ByteBufferAllocator;
39  import org.apache.http.params.HttpParams;
40  
41  @Deprecated
42  public class DefaultClientAsyncConnection
43                      extends DefaultNHttpClientConnection implements ClientAsyncConnection {
44  
45      private final Log headerLog = LogFactory.getLog("org.apache.http.headers");
46      private final Log wireLog   = LogFactory.getLog("org.apache.http.wire");
47      private final Log log;
48  
49      private final String id;
50      private IOSession original;
51  
52      public DefaultClientAsyncConnection(
53              final String id,
54              final IOSession ioSession,
55              final HttpResponseFactory responseFactory,
56              final ByteBufferAllocator allocator,
57              final HttpParams params) {
58          super(ioSession, responseFactory, allocator, params);
59          this.id = id;
60          this.original = ioSession;
61          this.log = LogFactory.getLog(ioSession.getClass());
62          if (this.log.isDebugEnabled() || this.wireLog.isDebugEnabled()) {
63              bind(new LoggingIOSession(ioSession, this.id, this.log, this.wireLog));
64          }
65      }
66  
67      @Override
68      public void upgrade(final IOSession ioSession) {
69          this.original = ioSession;
70          if (this.log.isDebugEnabled() || this.wireLog.isDebugEnabled()) {
71              this.log.debug(this.id + " Upgrade session " + ioSession);
72              bind(new LoggingIOSession(ioSession, this.id, this.headerLog, this.wireLog));
73          } else {
74              bind(ioSession);
75          }
76      }
77  
78      @Override
79      public IOSession getIOSession() {
80          return this.original;
81      }
82  
83      public String getId() {
84          return this.id;
85      }
86  
87      @Override
88      protected void onResponseReceived(final HttpResponse response) {
89          if (response != null && this.headerLog.isDebugEnabled()) {
90              this.headerLog.debug(this.id + " << " + response.getStatusLine().toString());
91              final Header[] headers = response.getAllHeaders();
92              for (final Header header : headers) {
93                  this.headerLog.debug(this.id + " << " + header.toString());
94              }
95          }
96      }
97  
98      @Override
99      protected void onRequestSubmitted(final HttpRequest request) {
100         if (request != null && this.headerLog.isDebugEnabled()) {
101             this.headerLog.debug(this.id + " >> " + request.getRequestLine().toString());
102             final Header[] headers = request.getAllHeaders();
103             for (final Header header : headers) {
104                 this.headerLog.debug(this.id + " >> " + header.toString());
105             }
106         }
107     }
108 
109     @Override
110     public String toString() {
111         final StringBuilder buf = new StringBuilder();
112         buf.append(this.id);
113         buf.append(" [");
114         switch (this.status) {
115         case ACTIVE:
116             buf.append("ACTIVE");
117             if (this.inbuf.hasData()) {
118                 buf.append("(").append(this.inbuf.length()).append(")");
119             }
120             break;
121         case CLOSING:
122             buf.append("CLOSING");
123             break;
124         case CLOSED:
125             buf.append("CLOSED");
126             break;
127         }
128         buf.append("]");
129         return buf.toString();
130     }
131 
132 }