Skip to content

Commit 123c019

Browse files
committed
Remove unintended dependency on Servlet API in SockJS
Add a factory method in ServerHttpRequest for creating a ServerHttpAsyncRequestControl.
1 parent 15a2f03 commit 123c019

File tree

7 files changed

+42
-30
lines changed

7 files changed

+42
-30
lines changed

spring-web/src/main/java/org/springframework/http/server/ServerHttpAsyncResponseControl.java renamed to spring-web/src/main/java/org/springframework/http/server/ServerHttpAsyncRequestControl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @author Rossen Stoyanchev
2424
* @since 4.0
2525
*/
26-
public interface ServerHttpAsyncResponseControl {
26+
public interface ServerHttpAsyncRequestControl {
2727

2828
/**
2929
* Enable asynchronous processing after which the response remains open until a call
@@ -42,7 +42,7 @@ public interface ServerHttpAsyncResponseControl {
4242
/**
4343
* Whether asynchronous request processing has been started.
4444
*/
45-
boolean hasStarted();
45+
boolean isStarted();
4646

4747
/**
4848
* Causes asynchronous request processing to be completed.

spring-web/src/main/java/org/springframework/http/server/ServerHttpRequest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* Represents a server-side HTTP request.
2929
*
3030
* @author Arjen Poutsma
31+
* @author Rossen Stoyanchev
3132
* @since 3.0
3233
*/
3334
public interface ServerHttpRequest extends HttpRequest, HttpInputMessage {
@@ -39,7 +40,6 @@ public interface ServerHttpRequest extends HttpRequest, HttpInputMessage {
3940

4041
/**
4142
* Return the cookie values parsed from the "Cookie" request header.
42-
* @return the cookies
4343
*/
4444
Map<String, Cookie> getCookies();
4545

@@ -60,4 +60,10 @@ public interface ServerHttpRequest extends HttpRequest, HttpInputMessage {
6060
*/
6161
String getRemoteAddress();
6262

63+
/**
64+
* Return a control that allows putting the request in asynchronous mode so the
65+
* response remains open until closed explicitly from the current or another thread.
66+
*/
67+
ServerHttpAsyncRequestControl getAsyncRequestControl(ServerHttpResponse response);
68+
6369
}

spring-web/src/main/java/org/springframework/http/server/ServletServerHttpAsyncRequestControl.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929

3030

3131
/**
32-
* A {@link ServerHttpAsyncResponseControl} to use on Servlet containers (Servlet 3.0+).
32+
* A {@link ServerHttpAsyncRequestControl} to use on Servlet containers (Servlet 3.0+).
3333
*
3434
* @author Rossen Stoyanchev
3535
* @since 4.0
3636
*/
37-
public class ServletServerHttpAsyncRequestControl implements ServerHttpAsyncResponseControl, AsyncListener {
37+
public class ServletServerHttpAsyncRequestControl implements ServerHttpAsyncRequestControl, AsyncListener {
3838

3939
private static long NO_TIMEOUT_VALUE = Long.MIN_VALUE;
4040

@@ -52,27 +52,24 @@ public class ServletServerHttpAsyncRequestControl implements ServerHttpAsyncResp
5252
* {@link ServletServerHttpRequest} and {@link ServletServerHttpResponse}
5353
* respectively.
5454
*/
55-
public ServletServerHttpAsyncRequestControl(ServerHttpRequest request, ServerHttpResponse response) {
55+
public ServletServerHttpAsyncRequestControl(ServletServerHttpRequest request, ServletServerHttpResponse response) {
5656

5757
Assert.notNull(request, "request is required");
5858
Assert.notNull(response, "response is required");
5959

60-
Assert.isInstanceOf(ServletServerHttpRequest.class, request);
61-
Assert.isInstanceOf(ServletServerHttpResponse.class, response);
62-
63-
this.request = (ServletServerHttpRequest) request;
64-
this.response = (ServletServerHttpResponse) response;
65-
66-
Assert.isTrue(this.request.getServletRequest().isAsyncSupported(),
60+
Assert.isTrue(request.getServletRequest().isAsyncSupported(),
6761
"Async support must be enabled on a servlet and for all filters involved " +
6862
"in async request processing. This is done in Java code using the Servlet API " +
6963
"or by adding \"<async-supported>true</async-supported>\" to servlet and " +
7064
"filter declarations in web.xml. Also you must use a Servlet 3.0+ container");
65+
66+
this.request = request;
67+
this.response = response;
7168
}
7269

7370

7471
@Override
75-
public boolean hasStarted() {
72+
public boolean isStarted() {
7673
return ((this.asyncContext != null) && this.request.getServletRequest().isAsyncStarted());
7774
}
7875

@@ -91,7 +88,7 @@ public void start(long timeout) {
9188

9289
Assert.state(!isCompleted(), "Async processing has already completed");
9390

94-
if (hasStarted()) {
91+
if (isStarted()) {
9592
return;
9693
}
9794

@@ -108,7 +105,7 @@ public void start(long timeout) {
108105

109106
@Override
110107
public void complete() {
111-
if (hasStarted() && !isCompleted()) {
108+
if (isStarted() && !isCompleted()) {
112109
this.asyncContext.complete();
113110
}
114111
}

spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
7272

7373
private MultiValueMap<String, String> queryParams;
7474

75+
private ServerHttpAsyncRequestControl asyncRequestControl;
7576

7677
/**
7778
* Construct a new instance of the ServletServerHttpRequest based on the given {@link HttpServletRequest}.
@@ -238,4 +239,14 @@ private InputStream getBodyFromServletRequestParameters(HttpServletRequest reque
238239
return new ByteArrayInputStream(bos.toByteArray());
239240
}
240241

242+
@Override
243+
public ServerHttpAsyncRequestControl getAsyncRequestControl(ServerHttpResponse response) {
244+
if (this.asyncRequestControl == null) {
245+
Assert.isInstanceOf(ServletServerHttpResponse.class, response);
246+
ServletServerHttpResponse servletServerResponse = (ServletServerHttpResponse) response;
247+
this.asyncRequestControl = new ServletServerHttpAsyncRequestControl(this, servletServerResponse);
248+
}
249+
return this.asyncRequestControl;
250+
}
251+
241252
}

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020
import java.util.concurrent.ArrayBlockingQueue;
2121
import java.util.concurrent.BlockingQueue;
2222

23-
import org.springframework.http.server.ServerHttpAsyncResponseControl;
23+
import org.springframework.http.server.ServerHttpAsyncRequestControl;
2424
import org.springframework.http.server.ServerHttpRequest;
2525
import org.springframework.http.server.ServerHttpResponse;
26-
import org.springframework.http.server.ServletServerHttpAsyncRequestControl;
2726
import org.springframework.util.Assert;
2827
import org.springframework.web.socket.CloseStatus;
2928
import org.springframework.web.socket.WebSocketHandler;
@@ -48,7 +47,7 @@ public abstract class AbstractHttpSockJsSession extends AbstractSockJsSession {
4847

4948
private ServerHttpResponse response;
5049

51-
private ServerHttpAsyncResponseControl asyncControl;
50+
private ServerHttpAsyncRequestControl asyncRequestControl;
5251

5352
private String protocol;
5453

@@ -109,7 +108,7 @@ public synchronized void setLongPollingRequest(ServerHttpRequest request, Server
109108
return;
110109
}
111110
try {
112-
this.asyncControl.start(-1);
111+
this.asyncRequestControl.start(-1);
113112
scheduleHeartbeat();
114113
tryFlushCache();
115114
}
@@ -125,14 +124,14 @@ private void udpateRequest(ServerHttpRequest request, ServerHttpResponse respons
125124
Assert.notNull(frameFormat, "expected frameFormat");
126125
this.request = request;
127126
this.response = response;
128-
this.asyncControl = new ServletServerHttpAsyncRequestControl(this.request, this.response);
127+
this.asyncRequestControl = request.getAsyncRequestControl(response);
129128
this.frameFormat = frameFormat;
130129
}
131130

132131

133132
@Override
134133
public synchronized boolean isActive() {
135-
return ((this.asyncControl != null) && (!this.asyncControl.isCompleted()));
134+
return ((this.asyncRequestControl != null) && (!this.asyncRequestControl.isCompleted()));
136135
}
137136

138137
protected BlockingQueue<String> getMessageCache() {
@@ -172,18 +171,18 @@ protected void disconnect(CloseStatus status) {
172171

173172
protected synchronized void resetRequest() {
174173
updateLastActiveTime();
175-
if (isActive() && this.asyncControl.hasStarted()) {
174+
if (isActive() && this.asyncRequestControl.isStarted()) {
176175
try {
177176
logger.debug("Completing asynchronous request");
178-
this.asyncControl.complete();
177+
this.asyncRequestControl.complete();
179178
}
180179
catch (Throwable ex) {
181180
logger.error("Failed to complete request: " + ex.getMessage());
182181
}
183182
}
184183
this.request = null;
185184
this.response = null;
186-
this.asyncControl = null;
185+
this.asyncRequestControl = null;
187186
}
188187

189188
@Override

spring-websocket/src/test/java/org/springframework/web/socket/AbstractHttpRequestTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
package org.springframework.web.socket;
1818

1919
import org.junit.Before;
20-
import org.springframework.http.server.ServerHttpAsyncResponseControl;
20+
import org.springframework.http.server.ServerHttpAsyncRequestControl;
2121
import org.springframework.http.server.ServerHttpRequest;
2222
import org.springframework.http.server.ServerHttpResponse;
23-
import org.springframework.http.server.ServletServerHttpAsyncRequestControl;
2423
import org.springframework.http.server.ServletServerHttpRequest;
2524
import org.springframework.http.server.ServletServerHttpResponse;
2625
import org.springframework.mock.web.test.MockHttpServletRequest;
@@ -41,7 +40,7 @@ public class AbstractHttpRequestTests {
4140

4241
protected MockHttpServletResponse servletResponse;
4342

44-
protected ServerHttpAsyncResponseControl asyncControl;
43+
protected ServerHttpAsyncRequestControl asyncControl;
4544

4645

4746
@Before
@@ -57,7 +56,7 @@ protected void setRequest(String method, String requestUri) {
5756
protected void resetRequestAndResponse() {
5857
resetRequest();
5958
resetResponse();
60-
this.asyncControl = new ServletServerHttpAsyncRequestControl(this.request, this.response);
59+
this.asyncControl = this.request.getAsyncRequestControl(this.response);
6160
}
6261

6362
protected void resetRequest() {

spring-websocket/src/test/java/org/springframework/web/socket/sockjs/support/AbstractSockJsServiceTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ protected void handleRawWebSocketRequest(ServerHttpRequest req, ServerHttpRespon
246246

247247
@Override
248248
protected void handleTransportRequest(ServerHttpRequest req, ServerHttpResponse res, WebSocketHandler handler,
249-
String sessionId, String transport) throws IOException, SockJsException {
249+
String sessionId, String transport) throws SockJsException {
250250

251251
this.sessionId = sessionId;
252252
this.transport = transport;

0 commit comments

Comments
 (0)