001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.reactive;
018
019import java.util.function.Function;
020
021import reactor.core.publisher.Mono;
022
023import org.springframework.web.server.ServerWebExchange;
024
025/**
026 * Contract that decouples the {@link DispatcherHandler} from the details of
027 * invoking a handler and makes it possible to support any handler type.
028 *
029 * @author Rossen Stoyanchev
030 * @author Sebastien Deleuze
031 * @since 5.0
032 */
033public interface HandlerAdapter {
034
035        /**
036         * Whether this {@code HandlerAdapter} supports the given {@code handler}.
037         * @param handler the handler object to check
038         * @return whether or not the handler is supported
039         */
040        boolean supports(Object handler);
041
042        /**
043         * Handle the request with the given handler.
044         * <p>Implementations are encouraged to handle exceptions resulting from the
045         * invocation of a handler in order and if necessary to return an alternate
046         * result that represents an error response.
047         * <p>Furthermore since an async {@code HandlerResult} may produce an error
048         * later during result handling implementations are also encouraged to
049         * {@link HandlerResult#setExceptionHandler(Function) set an exception
050         * handler} on the {@code HandlerResult} so that may also be applied later
051         * after result handling.
052         * @param exchange current server exchange
053         * @param handler the selected handler which must have been previously
054         * checked via {@link #supports(Object)}
055         * @return {@link Mono} that emits a single {@code HandlerResult} or none if
056         * the request has been fully handled and doesn't require further handling.
057         */
058        Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler);
059
060}