Search in sources :

Example 36 with HttpServletResponse

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

the class ServletInvocableHandlerMethod method disableContentCachingIfNecessary.

private void disableContentCachingIfNecessary(ServletWebRequest webRequest) {
    if (isRequestNotModified(webRequest)) {
        HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
        Assert.notNull(response, "Expected HttpServletResponse");
        if (StringUtils.hasText(response.getHeader(HttpHeaders.ETAG))) {
            HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
            Assert.notNull(request, "Expected HttpServletRequest");
        }
    }
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) HttpServletResponse(jakarta.servlet.http.HttpServletResponse)

Example 37 with HttpServletResponse

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

the class JettyRequestUpgradeStrategy method upgrade.

@Override
public void upgrade(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol, List<WebSocketExtension> selectedExtensions, @Nullable Principal user, WebSocketHandler handler, Map<String, Object> attributes) throws HandshakeFailureException {
    Assert.isInstanceOf(ServletServerHttpRequest.class, request, "ServletServerHttpRequest required");
    HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
    ServletContext servletContext = servletRequest.getServletContext();
    Assert.isInstanceOf(ServletServerHttpResponse.class, response, "ServletServerHttpResponse required");
    HttpServletResponse servletResponse = ((ServletServerHttpResponse) response).getServletResponse();
    JettyWebSocketSession session = new JettyWebSocketSession(attributes, user);
    JettyWebSocketHandlerAdapter handlerAdapter = new JettyWebSocketHandlerAdapter(handler, session);
    JettyWebSocketCreator webSocketCreator = (upgradeRequest, upgradeResponse) -> {
        if (selectedProtocol != null) {
            upgradeResponse.setAcceptedSubProtocol(selectedProtocol);
        }
        return handlerAdapter;
    };
    JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
    try {
        container.upgrade(webSocketCreator, servletRequest, servletResponse);
    } catch (UndeclaredThrowableException ex) {
        throw new HandshakeFailureException("Failed to upgrade", ex.getUndeclaredThrowable());
    } catch (Exception ex) {
        throw new HandshakeFailureException("Failed to upgrade", ex);
    }
}
Also used : WebSocketHandler(org.springframework.web.socket.WebSocketHandler) RequestUpgradeStrategy(org.springframework.web.socket.server.RequestUpgradeStrategy) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession) List(java.util.List) Principal(java.security.Principal) JettyWebSocketCreator(org.eclipse.jetty.websocket.server.JettyWebSocketCreator) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) JettyWebSocketHandlerAdapter(org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter) JettyWebSocketServerContainer(org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) Map(java.util.Map) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) Nullable(org.springframework.lang.Nullable) ServletContext(jakarta.servlet.ServletContext) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) WebSocketExtension(org.springframework.web.socket.WebSocketExtension) Collections(java.util.Collections) Assert(org.springframework.util.Assert) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) JettyWebSocketCreator(org.eclipse.jetty.websocket.server.JettyWebSocketCreator) JettyWebSocketHandlerAdapter(org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) JettyWebSocketServerContainer(org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) ServletContext(jakarta.servlet.ServletContext)

Example 38 with HttpServletResponse

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

the class UndertowRequestUpgradeStrategy method upgradeInternal.

@Override
protected void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol, List<Extension> selectedExtensions, Endpoint endpoint) throws HandshakeFailureException {
    HttpServletRequest servletRequest = getHttpServletRequest(request);
    HttpServletResponse servletResponse = getHttpServletResponse(response);
    StringBuffer requestUrl = servletRequest.getRequestURL();
    // shouldn't matter
    String path = servletRequest.getRequestURI();
    Map<String, String> pathParams = Collections.emptyMap();
    ServerEndpointRegistration endpointConfig = new ServerEndpointRegistration(path, endpoint);
    endpointConfig.setSubprotocols(Collections.singletonList(selectedProtocol));
    endpointConfig.setExtensions(selectedExtensions);
    try {
        getContainer(servletRequest).doUpgrade(servletRequest, servletResponse, endpointConfig, pathParams);
    } catch (ServletException ex) {
        throw new HandshakeFailureException("Servlet request failed to upgrade to WebSocket: " + requestUrl, ex);
    } catch (IOException ex) {
        throw new HandshakeFailureException("Response update failed during upgrade to WebSocket: " + requestUrl, ex);
    }
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletException(jakarta.servlet.ServletException) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) IOException(java.io.IOException)

Example 39 with HttpServletResponse

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

the class SockJsServiceTests method handleInfoGetWildflyNPE.

// SPR-11919
@Test
public void handleInfoGetWildflyNPE() throws IOException {
    HttpServletResponse mockResponse = mock(HttpServletResponse.class);
    ServletOutputStream ous = mock(ServletOutputStream.class);
    given(mockResponse.getHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).willThrow(NullPointerException.class);
    given(mockResponse.getOutputStream()).willReturn(ous);
    this.response = new ServletServerHttpResponse(mockResponse);
    handleRequest("GET", "/echo/info", HttpStatus.OK);
    verify(mockResponse, times(1)).getOutputStream();
}
Also used : ServletOutputStream(jakarta.servlet.ServletOutputStream) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 40 with HttpServletResponse

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

the class HttpEntityMethodProcessorTests method handleReturnValueWithETagAndETagFilter.

// SPR-13423
@Test
public void handleReturnValueWithETagAndETagFilter() throws Exception {
    String eTagValue = "\"deadb33f8badf00d\"";
    String content = "body";
    Method method = getClass().getDeclaredMethod("handle");
    MethodParameter returnType = new MethodParameter(method, -1);
    FilterChain chain = (req, res) -> {
        ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(eTagValue).body(content);
        try {
            ServletWebRequest requestToUse = new ServletWebRequest((HttpServletRequest) req, (HttpServletResponse) res);
            new HttpEntityMethodProcessor(Collections.singletonList(new StringHttpMessageConverter())).handleReturnValue(returnValue, returnType, mavContainer, requestToUse);
            assertThat(this.servletResponse.getContentAsString()).as("Response body was cached? It should be written directly to the raw response").isEqualTo(content);
        } catch (Exception ex) {
            throw new IllegalStateException(ex);
        }
    };
    this.servletRequest.setMethod("GET");
    new ShallowEtagHeaderFilter().doFilter(this.servletRequest, this.servletResponse, chain);
    assertThat(this.servletResponse.getStatus()).isEqualTo(200);
    assertThat(this.servletResponse.getHeader(HttpHeaders.ETAG)).isEqualTo(eTagValue);
    assertThat(this.servletResponse.getContentAsString()).isEqualTo(content);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ArrayList(java.util.ArrayList) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) HandlerMethod(org.springframework.web.method.HandlerMethod) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MethodParameter(org.springframework.core.MethodParameter) Nullable(org.springframework.lang.Nullable) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) Method(java.lang.reflect.Method) ShallowEtagHeaderFilter(org.springframework.web.filter.ShallowEtagHeaderFilter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) FilterChain(jakarta.servlet.FilterChain) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) Serializable(java.io.Serializable) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.jupiter.api.Test) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) WebDataBinder(org.springframework.web.bind.WebDataBinder) ResponseEntity(org.springframework.http.ResponseEntity) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Collections(java.util.Collections) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) ShallowEtagHeaderFilter(org.springframework.web.filter.ShallowEtagHeaderFilter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ResponseEntity(org.springframework.http.ResponseEntity) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

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