Polishing

This commit is contained in:
Juergen Hoeller 2016-11-04 12:25:38 +01:00
parent ac774cdcef
commit 17f5f86a54
2 changed files with 51 additions and 43 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) {
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 {

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.
@ -33,13 +33,11 @@ import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.NetworkConnector; import org.eclipse.jetty.server.NetworkConnector;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@ -114,6 +112,7 @@ public class AbstractJettyServerTestCase {
} }
} }
/** Servlet that sets the given status code. */ /** Servlet that sets the given status code. */
@SuppressWarnings("serial") @SuppressWarnings("serial")
private static class StatusCodeServlet extends GenericServlet { private static class StatusCodeServlet extends GenericServlet {
@ -131,6 +130,7 @@ public class AbstractJettyServerTestCase {
} }
} }
/** Servlet that returns an error message for a given status code. */ /** Servlet that returns an error message for a given status code. */
@SuppressWarnings("serial") @SuppressWarnings("serial")
private static class ErrorServlet extends GenericServlet { private static class ErrorServlet extends GenericServlet {
@ -147,6 +147,7 @@ public class AbstractJettyServerTestCase {
} }
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")
private static class GetServlet extends HttpServlet { private static class GetServlet extends HttpServlet {
@ -170,6 +171,7 @@ public class AbstractJettyServerTestCase {
} }
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")
private static class PostServlet extends HttpServlet { private static class PostServlet extends HttpServlet {
@ -203,6 +205,7 @@ public class AbstractJettyServerTestCase {
} }
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")
private static class JsonPostServlet extends HttpServlet { private static class JsonPostServlet extends HttpServlet {
@ -230,6 +233,7 @@ public class AbstractJettyServerTestCase {
} }
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")
private static class PutServlet extends HttpServlet { private static class PutServlet extends HttpServlet {
@ -250,6 +254,7 @@ public class AbstractJettyServerTestCase {
} }
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")
private static class UriServlet extends HttpServlet { private static class UriServlet extends HttpServlet {
@ -261,6 +266,7 @@ public class AbstractJettyServerTestCase {
} }
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")
private static class MultipartServlet extends HttpServlet { private static class MultipartServlet extends HttpServlet {
@ -300,6 +306,7 @@ public class AbstractJettyServerTestCase {
} }
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")
private static class FormServlet extends HttpServlet { private static class FormServlet extends HttpServlet {
@ -322,15 +329,14 @@ public class AbstractJettyServerTestCase {
} }
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")
private static class DeleteServlet extends HttpServlet { private static class DeleteServlet extends HttpServlet {
@Override @Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
throws ServletException, IOException {
resp.setStatus(200); resp.setStatus(200);
} }
} }
} }