Reuse our UriScheme enum instead of magic strings.

This commit is contained in:
Gary Gregory 2018-10-03 09:25:17 -06:00
parent d711bd637e
commit 8e486b356e
8 changed files with 24 additions and 16 deletions

View File

@ -51,6 +51,7 @@
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.HttpVersion;
import org.apache.hc.core5.http.ProtocolVersion;
import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.message.MessageSupport;
import org.apache.hc.core5.http.protocol.HttpContext;
import org.apache.hc.core5.util.VersionInfo;
@ -280,7 +281,7 @@ String generateViaHeader(final HttpMessage msg) {
final String value;
final int major = pv.getMajor();
final int minor = pv.getMinor();
if ("http".equalsIgnoreCase(pv.getProtocol())) {
if (URIScheme.HTTP.same(pv.getProtocol())) {
value = String.format("%d.%d localhost (Apache-HttpClient/%s (cache))", major, minor,
release);
} else {

View File

@ -32,6 +32,7 @@
import org.apache.hc.client5.http.utils.URIUtils;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.net.URIAuthority;
import org.apache.hc.core5.net.URIBuilder;
import org.apache.hc.core5.util.Args;
@ -65,7 +66,7 @@ public static String getRequestUri(final HttpRequest request, final HttpHost tar
final URIAuthority authority = request.getAuthority();
if (authority != null) {
final String scheme = request.getScheme();
buf.append(scheme != null ? scheme : "http").append("://");
buf.append(scheme != null ? scheme : URIScheme.HTTP.id).append("://");
buf.append(authority.getHostName());
if (authority.getPort() >= 0) {
buf.append(":").append(authority.getPort());
@ -103,12 +104,12 @@ public static URI normalize(final URI requestUri) throws URISyntaxException {
final URIBuilder builder = new URIBuilder(requestUri.isAbsolute() ? URIUtils.resolve(BASE_URI, requestUri) : requestUri) ;
if (builder.getHost() != null) {
if (builder.getScheme() == null) {
builder.setScheme("http");
builder.setScheme(URIScheme.HTTP.id);
}
if (builder.getPort() <= -1) {
if ("http".equalsIgnoreCase(builder.getScheme())) {
if (URIScheme.HTTP.same(builder.getScheme())) {
builder.setPort(80);
} else if ("https".equalsIgnoreCase(builder.getScheme())) {
} else if (URIScheme.HTTPS.same(builder.getScheme())) {
builder.setPort(443);
}
}

View File

@ -36,6 +36,7 @@
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;
@ -66,8 +67,8 @@ <T extends HttpClientBuilder> T configure(final T clientBuilder) {
private Registry<ConnectionSocketFactory> createSocketFactoryRegistry(
final TrustedHostsConfiguration trustedHostsConfiguration) {
return RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", createSocketFactory(trustedHostsConfiguration))
.register(URIScheme.HTTP.id, PlainConnectionSocketFactory.INSTANCE)
.register(URIScheme.HTTPS.id, createSocketFactory(trustedHostsConfiguration))
.build();
}

View File

@ -30,6 +30,7 @@
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.util.Args;
/**
@ -50,9 +51,9 @@ public int resolve(final HttpHost host) {
return port;
}
final String name = host.getSchemeName();
if (name.equalsIgnoreCase("http")) {
if (URIScheme.HTTP.same(name)) {
return 80;
} else if (name.equalsIgnoreCase("https")) {
} else if (URIScheme.HTTPS.same(name)) {
return 443;
} else {
return -1;

View File

@ -46,6 +46,7 @@
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.protocol.HttpContext;
import org.apache.hc.core5.util.Args;
@ -127,7 +128,7 @@ public Credentials getCredentials(final AuthScope authscope, final HttpContext c
final String host = authscope.getHost();
if (host != null) {
final HttpClientContext clientContext = context != null ? HttpClientContext.adapt(context) : null;
final String protocol = authscope.getProtocol() != null ? authscope.getProtocol() : (authscope.getPort() == 443 ? "https" : "http");
final String protocol = authscope.getProtocol() != null ? authscope.getProtocol() : (authscope.getPort() == 443 ? URIScheme.HTTPS.id : URIScheme.HTTP.id);
PasswordAuthentication systemcreds = getSystemCreds(
protocol, authscope, Authenticator.RequestorType.SERVER, clientContext);
if (systemcreds == null) {

View File

@ -53,6 +53,7 @@
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.config.Lookup;
import org.apache.hc.core5.http.config.Registry;
import org.apache.hc.core5.http.config.RegistryBuilder;
@ -107,8 +108,8 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan
private static Registry<ConnectionSocketFactory> getDefaultRegistry() {
return RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", SSLConnectionSocketFactory.getSocketFactory())
.register(URIScheme.HTTP.id, PlainConnectionSocketFactory.getSocketFactory())
.register(URIScheme.HTTPS.id, SSLConnectionSocketFactory.getSocketFactory())
.build();
}

View File

@ -55,6 +55,7 @@
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.config.Registry;
import org.apache.hc.core5.http.config.RegistryBuilder;
import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
@ -120,8 +121,8 @@ public class PoolingHttpClientConnectionManager
public PoolingHttpClientConnectionManager() {
this(RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", SSLConnectionSocketFactory.getSocketFactory())
.register(URIScheme.HTTP.id, PlainConnectionSocketFactory.getSocketFactory())
.register(URIScheme.HTTPS.id, SSLConnectionSocketFactory.getSocketFactory())
.build());
}

View File

@ -34,6 +34,7 @@
import org.apache.hc.client5.http.socket.LayeredConnectionSocketFactory;
import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.config.RegistryBuilder;
import org.apache.hc.core5.http.io.HttpConnectionFactory;
import org.apache.hc.core5.http.io.SocketConfig;
@ -199,8 +200,8 @@ public PoolingHttpClientConnectionManager build() {
@SuppressWarnings("resource")
final PoolingHttpClientConnectionManager poolingmgr = new PoolingHttpClientConnectionManager(
RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory != null ? sslSocketFactory :
.register(URIScheme.HTTP.id, PlainConnectionSocketFactory.getSocketFactory())
.register(URIScheme.HTTPS.id, sslSocketFactory != null ? sslSocketFactory :
(systemProperties ?
SSLConnectionSocketFactory.getSystemSocketFactory() :
SSLConnectionSocketFactory.getSocketFactory()))