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.io.IOException;
30  import java.net.InetAddress;
31  
32  import javax.net.ssl.SSLSession;
33  
34  import org.apache.http.HttpConnectionMetrics;
35  import org.apache.http.HttpException;
36  import org.apache.http.HttpRequest;
37  import org.apache.http.HttpResponse;
38  import org.apache.http.impl.conn.ConnectionShutdownException;
39  import org.apache.http.nio.NHttpClientConnection;
40  import org.apache.http.nio.conn.ManagedNHttpClientConnection;
41  import org.apache.http.nio.reactor.IOSession;
42  import org.apache.http.protocol.HttpContext;
43  
44  class CPoolProxy implements ManagedNHttpClientConnection {
45  
46      private volatile CPoolEntry poolEntry;
47  
48      CPoolProxy(final CPoolEntry entry) {
49          super();
50          this.poolEntry = entry;
51      }
52  
53      CPoolEntry getPoolEntry() {
54          return this.poolEntry;
55      }
56  
57      CPoolEntry detach() {
58          final CPoolEntry local = this.poolEntry;
59          this.poolEntry = null;
60          return local;
61      }
62  
63      ManagedNHttpClientConnection getConnection() {
64          final CPoolEntry local = this.poolEntry;
65          if (local == null) {
66              return null;
67          }
68          return local.getConnection();
69      }
70  
71      ManagedNHttpClientConnection getValidConnection() {
72          final ManagedNHttpClientConnection conn = getConnection();
73          if (conn == null) {
74              throw new ConnectionShutdownException();
75          }
76          return conn;
77      }
78  
79      @Override
80      public void close() throws IOException {
81          final CPoolEntry local = this.poolEntry;
82          if (local != null) {
83              local.closeConnection();
84          }
85      }
86  
87      @Override
88      public void shutdown() throws IOException {
89          final CPoolEntry local = this.poolEntry;
90          if (local != null) {
91              local.shutdownConnection();
92          }
93      }
94  
95      @Override
96      public HttpConnectionMetrics getMetrics() {
97          return getValidConnection().getMetrics();
98      }
99  
100     @Override
101     public void requestInput() {
102         final NHttpClientConnection conn = getConnection();
103         if (conn != null) {
104             conn.requestInput();
105         }
106     }
107 
108     @Override
109     public void suspendInput() {
110         final NHttpClientConnection conn = getConnection();
111         if (conn != null) {
112             conn.suspendInput();
113         }
114     }
115 
116     @Override
117     public void requestOutput() {
118         final NHttpClientConnection conn = getConnection();
119         if (conn != null) {
120             conn.requestOutput();
121         }
122     }
123 
124     @Override
125     public void suspendOutput() {
126         final NHttpClientConnection conn = getConnection();
127         if (conn != null) {
128             conn.suspendOutput();
129         }
130     }
131 
132     @Override
133     public InetAddress getLocalAddress() {
134         return getValidConnection().getLocalAddress();
135     }
136 
137     @Override
138     public int getLocalPort() {
139         return getValidConnection().getLocalPort();
140     }
141 
142     @Override
143     public InetAddress getRemoteAddress() {
144         return getValidConnection().getRemoteAddress();
145     }
146 
147     @Override
148     public int getRemotePort() {
149         return getValidConnection().getRemotePort();
150     }
151 
152     @Override
153     public boolean isOpen() {
154         final CPoolEntry local = this.poolEntry;
155         if (local != null) {
156             return !local.isClosed();
157         } else {
158             return false;
159         }
160     }
161 
162     @Override
163     public boolean isStale() {
164         final NHttpClientConnection conn = getConnection();
165         if (conn != null) {
166             return !conn.isOpen();
167         } else {
168             return false;
169         }
170     }
171 
172     @Override
173     public void setSocketTimeout(final int i) {
174         getValidConnection().setSocketTimeout(i);
175     }
176 
177     @Override
178     public int getSocketTimeout() {
179         return getValidConnection().getSocketTimeout();
180     }
181 
182     @Override
183     public void submitRequest(final HttpRequest request) throws IOException, HttpException {
184         getValidConnection().submitRequest(request);
185     }
186 
187     @Override
188     public boolean isRequestSubmitted() {
189         return getValidConnection().isRequestSubmitted();
190     }
191 
192     @Override
193     public void resetOutput() {
194         getValidConnection().resetOutput();
195     }
196 
197     @Override
198     public void resetInput() {
199         getValidConnection().resetInput();
200     }
201 
202     @Override
203     public int getStatus() {
204         return getValidConnection().getStatus();
205     }
206 
207     @Override
208     public HttpRequest getHttpRequest() {
209         return getValidConnection().getHttpRequest();
210     }
211 
212     @Override
213     public HttpResponse getHttpResponse() {
214         return getValidConnection().getHttpResponse();
215     }
216 
217     @Override
218     public HttpContext getContext() {
219         return getValidConnection().getContext();
220     }
221 
222     public static NHttpClientConnection newProxy(final CPoolEntry poolEntry) {
223         return new CPoolProxy(poolEntry);
224     }
225 
226     private static CPoolProxy getProxy(final NHttpClientConnection conn) {
227         if (!CPoolProxy.class.isInstance(conn)) {
228             throw new IllegalStateException("Unexpected connection proxy class: " + conn.getClass());
229         }
230         return CPoolProxy.class.cast(conn);
231     }
232 
233     public static CPoolEntry getPoolEntry(final NHttpClientConnection proxy) {
234         final CPoolEntry entry = getProxy(proxy).getPoolEntry();
235         if (entry == null) {
236             throw new ConnectionShutdownException();
237         }
238         return entry;
239     }
240 
241     public static CPoolEntry detach(final NHttpClientConnection proxy) {
242         return getProxy(proxy).detach();
243     }
244 
245     @Override
246     public String getId() {
247         return getValidConnection().getId();
248     }
249 
250     @Override
251     public void bind(final IOSession iosession) {
252         getValidConnection().bind(iosession);
253     }
254 
255     @Override
256     public IOSession getIOSession() {
257         return getValidConnection().getIOSession();
258     }
259 
260     @Override
261     public SSLSession getSSLSession() {
262         return getValidConnection().getSSLSession();
263     }
264 
265     @Override
266     public String toString() {
267         final StringBuilder sb = new StringBuilder("CPoolProxy{");
268         final ManagedNHttpClientConnection conn = getConnection();
269         if (conn != null) {
270             sb.append(conn);
271         } else {
272             sb.append("detached");
273         }
274         sb.append('}');
275         return sb.toString();
276     }
277 
278 }