Remove unintended dependency on Servlet API in SockJS

Add a factory method in ServerHttpRequest for creating a
ServerHttpAsyncRequestControl.
This commit is contained in:
Rossen Stoyanchev
2013-08-03 11:21:02 -04:00
parent 15a2f03459
commit 123c01908a
7 changed files with 42 additions and 30 deletions

View File

@@ -23,7 +23,7 @@ package org.springframework.http.server;
* @author Rossen Stoyanchev
* @since 4.0
*/
public interface ServerHttpAsyncResponseControl {
public interface ServerHttpAsyncRequestControl {
/**
* Enable asynchronous processing after which the response remains open until a call
@@ -42,7 +42,7 @@ public interface ServerHttpAsyncResponseControl {
/**
* Whether asynchronous request processing has been started.
*/
boolean hasStarted();
boolean isStarted();
/**
* Causes asynchronous request processing to be completed.

View File

@@ -28,6 +28,7 @@ import org.springframework.util.MultiValueMap;
* Represents a server-side HTTP request.
*
* @author Arjen Poutsma
* @author Rossen Stoyanchev
* @since 3.0
*/
public interface ServerHttpRequest extends HttpRequest, HttpInputMessage {
@@ -39,7 +40,6 @@ public interface ServerHttpRequest extends HttpRequest, HttpInputMessage {
/**
* Return the cookie values parsed from the "Cookie" request header.
* @return the cookies
*/
Map<String, Cookie> getCookies();
@@ -60,4 +60,10 @@ public interface ServerHttpRequest extends HttpRequest, HttpInputMessage {
*/
String getRemoteAddress();
/**
* Return a control that allows putting the request in asynchronous mode so the
* response remains open until closed explicitly from the current or another thread.
*/
ServerHttpAsyncRequestControl getAsyncRequestControl(ServerHttpResponse response);
}

View File

@@ -29,12 +29,12 @@ import org.springframework.util.Assert;
/**
* A {@link ServerHttpAsyncResponseControl} to use on Servlet containers (Servlet 3.0+).
* A {@link ServerHttpAsyncRequestControl} to use on Servlet containers (Servlet 3.0+).
*
* @author Rossen Stoyanchev
* @since 4.0
*/
public class ServletServerHttpAsyncRequestControl implements ServerHttpAsyncResponseControl, AsyncListener {
public class ServletServerHttpAsyncRequestControl implements ServerHttpAsyncRequestControl, AsyncListener {
private static long NO_TIMEOUT_VALUE = Long.MIN_VALUE;
@@ -52,27 +52,24 @@ public class ServletServerHttpAsyncRequestControl implements ServerHttpAsyncResp
* {@link ServletServerHttpRequest} and {@link ServletServerHttpResponse}
* respectively.
*/
public ServletServerHttpAsyncRequestControl(ServerHttpRequest request, ServerHttpResponse response) {
public ServletServerHttpAsyncRequestControl(ServletServerHttpRequest request, ServletServerHttpResponse response) {
Assert.notNull(request, "request is required");
Assert.notNull(response, "response is required");
Assert.isInstanceOf(ServletServerHttpRequest.class, request);
Assert.isInstanceOf(ServletServerHttpResponse.class, response);
this.request = (ServletServerHttpRequest) request;
this.response = (ServletServerHttpResponse) response;
Assert.isTrue(this.request.getServletRequest().isAsyncSupported(),
Assert.isTrue(request.getServletRequest().isAsyncSupported(),
"Async support must be enabled on a servlet and for all filters involved " +
"in async request processing. This is done in Java code using the Servlet API " +
"or by adding \"<async-supported>true</async-supported>\" to servlet and " +
"filter declarations in web.xml. Also you must use a Servlet 3.0+ container");
this.request = request;
this.response = response;
}
@Override
public boolean hasStarted() {
public boolean isStarted() {
return ((this.asyncContext != null) && this.request.getServletRequest().isAsyncStarted());
}
@@ -91,7 +88,7 @@ public class ServletServerHttpAsyncRequestControl implements ServerHttpAsyncResp
Assert.state(!isCompleted(), "Async processing has already completed");
if (hasStarted()) {
if (isStarted()) {
return;
}
@@ -108,7 +105,7 @@ public class ServletServerHttpAsyncRequestControl implements ServerHttpAsyncResp
@Override
public void complete() {
if (hasStarted() && !isCompleted()) {
if (isStarted() && !isCompleted()) {
this.asyncContext.complete();
}
}

View File

@@ -72,6 +72,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
private MultiValueMap<String, String> queryParams;
private ServerHttpAsyncRequestControl asyncRequestControl;
/**
* Construct a new instance of the ServletServerHttpRequest based on the given {@link HttpServletRequest}.
@@ -238,4 +239,14 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
return new ByteArrayInputStream(bos.toByteArray());
}
@Override
public ServerHttpAsyncRequestControl getAsyncRequestControl(ServerHttpResponse response) {
if (this.asyncRequestControl == null) {
Assert.isInstanceOf(ServletServerHttpResponse.class, response);
ServletServerHttpResponse servletServerResponse = (ServletServerHttpResponse) response;
this.asyncRequestControl = new ServletServerHttpAsyncRequestControl(this, servletServerResponse);
}
return this.asyncRequestControl;
}
}