diff --git a/spring-web/src/main/java/org/springframework/http/client/Netty4ClientHttpRequestFactory.java b/spring-web/src/main/java/org/springframework/http/client/Netty4ClientHttpRequestFactory.java index 584a9da1e6..9e3e2320a3 100644 --- a/spring-web/src/main/java/org/springframework/http/client/Netty4ClientHttpRequestFactory.java +++ b/spring-web/src/main/java/org/springframework/http/client/Netty4ClientHttpRequestFactory.java @@ -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"); * 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; /** - * {@link org.springframework.http.client.ClientHttpRequestFactory} implementation that - * uses Netty 4 to create requests. + * {@link org.springframework.http.client.ClientHttpRequestFactory} implementation + * that uses Netty 4 to create requests. * - *
Allows to use a pre-configured {@link EventLoopGroup} instance - useful for sharing - * across multiple clients. + *
Allows to use a pre-configured {@link EventLoopGroup} instance: useful for + * sharing across multiple clients. * * @author Arjen Poutsma * @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. *
By default this is set to {@link #DEFAULT_MAX_RESPONSE_SIZE}. @@ -150,9 +141,41 @@ public class Netty4ClientHttpRequestFactory implements ClientHttpRequestFactory, 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) { - boolean isSecure = (uri.getPort() == 443) - || (uri.getPort() == -1 && "https".equalsIgnoreCase(uri.getScheme())); + boolean isSecure = (uri.getPort() == 443 || + (uri.getPort() == -1 && "https".equalsIgnoreCase(uri.getScheme()))); if (isSecure) { if (this.sslBootstrap == null) { 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 public void destroy() throws InterruptedException { diff --git a/spring-web/src/test/java/org/springframework/web/client/AbstractJettyServerTestCase.java b/spring-web/src/test/java/org/springframework/web/client/AbstractJettyServerTestCase.java index adf5449f0a..433c4d5171 100644 --- a/spring-web/src/test/java/org/springframework/web/client/AbstractJettyServerTestCase.java +++ b/spring-web/src/test/java/org/springframework/web/client/AbstractJettyServerTestCase.java @@ -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"); * you may not use this file except in compliance with the License. @@ -33,13 +33,11 @@ import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; - import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.NetworkConnector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; - import org.junit.AfterClass; import org.junit.BeforeClass; @@ -114,6 +112,7 @@ public class AbstractJettyServerTestCase { } } + /** Servlet that sets the given status code. */ @SuppressWarnings("serial") private static class StatusCodeServlet extends GenericServlet { @@ -131,6 +130,7 @@ public class AbstractJettyServerTestCase { } } + /** Servlet that returns an error message for a given status code. */ @SuppressWarnings("serial") private static class ErrorServlet extends GenericServlet { @@ -147,6 +147,7 @@ public class AbstractJettyServerTestCase { } } + @SuppressWarnings("serial") private static class GetServlet extends HttpServlet { @@ -170,6 +171,7 @@ public class AbstractJettyServerTestCase { } } + @SuppressWarnings("serial") private static class PostServlet extends HttpServlet { @@ -203,6 +205,7 @@ public class AbstractJettyServerTestCase { } } + @SuppressWarnings("serial") private static class JsonPostServlet extends HttpServlet { @@ -230,6 +233,7 @@ public class AbstractJettyServerTestCase { } } + @SuppressWarnings("serial") private static class PutServlet extends HttpServlet { @@ -250,6 +254,7 @@ public class AbstractJettyServerTestCase { } } + @SuppressWarnings("serial") private static class UriServlet extends HttpServlet { @@ -261,6 +266,7 @@ public class AbstractJettyServerTestCase { } } + @SuppressWarnings("serial") private static class MultipartServlet extends HttpServlet { @@ -300,6 +306,7 @@ public class AbstractJettyServerTestCase { } } + @SuppressWarnings("serial") private static class FormServlet extends HttpServlet { @@ -322,15 +329,14 @@ public class AbstractJettyServerTestCase { } } + @SuppressWarnings("serial") private static class DeleteServlet extends HttpServlet { @Override - protected void doDelete(HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException { + protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setStatus(200); } - } }