|
1 | 1 | /* |
2 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 | 3 | * |
4 | | - * Copyright (c) 2010-2014 Oracle and/or its affiliates. All rights reserved. |
| 4 | + * Copyright (c) 2010-2015 Oracle and/or its affiliates. All rights reserved. |
5 | 5 | * |
6 | 6 | * The contents of this file are subject to the terms of either the GNU |
7 | 7 | * General Public License Version 2 only ("GPL") or the Common Development |
|
57 | 57 | import java.util.logging.Logger; |
58 | 58 |
|
59 | 59 | import javax.ws.rs.ProcessingException; |
| 60 | +import javax.ws.rs.client.Client; |
60 | 61 | import javax.ws.rs.core.Configuration; |
61 | 62 | import javax.ws.rs.core.HttpHeaders; |
62 | 63 | import javax.ws.rs.core.MultivaluedMap; |
@@ -203,92 +204,81 @@ class ApacheConnector implements Connector { |
203 | 204 | /** |
204 | 205 | * Create the new Apache HTTP Client connector. |
205 | 206 | * |
| 207 | + * @param client JAX-RS client instance for which the connector is being created. |
206 | 208 | * @param config client configuration. |
207 | 209 | */ |
208 | | - ApacheConnector(final Configuration config) { |
209 | | - Object reqConfig = null; |
210 | | - |
211 | | - if (config != null) { |
212 | | - final Object connectionManager = config.getProperties().get(ApacheClientProperties.CONNECTION_MANAGER); |
213 | | - |
214 | | - if (connectionManager != null) { |
215 | | - if (!(connectionManager instanceof HttpClientConnectionManager)) { |
216 | | - LOGGER.log( |
217 | | - Level.WARNING, |
218 | | - LocalizationMessages.IGNORING_VALUE_OF_PROPERTY( |
219 | | - ApacheClientProperties.CONNECTION_MANAGER, |
220 | | - connectionManager.getClass().getName(), |
221 | | - HttpClientConnectionManager.class.getName()) |
222 | | - ); |
223 | | - } |
| 210 | + ApacheConnector(final Client client, final Configuration config) { |
| 211 | + final Object connectionManager = config.getProperties().get(ApacheClientProperties.CONNECTION_MANAGER); |
| 212 | + if (connectionManager != null) { |
| 213 | + if (!(connectionManager instanceof HttpClientConnectionManager)) { |
| 214 | + LOGGER.log( |
| 215 | + Level.WARNING, |
| 216 | + LocalizationMessages.IGNORING_VALUE_OF_PROPERTY( |
| 217 | + ApacheClientProperties.CONNECTION_MANAGER, |
| 218 | + connectionManager.getClass().getName(), |
| 219 | + HttpClientConnectionManager.class.getName()) |
| 220 | + ); |
224 | 221 | } |
| 222 | + } |
225 | 223 |
|
226 | | - reqConfig = config.getProperties().get(ApacheClientProperties.REQUEST_CONFIG); |
227 | | - if (reqConfig != null) { |
228 | | - if (!(reqConfig instanceof RequestConfig)) { |
229 | | - LOGGER.log( |
230 | | - Level.WARNING, |
231 | | - LocalizationMessages.IGNORING_VALUE_OF_PROPERTY( |
232 | | - ApacheClientProperties.REQUEST_CONFIG, |
233 | | - reqConfig.getClass().getName(), |
234 | | - RequestConfig.class.getName()) |
235 | | - ); |
236 | | - reqConfig = null; |
237 | | - } |
| 224 | + Object reqConfig = config.getProperties().get(ApacheClientProperties.REQUEST_CONFIG); |
| 225 | + if (reqConfig != null) { |
| 226 | + if (!(reqConfig instanceof RequestConfig)) { |
| 227 | + LOGGER.log( |
| 228 | + Level.WARNING, |
| 229 | + LocalizationMessages.IGNORING_VALUE_OF_PROPERTY( |
| 230 | + ApacheClientProperties.REQUEST_CONFIG, |
| 231 | + reqConfig.getClass().getName(), |
| 232 | + RequestConfig.class.getName()) |
| 233 | + ); |
| 234 | + reqConfig = null; |
238 | 235 | } |
239 | 236 | } |
240 | 237 |
|
241 | | - final SSLContext sslContext = getSslContext(config); |
| 238 | + final SSLContext sslContext = getSslContext(client, config); |
242 | 239 | final HttpClientBuilder clientBuilder = HttpClientBuilder.create(); |
243 | 240 |
|
244 | 241 | clientBuilder.setConnectionManager(getConnectionManager(config, sslContext)); |
245 | 242 | clientBuilder.setSslcontext(sslContext); |
246 | 243 |
|
247 | 244 | final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); |
248 | 245 |
|
249 | | - int connectTimeout = 0; |
250 | | - int socketTimeout = 0; |
251 | | - boolean ignoreCookies = false; |
252 | | - if (config != null) { |
253 | | - connectTimeout = ClientProperties.getValue(config.getProperties(), ClientProperties.CONNECT_TIMEOUT, 0); |
254 | | - socketTimeout = ClientProperties.getValue(config.getProperties(), ClientProperties.READ_TIMEOUT, 0); |
255 | | - ignoreCookies = PropertiesHelper.isProperty(config.getProperties(), ApacheClientProperties.DISABLE_COOKIES); |
256 | | - |
257 | | - final Object credentialsProvider = config.getProperty(ApacheClientProperties.CREDENTIALS_PROVIDER); |
258 | | - if (credentialsProvider != null && (credentialsProvider instanceof CredentialsProvider)) { |
259 | | - clientBuilder.setDefaultCredentialsProvider((CredentialsProvider) credentialsProvider); |
260 | | - } |
| 246 | + final int connectTimeout = ClientProperties.getValue(config.getProperties(), ClientProperties.CONNECT_TIMEOUT, 0); |
| 247 | + final int socketTimeout = ClientProperties.getValue(config.getProperties(), ClientProperties.READ_TIMEOUT, 0); |
| 248 | + final boolean ignoreCookies = PropertiesHelper.isProperty(config.getProperties(), ApacheClientProperties.DISABLE_COOKIES); |
261 | 249 |
|
262 | | - final Object proxyUri; |
263 | | - proxyUri = config.getProperty(ClientProperties.PROXY_URI); |
264 | | - if (proxyUri != null) { |
265 | | - final URI u = getProxyUri(proxyUri); |
266 | | - final HttpHost proxy = new HttpHost(u.getHost(), u.getPort(), u.getScheme()); |
267 | | - final String userName; |
268 | | - userName = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_USERNAME, String.class); |
269 | | - if (userName != null) { |
270 | | - final String password; |
271 | | - password = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_PASSWORD, String.class); |
272 | | - |
273 | | - if (password != null) { |
274 | | - final CredentialsProvider credsProvider = new BasicCredentialsProvider(); |
275 | | - credsProvider.setCredentials( |
276 | | - new AuthScope(u.getHost(), u.getPort()), |
277 | | - new UsernamePasswordCredentials(userName, password) |
278 | | - ); |
279 | | - clientBuilder.setDefaultCredentialsProvider(credsProvider); |
280 | | - } |
| 250 | + final Object credentialsProvider = config.getProperty(ApacheClientProperties.CREDENTIALS_PROVIDER); |
| 251 | + if (credentialsProvider != null && (credentialsProvider instanceof CredentialsProvider)) { |
| 252 | + clientBuilder.setDefaultCredentialsProvider((CredentialsProvider) credentialsProvider); |
| 253 | + } |
| 254 | + |
| 255 | + final Object proxyUri; |
| 256 | + proxyUri = config.getProperty(ClientProperties.PROXY_URI); |
| 257 | + if (proxyUri != null) { |
| 258 | + final URI u = getProxyUri(proxyUri); |
| 259 | + final HttpHost proxy = new HttpHost(u.getHost(), u.getPort(), u.getScheme()); |
| 260 | + final String userName; |
| 261 | + userName = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_USERNAME, String.class); |
| 262 | + if (userName != null) { |
| 263 | + final String password; |
| 264 | + password = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_PASSWORD, String.class); |
| 265 | + |
| 266 | + if (password != null) { |
| 267 | + final CredentialsProvider credsProvider = new BasicCredentialsProvider(); |
| 268 | + credsProvider.setCredentials( |
| 269 | + new AuthScope(u.getHost(), u.getPort()), |
| 270 | + new UsernamePasswordCredentials(userName, password) |
| 271 | + ); |
| 272 | + clientBuilder.setDefaultCredentialsProvider(credsProvider); |
281 | 273 | } |
282 | | - clientBuilder.setProxy(proxy); |
283 | 274 | } |
284 | | - |
285 | | - final Boolean preemptiveBasicAuthProperty = (Boolean) config.getProperties() |
286 | | - .get(ApacheClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION); |
287 | | - this.preemptiveBasicAuth = (preemptiveBasicAuthProperty != null) ? preemptiveBasicAuthProperty : false; |
288 | | - } else { |
289 | | - this.preemptiveBasicAuth = false; |
| 275 | + clientBuilder.setProxy(proxy); |
290 | 276 | } |
291 | 277 |
|
| 278 | + final Boolean preemptiveBasicAuthProperty = (Boolean) config.getProperties() |
| 279 | + .get(ApacheClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION); |
| 280 | + this.preemptiveBasicAuth = (preemptiveBasicAuthProperty != null) ? preemptiveBasicAuthProperty : false; |
| 281 | + |
292 | 282 |
|
293 | 283 | if (reqConfig != null) { |
294 | 284 | final RequestConfig.Builder reqConfigBuilder = RequestConfig.copy((RequestConfig) reqConfig); |
@@ -321,20 +311,16 @@ class ApacheConnector implements Connector { |
321 | 311 | this.client = clientBuilder.build(); |
322 | 312 | } |
323 | 313 |
|
324 | | - private SSLContext getSslContext(final Configuration config) { |
325 | | - if (config == null) { |
326 | | - return null; |
327 | | - } |
328 | | - |
| 314 | + private SSLContext getSslContext(final Client client, final Configuration config) { |
329 | 315 | final SslConfigurator sslConfigurator = ApacheClientProperties.getValue( |
330 | 316 | config.getProperties(), |
331 | 317 | ApacheClientProperties.SSL_CONFIG, |
332 | 318 | SslConfigurator.class); |
333 | 319 |
|
334 | | - return sslConfigurator != null ? sslConfigurator.createSSLContext() : null; |
| 320 | + return sslConfigurator != null ? sslConfigurator.createSSLContext() : client.getSslContext(); |
335 | 321 | } |
336 | 322 |
|
337 | | - HttpClientConnectionManager getConnectionManager(final Configuration config, final SSLContext sslContext) { |
| 323 | + private HttpClientConnectionManager getConnectionManager(final Configuration config, final SSLContext sslContext) { |
338 | 324 | final Object cmObject = config.getProperties().get(ApacheClientProperties.CONNECTION_MANAGER); |
339 | 325 |
|
340 | 326 | // Connection manager from configuration. |
@@ -397,7 +383,7 @@ private HttpClientConnectionManager createConnectionManager( |
397 | 383 | .build(); |
398 | 384 |
|
399 | 385 | final Integer chunkSize = ClientProperties.getValue(config.getProperties(), |
400 | | - ClientProperties.CHUNKED_ENCODING_SIZE, 4096, Integer.class); |
| 386 | + ClientProperties.CHUNKED_ENCODING_SIZE, ClientProperties.DEFAULT_CHUNK_SIZE, Integer.class); |
401 | 387 |
|
402 | 388 | final PoolingHttpClientConnectionManager connectionManager = |
403 | 389 | new PoolingHttpClientConnectionManager(registry, new ConnectionFactory(chunkSize)); |
@@ -507,7 +493,7 @@ public ClientResponse apply(final ClientRequest clientRequest) throws Processing |
507 | 493 |
|
508 | 494 |
|
509 | 495 | try { |
510 | | - responseContext.setEntityStream(new HttpClientResponseInputStream(response)); |
| 496 | + responseContext.setEntityStream(new HttpClientResponseInputStream(getInputStream(response))); |
511 | 497 | } catch (final IOException e) { |
512 | 498 | LOGGER.log(Level.SEVERE, null, e); |
513 | 499 | } |
@@ -638,8 +624,8 @@ private static Map<String, String> writeOutBoundHeaders(final MultivaluedMap<Str |
638 | 624 |
|
639 | 625 | private static final class HttpClientResponseInputStream extends FilterInputStream { |
640 | 626 |
|
641 | | - HttpClientResponseInputStream(final CloseableHttpResponse response) throws IOException { |
642 | | - super(getInputStream(response)); |
| 627 | + HttpClientResponseInputStream(final InputStream inputStream) throws IOException { |
| 628 | + super(inputStream); |
643 | 629 | } |
644 | 630 |
|
645 | 631 | @Override |
|
0 commit comments