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.impl.client;
29  
30  import java.io.IOException;
31  import java.util.concurrent.TimeUnit;
32  
33  import org.apache.http.HttpException;
34  import org.apache.http.HttpHost;
35  import org.apache.http.HttpRequest;
36  import org.apache.http.annotation.Contract;
37  import org.apache.http.annotation.ThreadingBehavior;
38  import org.apache.http.client.ClientProtocolException;
39  import org.apache.http.client.config.RequestConfig;
40  import org.apache.http.client.methods.CloseableHttpResponse;
41  import org.apache.http.client.methods.Configurable;
42  import org.apache.http.client.methods.HttpExecutionAware;
43  import org.apache.http.client.methods.HttpRequestWrapper;
44  import org.apache.http.client.protocol.HttpClientContext;
45  import org.apache.http.conn.ClientConnectionManager;
46  import org.apache.http.conn.ClientConnectionRequest;
47  import org.apache.http.conn.HttpClientConnectionManager;
48  import org.apache.http.conn.ManagedClientConnection;
49  import org.apache.http.conn.routing.HttpRoute;
50  import org.apache.http.conn.scheme.SchemeRegistry;
51  import org.apache.http.impl.DefaultConnectionReuseStrategy;
52  import org.apache.http.impl.execchain.MinimalClientExec;
53  import org.apache.http.params.BasicHttpParams;
54  import org.apache.http.params.HttpParams;
55  import org.apache.http.protocol.BasicHttpContext;
56  import org.apache.http.protocol.HttpContext;
57  import org.apache.http.protocol.HttpRequestExecutor;
58  import org.apache.http.util.Args;
59  
60  /**
61   * Internal class.
62   *
63   * @since 4.3
64   */
65  @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
66  @SuppressWarnings("deprecation")
67  class MinimalHttpClient extends CloseableHttpClient {
68  
69      private final HttpClientConnectionManager connManager;
70      private final MinimalClientExec requestExecutor;
71      private final HttpParams params;
72  
73      public MinimalHttpClient(
74              final HttpClientConnectionManager connManager) {
75          super();
76          this.connManager = Args.notNull(connManager, "HTTP connection manager");
77          this.requestExecutor = new MinimalClientExec(
78                  new HttpRequestExecutor(),
79                  connManager,
80                  DefaultConnectionReuseStrategy.INSTANCE,
81                  DefaultConnectionKeepAliveStrategy.INSTANCE);
82          this.params = new BasicHttpParams();
83      }
84  
85      @Override
86      protected CloseableHttpResponse doExecute(
87              final HttpHost target,
88              final HttpRequest request,
89              final HttpContext context) throws IOException, ClientProtocolException {
90          Args.notNull(target, "Target host");
91          Args.notNull(request, "HTTP request");
92          HttpExecutionAware execAware = null;
93          if (request instanceof HttpExecutionAware) {
94              execAware = (HttpExecutionAware) request;
95          }
96          try {
97              final HttpRequestWrapper wrapper = HttpRequestWrapper.wrap(request);
98              final HttpClientContext localcontext = HttpClientContext.adapt(
99                  context != null ? context : new BasicHttpContext());
100             final HttpRoute route = new HttpRoute(target);
101             RequestConfig config = null;
102             if (request instanceof Configurable) {
103                 config = ((Configurable) request).getConfig();
104             }
105             if (config != null) {
106                 localcontext.setRequestConfig(config);
107             }
108             return this.requestExecutor.execute(route, wrapper, localcontext, execAware);
109         } catch (final HttpException httpException) {
110             throw new ClientProtocolException(httpException);
111         }
112     }
113 
114     @Override
115     public HttpParams getParams() {
116         return this.params;
117     }
118 
119     @Override
120     public void close() {
121         this.connManager.shutdown();
122     }
123 
124     @Override
125     public ClientConnectionManager getConnectionManager() {
126 
127         return new ClientConnectionManager() {
128 
129             @Override
130             public void shutdown() {
131                 connManager.shutdown();
132             }
133 
134             @Override
135             public ClientConnectionRequest requestConnection(
136                     final HttpRoute route, final Object state) {
137                 throw new UnsupportedOperationException();
138             }
139 
140             @Override
141             public void releaseConnection(
142                     final ManagedClientConnection conn,
143                     final long validDuration, final TimeUnit timeUnit) {
144                 throw new UnsupportedOperationException();
145             }
146 
147             @Override
148             public SchemeRegistry getSchemeRegistry() {
149                 throw new UnsupportedOperationException();
150             }
151 
152             @Override
153             public void closeIdleConnections(final long idletime, final TimeUnit tunit) {
154                 connManager.closeIdleConnections(idletime, tunit);
155             }
156 
157             @Override
158             public void closeExpiredConnections() {
159                 connManager.closeExpiredConnections();
160             }
161 
162         };
163 
164     }
165 
166 }