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