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         return local != null ? !local.isClosed() : false;
156     }
157 
158     @Override
159     public boolean isStale() {
160         final NHttpClientConnection conn = getConnection();
161         return conn != null ? !conn.isOpen() : false;
162     }
163 
164     @Override
165     public void setSocketTimeout(final int i) {
166         getValidConnection().setSocketTimeout(i);
167     }
168 
169     @Override
170     public int getSocketTimeout() {
171         return getValidConnection().getSocketTimeout();
172     }
173 
174     @Override
175     public void submitRequest(final HttpRequest request) throws IOException, HttpException {
176         getValidConnection().submitRequest(request);
177     }
178 
179     @Override
180     public boolean isRequestSubmitted() {
181         return getValidConnection().isRequestSubmitted();
182     }
183 
184     @Override
185     public void resetOutput() {
186         getValidConnection().resetOutput();
187     }
188 
189     @Override
190     public void resetInput() {
191         getValidConnection().resetInput();
192     }
193 
194     @Override
195     public int getStatus() {
196         return getValidConnection().getStatus();
197     }
198 
199     @Override
200     public HttpRequest getHttpRequest() {
201         return getValidConnection().getHttpRequest();
202     }
203 
204     @Override
205     public HttpResponse getHttpResponse() {
206         return getValidConnection().getHttpResponse();
207     }
208 
209     @Override
210     public HttpContext getContext() {
211         return getValidConnection().getContext();
212     }
213 
214     public static NHttpClientConnection newProxy(final CPoolEntry poolEntry) {
215         return new CPoolProxy(poolEntry);
216     }
217 
218     private static CPoolProxy getProxy(final NHttpClientConnection conn) {
219         if (!CPoolProxy.class.isInstance(conn)) {
220             throw new IllegalStateException("Unexpected connection proxy class: " + conn.getClass());
221         }
222         return CPoolProxy.class.cast(conn);
223     }
224 
225     public static CPoolEntry getPoolEntry(final NHttpClientConnection proxy) {
226         final CPoolEntry entry = getProxy(proxy).getPoolEntry();
227         if (entry == null) {
228             throw new ConnectionShutdownException();
229         }
230         return entry;
231     }
232 
233     public static CPoolEntry detach(final NHttpClientConnection proxy) {
234         return getProxy(proxy).detach();
235     }
236 
237     @Override
238     public String getId() {
239         return getValidConnection().getId();
240     }
241 
242     @Override
243     public void bind(final IOSession ioSession) {
244         getValidConnection().bind(ioSession);
245     }
246 
247     @Override
248     public IOSession getIOSession() {
249         return getValidConnection().getIOSession();
250     }
251 
252     @Override
253     public SSLSession getSSLSession() {
254         return getValidConnection().getSSLSession();
255     }
256 
257     @Override
258     public String toString() {
259         final StringBuilder sb = new StringBuilder("CPoolProxy{");
260         final ManagedNHttpClientConnection conn = getConnection();
261         if (conn != null) {
262             sb.append(conn);
263         } else {
264             sb.append("detached");
265         }
266         sb.append('}');
267         return sb.toString();
268     }
269 
270 }