Search in sources :

Example 6 with ServletServerHttpResponse

use of org.springframework.http.server.ServletServerHttpResponse in project spring-boot by spring-projects.

the class HttpRestartServerTests method sendNoContent.

@Test
public void sendNoContent() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    this.server.handle(new ServletServerHttpRequest(request), new ServletServerHttpResponse(response));
    verifyZeroInteractions(this.delegate);
    assertThat(response.getStatus()).isEqualTo(500);
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 7 with ServletServerHttpResponse

use of org.springframework.http.server.ServletServerHttpResponse in project spring-framework by spring-projects.

the class HttpHeadersReturnValueHandler method handleReturnValue.

@Override
@SuppressWarnings("resource")
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
    mavContainer.setRequestHandled(true);
    Assert.isInstanceOf(HttpHeaders.class, returnValue, "HttpHeaders expected");
    HttpHeaders headers = (HttpHeaders) returnValue;
    if (!headers.isEmpty()) {
        HttpServletResponse servletResponse = webRequest.getNativeResponse(HttpServletResponse.class);
        ServletServerHttpResponse outputMessage = new ServletServerHttpResponse(servletResponse);
        outputMessage.getHeaders().putAll(headers);
        // flush headers
        outputMessage.getBody();
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse)

Example 8 with ServletServerHttpResponse

use of org.springframework.http.server.ServletServerHttpResponse in project spring-framework by spring-projects.

the class ResponseBodyEmitterReturnValueHandler method handleReturnValue.

@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 (returnValue instanceof ResponseEntity) {
        ResponseEntity<?> responseEntity = (ResponseEntity<?>) returnValue;
        response.setStatus(responseEntity.getStatusCodeValue());
        outputMessage.getHeaders().putAll(responseEntity.getHeaders());
        returnValue = responseEntity.getBody();
        if (returnValue == null) {
            mavContainer.setRequestHandled(true);
            outputMessage.flush();
            return;
        }
    }
    ServletRequest request = webRequest.getNativeRequest(ServletRequest.class);
    ShallowEtagHeaderFilter.disableContentCaching(request);
    ResponseBodyEmitterAdapter adapter = getAdapterFor(returnValue.getClass());
    if (adapter == null) {
        throw new IllegalStateException("Could not find ResponseBodyEmitterAdapter for return value type: " + returnValue.getClass());
    }
    ResponseBodyEmitter emitter = adapter.adaptToEmitter(returnValue, outputMessage);
    emitter.extendResponse(outputMessage);
    // Commit the response and wrap to ignore further header changes
    outputMessage.getBody();
    outputMessage.flush();
    outputMessage = new StreamingServletServerHttpResponse(outputMessage);
    DeferredResult<?> deferredResult = new DeferredResult<>(emitter.getTimeout());
    WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer);
    HttpMessageConvertingHandler handler = new HttpMessageConvertingHandler(outputMessage, deferredResult);
    emitter.initialize(handler);
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ResponseEntity(org.springframework.http.ResponseEntity) DeferredResult(org.springframework.web.context.request.async.DeferredResult)

Example 9 with ServletServerHttpResponse

use of org.springframework.http.server.ServletServerHttpResponse in project spring-framework by spring-projects.

the class StreamingResponseBodyReturnValueHandler method handleReturnValue.

@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 (returnValue instanceof ResponseEntity) {
        ResponseEntity<?> responseEntity = (ResponseEntity<?>) returnValue;
        response.setStatus(responseEntity.getStatusCodeValue());
        outputMessage.getHeaders().putAll(responseEntity.getHeaders());
        returnValue = responseEntity.getBody();
        if (returnValue == null) {
            mavContainer.setRequestHandled(true);
            outputMessage.flush();
            return;
        }
    }
    ServletRequest request = webRequest.getNativeRequest(ServletRequest.class);
    ShallowEtagHeaderFilter.disableContentCaching(request);
    Assert.isInstanceOf(StreamingResponseBody.class, returnValue, "StreamingResponseBody expected");
    StreamingResponseBody streamingBody = (StreamingResponseBody) returnValue;
    Callable<Void> callable = new StreamingResponseBodyTask(outputMessage.getBody(), streamingBody);
    WebAsyncUtils.getAsyncManager(webRequest).startCallableProcessing(callable, mavContainer);
}
Also used : ServletRequest(javax.servlet.ServletRequest) ResponseEntity(org.springframework.http.ResponseEntity) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse)

Example 10 with ServletServerHttpResponse

use of org.springframework.http.server.ServletServerHttpResponse in project spring-framework by spring-projects.

the class JettyRequestUpgradeStrategy method upgrade.

@Override
public void upgrade(ServerHttpRequest request, ServerHttpResponse response, String selectedProtocol, List<WebSocketExtension> selectedExtensions, Principal user, WebSocketHandler wsHandler, Map<String, Object> attributes) throws HandshakeFailureException {
    Assert.isInstanceOf(ServletServerHttpRequest.class, request, "ServletServerHttpRequest required");
    HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
    Assert.isInstanceOf(ServletServerHttpResponse.class, response, "ServletServerHttpResponse required");
    HttpServletResponse servletResponse = ((ServletServerHttpResponse) response).getServletResponse();
    Assert.isTrue(this.factory.isUpgradeRequest(servletRequest, servletResponse), "Not a WebSocket handshake");
    JettyWebSocketSession session = new JettyWebSocketSession(attributes, user);
    JettyWebSocketHandlerAdapter handlerAdapter = new JettyWebSocketHandlerAdapter(wsHandler, session);
    WebSocketHandlerContainer container = new WebSocketHandlerContainer(handlerAdapter, selectedProtocol, selectedExtensions);
    try {
        containerHolder.set(container);
        this.factory.acceptWebSocket(servletRequest, servletResponse);
    } catch (IOException ex) {
        throw new HandshakeFailureException("Response update failed during upgrade to WebSocket: " + request.getURI(), ex);
    } finally {
        containerHolder.remove();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) IOException(java.io.IOException) JettyWebSocketHandlerAdapter(org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter)

Aggregations

ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)28 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)18 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)8 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 Before (org.junit.Before)6 Test (org.junit.Test)6 ServerHttpResponse (org.springframework.http.server.ServerHttpResponse)6 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)4 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)4 ResponseEntity (org.springframework.http.ResponseEntity)3 ServletRequest (javax.servlet.ServletRequest)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpHeaders (org.springframework.http.HttpHeaders)2 MediaType (org.springframework.http.MediaType)2 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)2 HandshakeFailureException (org.springframework.web.socket.server.HandshakeFailureException)2 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ByteChannel (java.nio.channels.ByteChannel)1