Search in sources :

Example 31 with ServletServerHttpResponse

use of org.springframework.http.server.ServletServerHttpResponse in project flytecnologia-api by jullierme.

the class FlyRefreshTokenPostProcessor method beforeBodyWrite.

@Override
public OAuth2AccessToken beforeBodyWrite(OAuth2AccessToken body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
    HttpServletRequest req = ((ServletServerHttpRequest) request).getServletRequest();
    HttpServletResponse resp = ((ServletServerHttpResponse) response).getServletResponse();
    DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) body;
    String refreshToken = body.getRefreshToken().getValue();
    addRefreshTokenInCookie(refreshToken, req, resp);
    deleteRefreshTokenFromBody(token);
    return body;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)

Example 32 with ServletServerHttpResponse

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

the class RequestResponseBodyAdviceChainTests method setup.

@BeforeEach
public void setup() {
    this.body = "body";
    this.contentType = MediaType.TEXT_PLAIN;
    this.converterType = StringHttpMessageConverter.class;
    this.paramType = new MethodParameter(ClassUtils.getMethod(this.getClass(), "handle", String.class), 0);
    this.returnType = new MethodParameter(ClassUtils.getMethod(this.getClass(), "handle", String.class), -1);
    this.request = new ServletServerHttpRequest(new MockHttpServletRequest());
    this.response = new ServletServerHttpResponse(new MockHttpServletResponse());
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) MethodParameter(org.springframework.core.MethodParameter) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 33 with ServletServerHttpResponse

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

the class WebSocketHttpRequestHandler method handleRequest.

@Override
public void handleRequest(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException {
    ServerHttpRequest request = new ServletServerHttpRequest(servletRequest);
    ServerHttpResponse response = new ServletServerHttpResponse(servletResponse);
    HandshakeInterceptorChain chain = new HandshakeInterceptorChain(this.interceptors, this.wsHandler);
    HandshakeFailureException failure = null;
    try {
        if (logger.isDebugEnabled()) {
            logger.debug(servletRequest.getMethod() + " " + servletRequest.getRequestURI());
        }
        Map<String, Object> attributes = new HashMap<>();
        if (!chain.applyBeforeHandshake(request, response, attributes)) {
            return;
        }
        this.handshakeHandler.doHandshake(request, response, this.wsHandler, attributes);
        chain.applyAfterHandshake(request, response, null);
    } catch (HandshakeFailureException ex) {
        failure = ex;
    } catch (Exception ex) {
        failure = new HandshakeFailureException("Uncaught failure for request " + request.getURI(), ex);
    } finally {
        if (failure != null) {
            chain.applyAfterHandshake(request, response, failure);
            response.close();
            throw failure;
        }
        response.close();
    }
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) HashMap(java.util.HashMap) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ServletException(jakarta.servlet.ServletException) IOException(java.io.IOException) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException)

Example 34 with ServletServerHttpResponse

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

the class SockJsHttpRequestHandler method handleRequest.

@Override
public void handleRequest(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException {
    ServerHttpRequest request = new ServletServerHttpRequest(servletRequest);
    ServerHttpResponse response = new ServletServerHttpResponse(servletResponse);
    try {
        this.sockJsService.handleRequest(request, response, getSockJsPath(servletRequest), this.webSocketHandler);
    } catch (Exception ex) {
        throw new SockJsException("Uncaught failure in SockJS request, uri=" + request.getURI(), ex);
    }
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) SockJsException(org.springframework.web.socket.sockjs.SockJsException) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) SockJsException(org.springframework.web.socket.sockjs.SockJsException) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException)

Example 35 with ServletServerHttpResponse

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

the class TransportHandlingSockJsService method handleTransportRequest.

@Override
protected void handleTransportRequest(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler handler, String sessionId, String transport) throws SockJsException {
    TransportType transportType = TransportType.fromValue(transport);
    if (transportType == null) {
        if (logger.isWarnEnabled()) {
            logger.warn(LogFormatUtils.formatValue("Unknown transport type for " + request.getURI(), -1, true));
        }
        response.setStatusCode(HttpStatus.NOT_FOUND);
        return;
    }
    TransportHandler transportHandler = this.handlers.get(transportType);
    if (transportHandler == null) {
        if (logger.isWarnEnabled()) {
            logger.warn(LogFormatUtils.formatValue("No TransportHandler for " + request.getURI(), -1, true));
        }
        response.setStatusCode(HttpStatus.NOT_FOUND);
        return;
    }
    SockJsException failure = null;
    HandshakeInterceptorChain chain = new HandshakeInterceptorChain(this.interceptors, handler);
    try {
        HttpMethod supportedMethod = transportType.getHttpMethod();
        if (supportedMethod != request.getMethod()) {
            if (request.getMethod() == HttpMethod.OPTIONS && transportType.supportsCors()) {
                if (checkOrigin(request, response, HttpMethod.OPTIONS, supportedMethod)) {
                    response.setStatusCode(HttpStatus.NO_CONTENT);
                    addCacheHeaders(response);
                }
            } else if (transportType.supportsCors()) {
                sendMethodNotAllowed(response, supportedMethod, HttpMethod.OPTIONS);
            } else {
                sendMethodNotAllowed(response, supportedMethod);
            }
            return;
        }
        SockJsSession session = this.sessions.get(sessionId);
        boolean isNewSession = false;
        if (session == null) {
            if (transportHandler instanceof SockJsSessionFactory) {
                Map<String, Object> attributes = new HashMap<>();
                if (!chain.applyBeforeHandshake(request, response, attributes)) {
                    return;
                }
                SockJsSessionFactory sessionFactory = (SockJsSessionFactory) transportHandler;
                session = createSockJsSession(sessionId, sessionFactory, handler, attributes);
                isNewSession = true;
            } else {
                response.setStatusCode(HttpStatus.NOT_FOUND);
                if (logger.isDebugEnabled()) {
                    logger.debug("Session not found, sessionId=" + sessionId + ". The session may have been closed " + "(e.g. missed heart-beat) while a message was coming in.");
                }
                return;
            }
        } else {
            Principal principal = session.getPrincipal();
            if (principal != null && !principal.equals(request.getPrincipal())) {
                logger.debug("The user for the session does not match the user for the request.");
                response.setStatusCode(HttpStatus.NOT_FOUND);
                return;
            }
            if (!transportHandler.checkSessionType(session)) {
                logger.debug("Session type does not match the transport type for the request.");
                response.setStatusCode(HttpStatus.NOT_FOUND);
                return;
            }
        }
        if (transportType.sendsNoCacheInstruction()) {
            addNoCacheHeaders(response);
        }
        if (transportType.supportsCors() && !checkOrigin(request, response)) {
            return;
        }
        transportHandler.handleRequest(request, response, handler, session);
        if (isNewSession && (response instanceof ServletServerHttpResponse)) {
            int status = ((ServletServerHttpResponse) response).getServletResponse().getStatus();
            if (HttpStatus.valueOf(status).is4xxClientError()) {
                this.sessions.remove(sessionId);
            }
        }
        chain.applyAfterHandshake(request, response, null);
    } catch (SockJsException ex) {
        failure = ex;
    } catch (Exception ex) {
        failure = new SockJsException("Uncaught failure for request " + request.getURI(), sessionId, ex);
    } finally {
        if (failure != null) {
            chain.applyAfterHandshake(request, response, failure);
            throw failure;
        }
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) SockJsException(org.springframework.web.socket.sockjs.SockJsException) IOException(java.io.IOException) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) SockJsException(org.springframework.web.socket.sockjs.SockJsException) HandshakeInterceptorChain(org.springframework.web.socket.server.support.HandshakeInterceptorChain) HttpMethod(org.springframework.http.HttpMethod) Principal(java.security.Principal)

Aggregations

ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)40 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)22 ServerHttpResponse (org.springframework.http.server.ServerHttpResponse)8 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)8 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)7 Test (org.junit.jupiter.api.Test)7 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)7 IOException (java.io.IOException)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)5 MediaType (org.springframework.http.MediaType)4 ResponseEntity (org.springframework.http.ResponseEntity)4 HashMap (java.util.HashMap)3 List (java.util.List)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 HttpHeaders (org.springframework.http.HttpHeaders)3 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)3 ServletException (jakarta.servlet.ServletException)2 ServletRequest (jakarta.servlet.ServletRequest)2