Removed OSGi module
This commit is contained in:
parent
b452006756
commit
5797e2475a
|
@ -1,186 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
====================================================================
|
||||
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 />.
|
||||
--><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.apache.httpcomponents.client5</groupId>
|
||||
<artifactId>httpclient5-parent</artifactId>
|
||||
<version>5.0-beta3-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>httpclient5-osgi</artifactId>
|
||||
<name>Apache HttpClient OSGi bundle</name>
|
||||
<inceptionYear>1999</inceptionYear>
|
||||
<description>
|
||||
Apache HttpComponents Client (OSGi bundle)
|
||||
</description>
|
||||
<url>http://hc.apache.org/httpcomponents-client</url>
|
||||
<packaging>bundle</packaging>
|
||||
|
||||
<properties>
|
||||
<httpcore.osgi.import.version>"5.0-beta1"</httpcore.osgi.import.version>
|
||||
<slf4j.osgi.import.version>"[1.7, 1.8)"</slf4j.osgi.import.version>
|
||||
<osgi.framework.version>5.0.0</osgi.framework.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents.client5</groupId>
|
||||
<artifactId>httpclient5</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<!-- Depend on the OSGi bundle - the bundle plugin knows what to do. -->
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents.core5</groupId>
|
||||
<artifactId>httpcore5-osgi</artifactId>
|
||||
<version>${httpcore.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents.client5</groupId>
|
||||
<artifactId>httpclient5-cache</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents.client5</groupId>
|
||||
<artifactId>httpclient5-fluent</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.osgi</groupId>
|
||||
<artifactId>org.osgi.core</artifactId>
|
||||
<version>${osgi.framework.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.osgi</groupId>
|
||||
<artifactId>org.osgi.compendium</artifactId>
|
||||
<version>${osgi.framework.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-bundle-plugin</artifactId>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<instructions>
|
||||
<Bundle-Name>Apache ${project.name}</Bundle-Name>
|
||||
<Bundle-SymbolicName>${project.groupId}.httpclient</Bundle-SymbolicName>
|
||||
<_exportcontents>
|
||||
org.apache.hc.client5.*;version=${project.version}
|
||||
</_exportcontents>
|
||||
<Embed-Dependency>*;scope=compile|runtime;inline=true</Embed-Dependency>
|
||||
<Import-Package>
|
||||
javax.crypto,
|
||||
javax.crypto.spec,
|
||||
javax.naming,
|
||||
javax.naming.directory,
|
||||
javax.naming.ldap,
|
||||
javax.net,
|
||||
javax.net.ssl,
|
||||
javax.security.auth.x500,
|
||||
org.ietf.jgss,,
|
||||
org.osgi.framework,
|
||||
org.osgi.service.cm,
|
||||
org.slf4j;version=${slf4j.osgi.import.version},
|
||||
org.apache.hc.core5.*;version=${httpcore.osgi.import.version},
|
||||
org.ehcache.*;resolution:=optional,
|
||||
net.spy.memcached.*;resolution:=optional
|
||||
</Import-Package>
|
||||
<Bundle-Activator>org.apache.hc.client5.http.osgi.impl.HttpProxyConfigurationActivator</Bundle-Activator>
|
||||
<!-- Stop the JAVA_1_n_HOME variables from being treated as headers by Bnd -->
|
||||
<_removeheaders>JAVA_1_3_HOME,JAVA_1_4_HOME</_removeheaders>
|
||||
</instructions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
<version>2.22.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>integration-test</goal>
|
||||
<goal>verify</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<java.awt.headless>true</java.awt.headless>
|
||||
<project.build.directory>${project.build.directory}</project.build.directory>
|
||||
<project.version>${project.version}</project.version>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<finalName>org.apache.httpcomponents.httpclient_${project.version}</finalName>
|
||||
</build>
|
||||
|
||||
<reporting>
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-project-info-reports-plugin</artifactId>
|
||||
<inherited>false</inherited>
|
||||
<reportSets>
|
||||
<reportSet>
|
||||
<reports>
|
||||
<report>dependencies</report>
|
||||
<report>dependency-info</report>
|
||||
<report>summary</report>
|
||||
</reports>
|
||||
</reportSet>
|
||||
</reportSets>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</reporting>
|
||||
|
||||
</project>
|
|
@ -1,158 +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 java.util.Locale;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
interface HostMatcher {
|
||||
|
||||
public static final String DOT = ".";
|
||||
|
||||
/**
|
||||
* The IP mask pattern against which hosts are matched.
|
||||
*/
|
||||
public static final Pattern IP_MASK_PATTERN = Pattern.compile("^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
|
||||
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
|
||||
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
|
||||
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
|
||||
|
||||
boolean matches(String host);
|
||||
|
||||
public static class HostMatcherFactory {
|
||||
|
||||
public static HostMatcher createMatcher(final String name) {
|
||||
final NetworkAddress na = NetworkAddress.parse(name);
|
||||
if (na != null) {
|
||||
return new IPAddressMatcher(na);
|
||||
}
|
||||
|
||||
if (name.startsWith(DOT)) {
|
||||
return new DomainNameMatcher(name);
|
||||
}
|
||||
|
||||
return new HostNameMatcher(name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class HostNameMatcher implements HostMatcher {
|
||||
|
||||
private final String hostName;
|
||||
|
||||
HostNameMatcher(final String hostName) {
|
||||
this.hostName = hostName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(final String host) {
|
||||
return hostName.equalsIgnoreCase(host);
|
||||
}
|
||||
}
|
||||
|
||||
public static class DomainNameMatcher implements HostMatcher {
|
||||
|
||||
private final String domainName;
|
||||
|
||||
DomainNameMatcher(final String domainName) {
|
||||
this.domainName = domainName.toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(final String host) {
|
||||
return host.toLowerCase(Locale.ROOT).endsWith(domainName);
|
||||
}
|
||||
}
|
||||
|
||||
public static class IPAddressMatcher implements HostMatcher {
|
||||
|
||||
private final NetworkAddress address;
|
||||
|
||||
IPAddressMatcher(final NetworkAddress address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(final String host) {
|
||||
final NetworkAddress hostAddress = NetworkAddress.parse(host);
|
||||
return hostAddress != null && address.address == (hostAddress.address & address.mask);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class NetworkAddress {
|
||||
|
||||
final int address;
|
||||
|
||||
final int mask;
|
||||
|
||||
static NetworkAddress parse(final String adrSpec) {
|
||||
|
||||
if (null != adrSpec) {
|
||||
final Matcher nameMatcher = IP_MASK_PATTERN.matcher(adrSpec);
|
||||
if (nameMatcher.matches()) {
|
||||
try {
|
||||
final int i1 = toInt(nameMatcher.group(1), 255);
|
||||
final int i2 = toInt(nameMatcher.group(2), 255);
|
||||
final int i3 = toInt(nameMatcher.group(3), 255);
|
||||
final int i4 = toInt(nameMatcher.group(4), 255);
|
||||
final int ip = i1 << 24 | i2 << 16 | i3 << 8 | i4;
|
||||
|
||||
int mask = toInt(nameMatcher.group(4), 32);
|
||||
mask = (mask == 32) ? -1 : -1 - (-1 >>> mask);
|
||||
|
||||
return new NetworkAddress(ip, mask);
|
||||
} catch (final NumberFormatException nfe) {
|
||||
// not expected after the pattern match !
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static int toInt(final String value, final int max) {
|
||||
if (value == null || value.isEmpty()) {
|
||||
return max;
|
||||
}
|
||||
|
||||
int number = Integer.parseInt(value);
|
||||
if (number > max) {
|
||||
number = max;
|
||||
}
|
||||
return number;
|
||||
}
|
||||
|
||||
NetworkAddress(final int address, final int mask) {
|
||||
this.address = address;
|
||||
this.mask = mask;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,79 +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 org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
|
||||
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
|
||||
import org.apache.hc.client5.http.osgi.services.ProxyConfiguration;
|
||||
import org.apache.hc.client5.http.osgi.services.TrustedHostsConfiguration;
|
||||
import org.apache.hc.client5.http.socket.ConnectionSocketFactory;
|
||||
import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory;
|
||||
import org.apache.hc.core5.http.URIScheme;
|
||||
import org.apache.hc.core5.http.config.Registry;
|
||||
import org.apache.hc.core5.http.config.RegistryBuilder;
|
||||
|
||||
final class HttpClientBuilderConfigurator {
|
||||
|
||||
private final OSGiCredentialsProvider credentialsProvider;
|
||||
|
||||
private final OSGiHttpRoutePlanner routePlanner;
|
||||
|
||||
private final Registry<ConnectionSocketFactory> socketFactoryRegistry;
|
||||
|
||||
HttpClientBuilderConfigurator(
|
||||
final List<ProxyConfiguration> proxyConfigurations,
|
||||
final TrustedHostsConfiguration trustedHostsConfiguration) {
|
||||
credentialsProvider = new OSGiCredentialsProvider(proxyConfigurations);
|
||||
routePlanner = new OSGiHttpRoutePlanner(proxyConfigurations);
|
||||
socketFactoryRegistry = createSocketFactoryRegistry(trustedHostsConfiguration);
|
||||
}
|
||||
|
||||
<T extends HttpClientBuilder> T configure(final T clientBuilder) {
|
||||
clientBuilder
|
||||
.setDefaultCredentialsProvider(credentialsProvider)
|
||||
.setRoutePlanner(routePlanner)
|
||||
.setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry));
|
||||
return clientBuilder;
|
||||
}
|
||||
|
||||
private Registry<ConnectionSocketFactory> createSocketFactoryRegistry(
|
||||
final TrustedHostsConfiguration trustedHostsConfiguration) {
|
||||
return RegistryBuilder.<ConnectionSocketFactory>create()
|
||||
.register(URIScheme.HTTP.id, PlainConnectionSocketFactory.INSTANCE)
|
||||
.register(URIScheme.HTTPS.id, createSocketFactory(trustedHostsConfiguration))
|
||||
.build();
|
||||
}
|
||||
|
||||
private ConnectionSocketFactory createSocketFactory(
|
||||
final TrustedHostsConfiguration trustedHostsConfiguration) {
|
||||
return new RelaxedLayeredConnectionSocketFactory(trustedHostsConfiguration, getSocketFactory());
|
||||
}
|
||||
}
|
|
@ -1,219 +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 java.util.Dictionary;
|
||||
import java.util.Hashtable;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.client5.http.osgi.services.CachingHttpClientBuilderFactory;
|
||||
import org.apache.hc.client5.http.osgi.services.HttpClientBuilderFactory;
|
||||
import org.apache.hc.client5.http.osgi.services.ProxyConfiguration;
|
||||
import org.apache.hc.core5.io.Closer;
|
||||
import org.osgi.framework.BundleActivator;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.framework.Constants;
|
||||
import org.osgi.framework.ServiceReference;
|
||||
import org.osgi.framework.ServiceRegistration;
|
||||
import org.osgi.service.cm.ConfigurationException;
|
||||
import org.osgi.service.cm.ManagedService;
|
||||
import org.osgi.service.cm.ManagedServiceFactory;
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
public final class HttpProxyConfigurationActivator implements BundleActivator, ManagedServiceFactory {
|
||||
|
||||
private static final String PROXY_SERVICE_FACTORY_NAME = "Apache HTTP Client Proxy Configuration Factory";
|
||||
|
||||
private static final String PROXY_SERVICE_PID = "org.apache.hc.client5.http.proxyconfigurator";
|
||||
|
||||
private static final String TRUSTED_HOSTS_SERVICE_NAME = "Apache HTTP Client Trusted Hosts Configuration";
|
||||
|
||||
private static final String TRUSTED_HOSTS_PID = "org.apache.hc.client5.http.trustedhosts";
|
||||
|
||||
private static final String BUILDER_FACTORY_SERVICE_NAME = "Apache HTTP Client Client Factory";
|
||||
|
||||
private static final String BUILDER_FACTORY_SERVICE_PID = "org.apache.hc.client5.http.httpclientfactory";
|
||||
|
||||
private static final String CACHEABLE_BUILDER_FACTORY_SERVICE_NAME = "Apache HTTP Client Caching Client Factory";
|
||||
|
||||
private static final String CACHEABLE_BUILDER_FACTORY_SERVICE_PID = "org.apache.hc.client5.http.cachinghttpclientfactory";
|
||||
|
||||
private ServiceRegistration<ManagedServiceFactory> configurator;
|
||||
|
||||
private ServiceRegistration<ManagedService> trustedHostConfiguration;
|
||||
|
||||
private ServiceRegistration<HttpClientBuilderFactory> clientFactory;
|
||||
|
||||
private ServiceRegistration<CachingHttpClientBuilderFactory> cachingClientFactory;
|
||||
|
||||
private BundleContext context;
|
||||
|
||||
private final Map<String, ServiceRegistration<ProxyConfiguration>> registeredConfigurations = new LinkedHashMap<>();
|
||||
|
||||
private final List<ProxyConfiguration> proxyConfigurations = new CopyOnWriteArrayList<>();
|
||||
|
||||
private final HttpClientTracker httpClientTracker = new HttpClientTracker();
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void start(final BundleContext context) throws Exception {
|
||||
this.context = context;
|
||||
|
||||
// ensure we receive configurations for the proxy selector
|
||||
final Hashtable<String, Object> props = new Hashtable<>();
|
||||
props.put(Constants.SERVICE_PID, getName());
|
||||
props.put(Constants.SERVICE_VENDOR, context.getBundle().getHeaders().get(Constants.BUNDLE_VENDOR));
|
||||
props.put(Constants.SERVICE_DESCRIPTION, PROXY_SERVICE_FACTORY_NAME);
|
||||
|
||||
configurator = context.registerService(ManagedServiceFactory.class, this, props);
|
||||
|
||||
props.clear();
|
||||
props.put(Constants.SERVICE_PID, TRUSTED_HOSTS_PID);
|
||||
props.put(Constants.SERVICE_VENDOR, context.getBundle().getHeaders().get(Constants.BUNDLE_VENDOR));
|
||||
props.put(Constants.SERVICE_DESCRIPTION, TRUSTED_HOSTS_SERVICE_NAME);
|
||||
final OSGiTrustedHostsConfiguration trustedHosts = new OSGiTrustedHostsConfiguration();
|
||||
trustedHostConfiguration = context.registerService(ManagedService.class, trustedHosts, props);
|
||||
|
||||
final HttpClientBuilderConfigurator configurator =
|
||||
new HttpClientBuilderConfigurator(proxyConfigurations, trustedHosts);
|
||||
|
||||
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(configurator, httpClientTracker),
|
||||
props);
|
||||
|
||||
props.clear();
|
||||
props.put(Constants.SERVICE_PID, CACHEABLE_BUILDER_FACTORY_SERVICE_PID);
|
||||
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(configurator, httpClientTracker),
|
||||
props);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void stop(final BundleContext context) throws Exception {
|
||||
// unregister services
|
||||
for (final ServiceRegistration<ProxyConfiguration> registeredConfiguration : registeredConfigurations.values()) {
|
||||
safeUnregister(registeredConfiguration);
|
||||
}
|
||||
// remove all tracked services
|
||||
registeredConfigurations.clear();
|
||||
|
||||
safeUnregister(configurator);
|
||||
safeUnregister(clientFactory);
|
||||
safeUnregister(cachingClientFactory);
|
||||
safeUnregister(trustedHostConfiguration);
|
||||
|
||||
// ensure all http clients are closed
|
||||
httpClientTracker.closeAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String getName() {
|
||||
return PROXY_SERVICE_PID;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void updated(final String pid, final Dictionary<String, ?> config) throws ConfigurationException {
|
||||
final ServiceRegistration<ProxyConfiguration> registration = registeredConfigurations.get(pid);
|
||||
final OSGiProxyConfiguration proxyConfiguration;
|
||||
|
||||
if (registration == null) {
|
||||
proxyConfiguration = new OSGiProxyConfiguration();
|
||||
final ServiceRegistration<ProxyConfiguration> configurationRegistration =
|
||||
context.registerService(ProxyConfiguration.class,
|
||||
proxyConfiguration,
|
||||
config);
|
||||
registeredConfigurations.put(pid, configurationRegistration);
|
||||
proxyConfigurations.add(proxyConfiguration);
|
||||
} else {
|
||||
proxyConfiguration = (OSGiProxyConfiguration) context.getService(registration.getReference());
|
||||
}
|
||||
|
||||
proxyConfiguration.update(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void deleted(final String pid) {
|
||||
final ServiceRegistration<ProxyConfiguration> registration = registeredConfigurations.remove(pid);
|
||||
if (registration != null) {
|
||||
final ServiceReference<ProxyConfiguration> ref = registration.getReference();
|
||||
final ProxyConfiguration config = context.getService(ref);
|
||||
proxyConfigurations.remove(config);
|
||||
context.ungetService(ref);
|
||||
safeUnregister(registration);
|
||||
}
|
||||
}
|
||||
|
||||
private static <S> boolean safeUnregister(final ServiceRegistration<S> serviceRegistration) {
|
||||
if (serviceRegistration != null) {
|
||||
serviceRegistration.unregister();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static class HttpClientTracker {
|
||||
|
||||
private final List<CloseableHttpClient> trackedHttpClients = new WeakList<>();
|
||||
|
||||
synchronized void track(final CloseableHttpClient client) {
|
||||
trackedHttpClients.add(client);
|
||||
}
|
||||
|
||||
synchronized void closeAll() {
|
||||
for (final CloseableHttpClient client : trackedHttpClients) {
|
||||
Closer.closeQuietly(client);
|
||||
}
|
||||
trackedHttpClients.clear();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,60 +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 org.apache.hc.client5.http.impl.cache.CachingHttpClientBuilder;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.client5.http.osgi.services.CachingHttpClientBuilderFactory;
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
final class OSGiCachingClientBuilderFactory implements CachingHttpClientBuilderFactory {
|
||||
|
||||
private final HttpClientBuilderConfigurator configurator;
|
||||
|
||||
private final HttpProxyConfigurationActivator.HttpClientTracker httpClientTracker;
|
||||
|
||||
OSGiCachingClientBuilderFactory(
|
||||
final HttpClientBuilderConfigurator configurator,
|
||||
final HttpProxyConfigurationActivator.HttpClientTracker httpClientTracker) {
|
||||
this.configurator = configurator;
|
||||
this.httpClientTracker = httpClientTracker;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CachingHttpClientBuilder newBuilder() {
|
||||
return configurator.configure(new CachingHttpClientBuilder() {
|
||||
@Override
|
||||
public CloseableHttpClient build() {
|
||||
final CloseableHttpClient client = super.build();
|
||||
httpClientTracker.track(client);
|
||||
return client;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,60 +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 org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
|
||||
import org.apache.hc.client5.http.osgi.services.HttpClientBuilderFactory;
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
final class OSGiClientBuilderFactory implements HttpClientBuilderFactory {
|
||||
|
||||
private final HttpClientBuilderConfigurator configurator;
|
||||
|
||||
private final HttpProxyConfigurationActivator.HttpClientTracker httpClientTracker;
|
||||
|
||||
OSGiClientBuilderFactory(
|
||||
final HttpClientBuilderConfigurator configurator,
|
||||
final HttpProxyConfigurationActivator.HttpClientTracker httpClientTracker) {
|
||||
this.configurator = configurator;
|
||||
this.httpClientTracker = httpClientTracker;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpClientBuilder newBuilder() {
|
||||
return configurator.configure(new HttpClientBuilder() {
|
||||
@Override
|
||||
public CloseableHttpClient build() {
|
||||
final CloseableHttpClient client = super.build();
|
||||
httpClientTracker.track(client);
|
||||
return client;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,118 +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 java.util.List;
|
||||
|
||||
import org.apache.hc.client5.http.auth.AuthScope;
|
||||
import org.apache.hc.client5.http.auth.Credentials;
|
||||
import org.apache.hc.client5.http.auth.CredentialsStore;
|
||||
import org.apache.hc.client5.http.auth.NTCredentials;
|
||||
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.hc.client5.http.osgi.services.ProxyConfiguration;
|
||||
import org.apache.hc.core5.http.protocol.HttpContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
final class OSGiCredentialsProvider implements CredentialsStore {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(OSGiCredentialsProvider.class);
|
||||
|
||||
private static final int HOST_AND_PORT_MATCH = 12;
|
||||
|
||||
private static final String BASIC_SCHEME_NAME = "BASIC";
|
||||
|
||||
private static final String NTLM_SCHEME_NAME = "NTLM";
|
||||
|
||||
private final List<ProxyConfiguration> proxyConfigurations;
|
||||
|
||||
OSGiCredentialsProvider(final List<ProxyConfiguration> proxyConfigurations) {
|
||||
this.proxyConfigurations = proxyConfigurations;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void setCredentials(final AuthScope authscope, final Credentials credentials) {
|
||||
// do nothing, not used in this version
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Credentials getCredentials(final AuthScope authScope, final HttpContext context) {
|
||||
// iterate over all active proxy configurations at the moment of getting the credential
|
||||
for (final ProxyConfiguration config : proxyConfigurations) {
|
||||
if (config.isEnabled() && isSuitable(config, authScope)) {
|
||||
final String scheme = authScope.getAuthScheme();
|
||||
if (BASIC_SCHEME_NAME.equals(scheme)) {
|
||||
return new UsernamePasswordCredentials(config.getUsername(), config.getPassword().toCharArray());
|
||||
} else if (NTLM_SCHEME_NAME.equals(scheme)) {
|
||||
return createNTCredentials(config);
|
||||
} else {
|
||||
log.debug("credentials requested for unsupported authentication scheme " + scheme);
|
||||
}
|
||||
}
|
||||
}
|
||||
// credentials no longer available!
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void clear() {
|
||||
// do nothing, not used in this version
|
||||
}
|
||||
|
||||
// suitable configurations match at least the host and port of the AuthScope
|
||||
private boolean isSuitable(final ProxyConfiguration config, final AuthScope authScope) {
|
||||
return authScope.match(new AuthScope(config.getHostname(), config.getPort())) >= HOST_AND_PORT_MATCH;
|
||||
}
|
||||
|
||||
private static Credentials createNTCredentials(final ProxyConfiguration config) {
|
||||
final String domainAndUsername = config.getUsername();
|
||||
final String username;
|
||||
final String domain;
|
||||
final int index = domainAndUsername.indexOf("\\");
|
||||
if (index > -1) {
|
||||
username = domainAndUsername.substring(index + 1);
|
||||
domain = domainAndUsername.substring(0, index);
|
||||
} else {
|
||||
username = domainAndUsername;
|
||||
domain = null;
|
||||
}
|
||||
return new NTCredentials(username, config.getPassword().toCharArray(), null, domain);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,73 +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.osgi.impl.HostMatcher.HostMatcherFactory.createMatcher;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.hc.client5.http.impl.routing.DefaultRoutePlanner;
|
||||
import org.apache.hc.client5.http.osgi.services.ProxyConfiguration;
|
||||
import org.apache.hc.core5.http.HttpException;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.protocol.HttpContext;
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
final class OSGiHttpRoutePlanner extends DefaultRoutePlanner {
|
||||
|
||||
private final List<ProxyConfiguration> proxyConfigurations;
|
||||
|
||||
public OSGiHttpRoutePlanner(final List<ProxyConfiguration> proxyConfigurations) {
|
||||
super(null);
|
||||
this.proxyConfigurations = proxyConfigurations;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected HttpHost determineProxy(final HttpHost target, final HttpContext context) throws HttpException {
|
||||
HttpHost proxyHost = null;
|
||||
for (final ProxyConfiguration proxyConfiguration : proxyConfigurations) {
|
||||
if (proxyConfiguration.isEnabled()) {
|
||||
for (final String exception : proxyConfiguration.getProxyExceptions()) {
|
||||
if (createMatcher(exception).matches(target.getHostName())) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (null == proxyHost) {
|
||||
proxyHost = new HttpHost(proxyConfiguration.getHostname(), proxyConfiguration.getPort());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return proxyHost;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,142 +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 java.lang.String.format;
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.apache.hc.client5.http.osgi.impl.PropertiesUtils.to;
|
||||
|
||||
import java.util.Dictionary;
|
||||
|
||||
import org.apache.hc.client5.http.osgi.services.ProxyConfiguration;
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
public final class OSGiProxyConfiguration implements ProxyConfiguration {
|
||||
|
||||
/**
|
||||
* Property indicating whether this particular proxy is enabled (shall be used or not). Defaults to true.
|
||||
*/
|
||||
private static final String PROPERTYNAME_PROXY_ENABLED = "proxy.enabled";
|
||||
|
||||
private static final Boolean PROPERTYDEFAULT_PROXY_ENABLED = Boolean.TRUE;
|
||||
|
||||
/**
|
||||
* Property representing the hostname of the proxy. Defaults to empty.
|
||||
*/
|
||||
private static final String PROPERTYNAME_PROXY_HOSTNAME = "proxy.host";
|
||||
|
||||
private static final String PROPERTYDEFAULT_PROXY_HOSTNAME = "";
|
||||
|
||||
/**
|
||||
* Property representing the port of the proxy. Defaults to 0.
|
||||
*/
|
||||
private static final String PROPERTYNAME_PROXY_PORT = "proxy.port";
|
||||
|
||||
private static final Integer PROPERTYDEFAULT_PROXY_PORT = Integer.valueOf(0);
|
||||
|
||||
/**
|
||||
* Property representing the username to authenticate with towards the proxy. Defaults to empty.
|
||||
*/
|
||||
private static final String PROPERTYNAME_PROXY_USERNAME = "proxy.user";
|
||||
|
||||
private static final String PROPERTYDEFAULT_PROXY_USERNAME = "";
|
||||
|
||||
/**
|
||||
* Property representing the password to authenticate with towards the proxy. Defaults to empty.
|
||||
*/
|
||||
private static final String PROPERTYNAME_PROXY_PASSWORD = "proxy.password";
|
||||
|
||||
private static final String PROPERTYDEFAULT_PROXY_PASSWORD = "";
|
||||
|
||||
/**
|
||||
* A multivalue property representing host patterns for which no proxy shall be used. By default localhost is
|
||||
* excluded.
|
||||
*/
|
||||
private static final String PROPERTYNAME_PROXY_EXCEPTIONS = "proxy.exceptions";
|
||||
|
||||
private static final String[] PROPERTYDEFAULT_PROXY_EXCEPTIONS = new String[]{"localhost", "127.0.0.1"};
|
||||
|
||||
private Boolean enabled = Boolean.FALSE; // fewer boxing conversions needed when stored as an object
|
||||
|
||||
private String hostname;
|
||||
|
||||
private Integer port = Integer.valueOf(0); // fewer boxing conversions needed when stored as an object
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
private String[] proxyExceptions;
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return enabled.booleanValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHostname() {
|
||||
return hostname;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return port.intValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getProxyExceptions() {
|
||||
return proxyExceptions;
|
||||
}
|
||||
|
||||
public void update(final Dictionary<String, ?> config) {
|
||||
enabled = to(config.get(PROPERTYNAME_PROXY_ENABLED), boolean.class, PROPERTYDEFAULT_PROXY_ENABLED);
|
||||
hostname = to(config.get(PROPERTYNAME_PROXY_HOSTNAME), String.class, PROPERTYDEFAULT_PROXY_HOSTNAME);
|
||||
port = to(config.get(PROPERTYNAME_PROXY_PORT), int.class, PROPERTYDEFAULT_PROXY_PORT);
|
||||
username = to(config.get(PROPERTYNAME_PROXY_USERNAME), String.class, PROPERTYDEFAULT_PROXY_USERNAME);
|
||||
password = to(config.get(PROPERTYNAME_PROXY_PASSWORD), String.class, PROPERTYDEFAULT_PROXY_PASSWORD);
|
||||
proxyExceptions = to(config.get(PROPERTYNAME_PROXY_EXCEPTIONS), String[].class, PROPERTYDEFAULT_PROXY_EXCEPTIONS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return format("ProxyConfiguration [enabled=%s, hostname=%s, port=%s, username=%s, password=%s, proxyExceptions=%s]",
|
||||
enabled, hostname, port, username, password, asList(proxyExceptions));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,104 +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 java.lang.String.format;
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.apache.hc.client5.http.osgi.impl.PropertiesUtils.to;
|
||||
|
||||
import java.util.Dictionary;
|
||||
|
||||
import org.apache.hc.client5.http.osgi.services.TrustedHostsConfiguration;
|
||||
import org.osgi.service.cm.ConfigurationException;
|
||||
import org.osgi.service.cm.ManagedService;
|
||||
|
||||
/**
|
||||
* @since 5.0-alpha2
|
||||
*/
|
||||
final class OSGiTrustedHostsConfiguration implements ManagedService, TrustedHostsConfiguration {
|
||||
|
||||
/**
|
||||
* Property indicating whether this particular configuration is enabled (shall be used or not). Defaults to true.
|
||||
*/
|
||||
private static final String PROPERTYNAME_TRUSTEDHOSTS_ENABLED = "trustedhosts.enabled";
|
||||
|
||||
private static final Boolean PROPERTYDEFAULT_TRUSTEDHOSTS_ENABLED = Boolean.TRUE;
|
||||
|
||||
/**
|
||||
* Property indicating whether this particular configuration . Defaults to false.
|
||||
*/
|
||||
private static final String PROPERTYNAME_TRUST_ALL = "trustedhosts.trustAll";
|
||||
|
||||
private static final Boolean PROPERTYDEFAULT_TRUST_ALL = Boolean.FALSE;
|
||||
|
||||
/**
|
||||
* A multivalue property representing host patterns which is an acceptable match with the server's authentication scheme.
|
||||
* By default <code>localhost</code> (<code>127.0.0.1</code>) is trusted.
|
||||
*/
|
||||
private static final String PROPERTYNAME_TRUSTED_HOSTS = "trustedhosts.hosts";
|
||||
|
||||
private static final String[] PROPERTYDEFAULT_TRUSTED_HOSTS = new String[]{ "localhost", "127.0.0.1" };
|
||||
|
||||
private Boolean enabled = PROPERTYDEFAULT_TRUSTEDHOSTS_ENABLED; // fewer boxing conversions needed when stored as an object
|
||||
|
||||
private Boolean trustAll = PROPERTYDEFAULT_TRUST_ALL; // fewer boxing conversions needed when stored as an object
|
||||
|
||||
private String[] trustedHosts;
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean trustAll() {
|
||||
return trustAll;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getTrustedHosts() {
|
||||
return trustedHosts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updated(final Dictionary<String, ?> config) throws ConfigurationException {
|
||||
if (config == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
enabled = to(config.get(PROPERTYNAME_TRUSTEDHOSTS_ENABLED), boolean.class, PROPERTYDEFAULT_TRUSTEDHOSTS_ENABLED);
|
||||
trustAll = to(config.get(PROPERTYNAME_TRUST_ALL), boolean.class, PROPERTYDEFAULT_TRUST_ALL);
|
||||
trustedHosts = to(config.get(PROPERTYNAME_TRUSTED_HOSTS), String[].class, PROPERTYDEFAULT_TRUSTED_HOSTS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return format("ProxyConfiguration [enabled=%s, trustAll=%s, trustedHosts=%s]",
|
||||
enabled, trustAll, asList(trustedHosts));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,205 +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 java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
final class PropertiesUtils {
|
||||
|
||||
private static final Map<Class<?>, PropertyConverter<?>> CONVERTERS_REGISTRY =
|
||||
new HashMap<>();
|
||||
|
||||
static {
|
||||
register(new BooleanPropertyConverter(), boolean.class, Boolean.class);
|
||||
register(new StringPropertyConverter(), String.class);
|
||||
register(new StringArrayPropertyConverter(), String[].class);
|
||||
register(new IntegerPropertyConverter(), int.class, Integer.class);
|
||||
register(new LongPropertyConverter(), long.class, Long.class);
|
||||
register(new DoublePropertyConverter(), double.class, Double.class);
|
||||
}
|
||||
|
||||
private static void register(final PropertyConverter<?> converter, final Class<?>...targetTypes) {
|
||||
for (final Class<?> targetType : targetTypes) {
|
||||
CONVERTERS_REGISTRY.put(targetType, converter);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T to(final Object propValue, final Class<T> targetType, final T defaultValue) {
|
||||
Object v = propValue;
|
||||
if (v == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
if (!targetType.isArray()) {
|
||||
v = toObject(v);
|
||||
}
|
||||
|
||||
if (targetType.isInstance(v)) {
|
||||
return targetType.cast(v);
|
||||
}
|
||||
|
||||
if (CONVERTERS_REGISTRY.containsKey(targetType)) {
|
||||
@SuppressWarnings("unchecked") final // type driven by targetType
|
||||
PropertyConverter<T> converter = (PropertyConverter<T>) CONVERTERS_REGISTRY.get(targetType);
|
||||
try {
|
||||
return converter.to(v);
|
||||
} catch (final Exception ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the parameter as a single value. If the
|
||||
* parameter is neither an array nor a {@code java.util.Collection} the
|
||||
* parameter is returned unmodified. If the parameter is a non-empty array,
|
||||
* the first array element is returned. If the property is a non-empty
|
||||
* {@code java.util.Collection}, the first collection element is returned.
|
||||
*
|
||||
* @param propValue the parameter to convert.
|
||||
*/
|
||||
private static Object toObject(final Object propValue) {
|
||||
if (propValue.getClass().isArray()) {
|
||||
final Object[] prop = (Object[]) propValue;
|
||||
return prop.length > 0 ? prop[0] : null;
|
||||
}
|
||||
|
||||
if (propValue instanceof Collection<?>) {
|
||||
final Collection<?> prop = (Collection<?>) propValue;
|
||||
return prop.isEmpty() ? null : prop.iterator().next();
|
||||
}
|
||||
|
||||
return propValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hidden constructor, this class must not be instantiated.
|
||||
*/
|
||||
private PropertiesUtils() {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
private interface PropertyConverter<T> {
|
||||
|
||||
T to(Object propValue);
|
||||
|
||||
}
|
||||
|
||||
private static class BooleanPropertyConverter implements PropertyConverter<Boolean> {
|
||||
|
||||
@Override
|
||||
public Boolean to(final Object propValue) {
|
||||
return Boolean.valueOf(String.valueOf(propValue));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class StringPropertyConverter implements PropertyConverter<String> {
|
||||
|
||||
@Override
|
||||
public String to(final Object propValue) {
|
||||
return String.valueOf(propValue);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class StringArrayPropertyConverter implements PropertyConverter<String[]> {
|
||||
|
||||
@Override
|
||||
public String[] to(final Object propValue) {
|
||||
if (propValue instanceof String) {
|
||||
// single string
|
||||
return new String[] { (String) propValue };
|
||||
}
|
||||
|
||||
if (propValue.getClass().isArray()) {
|
||||
// other array
|
||||
final Object[] valueArray = (Object[]) propValue;
|
||||
final List<String> values = new ArrayList<>(valueArray.length);
|
||||
for (final Object value : valueArray) {
|
||||
if (value != null) {
|
||||
values.add(value.toString());
|
||||
}
|
||||
}
|
||||
return values.toArray(new String[values.size()]);
|
||||
|
||||
}
|
||||
|
||||
if (propValue instanceof Collection<?>) {
|
||||
// collection
|
||||
final Collection<?> valueCollection = (Collection<?>) propValue;
|
||||
final List<String> valueList = new ArrayList<>(valueCollection.size());
|
||||
for (final Object value : valueCollection) {
|
||||
if (value != null) {
|
||||
valueList.add(value.toString());
|
||||
}
|
||||
}
|
||||
return valueList.toArray(new String[valueList.size()]);
|
||||
}
|
||||
|
||||
// don't care, fall through to default value
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class IntegerPropertyConverter implements PropertyConverter<Integer> {
|
||||
|
||||
@Override
|
||||
public Integer to(final Object propValue) {
|
||||
return Integer.valueOf(String.valueOf(propValue));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class LongPropertyConverter implements PropertyConverter<Long> {
|
||||
|
||||
@Override
|
||||
public Long to(final Object propValue) {
|
||||
return Long.valueOf(String.valueOf(propValue));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class DoublePropertyConverter implements PropertyConverter<Double> {
|
||||
|
||||
@Override
|
||||
public Double to(final Object propValue) {
|
||||
return Double.valueOf(String.valueOf(propValue));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,91 +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.osgi.impl.HostMatcher.HostMatcherFactory.createMatcher;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
import org.apache.hc.client5.http.osgi.services.TrustedHostsConfiguration;
|
||||
import org.apache.hc.client5.http.socket.LayeredConnectionSocketFactory;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.protocol.HttpContext;
|
||||
import org.apache.hc.core5.util.TimeValue;
|
||||
|
||||
final class RelaxedLayeredConnectionSocketFactory implements LayeredConnectionSocketFactory {
|
||||
|
||||
private final TrustedHostsConfiguration trustedHostsConfiguration;
|
||||
|
||||
private final LayeredConnectionSocketFactory defaultSocketFactory;
|
||||
|
||||
public RelaxedLayeredConnectionSocketFactory(final TrustedHostsConfiguration trustedHostsConfiguration,
|
||||
final LayeredConnectionSocketFactory defaultSocketFactory) {
|
||||
this.trustedHostsConfiguration = trustedHostsConfiguration;
|
||||
this.defaultSocketFactory = defaultSocketFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Socket createLayeredSocket(final Socket socket,
|
||||
final String target,
|
||||
final int port,
|
||||
final HttpContext context) throws IOException {
|
||||
if (trustedHostsConfiguration.isEnabled()) {
|
||||
// if trust all there is no check to perform
|
||||
if (trustedHostsConfiguration.trustAll()) {
|
||||
return socket;
|
||||
}
|
||||
|
||||
// blindly verify the host if in the trust list
|
||||
for (final String trustedHost : trustedHostsConfiguration.getTrustedHosts()) {
|
||||
if (createMatcher(trustedHost).matches(target)) {
|
||||
return socket;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// fall back to the default behavior
|
||||
return defaultSocketFactory.createLayeredSocket(socket, target, port, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Socket createSocket(final HttpContext context) throws IOException {
|
||||
return defaultSocketFactory.createSocket(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Socket connectSocket(final TimeValue connectTimeout,
|
||||
final Socket sock,
|
||||
final HttpHost host,
|
||||
final InetSocketAddress remoteAddress,
|
||||
final InetSocketAddress localAddress,
|
||||
final HttpContext context) throws IOException {
|
||||
return defaultSocketFactory.connectSocket(connectTimeout, sock, host, remoteAddress, localAddress, context);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,129 +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 java.lang.ref.WeakReference;
|
||||
import java.util.AbstractList;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* Implementation of a list backed by WeakReference objects.
|
||||
* This is not a general purpose list and is only meant to be used by this package. It cannot correctly manage null entries by design.
|
||||
*/
|
||||
class WeakList<T> extends AbstractList<T> {
|
||||
|
||||
private final List<WeakReference<T>> innerList;
|
||||
|
||||
public WeakList() {
|
||||
this.innerList = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get(final int index) {
|
||||
return innerList.get(index).get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
checkReferences();
|
||||
return innerList.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(final T t) {
|
||||
return innerList.add(new WeakReference<>(t));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
innerList.clear();
|
||||
}
|
||||
|
||||
private void checkReferences() {
|
||||
final ListIterator<WeakReference<T>> references = innerList.listIterator();
|
||||
while (references.hasNext()) {
|
||||
final WeakReference<T> reference = references.next();
|
||||
if (reference.get() == null) {
|
||||
references.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return new WeakIterator<>(innerList.iterator());
|
||||
}
|
||||
|
||||
private class WeakIterator<T> implements Iterator<T> {
|
||||
|
||||
private final Iterator<WeakReference<T>> innerIterator;
|
||||
|
||||
private WeakReference<T> next;
|
||||
|
||||
public WeakIterator(final Iterator<WeakReference<T>> innerIterator) {
|
||||
this.innerIterator = innerIterator;
|
||||
fetchNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return next != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
if (next != null) {
|
||||
final T result = next.get();
|
||||
fetchNext();
|
||||
return result;
|
||||
}
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
private void fetchNext() {
|
||||
while (innerIterator.hasNext()) {
|
||||
final WeakReference<T> ref = innerIterator.next();
|
||||
final T obj = ref.get();
|
||||
if (obj != null) {
|
||||
next = ref;
|
||||
return;
|
||||
}
|
||||
}
|
||||
next = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,27 +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;
|
|
@ -1,31 +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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
package org.apache.hc.client5.http.osgi;
|
|
@ -1,35 +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.services;
|
||||
|
||||
import org.apache.hc.client5.http.impl.cache.CachingHttpClientBuilder;
|
||||
|
||||
public interface CachingHttpClientBuilderFactory {
|
||||
|
||||
CachingHttpClientBuilder newBuilder();
|
||||
|
||||
}
|
|
@ -1,38 +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.services;
|
||||
|
||||
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
public interface HttpClientBuilderFactory {
|
||||
|
||||
HttpClientBuilder newBuilder();
|
||||
|
||||
}
|
|
@ -1,46 +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.services;
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
public interface ProxyConfiguration {
|
||||
|
||||
boolean isEnabled();
|
||||
|
||||
String getHostname();
|
||||
|
||||
int getPort();
|
||||
|
||||
String getUsername();
|
||||
|
||||
String getPassword();
|
||||
|
||||
String[] getProxyExceptions();
|
||||
|
||||
}
|
|
@ -1,57 +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.services;
|
||||
|
||||
/**
|
||||
* @since 5.0-alpha2
|
||||
*/
|
||||
public interface TrustedHostsConfiguration {
|
||||
|
||||
/**
|
||||
* Flag to mark if current configuration has to be processed when creating SSL sessions..
|
||||
*
|
||||
* @return true if current configuration has to be processed when creating an SSL session, false otherwise.
|
||||
*/
|
||||
boolean isEnabled();
|
||||
|
||||
/**
|
||||
* Flag to mark all SSL certificates are blindly trusted by the client.
|
||||
*
|
||||
* Pay attention on no enabling this feature in production environment as it is totally insecure.
|
||||
*
|
||||
* @return true if all SSL certificates are blindly trusted by the client, false otherwise.
|
||||
*/
|
||||
boolean trustAll();
|
||||
|
||||
/**
|
||||
* The list of trusted hosts for which self-signed certificate is acceptable.
|
||||
*
|
||||
* @return an array representing the list of trusted hosts for which self-signed certificate is acceptable.
|
||||
*/
|
||||
String[] getTrustedHosts();
|
||||
|
||||
}
|
|
@ -1,31 +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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
package org.apache.hc.client5.http.osgi.services;
|
|
@ -1,94 +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.
|
||||
#
|
||||
|
||||
proxyconfigurator.name = Apache HTTP Components Proxy Configurator
|
||||
proxyconfigurator.description = Factory configuration for transparent proxies used by every HTTP Client.
|
||||
|
||||
proxyconfig.name = Apache HTTP Components Proxy Configuration
|
||||
proxyconfig.description = Proxy configuration for central and transparent proxying of http client connections.
|
||||
|
||||
proxy.enabled.name = Enable HTTP Proxy
|
||||
proxy.enabled.description = Whether to enable or disable this particular proxy configuration. \
|
||||
The default value is false.
|
||||
|
||||
proxy.host.name = HTTP Proxy Host
|
||||
proxy.host.description = Host name (or IP Address) of the HTTP Proxy. This property is ignored if \
|
||||
this proxy configuration is disabled. This property does not have a default value. Enabling \
|
||||
this proxy but not setting the HTTP Proxy Host effectively disables this configuration.
|
||||
|
||||
proxy.port.name = HTTP Proxy Port
|
||||
proxy.port.description = TCP port of the HTTP Proxy. This property is ignored if \
|
||||
this proxy configuration is disabled. This property does not have a default value. Enabling \
|
||||
this proxy but not setting the HTTP Proxy Port effectively disables this configuration.
|
||||
|
||||
proxy.user.name = HTTP Proxy User
|
||||
proxy.user.description = The name of the user to authenticate as with the HTTP \
|
||||
Proxy Host. If this field is empty, the proxy is considered to not be \
|
||||
authenticated. The default is empty. This property is ignored if proxying is \
|
||||
disabled or the proxy host is not properly configured.
|
||||
|
||||
proxy.password.name = HTTP Proxy Password
|
||||
proxy.password.description = The password of the HTTP Proxy user to authenticate \
|
||||
with. The default is empty. This property is ignored if proxying is \
|
||||
disabled or the proxy host is not properly configured.
|
||||
|
||||
proxy.ntlm.host.name = HTTP Proxy NTLM Host
|
||||
proxy.ntlm.host.description = The host the authentication request is \
|
||||
originating from. Essentially, the computer name for this machine. By default \
|
||||
the credentials assume simple username password authentication. If the proxy \
|
||||
happens to be a Microsoft IIS Server using NTLM authentication this property \
|
||||
must be set to the NT Domain name of the user to authenticate as. This is \
|
||||
not set by default.
|
||||
|
||||
proxy.ntlm.domain.name = HTTP Proxy NTLM Domain
|
||||
proxy.ntlm.domain.description = The NTLM domain to authenticate within. By \
|
||||
default the credentials assume simple username password authentication. If \
|
||||
the proxy happens to be a Microsoft IIS Server using NTLM authentication this \
|
||||
property must be set to the NT Domain name of the user to authenticate as. \
|
||||
This is not set by default.
|
||||
|
||||
proxy.exceptions.name = No Proxy For
|
||||
proxy.exceptions.description = Lists domain names, host names, IP Addresses or \
|
||||
or network addresses for which this proxy configuration should not be used. A domain \
|
||||
name indicating all hosts of a domain is indicated by a leading dot, e.g. \
|
||||
".day.com". A network address is indicated with subnet mask notation indicating \
|
||||
the number of bits make up the network address, e.g 192.168.1.0/24 means the \
|
||||
class C network "192.168.1". Note that for proxy selection, the host name of \
|
||||
URL is not resolved but directly compared to the list of exceptions. For this \
|
||||
reason you might want to indicate both the network address and the domain for \
|
||||
targets which should not be passed through the proxy. This property has no \
|
||||
effect if this proxy configuration is disabled. The default value is [ localhost, \
|
||||
127.0.0.1 ].
|
||||
|
||||
trustedhosts.name = Apache HTTP Client Trusted Hosts Configuration
|
||||
trustedhosts.description = SSL configuration for trusted SSL connections with server self-signed certificates.
|
||||
|
||||
trustedhosts.enabled.name = SSL configuration enabled
|
||||
trustedhosts.enabled.description = Mark this SSL configuration be taken in consideration \
|
||||
when creating a new HTTP CLient instance.
|
||||
|
||||
trustedhosts.trustAll.name = Trust all SSL certificates
|
||||
trustedhosts.trustAll.description = Allows the new HTTP Client instance to \
|
||||
blindly trust all SSL certificates.\
|
||||
Pay attention on no enabling this feature in production environment as it is totally insecure!
|
||||
|
||||
trustedhosts.hosts.name = Trust SSL self-signed certificates only for specified hosts
|
||||
trustedhosts.hosts.description = Allows the new HTTP Client instance to \
|
||||
trust all SSL self-signed certificates coming only from the specified hosts. \
|
||||
The default value is [ localhost, 127.0.0.1 ].
|
|
@ -1,108 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
====================================================================
|
||||
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 />.
|
||||
-->
|
||||
<metatype:MetaData xmlns:metatype="http://www.osgi.org/xmlns/metatype/v1.0.0" localization="metatype">
|
||||
|
||||
<OCD id="org.apache.hc.client5.http.proxyconfigurator"
|
||||
name="%proxyconfig.name"
|
||||
description="%proxyconfig.description">
|
||||
|
||||
<AD id="proxy.enabled"
|
||||
type="Boolean"
|
||||
default="false"
|
||||
name="%proxy.enabled.name"
|
||||
description="%proxy.enabled.description" />
|
||||
|
||||
<AD id="proxy.host"
|
||||
type="String"
|
||||
default=""
|
||||
name="%proxy.host.name"
|
||||
description="%proxy.host.description" />
|
||||
|
||||
<AD id="proxy.port"
|
||||
type="Integer"
|
||||
default="8080"
|
||||
name="%proxy.port.name"
|
||||
description="%proxy.port.description" />
|
||||
|
||||
<AD id="proxy.user"
|
||||
type="String"
|
||||
default=""
|
||||
name="%proxy.user.name"
|
||||
description="%proxy.user.description" />
|
||||
|
||||
<AD id="proxy.password"
|
||||
type="String"
|
||||
default=""
|
||||
name="%proxy.password.name"
|
||||
description="%proxy.password.description" />
|
||||
|
||||
<AD id="proxy.exceptions"
|
||||
type="String"
|
||||
default="localhost,127.0.0.1"
|
||||
cardinality="2147483647"
|
||||
name="%proxy.exceptions.name"
|
||||
description="%proxy.exceptions.description" />
|
||||
|
||||
<AD id="webconsole.configurationFactory.nameHint"
|
||||
type="String"
|
||||
default="{proxy.host}:{proxy.port} (enabled = {proxy.enabled})"/>
|
||||
|
||||
</OCD>
|
||||
|
||||
<OCD id="org.apache.hc.client5.http.trustedhosts"
|
||||
name="%trustedhosts.name"
|
||||
description="%trustedhosts.description">
|
||||
|
||||
<AD id="trustedhosts.enabled"
|
||||
type="Boolean"
|
||||
default="true"
|
||||
name="%trustedhosts.enabled.name"
|
||||
description="%trustedhosts.enabled.description" />
|
||||
|
||||
<AD id="trustedhosts.trustAll"
|
||||
type="Boolean"
|
||||
default="false"
|
||||
name="%trustedhosts.trustAll.name"
|
||||
description="%trustedhosts.trustAll.description" />
|
||||
|
||||
<AD id="trustedhosts.hosts"
|
||||
type="String"
|
||||
default="localhost,127.0.0.1"
|
||||
cardinality="2147483647"
|
||||
name="%trustedhosts.hosts.name"
|
||||
description="%trustedhosts.hosts.description" />
|
||||
|
||||
</OCD>
|
||||
|
||||
<Designate pid="org.apache.hc.client5.http.proxyconfigurator" factoryPid="org.apache.hc.client5.http.proxyconfigurator">
|
||||
<Object ocdref="org.apache.hc.client5.http.proxyconfigurator" />
|
||||
</Designate>
|
||||
<Designate pid="org.apache.hc.client5.http.trustedhosts">
|
||||
<Object ocdref="org.apache.hc.client5.http.trustedhosts" />
|
||||
</Designate>
|
||||
|
||||
</metatype:MetaData>
|
|
@ -1,87 +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.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.apache.hc.client5.http.osgi.impl.HostMatcher.DomainNameMatcher;
|
||||
import org.apache.hc.client5.http.osgi.impl.HostMatcher.HostMatcherFactory;
|
||||
import org.apache.hc.client5.http.osgi.impl.HostMatcher.HostNameMatcher;
|
||||
import org.apache.hc.client5.http.osgi.impl.HostMatcher.IPAddressMatcher;
|
||||
import org.apache.hc.client5.http.osgi.impl.HostMatcher.NetworkAddress;
|
||||
import org.junit.Test;
|
||||
|
||||
public final class HostMatcherTest {
|
||||
|
||||
@Test
|
||||
public void testNetworkAddress() {
|
||||
final NetworkAddress nullNetworkAddress = NetworkAddress.parse("www.apache.org");
|
||||
assertNull(nullNetworkAddress);
|
||||
|
||||
final NetworkAddress na = NetworkAddress.parse("127.0.0.1");
|
||||
assertEquals(2130706433, na.address);
|
||||
assertEquals(-2147483648, na.mask);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIPAddressMatcher() {
|
||||
final NetworkAddress na = NetworkAddress.parse("127.0.0.1");
|
||||
final IPAddressMatcher ipam = new IPAddressMatcher(na);
|
||||
assertFalse(ipam.matches("127.0.0.255"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDomainNameMatcher() {
|
||||
final DomainNameMatcher dnm = new DomainNameMatcher(".apache.org");
|
||||
assertTrue(dnm.matches("www.apache.org"));
|
||||
assertTrue(dnm.matches("hc.apache.org"));
|
||||
assertTrue(dnm.matches("commons.apache.org"));
|
||||
assertTrue(dnm.matches("cocoon.apache.org"));
|
||||
assertFalse(dnm.matches("www.gnu.org"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHostNameMatcher() {
|
||||
final HostNameMatcher hnm = new HostNameMatcher("www.apache.org");
|
||||
assertTrue(hnm.matches("www.apache.org"));
|
||||
assertTrue(hnm.matches("WwW.APACHE.org"));
|
||||
assertTrue(hnm.matches("wWw.apache.ORG"));
|
||||
assertTrue(hnm.matches("WWW.APACHE.ORG"));
|
||||
assertFalse(hnm.matches("www.gnu.org"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHostMatcherFactory() {
|
||||
assertTrue(HostMatcherFactory.createMatcher("127.0.0.1") instanceof IPAddressMatcher);
|
||||
assertTrue(HostMatcherFactory.createMatcher(".apache.org") instanceof DomainNameMatcher);
|
||||
assertTrue(HostMatcherFactory.createMatcher("www.apache.org") instanceof HostNameMatcher);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,105 +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 java.util.Arrays.asList;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import org.apache.hc.client5.http.auth.AuthScope;
|
||||
import org.apache.hc.client5.http.auth.Credentials;
|
||||
import org.apache.hc.client5.http.auth.CredentialsProvider;
|
||||
import org.apache.hc.client5.http.auth.NTCredentials;
|
||||
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.hc.client5.http.osgi.services.ProxyConfiguration;
|
||||
import org.apache.hc.core5.http.protocol.BasicHttpContext;
|
||||
import org.apache.hc.core5.http.protocol.HttpContext;
|
||||
import org.junit.Test;
|
||||
|
||||
public class OSGiCredentialsProviderTest {
|
||||
|
||||
private static final String HOST = "proxy.example.org";
|
||||
|
||||
private static final int PORT = 8080;
|
||||
|
||||
private static final HttpContext HTTP_CONTEXT = new BasicHttpContext();
|
||||
|
||||
@Test
|
||||
public void basicAuthentication() {
|
||||
final CredentialsProvider provider = credentialsProvider(proxy("user", "secret"));
|
||||
final Credentials credentials = provider.getCredentials(new AuthScope("http", HOST, PORT, null, "BASIC"), HTTP_CONTEXT);
|
||||
assertThat(credentials, instanceOf(UsernamePasswordCredentials.class));
|
||||
assertCredentials((UsernamePasswordCredentials) credentials, "user", "secret");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ntlmAuthenticationWithoutDomain() {
|
||||
final CredentialsProvider provider = credentialsProvider(proxy("user", "secret"));
|
||||
final Credentials credentials = provider.getCredentials(new AuthScope("http", HOST, PORT, null, "NTLM"), HTTP_CONTEXT);
|
||||
assertThat(credentials, instanceOf(NTCredentials.class));
|
||||
assertCredentials((NTCredentials) credentials, "user", "secret", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ntlmAuthenticationWithDomain() {
|
||||
final CredentialsProvider provider = credentialsProvider(proxy("DOMAIN\\user", "secret"));
|
||||
final Credentials credentials = provider.getCredentials(new AuthScope("http", HOST, PORT, null, "NTLM"), HTTP_CONTEXT);
|
||||
assertThat(credentials, instanceOf(NTCredentials.class));
|
||||
assertCredentials((NTCredentials) credentials, "user", "secret", "DOMAIN");
|
||||
}
|
||||
|
||||
private CredentialsProvider credentialsProvider(final ProxyConfiguration... proxies) {
|
||||
return new OSGiCredentialsProvider(asList(proxies));
|
||||
}
|
||||
|
||||
private void assertCredentials(final UsernamePasswordCredentials credentials, final String user, final String password) {
|
||||
assertThat("Username mismatch", credentials.getUserName(), equalTo(user));
|
||||
assertThat("Password mismatch", credentials.getPassword(), equalTo(password.toCharArray()));
|
||||
}
|
||||
|
||||
private void assertCredentials(final NTCredentials credentials, final String user, final String password, final String domain) {
|
||||
assertThat("Username mismatch", credentials.getUserName(), equalTo(user));
|
||||
assertThat("Password mismatch", credentials.getPassword(), equalTo(password.toCharArray()));
|
||||
assertThat("Domain mismatch", credentials.getDomain(), equalTo(domain));
|
||||
}
|
||||
|
||||
private ProxyConfiguration proxy(final String username, final String password) {
|
||||
final OSGiProxyConfiguration proxyConfiguration = new OSGiProxyConfiguration();
|
||||
final Hashtable<String, Object> config = new Hashtable<>();
|
||||
config.put("proxy.enabled", true);
|
||||
config.put("proxy.host", HOST);
|
||||
config.put("proxy.port", PORT);
|
||||
config.put("proxy.user", username);
|
||||
config.put("proxy.password", password);
|
||||
config.put("proxy.exceptions", new String[0]);
|
||||
proxyConfiguration.update(config);
|
||||
return proxyConfiguration;
|
||||
}
|
||||
}
|
|
@ -1,61 +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.hamcrest.CoreMatchers.containsString;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.Dictionary;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class OSGiProxyConfigurationTest {
|
||||
|
||||
@Test
|
||||
public void testToString() {
|
||||
|
||||
final Dictionary<String, Object> config = new Hashtable<>();
|
||||
config.put("proxy.enabled", false);
|
||||
config.put("proxy.host", "h");
|
||||
config.put("proxy.port", 1);
|
||||
config.put("proxy.user", "u");
|
||||
config.put("proxy.password", "p");
|
||||
config.put("proxy.exceptions", new String[]{"e"});
|
||||
|
||||
final OSGiProxyConfiguration configuration = new OSGiProxyConfiguration();
|
||||
configuration.update(config);
|
||||
|
||||
final String string = configuration.toString();
|
||||
assertThat(string, containsString("enabled=false"));
|
||||
assertThat(string, containsString("hostname=h"));
|
||||
assertThat(string, containsString("port=1"));
|
||||
assertThat(string, containsString("username=u"));
|
||||
assertThat(string, containsString("password=p"));
|
||||
assertThat(string, containsString("proxyExceptions=[e]"));
|
||||
}
|
||||
}
|
|
@ -1,117 +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 java.util.Arrays.asList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import org.apache.hc.client5.http.osgi.services.ProxyConfiguration;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @since 4.4.3
|
||||
*/
|
||||
public class TestOSGiHttpRoutePlanner {
|
||||
|
||||
private final ProxyConfiguration pc1 = proxy("proxy1", 8080, "localhost", "127.0.0.1", ".apache.org");
|
||||
private final ProxyConfiguration pc2 = proxy("proxy2", 9090, "localhost", "127.0.0.1", ".oracle.com", "12.34.34.8");
|
||||
|
||||
@Test
|
||||
public void testDeterminProxy() throws Exception {
|
||||
OSGiHttpRoutePlanner planner = new OSGiHttpRoutePlanner(singletonList(pc1));
|
||||
|
||||
HttpHost proxy = planner.determineProxy(new HttpHost("localhost", 8090), null);
|
||||
assertNull(proxy);
|
||||
|
||||
proxy = planner.determineProxy(new HttpHost("there", 9090), null);
|
||||
assertNotNull(proxy);
|
||||
assertTrue(proxy.getHostName().equals("proxy1"));
|
||||
|
||||
proxy = planner.determineProxy(new HttpHost("10.2.144.23", 4554), null);
|
||||
assertNotNull(proxy);
|
||||
assertTrue(proxy.getHostName().equals("proxy1"));
|
||||
|
||||
final InetAddress addr = InetAddress.getByName("localhost");
|
||||
proxy = planner.determineProxy(new HttpHost(addr, 4554), null);
|
||||
assertNull(proxy);
|
||||
|
||||
proxy = planner.determineProxy(new HttpHost("hc.apache.org", 4554), null);
|
||||
assertNull(proxy);
|
||||
|
||||
|
||||
// test with more than one registration of proxyConfiguration
|
||||
planner = new OSGiHttpRoutePlanner(asList(pc1, pc2));
|
||||
proxy = planner.determineProxy(new HttpHost("localhost", 8090), null);
|
||||
assertNull(proxy);
|
||||
|
||||
proxy = planner.determineProxy(new HttpHost("there", 9090), null);
|
||||
assertNotNull(proxy);
|
||||
assertTrue(proxy.getHostName().equals("proxy1")); // the first one
|
||||
|
||||
proxy = planner.determineProxy(new HttpHost(addr, 4554), null);
|
||||
assertNull(proxy);
|
||||
|
||||
proxy = planner.determineProxy(new HttpHost("hc.apache.org", 4554), null);
|
||||
assertNull(proxy);
|
||||
|
||||
proxy = planner.determineProxy(new HttpHost("docs.oracle.com", 4554), null);
|
||||
assertNull(proxy);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMasking() throws Exception {
|
||||
final OSGiHttpRoutePlanner planner = new OSGiHttpRoutePlanner(singletonList(pc2));
|
||||
|
||||
HttpHost proxy = planner.determineProxy(new HttpHost("12.34.34.2", 4554), null);
|
||||
assertNotNull(proxy);
|
||||
assertTrue(proxy.getHostName().equals("proxy2"));
|
||||
|
||||
proxy = planner.determineProxy(new HttpHost("12.34.34.8", 4554), null);
|
||||
assertNotNull(proxy);
|
||||
}
|
||||
|
||||
private ProxyConfiguration proxy(final String host, final int port, final String... exceptions) {
|
||||
final OSGiProxyConfiguration proxyConfiguration = new OSGiProxyConfiguration();
|
||||
final Hashtable<String, Object> config = new Hashtable<>();
|
||||
config.put("proxy.enabled", true);
|
||||
config.put("proxy.host", host);
|
||||
config.put("proxy.port", port);
|
||||
config.put("proxy.user", "");
|
||||
config.put("proxy.password", "");
|
||||
config.put("proxy.exceptions", exceptions);
|
||||
proxyConfiguration.update(config);
|
||||
return proxyConfiguration;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,73 +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.hamcrest.CoreMatchers.containsString;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.Dictionary;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import org.apache.hc.client5.http.osgi.services.TrustedHostsConfiguration;
|
||||
import org.junit.Test;
|
||||
import org.osgi.service.cm.ConfigurationException;
|
||||
|
||||
public final class TestOSGiTrustedHostsConfiguration {
|
||||
|
||||
@Test
|
||||
public void testSetValues() throws ConfigurationException {
|
||||
final TrustedHostsConfiguration configuration = getTrustedHostsConfiguration();
|
||||
|
||||
assertEquals(true, configuration.isEnabled());
|
||||
assertEquals(false, configuration.trustAll());
|
||||
assertArrayEquals(new String[]{ "localhost" }, configuration.getTrustedHosts());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() throws ConfigurationException {
|
||||
final TrustedHostsConfiguration configuration = getTrustedHostsConfiguration();
|
||||
|
||||
final String string = configuration.toString();
|
||||
assertThat(string, containsString("enabled=true"));
|
||||
assertThat(string, containsString("trustAll=false"));
|
||||
assertThat(string, containsString("trustedHosts=[localhost]"));
|
||||
}
|
||||
|
||||
private TrustedHostsConfiguration getTrustedHostsConfiguration() throws ConfigurationException {
|
||||
final Dictionary<String, Object> config = new Hashtable<>();
|
||||
config.put("trustedhosts.enabled", true);
|
||||
config.put("trustedhosts.trustAll", false);
|
||||
config.put("trustedhosts.hosts", new String[]{ "localhost" });
|
||||
|
||||
final OSGiTrustedHostsConfiguration configuration = new OSGiTrustedHostsConfiguration();
|
||||
configuration.updated(config);
|
||||
return configuration;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,132 +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.osgi.impl.PropertiesUtils.to;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
@SuppressWarnings("boxing") // test code
|
||||
public final class TestPropertiesUtils {
|
||||
|
||||
@Test
|
||||
public void toBoolean() {
|
||||
assertConverted(true, null, boolean.class, true);
|
||||
assertConverted(true, null, Boolean.class, true);
|
||||
assertConverted(false, "false", boolean.class, null);
|
||||
assertConverted(false, "false", Boolean.class, null);
|
||||
// whatever value is interpreted as `false` by Boolean.valueOf
|
||||
assertConverted(false, "not a boolean", boolean.class, true);
|
||||
assertConverted(false, "not a boolean", Boolean.class, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toSingleString() {
|
||||
assertConverted("fallback to default value", null, String.class, "fallback to default value");
|
||||
// use an object which represents the string
|
||||
assertConverted("use the passed value", new StringBuilder("use the passed value"), String.class, null);
|
||||
// use the "identity" converter
|
||||
assertConverted("use the passed value", "use the passed value", String.class, null);
|
||||
// convert another object
|
||||
assertConverted("456789", 456789, String.class, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toStringArray() {
|
||||
assertConvertedArray(new String[]{"fallback to default value"},
|
||||
null,
|
||||
String[].class,
|
||||
new String[]{"fallback to default value"});
|
||||
// a string is converted to an array with 1 element
|
||||
assertConvertedArray(new String[]{"a single string"},
|
||||
"a single string",
|
||||
String[].class,
|
||||
null);
|
||||
// use an object which represents the string
|
||||
assertConvertedArray(new String[]{"null objects", "will be ignored"},
|
||||
new Object[]{new StringBuilder("null objects"), null, new StringBuilder("will be ignored")},
|
||||
String[].class,
|
||||
null);
|
||||
// use the "identity" converter
|
||||
assertConvertedArray(new String[]{"null objects", "will be ignored"},
|
||||
new Object[]{"null objects", null, "will be ignored"},
|
||||
String[].class,
|
||||
null);
|
||||
assertConvertedArray(new String[]{"fallback to default value"},
|
||||
456789,
|
||||
String[].class,
|
||||
new String[]{"fallback to default value"});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toInt() {
|
||||
assertConverted(123, null, int.class, 123);
|
||||
assertConverted(123, null, Integer.class, 123);
|
||||
assertConverted(456, "456", int.class, null);
|
||||
assertConverted(456, "456", Integer.class, null);
|
||||
assertConverted(789, "not an integer", int.class, 789);
|
||||
assertConverted(789, "not an integer", Integer.class, 789);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toLong() {
|
||||
assertConverted(123l, null, long.class, 123l);
|
||||
assertConverted(123l, null, Long.class, 123l);
|
||||
assertConverted(456l, "456", long.class, null);
|
||||
assertConverted(456l, "456", Long.class, null);
|
||||
assertConverted(789l, "not a long", long.class, 789l);
|
||||
assertConverted(789l, "not a long", Long.class, 789l);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDouble() {
|
||||
assertConverted(123d, null, double.class, 123d);
|
||||
assertConverted(123d, null, Double.class, 123d);
|
||||
assertConverted(456d, "456", double.class, null);
|
||||
assertConverted(456d, "456", Double.class, null);
|
||||
assertConverted(789d, "not a double", double.class, 789d);
|
||||
assertConverted(789d, "not a double", Double.class, 789d);
|
||||
}
|
||||
|
||||
private static <T> void assertConverted(
|
||||
final T expected, final Object propValue, final Class<T> targetType, final T defaultValue) {
|
||||
final T actual = to(propValue, targetType, defaultValue);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
private static <T> void assertConvertedArray(
|
||||
final T[] expected, final Object propValue, final Class<T[]> targetType, final T[] defaultValue) {
|
||||
final T[] actual = to(propValue, targetType, defaultValue);
|
||||
assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,127 +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.junit.Assert.assertSame;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.util.Dictionary;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import org.apache.hc.client5.http.socket.LayeredConnectionSocketFactory;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.protocol.BasicHttpContext;
|
||||
import org.apache.hc.core5.http.protocol.HttpContext;
|
||||
import org.apache.hc.core5.util.TimeValue;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.osgi.service.cm.ConfigurationException;
|
||||
|
||||
public class TestRelaxedLayeredConnectionSocketFactory {
|
||||
|
||||
@Mock
|
||||
Socket insecureSocket;
|
||||
|
||||
@Mock
|
||||
Socket secureSocket;
|
||||
|
||||
private final HttpContext context = new BasicHttpContext();
|
||||
|
||||
@Test
|
||||
public void testTrustedAllConnections() throws Exception {
|
||||
final LayeredConnectionSocketFactory socketFactory = getLayeredConnectionSocketFactory(true, true);
|
||||
final Socket socket = socketFactory.createSocket(context);
|
||||
final Socket secureSocket = socketFactory.createLayeredSocket(socket, "localhost", 9999, context);
|
||||
assertSame(this.secureSocket, secureSocket);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTrustedConnections() throws Exception {
|
||||
final LayeredConnectionSocketFactory socketFactory = getLayeredConnectionSocketFactory(true, false, "localhost");
|
||||
final Socket socket = socketFactory.createSocket(context);
|
||||
final Socket localSecureSocket = socketFactory.createLayeredSocket(socket, "localhost", 9999, context);
|
||||
assertSame(this.insecureSocket, localSecureSocket);
|
||||
|
||||
final Socket apacheSecureSocket = socketFactory.createLayeredSocket(socket, "www.apache.org", 9999, context);
|
||||
assertSame(this.secureSocket, apacheSecureSocket);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotEabledConfiguration() throws Exception {
|
||||
final LayeredConnectionSocketFactory socketFactory = getLayeredConnectionSocketFactory(false, true);
|
||||
final Socket socket = socketFactory.createSocket(context);
|
||||
final Socket secureSocket = socketFactory.createLayeredSocket(socket, "localhost", 9999, context);
|
||||
assertSame(this.secureSocket, secureSocket);
|
||||
}
|
||||
|
||||
private LayeredConnectionSocketFactory getLayeredConnectionSocketFactory(
|
||||
final boolean enabled, final boolean trustAll, final String... trustedHosts) throws ConfigurationException {
|
||||
final OSGiTrustedHostsConfiguration configuration = new OSGiTrustedHostsConfiguration();
|
||||
configuration.updated(createConfig(enabled, trustAll, trustedHosts));
|
||||
|
||||
final LayeredConnectionSocketFactory defaultSocketFactory = new LayeredConnectionSocketFactory() {
|
||||
|
||||
@Override
|
||||
public Socket createSocket(final HttpContext context) throws IOException {
|
||||
return insecureSocket;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Socket connectSocket(final TimeValue connectTimeout,
|
||||
final Socket sock,
|
||||
final HttpHost host,
|
||||
final InetSocketAddress remoteAddress,
|
||||
final InetSocketAddress localAddress,
|
||||
final HttpContext context ) throws IOException {
|
||||
// not needed in this version
|
||||
return insecureSocket;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Socket createLayeredSocket(final Socket socket,
|
||||
final String target,
|
||||
final int port,
|
||||
final HttpContext context) throws IOException {
|
||||
return secureSocket;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
return new RelaxedLayeredConnectionSocketFactory(configuration, defaultSocketFactory);
|
||||
}
|
||||
|
||||
private Dictionary<String, Object> createConfig(final boolean enabled, final boolean trustAll, final String... trustedHosts) {
|
||||
final Dictionary<String, Object> config = new Hashtable<>();
|
||||
config.put("trustedhosts.enabled", enabled);
|
||||
config.put("trustedhosts.trustAll", trustAll);
|
||||
config.put("trustedhosts.hosts", trustedHosts);
|
||||
return config;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,73 +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.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class WeakListTest {
|
||||
|
||||
@Test
|
||||
public void testWeakList() {
|
||||
final WeakList<Object> list = new WeakList<>();
|
||||
list.add("hello");
|
||||
list.add(null);
|
||||
|
||||
// null objects are seen as GC'd, so we only expect a size of 1
|
||||
assertEquals(1, list.size());
|
||||
|
||||
final Iterator<Object> it = list.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
assertEquals("hello", it.next());
|
||||
assertFalse(it.hasNext());
|
||||
boolean thrown = false;
|
||||
try {
|
||||
it.next();
|
||||
} catch (final NoSuchElementException e) {
|
||||
thrown = true;
|
||||
}
|
||||
assertTrue(thrown);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearSupported() {
|
||||
final WeakList<Object> list = new WeakList<>();
|
||||
|
||||
list.add("hello");
|
||||
assertEquals(1, list.size());
|
||||
|
||||
list.clear();
|
||||
assertEquals(0, list.size());
|
||||
}
|
||||
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
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.
|
||||
-->
|
||||
<Configuration status="WARN" name="XMLConfigTest">
|
||||
<Appenders>
|
||||
<Console name="STDOUT">
|
||||
<PatternLayout pattern="%d %-5level [%t][%logger]%notEmpty{[%markerSimpleName]} %msg%n%xThrowable" />
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="WARN">
|
||||
<AppenderRef ref="STDOUT" />
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
Loading…
Reference in New Issue