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 java.nio.charset.CharsetDecoder;
30  import java.nio.charset.CharsetEncoder;
31  
32  import javax.net.ssl.SSLSession;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.http.Header;
36  import org.apache.http.HttpRequest;
37  import org.apache.http.HttpResponse;
38  import org.apache.http.config.MessageConstraints;
39  import org.apache.http.entity.ContentLengthStrategy;
40  import org.apache.http.impl.nio.DefaultNHttpClientConnection;
41  import org.apache.http.nio.NHttpMessageParserFactory;
42  import org.apache.http.nio.NHttpMessageWriterFactory;
43  import org.apache.http.nio.conn.ManagedNHttpClientConnection;
44  import org.apache.http.nio.reactor.IOSession;
45  import org.apache.http.nio.reactor.ssl.SSLIOSession;
46  import org.apache.http.nio.util.ByteBufferAllocator;
47  import org.apache.http.util.Args;
48  import org.apache.http.util.Asserts;
49  
50  class ManagedNHttpClientConnectionImpl
51                      extends DefaultNHttpClientConnection implements ManagedNHttpClientConnection {
52  
53      private final Log headerlog;
54      private final Log wirelog;
55      private final Log log;
56  
57      private final String id;
58      private IOSession original;
59  
60      public ManagedNHttpClientConnectionImpl(
61              final String id,
62              final Log log,
63              final Log headerlog,
64              final Log wirelog,
65              final IOSession iosession,
66              final int buffersize,
67              final int fragmentSizeHint,
68              final ByteBufferAllocator allocator,
69              final CharsetDecoder chardecoder,
70              final CharsetEncoder charencoder,
71              final MessageConstraints constraints,
72              final ContentLengthStrategy incomingContentStrategy,
73              final ContentLengthStrategy outgoingContentStrategy,
74              final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory,
75              final NHttpMessageParserFactory<HttpResponse> responseParserFactory) {
76          super(iosession, buffersize, fragmentSizeHint, allocator, chardecoder, charencoder, constraints,
77                  incomingContentStrategy, outgoingContentStrategy,
78                  requestWriterFactory, responseParserFactory);
79          this.id = id;
80          this.log = log;
81          this.headerlog = headerlog;
82          this.wirelog = wirelog;
83          this.original = iosession;
84          if (this.log.isDebugEnabled() || this.wirelog.isDebugEnabled()) {
85              super.bind(new LoggingIOSession(iosession, this.id, this.log, this.wirelog));
86          }
87      }
88  
89      @Override
90      public void bind(final IOSession iosession) {
91          Args.notNull(iosession, "I/O session");
92          Asserts.check(!iosession.isClosed(), "I/O session is closed");
93          this.status = ACTIVE;
94          this.original = iosession;
95          if (this.log.isDebugEnabled() || this.wirelog.isDebugEnabled()) {
96              this.log.debug(this.id + " Upgrade session " + iosession);
97              super.bind(new LoggingIOSession(iosession, this.id, this.log, this.wirelog));
98          } else {
99              super.bind(iosession);
100         }
101     }
102 
103     @Override
104     public IOSession getIOSession() {
105         return this.original;
106     }
107 
108     @Override
109     public SSLSession getSSLSession() {
110         if (this.original instanceof SSLIOSession) {
111             return ((SSLIOSession) this.original).getSSLSession();
112         } else {
113             return null;
114         }
115     }
116 
117     @Override
118     public String getId() {
119         return this.id;
120     }
121 
122     @Override
123     protected void onResponseReceived(final HttpResponse response) {
124         if (response != null && this.headerlog.isDebugEnabled()) {
125             this.headerlog.debug(this.id + " << " + response.getStatusLine().toString());
126             final Header[] headers = response.getAllHeaders();
127             for (final Header header : headers) {
128                 this.headerlog.debug(this.id + " << " + header.toString());
129             }
130         }
131     }
132 
133     @Override
134     protected void onRequestSubmitted(final HttpRequest request) {
135         if (request != null && this.headerlog.isDebugEnabled()) {
136             this.headerlog.debug(this.id + " >> " + request.getRequestLine().toString());
137             final Header[] headers = request.getAllHeaders();
138             for (final Header header : headers) {
139                 this.headerlog.debug(this.id + " >> " + header.toString());
140             }
141         }
142     }
143 
144     @Override
145     public String toString() {
146         final StringBuilder buf = new StringBuilder();
147         buf.append(this.id);
148         buf.append(" [");
149         switch (this.status) {
150         case ACTIVE:
151             buf.append("ACTIVE");
152             if (this.inbuf.hasData()) {
153                 buf.append("(").append(this.inbuf.length()).append(")");
154             }
155             break;
156         case CLOSING:
157             buf.append("CLOSING");
158             break;
159         case CLOSED:
160             buf.append("CLOSED");
161             break;
162         }
163         buf.append("]");
164         return buf.toString();
165     }
166 
167 }