1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.impl.client;
29
30 import java.io.Closeable;
31 import java.io.IOException;
32 import java.util.List;
33 import java.util.concurrent.TimeUnit;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.apache.http.HttpException;
38 import org.apache.http.HttpHost;
39 import org.apache.http.HttpRequest;
40 import org.apache.http.annotation.Contract;
41 import org.apache.http.annotation.ThreadingBehavior;
42 import org.apache.http.auth.AuthSchemeProvider;
43 import org.apache.http.auth.AuthState;
44 import org.apache.http.client.ClientProtocolException;
45 import org.apache.http.client.CookieStore;
46 import org.apache.http.client.CredentialsProvider;
47 import org.apache.http.client.config.RequestConfig;
48 import org.apache.http.client.methods.CloseableHttpResponse;
49 import org.apache.http.client.methods.Configurable;
50 import org.apache.http.client.methods.HttpExecutionAware;
51 import org.apache.http.client.methods.HttpRequestWrapper;
52 import org.apache.http.client.params.ClientPNames;
53 import org.apache.http.client.params.HttpClientParamConfig;
54 import org.apache.http.client.protocol.HttpClientContext;
55 import org.apache.http.config.Lookup;
56 import org.apache.http.conn.ClientConnectionManager;
57 import org.apache.http.conn.ClientConnectionRequest;
58 import org.apache.http.conn.HttpClientConnectionManager;
59 import org.apache.http.conn.ManagedClientConnection;
60 import org.apache.http.conn.routing.HttpRoute;
61 import org.apache.http.conn.routing.HttpRoutePlanner;
62 import org.apache.http.conn.scheme.SchemeRegistry;
63 import org.apache.http.cookie.CookieSpecProvider;
64 import org.apache.http.impl.execchain.ClientExecChain;
65 import org.apache.http.params.HttpParams;
66 import org.apache.http.params.HttpParamsNames;
67 import org.apache.http.protocol.BasicHttpContext;
68 import org.apache.http.protocol.HttpContext;
69 import org.apache.http.util.Args;
70
71
72
73
74
75
76 @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
77 @SuppressWarnings("deprecation")
78 class InternalHttpClient extends CloseableHttpClient implements Configurable {
79
80 private final Log log = LogFactory.getLog(getClass());
81
82 private final ClientExecChain execChain;
83 private final HttpClientConnectionManager connManager;
84 private final HttpRoutePlanner routePlanner;
85 private final Lookup<CookieSpecProvider> cookieSpecRegistry;
86 private final Lookup<AuthSchemeProvider> authSchemeRegistry;
87 private final CookieStore cookieStore;
88 private final CredentialsProvider credentialsProvider;
89 private final RequestConfig defaultConfig;
90 private final List<Closeable> closeables;
91
92 public InternalHttpClient(
93 final ClientExecChain execChain,
94 final HttpClientConnectionManager connManager,
95 final HttpRoutePlanner routePlanner,
96 final Lookup<CookieSpecProvider> cookieSpecRegistry,
97 final Lookup<AuthSchemeProvider> authSchemeRegistry,
98 final CookieStore cookieStore,
99 final CredentialsProvider credentialsProvider,
100 final RequestConfig defaultConfig,
101 final List<Closeable> closeables) {
102 super();
103 Args.notNull(execChain, "HTTP client exec chain");
104 Args.notNull(connManager, "HTTP connection manager");
105 Args.notNull(routePlanner, "HTTP route planner");
106 this.execChain = execChain;
107 this.connManager = connManager;
108 this.routePlanner = routePlanner;
109 this.cookieSpecRegistry = cookieSpecRegistry;
110 this.authSchemeRegistry = authSchemeRegistry;
111 this.cookieStore = cookieStore;
112 this.credentialsProvider = credentialsProvider;
113 this.defaultConfig = defaultConfig;
114 this.closeables = closeables;
115 }
116
117 private HttpRoute determineRoute(
118 final HttpHost target,
119 final HttpRequest request,
120 final HttpContext context) throws HttpException {
121 HttpHost host = target;
122 if (host == null) {
123 host = (HttpHost) request.getParams().getParameter(ClientPNames.DEFAULT_HOST);
124 }
125 return this.routePlanner.determineRoute(host, request, context);
126 }
127
128 private void setupContext(final HttpClientContext context) {
129 if (context.getAttribute(HttpClientContext.TARGET_AUTH_STATE) == null) {
130 context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, new AuthState());
131 }
132 if (context.getAttribute(HttpClientContext.PROXY_AUTH_STATE) == null) {
133 context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, new AuthState());
134 }
135 if (context.getAttribute(HttpClientContext.AUTHSCHEME_REGISTRY) == null) {
136 context.setAttribute(HttpClientContext.AUTHSCHEME_REGISTRY, this.authSchemeRegistry);
137 }
138 if (context.getAttribute(HttpClientContext.COOKIESPEC_REGISTRY) == null) {
139 context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry);
140 }
141 if (context.getAttribute(HttpClientContext.COOKIE_STORE) == null) {
142 context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
143 }
144 if (context.getAttribute(HttpClientContext.CREDS_PROVIDER) == null) {
145 context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider);
146 }
147 if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
148 context.setAttribute(HttpClientContext.REQUEST_CONFIG, this.defaultConfig);
149 }
150 }
151
152 @Override
153 protected CloseableHttpResponse doExecute(
154 final HttpHost target,
155 final HttpRequest request,
156 final HttpContext context) throws IOException, ClientProtocolException {
157 Args.notNull(request, "HTTP request");
158 HttpExecutionAware execAware = null;
159 if (request instanceof HttpExecutionAware) {
160 execAware = (HttpExecutionAware) request;
161 }
162 try {
163 final HttpRequestWrapper wrapper = HttpRequestWrapper.wrap(request, target);
164 final HttpClientContext localcontext = HttpClientContext.adapt(
165 context != null ? context : new BasicHttpContext());
166 RequestConfig config = null;
167 if (request instanceof Configurable) {
168 config = ((Configurable) request).getConfig();
169 }
170 if (config == null) {
171 final HttpParams params = request.getParams();
172 if (params instanceof HttpParamsNames) {
173 if (!((HttpParamsNames) params).getNames().isEmpty()) {
174 config = HttpClientParamConfig.getRequestConfig(params, this.defaultConfig);
175 }
176 } else {
177 config = HttpClientParamConfig.getRequestConfig(params, this.defaultConfig);
178 }
179 }
180 if (config != null) {
181 localcontext.setRequestConfig(config);
182 }
183 setupContext(localcontext);
184 final HttpRoute route = determineRoute(target, wrapper, localcontext);
185 return this.execChain.execute(route, wrapper, localcontext, execAware);
186 } catch (final HttpException httpException) {
187 throw new ClientProtocolException(httpException);
188 }
189 }
190
191 @Override
192 public RequestConfig getConfig() {
193 return this.defaultConfig;
194 }
195
196 @Override
197 public void close() {
198 if (this.closeables != null) {
199 for (final Closeable closeable: this.closeables) {
200 try {
201 closeable.close();
202 } catch (final IOException ex) {
203 this.log.error(ex.getMessage(), ex);
204 }
205 }
206 }
207 }
208
209 @Override
210 public HttpParams getParams() {
211 throw new UnsupportedOperationException();
212 }
213
214 @Override
215 public ClientConnectionManager getConnectionManager() {
216
217 return new ClientConnectionManager() {
218
219 @Override
220 public void shutdown() {
221 connManager.shutdown();
222 }
223
224 @Override
225 public ClientConnectionRequest requestConnection(
226 final HttpRoute route, final Object state) {
227 throw new UnsupportedOperationException();
228 }
229
230 @Override
231 public void releaseConnection(
232 final ManagedClientConnection conn,
233 final long validDuration, final TimeUnit timeUnit) {
234 throw new UnsupportedOperationException();
235 }
236
237 @Override
238 public SchemeRegistry getSchemeRegistry() {
239 throw new UnsupportedOperationException();
240 }
241
242 @Override
243 public void closeIdleConnections(final long idletime, final TimeUnit timeUnit) {
244 connManager.closeIdleConnections(idletime, timeUnit);
245 }
246
247 @Override
248 public void closeExpiredConnections() {
249 connManager.closeExpiredConnections();
250 }
251
252 };
253
254 }
255
256 }