8 changed files with 386 additions and 9 deletions
@ -0,0 +1,47 @@
@@ -0,0 +1,47 @@
|
||||
/* |
||||
* Copyright 2002-2015 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.web.servlet.mvc.method.annotation; |
||||
|
||||
|
||||
import java.io.IOException; |
||||
import java.io.OutputStream; |
||||
|
||||
/** |
||||
* A controller method return value type for asynchronous request processing |
||||
* where the application can write directly to the response {@code OutputStream} |
||||
* without holding up the Servlet container thread. |
||||
* |
||||
* <p><strong>Note:</strong> when using this option it is highly recommended to |
||||
* configure explicitly the TaskExecutor used in Spring MVC for executing |
||||
* asynchronous requests. Both the MVC Java config and the MVC namespaces provide |
||||
* options to configure asynchronous handling. If not using those, an application |
||||
* can set the {@code taskExecutor} property of |
||||
* {@link org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter |
||||
* RequestMappingHandlerAdapter}. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.2 |
||||
*/ |
||||
public interface StreamingResponseBody { |
||||
|
||||
/** |
||||
* A callback for writing to the response body. |
||||
* @param outputStream the stream for the response body |
||||
* @throws IOException an exception while writing |
||||
*/ |
||||
void writeTo(OutputStream outputStream) throws IOException; |
||||
|
||||
} |
||||
@ -0,0 +1,114 @@
@@ -0,0 +1,114 @@
|
||||
/* |
||||
* Copyright 2002-2015 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.web.servlet.mvc.method.annotation; |
||||
|
||||
import java.io.OutputStream; |
||||
import java.util.concurrent.Callable; |
||||
|
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
|
||||
import org.springframework.core.MethodParameter; |
||||
import org.springframework.core.ResolvableType; |
||||
import org.springframework.http.ResponseEntity; |
||||
import org.springframework.http.server.ServerHttpResponse; |
||||
import org.springframework.http.server.ServletServerHttpResponse; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.web.context.request.NativeWebRequest; |
||||
import org.springframework.web.context.request.async.WebAsyncUtils; |
||||
import org.springframework.web.method.support.HandlerMethodReturnValueHandler; |
||||
import org.springframework.web.method.support.ModelAndViewContainer; |
||||
|
||||
|
||||
/** |
||||
* Supports return values of type |
||||
* {@link org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody} |
||||
* and also {@code ResponseEntity<StreamingResponseBody>}. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.2 |
||||
*/ |
||||
public class StreamingResponseBodyReturnValueHandler implements HandlerMethodReturnValueHandler { |
||||
|
||||
private static final Log logger = LogFactory.getLog(StreamingResponseBodyReturnValueHandler.class); |
||||
|
||||
|
||||
@Override |
||||
public boolean supportsReturnType(MethodParameter returnType) { |
||||
if (StreamingResponseBody.class.isAssignableFrom(returnType.getParameterType())) { |
||||
return true; |
||||
} |
||||
else if (ResponseEntity.class.isAssignableFrom(returnType.getParameterType())) { |
||||
Class<?> bodyType = ResolvableType.forMethodParameter(returnType).getGeneric(0).resolve(); |
||||
return (bodyType != null && StreamingResponseBody.class.isAssignableFrom(bodyType)); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
@Override |
||||
public void handleReturnValue(Object returnValue, MethodParameter returnType, |
||||
ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { |
||||
|
||||
if (returnValue == null) { |
||||
mavContainer.setRequestHandled(true); |
||||
return; |
||||
} |
||||
|
||||
HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class); |
||||
ServerHttpResponse outputMessage = new ServletServerHttpResponse(response); |
||||
|
||||
if (ResponseEntity.class.isAssignableFrom(returnValue.getClass())) { |
||||
ResponseEntity<?> responseEntity = (ResponseEntity<?>) returnValue; |
||||
outputMessage.setStatusCode(responseEntity.getStatusCode()); |
||||
outputMessage.getHeaders().putAll(responseEntity.getHeaders()); |
||||
|
||||
returnValue = responseEntity.getBody(); |
||||
if (returnValue == null) { |
||||
mavContainer.setRequestHandled(true); |
||||
return; |
||||
} |
||||
} |
||||
|
||||
Assert.isInstanceOf(StreamingResponseBody.class, returnValue); |
||||
StreamingResponseBody streamingBody = (StreamingResponseBody) returnValue; |
||||
|
||||
Callable<Void> callable = new StreamingResponseBodyTask(outputMessage.getBody(), streamingBody); |
||||
WebAsyncUtils.getAsyncManager(webRequest).startCallableProcessing(callable, mavContainer); |
||||
} |
||||
|
||||
|
||||
private static class StreamingResponseBodyTask implements Callable<Void> { |
||||
|
||||
private final OutputStream outputStream; |
||||
|
||||
private final StreamingResponseBody streamingBody; |
||||
|
||||
|
||||
public StreamingResponseBodyTask(OutputStream outputStream, StreamingResponseBody streamingBody) { |
||||
this.outputStream = outputStream; |
||||
this.streamingBody = streamingBody; |
||||
} |
||||
|
||||
@Override |
||||
public Void call() throws Exception { |
||||
this.streamingBody.writeTo(this.outputStream); |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,170 @@
@@ -0,0 +1,170 @@
|
||||
/* |
||||
* Copyright 2002-2015 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.web.servlet.mvc.method.annotation; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.OutputStream; |
||||
import java.lang.reflect.Method; |
||||
import java.nio.charset.Charset; |
||||
import java.util.concurrent.CountDownLatch; |
||||
import java.util.concurrent.TimeUnit; |
||||
import java.util.concurrent.atomic.AtomicReference; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.core.MethodParameter; |
||||
import org.springframework.http.ResponseEntity; |
||||
import org.springframework.mock.web.test.MockHttpServletRequest; |
||||
import org.springframework.mock.web.test.MockHttpServletResponse; |
||||
import org.springframework.web.context.request.NativeWebRequest; |
||||
import org.springframework.web.context.request.ServletWebRequest; |
||||
import org.springframework.web.context.request.async.AsyncWebRequest; |
||||
import org.springframework.web.context.request.async.StandardServletAsyncWebRequest; |
||||
import org.springframework.web.context.request.async.WebAsyncUtils; |
||||
import org.springframework.web.method.support.ModelAndViewContainer; |
||||
|
||||
|
||||
/** |
||||
* Unit tests for |
||||
* {@link org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBodyReturnValueHandler}. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
*/ |
||||
public class StreamingResponseBodyReturnValueHandlerTests { |
||||
|
||||
private StreamingResponseBodyReturnValueHandler handler; |
||||
|
||||
private ModelAndViewContainer mavContainer; |
||||
|
||||
private NativeWebRequest webRequest; |
||||
|
||||
private MockHttpServletRequest request; |
||||
|
||||
private MockHttpServletResponse response; |
||||
|
||||
|
||||
@Before |
||||
public void setUp() throws Exception { |
||||
|
||||
this.handler = new StreamingResponseBodyReturnValueHandler(); |
||||
this.mavContainer = new ModelAndViewContainer(); |
||||
|
||||
this.request = new MockHttpServletRequest("GET", "/path"); |
||||
this.response = new MockHttpServletResponse(); |
||||
this.webRequest = new ServletWebRequest(this.request, this.response); |
||||
|
||||
AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response); |
||||
WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest); |
||||
this.request.setAsyncSupported(true); |
||||
} |
||||
|
||||
@Test |
||||
public void supportsReturnType() throws Exception { |
||||
assertTrue(this.handler.supportsReturnType(returnType(TestController.class, "handle"))); |
||||
assertTrue(this.handler.supportsReturnType(returnType(TestController.class, "handleResponseEntity"))); |
||||
assertFalse(this.handler.supportsReturnType(returnType(TestController.class, "handleResponseEntityString"))); |
||||
assertFalse(this.handler.supportsReturnType(returnType(TestController.class, "handleResponseEntityParameterized"))); |
||||
} |
||||
|
||||
@Test |
||||
public void streamingResponseBody() throws Exception { |
||||
|
||||
CountDownLatch latch = new CountDownLatch(1); |
||||
|
||||
MethodParameter returnType = returnType(TestController.class, "handle"); |
||||
StreamingResponseBody streamingBody = new StreamingResponseBody() { |
||||
|
||||
@Override |
||||
public void writeTo(OutputStream outputStream) throws IOException { |
||||
outputStream.write("foo".getBytes(Charset.forName("UTF-8"))); |
||||
latch.countDown(); |
||||
} |
||||
}; |
||||
this.handler.handleReturnValue(streamingBody, returnType, this.mavContainer, this.webRequest); |
||||
|
||||
assertTrue(this.request.isAsyncStarted()); |
||||
assertTrue(latch.await(5, TimeUnit.SECONDS)); |
||||
assertEquals("foo", this.response.getContentAsString()); |
||||
} |
||||
|
||||
|
||||
@Test |
||||
public void responseEntity() throws Exception { |
||||
|
||||
CountDownLatch latch = new CountDownLatch(1); |
||||
|
||||
MethodParameter returnType = returnType(TestController.class, "handleResponseEntity"); |
||||
ResponseEntity<StreamingResponseBody> emitter = ResponseEntity.ok().header("foo", "bar") |
||||
.body(new StreamingResponseBody() { |
||||
|
||||
@Override |
||||
public void writeTo(OutputStream outputStream) throws IOException { |
||||
outputStream.write("foo".getBytes(Charset.forName("UTF-8"))); |
||||
latch.countDown(); |
||||
} |
||||
}); |
||||
this.handler.handleReturnValue(emitter, returnType, this.mavContainer, this.webRequest); |
||||
|
||||
assertTrue(this.request.isAsyncStarted()); |
||||
assertEquals(200, this.response.getStatus()); |
||||
assertEquals("bar", this.response.getHeader("foo")); |
||||
|
||||
assertTrue(latch.await(5, TimeUnit.SECONDS)); |
||||
assertEquals("foo", this.response.getContentAsString()); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void responseEntityNoContent() throws Exception { |
||||
MethodParameter returnType = returnType(TestController.class, "handleResponseEntity"); |
||||
ResponseEntity<?> emitter = ResponseEntity.noContent().build(); |
||||
this.handler.handleReturnValue(emitter, returnType, this.mavContainer, this.webRequest); |
||||
|
||||
assertFalse(this.request.isAsyncStarted()); |
||||
assertEquals(204, this.response.getStatus()); |
||||
} |
||||
|
||||
|
||||
private MethodParameter returnType(Class<?> clazz, String methodName) throws NoSuchMethodException { |
||||
Method method = clazz.getDeclaredMethod(methodName); |
||||
return new MethodParameter(method, -1); |
||||
} |
||||
|
||||
|
||||
@SuppressWarnings("unused") |
||||
private static class TestController { |
||||
|
||||
private StreamingResponseBody handle() { |
||||
return null; |
||||
} |
||||
|
||||
private ResponseEntity<StreamingResponseBody> handleResponseEntity() { |
||||
return null; |
||||
} |
||||
|
||||
private ResponseEntity<String> handleResponseEntityString() { |
||||
return null; |
||||
} |
||||
|
||||
private ResponseEntity<AtomicReference<String>> handleResponseEntityParameterized() { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue