Example usage for org.springframework.web.server MethodNotAllowedException MethodNotAllowedException

List of usage examples for org.springframework.web.server MethodNotAllowedException MethodNotAllowedException

Introduction

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

Prototype

public MethodNotAllowedException(String method, @Nullable Collection<HttpMethod> supportedMethods) 

Source Link

Usage

From source file:org.springframework.web.reactive.resource.ResourceWebHandler.java

/**
 * Processes a resource request./*from  ww  w .  j a v a2 s.  c o  m*/
 * <p>Checks for the existence of the requested resource in the configured list of locations.
 * If the resource does not exist, a {@code 404} response will be returned to the client.
 * If the resource exists, the request will be checked for the presence of the
 * {@code Last-Modified} header, and its value will be compared against the last-modified
 * timestamp of the given resource, returning a {@code 304} status code if the
 * {@code Last-Modified} value  is greater. If the resource is newer than the
 * {@code Last-Modified} value, or the header is not present, the content resource
 * of the resource will be written to the response with caching headers
 * set to expire one year in the future.
 */
@Override
public Mono<Void> handle(ServerWebExchange exchange) {
    return getResource(exchange).switchIfEmpty(Mono.defer(() -> {
        logger.trace("No matching resource found - returning 404");
        return Mono.error(NOT_FOUND_EXCEPTION);
    })).flatMap(resource -> {
        try {
            if (HttpMethod.OPTIONS.matches(exchange.getRequest().getMethodValue())) {
                exchange.getResponse().getHeaders().add("Allow", "GET,HEAD,OPTIONS");
                return Mono.empty();
            }

            // Supported methods and required session
            HttpMethod httpMethod = exchange.getRequest().getMethod();
            if (!SUPPORTED_METHODS.contains(httpMethod)) {
                return Mono.error(new MethodNotAllowedException(exchange.getRequest().getMethodValue(),
                        SUPPORTED_METHODS));
            }

            // Header phase
            if (exchange.checkNotModified(Instant.ofEpochMilli(resource.lastModified()))) {
                logger.trace("Resource not modified - returning 304");
                return Mono.empty();
            }

            // Apply cache settings, if any
            if (getCacheControl() != null) {
                String value = getCacheControl().getHeaderValue();
                if (value != null) {
                    exchange.getResponse().getHeaders().setCacheControl(value);
                }
            }

            // Check the media type for the resource
            MediaType mediaType = MediaTypeFactory.getMediaType(resource).orElse(null);
            if (mediaType != null) {
                if (logger.isTraceEnabled()) {
                    logger.trace("Determined media type '" + mediaType + "' for " + resource);
                }
            } else {
                if (logger.isTraceEnabled()) {
                    logger.trace("No media type found " + "for " + resource
                            + " - not sending a content-type header");
                }
            }

            // Content phase
            if (HttpMethod.HEAD.matches(exchange.getRequest().getMethodValue())) {
                setHeaders(exchange, resource, mediaType);
                exchange.getResponse().getHeaders().set(HttpHeaders.ACCEPT_RANGES, "bytes");
                logger.trace("HEAD request - skipping content");
                return Mono.empty();
            }

            setHeaders(exchange, resource, mediaType);
            ResourceHttpMessageWriter writer = getResourceHttpMessageWriter();
            Assert.state(writer != null, "No ResourceHttpMessageWriter");
            return writer.write(Mono.just(resource), null, ResolvableType.forClass(Resource.class), mediaType,
                    exchange.getRequest(), exchange.getResponse(), Collections.emptyMap());
        } catch (IOException ex) {
            return Mono.error(ex);
        }
    });
}

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);
    }/*  w w  w .ja v  a  2  s.  c o  m*/

    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);
}