Search in sources :

Example 31 with HttpServletResponse

use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.

the class AbstractMessageConverterMethodProcessor method createOutputMessage.

/**
 * Creates a new {@link HttpOutputMessage} from the given {@link NativeWebRequest}.
 * @param webRequest the web request to create an output message from
 * @return the output message
 */
protected ServletServerHttpResponse createOutputMessage(NativeWebRequest webRequest) {
    HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
    Assert.state(response != null, "No HttpServletResponse");
    return new ServletServerHttpResponse(response);
}
Also used : HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse)

Example 32 with HttpServletResponse

use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.

the class HttpHeadersReturnValueHandler method handleReturnValue.

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

Example 33 with HttpServletResponse

use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.

the class ResponseBodyEmitterReturnValueHandler method handleReturnValue.

@Override
@SuppressWarnings("resource")
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
    if (returnValue == null) {
        mavContainer.setRequestHandled(true);
        return;
    }
    HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
    Assert.state(response != null, "No HttpServletResponse");
    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();
        returnType = returnType.nested();
        if (returnValue == null) {
            mavContainer.setRequestHandled(true);
            outputMessage.flush();
            return;
        }
    }
    ServletRequest request = webRequest.getNativeRequest(ServletRequest.class);
    Assert.state(request != null, "No ServletRequest");
    ResponseBodyEmitter emitter;
    if (returnValue instanceof ResponseBodyEmitter) {
        emitter = (ResponseBodyEmitter) returnValue;
    } else {
        emitter = this.reactiveHandler.handleValue(returnValue, returnType, mavContainer, webRequest);
        if (emitter == null) {
            // Not streaming: write headers without committing response..
            outputMessage.getHeaders().forEach((headerName, headerValues) -> {
                for (String headerValue : headerValues) {
                    response.addHeader(headerName, headerValue);
                }
            });
            return;
        }
    }
    emitter.extendResponse(outputMessage);
    // At this point we know we're streaming..
    ShallowEtagHeaderFilter.disableContentCaching(request);
    // Wrap the response to ignore further header changes
    // Headers will be flushed at the first write
    outputMessage = new StreamingServletServerHttpResponse(outputMessage);
    HttpMessageConvertingHandler handler;
    try {
        DeferredResult<?> deferredResult = new DeferredResult<>(emitter.getTimeout());
        WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer);
        handler = new HttpMessageConvertingHandler(outputMessage, deferredResult);
    } catch (Throwable ex) {
        emitter.initializeWithError(ex);
        throw ex;
    }
    emitter.initialize(handler);
}
Also used : ServletRequest(jakarta.servlet.ServletRequest) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) DelegatingServerHttpResponse(org.springframework.http.server.DelegatingServerHttpResponse) ResponseEntity(org.springframework.http.ResponseEntity) DeferredResult(org.springframework.web.context.request.async.DeferredResult)

Example 34 with HttpServletResponse

use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.

the class StreamingResponseBodyReturnValueHandler method handleReturnValue.

@Override
@SuppressWarnings("resource")
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
    if (returnValue == null) {
        mavContainer.setRequestHandled(true);
        return;
    }
    HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
    Assert.state(response != null, "No HttpServletResponse");
    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);
    Assert.state(request != null, "No ServletRequest");
    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(jakarta.servlet.ServletRequest) ResponseEntity(org.springframework.http.ResponseEntity) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse)

Example 35 with HttpServletResponse

use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.

the class ServletInvocableHandlerMethod method setResponseStatus.

/**
 * Set the response status according to the {@link ResponseStatus} annotation.
 */
private void setResponseStatus(ServletWebRequest webRequest) throws IOException {
    HttpStatus status = getResponseStatus();
    if (status == null) {
        return;
    }
    HttpServletResponse response = webRequest.getResponse();
    if (response != null) {
        String reason = getResponseStatusReason();
        if (StringUtils.hasText(reason)) {
            response.sendError(status.value(), reason);
        } else {
            response.setStatus(status.value());
        }
    }
    // To be picked up by RedirectView
    webRequest.getRequest().setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, status);
}
Also used : HttpStatus(org.springframework.http.HttpStatus) HttpServletResponse(jakarta.servlet.http.HttpServletResponse)

Aggregations

HttpServletResponse (jakarta.servlet.http.HttpServletResponse)118 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)76 Test (org.junit.jupiter.api.Test)47 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)34 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)31 FilterChain (jakarta.servlet.FilterChain)22 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)18 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)16 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)15 ServletException (jakarta.servlet.ServletException)14 StandardCharsets (java.nio.charset.StandardCharsets)14 HttpServlet (jakarta.servlet.http.HttpServlet)13 IOException (java.io.IOException)12 HashMap (java.util.HashMap)12 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)10 Test (org.junit.Test)10 Authentication (org.springframework.security.core.Authentication)10 FileCopyUtils (org.springframework.util.FileCopyUtils)9 BeforeEach (org.junit.jupiter.api.BeforeEach)8 Collections (java.util.Collections)7