Polishing

This commit is contained in:
Juergen Hoeller 2016-11-04 12:25:38 +01:00 committed by Brian Clozel
parent 62631bfe33
commit 1608d0596d
1 changed files with 39 additions and 37 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -42,11 +42,11 @@ import org.springframework.http.HttpMethod;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* {@link org.springframework.http.client.ClientHttpRequestFactory} implementation that * {@link org.springframework.http.client.ClientHttpRequestFactory} implementation
* uses <a href="http://netty.io/">Netty 4</a> to create requests. * that uses <a href="http://netty.io/">Netty 4</a> to create requests.
* *
* <p>Allows to use a pre-configured {@link EventLoopGroup} instance - useful for sharing * <p>Allows to use a pre-configured {@link EventLoopGroup} instance: useful for
* across multiple clients. * sharing across multiple clients.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
@ -104,15 +104,6 @@ public class Netty4ClientHttpRequestFactory implements ClientHttpRequestFactory,
} }
private SslContext getDefaultClientSslContext() {
try {
return SslContextBuilder.forClient().build();
}
catch (SSLException exc) {
throw new IllegalStateException("Could not create default client SslContext", exc);
}
}
/** /**
* Set the default maximum response size. * Set the default maximum response size.
* <p>By default this is set to {@link #DEFAULT_MAX_RESPONSE_SIZE}. * <p>By default this is set to {@link #DEFAULT_MAX_RESPONSE_SIZE}.
@ -150,9 +141,41 @@ public class Netty4ClientHttpRequestFactory implements ClientHttpRequestFactory,
this.readTimeout = readTimeout; this.readTimeout = readTimeout;
} }
@Override
public void afterPropertiesSet() {
if (this.sslContext == null) {
this.sslContext = getDefaultClientSslContext();
}
}
private SslContext getDefaultClientSslContext() {
try {
return SslContextBuilder.forClient().build();
}
catch (SSLException ex) {
throw new IllegalStateException("Could not create default client SslContext", ex);
}
}
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
return createRequestInternal(uri, httpMethod);
}
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
return createRequestInternal(uri, httpMethod);
}
private Netty4ClientHttpRequest createRequestInternal(URI uri, HttpMethod httpMethod) {
return new Netty4ClientHttpRequest(getBootstrap(uri), uri, httpMethod);
}
private Bootstrap getBootstrap(URI uri) { private Bootstrap getBootstrap(URI uri) {
final boolean isSecure = (uri.getPort() == 443) boolean isSecure = (uri.getPort() == 443 ||
|| (uri.getPort() == -1 && "https".equalsIgnoreCase(uri.getScheme())); (uri.getPort() == -1 && "https".equalsIgnoreCase(uri.getScheme())));
if (isSecure) { if (isSecure) {
if (this.sslBootstrap == null) { if (this.sslBootstrap == null) {
this.sslBootstrap = buildBootstrap(true); this.sslBootstrap = buildBootstrap(true);
@ -201,27 +224,6 @@ public class Netty4ClientHttpRequestFactory implements ClientHttpRequestFactory,
} }
} }
@Override
public void afterPropertiesSet() throws Exception {
if (this.sslContext == null) {
this.sslContext = getDefaultClientSslContext();
}
}
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
return createRequestInternal(uri, httpMethod);
}
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
return createRequestInternal(uri, httpMethod);
}
private Netty4ClientHttpRequest createRequestInternal(URI uri, HttpMethod httpMethod) {
return new Netty4ClientHttpRequest(getBootstrap(uri), uri, httpMethod);
}
@Override @Override
public void destroy() throws InterruptedException { public void destroy() throws InterruptedException {