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