Skip to content

Commit 17f5f86

Browse files
committed
Polishing
1 parent ac774cd commit 17f5f86

File tree

2 files changed

+51
-43
lines changed

2 files changed

+51
-43
lines changed

spring-web/src/main/java/org/springframework/http/client/Netty4ClientHttpRequestFactory.java

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,11 +42,11 @@
4242
import org.springframework.util.Assert;
4343

4444
/**
45-
* {@link org.springframework.http.client.ClientHttpRequestFactory} implementation that
46-
* uses <a href="http://netty.io/">Netty 4</a> to create requests.
45+
* {@link org.springframework.http.client.ClientHttpRequestFactory} implementation
46+
* that uses <a href="http://netty.io/">Netty 4</a> to create requests.
4747
*
48-
* <p>Allows to use a pre-configured {@link EventLoopGroup} instance - useful for sharing
49-
* across multiple clients.
48+
* <p>Allows to use a pre-configured {@link EventLoopGroup} instance: useful for
49+
* sharing across multiple clients.
5050
*
5151
* @author Arjen Poutsma
5252
* @author Rossen Stoyanchev
@@ -104,15 +104,6 @@ public Netty4ClientHttpRequestFactory(EventLoopGroup eventLoopGroup) {
104104
}
105105

106106

107-
private SslContext getDefaultClientSslContext() {
108-
try {
109-
return SslContextBuilder.forClient().build();
110-
}
111-
catch (SSLException exc) {
112-
throw new IllegalStateException("Could not create default client SslContext", exc);
113-
}
114-
}
115-
116107
/**
117108
* Set the default maximum response size.
118109
* <p>By default this is set to {@link #DEFAULT_MAX_RESPONSE_SIZE}.
@@ -150,9 +141,41 @@ public void setReadTimeout(int readTimeout) {
150141
this.readTimeout = readTimeout;
151142
}
152143

144+
145+
@Override
146+
public void afterPropertiesSet() {
147+
if (this.sslContext == null) {
148+
this.sslContext = getDefaultClientSslContext();
149+
}
150+
}
151+
152+
private SslContext getDefaultClientSslContext() {
153+
try {
154+
return SslContextBuilder.forClient().build();
155+
}
156+
catch (SSLException ex) {
157+
throw new IllegalStateException("Could not create default client SslContext", ex);
158+
}
159+
}
160+
161+
162+
@Override
163+
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
164+
return createRequestInternal(uri, httpMethod);
165+
}
166+
167+
@Override
168+
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
169+
return createRequestInternal(uri, httpMethod);
170+
}
171+
172+
private Netty4ClientHttpRequest createRequestInternal(URI uri, HttpMethod httpMethod) {
173+
return new Netty4ClientHttpRequest(getBootstrap(uri), uri, httpMethod);
174+
}
175+
153176
private Bootstrap getBootstrap(URI uri) {
154-
boolean isSecure = (uri.getPort() == 443)
155-
|| (uri.getPort() == -1 && "https".equalsIgnoreCase(uri.getScheme()));
177+
boolean isSecure = (uri.getPort() == 443 ||
178+
(uri.getPort() == -1 && "https".equalsIgnoreCase(uri.getScheme())));
156179
if (isSecure) {
157180
if (this.sslBootstrap == null) {
158181
this.sslBootstrap = buildBootstrap(true);
@@ -201,27 +224,6 @@ protected void configureChannel(SocketChannelConfig config) {
201224
}
202225
}
203226

204-
@Override
205-
public void afterPropertiesSet() throws Exception {
206-
if (this.sslContext == null) {
207-
this.sslContext = getDefaultClientSslContext();
208-
}
209-
}
210-
211-
@Override
212-
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
213-
return createRequestInternal(uri, httpMethod);
214-
}
215-
216-
@Override
217-
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
218-
return createRequestInternal(uri, httpMethod);
219-
}
220-
221-
private Netty4ClientHttpRequest createRequestInternal(URI uri, HttpMethod httpMethod) {
222-
return new Netty4ClientHttpRequest(getBootstrap(uri), uri, httpMethod);
223-
}
224-
225227

226228
@Override
227229
public void destroy() throws InterruptedException {

spring-web/src/test/java/org/springframework/web/client/AbstractJettyServerTestCase.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,13 +33,11 @@
3333
import org.apache.commons.fileupload.FileUploadException;
3434
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
3535
import org.apache.commons.fileupload.servlet.ServletFileUpload;
36-
3736
import org.eclipse.jetty.server.Connector;
3837
import org.eclipse.jetty.server.NetworkConnector;
3938
import org.eclipse.jetty.server.Server;
4039
import org.eclipse.jetty.servlet.ServletContextHandler;
4140
import org.eclipse.jetty.servlet.ServletHolder;
42-
4341
import org.junit.AfterClass;
4442
import org.junit.BeforeClass;
4543

@@ -114,6 +112,7 @@ public static void stopJettyServer() throws Exception {
114112
}
115113
}
116114

115+
117116
/** Servlet that sets the given status code. */
118117
@SuppressWarnings("serial")
119118
private static class StatusCodeServlet extends GenericServlet {
@@ -131,6 +130,7 @@ public void service(ServletRequest request, ServletResponse response) throws
131130
}
132131
}
133132

133+
134134
/** Servlet that returns an error message for a given status code. */
135135
@SuppressWarnings("serial")
136136
private static class ErrorServlet extends GenericServlet {
@@ -147,6 +147,7 @@ public void service(ServletRequest request, ServletResponse response) throws Ser
147147
}
148148
}
149149

150+
150151
@SuppressWarnings("serial")
151152
private static class GetServlet extends HttpServlet {
152153

@@ -170,6 +171,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
170171
}
171172
}
172173

174+
173175
@SuppressWarnings("serial")
174176
private static class PostServlet extends HttpServlet {
175177

@@ -203,6 +205,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
203205
}
204206
}
205207

208+
206209
@SuppressWarnings("serial")
207210
private static class JsonPostServlet extends HttpServlet {
208211

@@ -230,6 +233,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
230233
}
231234
}
232235

236+
233237
@SuppressWarnings("serial")
234238
private static class PutServlet extends HttpServlet {
235239

@@ -250,6 +254,7 @@ protected void doPut(HttpServletRequest request, HttpServletResponse response)
250254
}
251255
}
252256

257+
253258
@SuppressWarnings("serial")
254259
private static class UriServlet extends HttpServlet {
255260

@@ -261,6 +266,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
261266
}
262267
}
263268

269+
264270
@SuppressWarnings("serial")
265271
private static class MultipartServlet extends HttpServlet {
266272

@@ -300,6 +306,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
300306
}
301307
}
302308

309+
303310
@SuppressWarnings("serial")
304311
private static class FormServlet extends HttpServlet {
305312

@@ -322,15 +329,14 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
322329
}
323330
}
324331

332+
325333
@SuppressWarnings("serial")
326334
private static class DeleteServlet extends HttpServlet {
327335

328336
@Override
329-
protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
330-
throws ServletException, IOException {
337+
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
331338
resp.setStatus(200);
332339
}
333-
334340
}
335341

336342
}

0 commit comments

Comments
 (0)