Example usage for org.springframework.web.server ServerWebExchange getRequest

List of usage examples for org.springframework.web.server ServerWebExchange getRequest

Introduction

In this page you can find the example usage for org.springframework.web.server ServerWebExchange getRequest.

Prototype

ServerHttpRequest getRequest();

Source Link

Document

Return the current HTTP request.

Usage

From source file:org.springframework.web.reactive.socket.server.support.HandshakeWebSocketService.java

@Override
public Mono<Void> handleRequest(ServerWebExchange exchange, WebSocketHandler handler) {
    ServerHttpRequest request = exchange.getRequest();
    HttpMethod method = request.getMethod();
    HttpHeaders headers = request.getHeaders();

    if (logger.isDebugEnabled()) {
        logger.debug("Handling " + request.getURI() + " with headers: " + headers);
    }// ww w.jav  a  2s .com

    if (HttpMethod.GET != method) {
        return Mono.error(
                new MethodNotAllowedException(request.getMethodValue(), Collections.singleton(HttpMethod.GET)));
    }

    if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) {
        return handleBadRequest("Invalid 'Upgrade' header: " + headers);
    }

    List<String> connectionValue = headers.getConnection();
    if (!connectionValue.contains("Upgrade") && !connectionValue.contains("upgrade")) {
        return handleBadRequest("Invalid 'Connection' header: " + headers);
    }

    String key = headers.getFirst(SEC_WEBSOCKET_KEY);
    if (key == null) {
        return handleBadRequest("Missing \"Sec-WebSocket-Key\" header");
    }

    String protocol = selectProtocol(headers, handler);
    return this.upgradeStrategy.upgrade(exchange, handler, protocol);
}

From source file:org.springframework.web.server.handler.ResponseStatusExceptionHandler.java

@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
    if (ex instanceof ResponseStatusException) {
        HttpStatus status = ((ResponseStatusException) ex).getStatus();
        if (exchange.getResponse().setStatusCode(status)) {
            if (status.is5xxServerError()) {
                logger.error(buildMessage(exchange.getRequest(), ex));
            } else if (status == HttpStatus.BAD_REQUEST) {
                logger.warn(buildMessage(exchange.getRequest(), ex));
            } else {
                logger.trace(buildMessage(exchange.getRequest(), ex));
            }/* w  w w  .  j av a2 s . c  om*/
            return exchange.getResponse().setComplete();
        }
    }
    return Mono.error(ex);
}