HTTPCLIENT-1777: [OSGi] remove duplication between OSGiClientBuilderFactory and OSGiCachingClientBuilderFactory
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1763260 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
4918d66253
commit
c08880c2c6
|
@ -26,55 +26,58 @@
|
|||
*/
|
||||
package org.apache.hc.client5.http.osgi.impl;
|
||||
|
||||
import static org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory.getSocketFactory;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
|
||||
import org.apache.hc.client5.http.impl.sync.CloseableHttpClient;
|
||||
import org.apache.hc.client5.http.impl.sync.HttpClientBuilder;
|
||||
import org.apache.hc.client5.http.osgi.services.ProxyConfiguration;
|
||||
import org.apache.hc.client5.http.socket.ConnectionSocketFactory;
|
||||
import org.apache.hc.client5.http.socket.LayeredConnectionSocketFactory;
|
||||
import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory;
|
||||
import org.apache.hc.core5.http.config.Registry;
|
||||
import org.apache.hc.core5.http.config.RegistryBuilder;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.framework.ServiceRegistration;
|
||||
import org.osgi.service.cm.ManagedService;
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
final class OSGiHttpClientBuilder extends HttpClientBuilder {
|
||||
import java.util.Map;
|
||||
|
||||
private final Collection<CloseableHttpClient> trackedHttpClients;
|
||||
import static org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory.getSocketFactory;
|
||||
|
||||
public OSGiHttpClientBuilder(
|
||||
final class HttpClientBuilderConfigurator {
|
||||
|
||||
private final OSGiCredentialsProvider credentialsProvider;
|
||||
|
||||
private final OSGiHttpRoutePlanner routePlanner;
|
||||
|
||||
private final Registry<ConnectionSocketFactory> socketFactoryRegistry;
|
||||
|
||||
HttpClientBuilderConfigurator(
|
||||
final BundleContext bundleContext,
|
||||
final Map<String, ServiceRegistration<ProxyConfiguration>> registeredConfigurations,
|
||||
final ServiceRegistration<ManagedService> trustedHostConfiguration,
|
||||
final List<CloseableHttpClient> trackedHttpClients) {
|
||||
this.trackedHttpClients = trackedHttpClients;
|
||||
setDefaultCredentialsProvider(
|
||||
new OSGiCredentialsProvider(bundleContext, registeredConfigurations));
|
||||
setRoutePlanner(
|
||||
new OSGiHttpRoutePlanner(bundleContext, registeredConfigurations));
|
||||
final LayeredConnectionSocketFactory defaultSocketFactory = getSocketFactory();
|
||||
setConnectionManager(new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create()
|
||||
.register("http", PlainConnectionSocketFactory.INSTANCE)
|
||||
.register("https", new RelaxedLayeredConnectionSocketFactory(bundleContext, trustedHostConfiguration, defaultSocketFactory))
|
||||
.build()));
|
||||
final ServiceRegistration<ManagedService> trustedHostsConfiguration) {
|
||||
credentialsProvider = new OSGiCredentialsProvider(bundleContext, registeredConfigurations);
|
||||
routePlanner = new OSGiHttpRoutePlanner(bundleContext, registeredConfigurations);
|
||||
socketFactoryRegistry = createSocketFactoryRegistry(bundleContext, trustedHostsConfiguration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CloseableHttpClient build() {
|
||||
final CloseableHttpClient httpClient = super.build();
|
||||
synchronized (trackedHttpClients) {
|
||||
trackedHttpClients.add(httpClient);
|
||||
}
|
||||
return httpClient;
|
||||
<T extends HttpClientBuilder> T configure(final T clientBuilder) {
|
||||
clientBuilder
|
||||
.setDefaultCredentialsProvider(credentialsProvider)
|
||||
.setRoutePlanner(routePlanner)
|
||||
.setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry));
|
||||
return clientBuilder;
|
||||
}
|
||||
|
||||
private Registry<ConnectionSocketFactory> createSocketFactoryRegistry(
|
||||
final BundleContext bundleContext,
|
||||
final ServiceRegistration<ManagedService> trustedHostsConfiguration) {
|
||||
return RegistryBuilder.<ConnectionSocketFactory>create()
|
||||
.register("http", PlainConnectionSocketFactory.INSTANCE)
|
||||
.register("https", createSocketFactory(bundleContext, trustedHostsConfiguration))
|
||||
.build();
|
||||
}
|
||||
|
||||
private ConnectionSocketFactory createSocketFactory(
|
||||
final BundleContext bundleContext,
|
||||
final ServiceRegistration<ManagedService> trustedHostsConfiguration) {
|
||||
return new RelaxedLayeredConnectionSocketFactory(bundleContext, trustedHostsConfiguration, getSocketFactory());
|
||||
}
|
||||
}
|
|
@ -108,15 +108,15 @@ public final class HttpProxyConfigurationActivator implements BundleActivator, M
|
|||
new OSGiTrustedHostsConfiguration(),
|
||||
props);
|
||||
|
||||
final HttpClientBuilderConfigurator configurator =
|
||||
new HttpClientBuilderConfigurator(context, registeredConfigurations, trustedHostConfiguration);
|
||||
|
||||
props.clear();
|
||||
props.put(Constants.SERVICE_PID, BUILDER_FACTORY_SERVICE_PID);
|
||||
props.put(Constants.SERVICE_VENDOR, context.getBundle().getHeaders().get(Constants.BUNDLE_VENDOR));
|
||||
props.put(Constants.SERVICE_DESCRIPTION, BUILDER_FACTORY_SERVICE_NAME);
|
||||
clientFactory = context.registerService(HttpClientBuilderFactory.class,
|
||||
new OSGiClientBuilderFactory(context,
|
||||
registeredConfigurations,
|
||||
trustedHostConfiguration,
|
||||
trackedHttpClients),
|
||||
new OSGiClientBuilderFactory(configurator, trackedHttpClients),
|
||||
props);
|
||||
|
||||
props.clear();
|
||||
|
@ -124,10 +124,7 @@ public final class HttpProxyConfigurationActivator implements BundleActivator, M
|
|||
props.put(Constants.SERVICE_VENDOR, context.getBundle().getHeaders().get(Constants.BUNDLE_VENDOR));
|
||||
props.put(Constants.SERVICE_DESCRIPTION, CACHEABLE_BUILDER_FACTORY_SERVICE_NAME);
|
||||
cachingClientFactory = context.registerService(CachingHttpClientBuilderFactory.class,
|
||||
new OSGiCachingClientBuilderFactory(context,
|
||||
registeredConfigurations,
|
||||
trustedHostConfiguration,
|
||||
trackedHttpClients),
|
||||
new OSGiCachingClientBuilderFactory(configurator, trackedHttpClients),
|
||||
props);
|
||||
}
|
||||
|
||||
|
|
|
@ -27,42 +27,36 @@
|
|||
package org.apache.hc.client5.http.osgi.impl;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.hc.client5.http.impl.cache.CachingHttpClientBuilder;
|
||||
import org.apache.hc.client5.http.impl.sync.CloseableHttpClient;
|
||||
import org.apache.hc.client5.http.osgi.services.CachingHttpClientBuilderFactory;
|
||||
import org.apache.hc.client5.http.osgi.services.ProxyConfiguration;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.framework.ServiceRegistration;
|
||||
import org.osgi.service.cm.ManagedService;
|
||||
|
||||
class OSGiCachingClientBuilderFactory implements CachingHttpClientBuilderFactory {
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
final class OSGiCachingClientBuilderFactory implements CachingHttpClientBuilderFactory {
|
||||
|
||||
private final BundleContext bundleContext;
|
||||
private final HttpClientBuilderConfigurator configurator;
|
||||
|
||||
private final Map<String, ServiceRegistration<ProxyConfiguration>> registeredConfigurations;
|
||||
private List<CloseableHttpClient> trackedHttpClients;
|
||||
|
||||
private final ServiceRegistration<ManagedService> trustedHostConfiguration;
|
||||
|
||||
private final List<CloseableHttpClient> trackedHttpClients;
|
||||
|
||||
public OSGiCachingClientBuilderFactory(
|
||||
final BundleContext bundleContext,
|
||||
final Map<String, ServiceRegistration<ProxyConfiguration>> registeredConfigurations,
|
||||
final ServiceRegistration<ManagedService> trustedHostConfiguration,
|
||||
OSGiCachingClientBuilderFactory(
|
||||
final HttpClientBuilderConfigurator configurator,
|
||||
final List<CloseableHttpClient> trackedHttpClients) {
|
||||
this.bundleContext = bundleContext;
|
||||
this.registeredConfigurations = registeredConfigurations;
|
||||
this.trustedHostConfiguration = trustedHostConfiguration;
|
||||
this.configurator = configurator;
|
||||
this.trackedHttpClients = trackedHttpClients;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CachingHttpClientBuilder newBuilder() {
|
||||
return new OSGiCachingHttpClientBuilder(bundleContext,
|
||||
registeredConfigurations,
|
||||
trustedHostConfiguration,
|
||||
trackedHttpClients);
|
||||
return configurator.configure(new CachingHttpClientBuilder() {
|
||||
@Override
|
||||
public CloseableHttpClient build() {
|
||||
final CloseableHttpClient client = super.build();
|
||||
trackedHttpClients.add(client);
|
||||
return client;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,76 +0,0 @@
|
|||
/*
|
||||
* ====================================================================
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.hc.client5.http.osgi.impl;
|
||||
|
||||
import static org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory.getSocketFactory;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.hc.client5.http.impl.cache.CachingHttpClientBuilder;
|
||||
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
|
||||
import org.apache.hc.client5.http.impl.sync.CloseableHttpClient;
|
||||
import org.apache.hc.client5.http.osgi.services.ProxyConfiguration;
|
||||
import org.apache.hc.client5.http.socket.ConnectionSocketFactory;
|
||||
import org.apache.hc.client5.http.socket.LayeredConnectionSocketFactory;
|
||||
import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory;
|
||||
import org.apache.hc.core5.http.config.RegistryBuilder;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.framework.ServiceRegistration;
|
||||
import org.osgi.service.cm.ManagedService;
|
||||
|
||||
final class OSGiCachingHttpClientBuilder extends CachingHttpClientBuilder {
|
||||
|
||||
private final List<CloseableHttpClient> trackedHttpClients;
|
||||
|
||||
public OSGiCachingHttpClientBuilder(
|
||||
final BundleContext bundleContext,
|
||||
final Map<String, ServiceRegistration<ProxyConfiguration>> registeredConfigurations,
|
||||
final ServiceRegistration<ManagedService> trustedHostConfiguration,
|
||||
final List<CloseableHttpClient> trackedHttpClients) {
|
||||
this.trackedHttpClients = trackedHttpClients;
|
||||
setDefaultCredentialsProvider(
|
||||
new OSGiCredentialsProvider(bundleContext, registeredConfigurations));
|
||||
setRoutePlanner(
|
||||
new OSGiHttpRoutePlanner(bundleContext, registeredConfigurations));
|
||||
final LayeredConnectionSocketFactory defaultSocketFactory = getSocketFactory();
|
||||
setConnectionManager(new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create()
|
||||
.register("http", PlainConnectionSocketFactory.INSTANCE)
|
||||
.register("https", new RelaxedLayeredConnectionSocketFactory(bundleContext, trustedHostConfiguration, defaultSocketFactory))
|
||||
.build()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CloseableHttpClient build() {
|
||||
final CloseableHttpClient httpClient = super.build();
|
||||
synchronized (trackedHttpClients) {
|
||||
trackedHttpClients.add(httpClient);
|
||||
}
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
}
|
|
@ -27,43 +27,36 @@
|
|||
package org.apache.hc.client5.http.osgi.impl;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.hc.client5.http.impl.sync.CloseableHttpClient;
|
||||
import org.apache.hc.client5.http.impl.sync.HttpClientBuilder;
|
||||
import org.apache.hc.client5.http.osgi.services.HttpClientBuilderFactory;
|
||||
import org.apache.hc.client5.http.osgi.services.ProxyConfiguration;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.framework.ServiceRegistration;
|
||||
import org.osgi.service.cm.ManagedService;
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
public final class OSGiClientBuilderFactory implements HttpClientBuilderFactory {
|
||||
final class OSGiClientBuilderFactory implements HttpClientBuilderFactory {
|
||||
|
||||
private final BundleContext bundleContext;
|
||||
|
||||
private final Map<String, ServiceRegistration<ProxyConfiguration>> registeredConfigurations;
|
||||
|
||||
private final ServiceRegistration<ManagedService> trustedHostConfiguration;
|
||||
private final HttpClientBuilderConfigurator configurator;
|
||||
|
||||
private final List<CloseableHttpClient> trackedHttpClients;
|
||||
|
||||
public OSGiClientBuilderFactory(
|
||||
final BundleContext bundleContext,
|
||||
final Map<String, ServiceRegistration<ProxyConfiguration>> registeredConfigurations,
|
||||
final ServiceRegistration<ManagedService> trustedHostConfiguration,
|
||||
OSGiClientBuilderFactory(
|
||||
final HttpClientBuilderConfigurator configurator,
|
||||
final List<CloseableHttpClient> trackedHttpClients) {
|
||||
this.bundleContext = bundleContext;
|
||||
this.registeredConfigurations = registeredConfigurations;
|
||||
this.trustedHostConfiguration = trustedHostConfiguration;
|
||||
this.configurator = configurator;
|
||||
this.trackedHttpClients = trackedHttpClients;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpClientBuilder newBuilder() {
|
||||
return new OSGiHttpClientBuilder(bundleContext, registeredConfigurations, trustedHostConfiguration, trackedHttpClients);
|
||||
return configurator.configure(new HttpClientBuilder() {
|
||||
@Override
|
||||
public CloseableHttpClient build() {
|
||||
final CloseableHttpClient client = super.build();
|
||||
trackedHttpClients.add(client);
|
||||
return client;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue