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.context.request;
018
019import org.springframework.lang.Nullable;
020import org.springframework.ui.ModelMap;
021
022/**
023 * Interface for general web request interception. Allows for being applied
024 * to Servlet request by building on the {@link WebRequest} abstraction.
025 *
026 * <p>This interface assumes MVC-style request processing: A handler gets executed,
027 * exposes a set of model objects, then a view gets rendered based on that model.
028 * Alternatively, a handler may also process the request completely, with no
029 * view to be rendered.
030 *
031 * <p>In an async processing scenario, the handler may be executed in a separate
032 * thread while the main thread exits without rendering or invoking the
033 * {@code postHandle} and {@code afterCompletion} callbacks. When concurrent
034 * handler execution completes, the request is dispatched back in order to
035 * proceed with rendering the model and all methods of this contract are invoked
036 * again. For further options and comments see
037 * {@code org.springframework.web.context.request.async.AsyncWebRequestInterceptor}
038 *
039 * <p>This interface is deliberately minimalistic to keep the dependencies of
040 * generic request interceptors as minimal as feasible.
041 *
042 * @author Juergen Hoeller
043 * @since 2.0
044 * @see ServletWebRequest
045 * @see org.springframework.web.servlet.DispatcherServlet
046 * @see org.springframework.web.servlet.handler.AbstractHandlerMapping#setInterceptors
047 * @see org.springframework.web.servlet.HandlerInterceptor
048 */
049public interface WebRequestInterceptor {
050
051        /**
052         * Intercept the execution of a request handler <i>before</i> its invocation.
053         * <p>Allows for preparing context resources (such as a Hibernate Session)
054         * and expose them as request attributes or as thread-local objects.
055         * @param request the current web request
056         * @throws Exception in case of errors
057         */
058        void preHandle(WebRequest request) throws Exception;
059
060        /**
061         * Intercept the execution of a request handler <i>after</i> its successful
062         * invocation, right before view rendering (if any).
063         * <p>Allows for modifying context resources after successful handler
064         * execution (for example, flushing a Hibernate Session).
065         * @param request the current web request
066         * @param model the map of model objects that will be exposed to the view
067         * (may be {@code null}). Can be used to analyze the exposed model
068         * and/or to add further model attributes, if desired.
069         * @throws Exception in case of errors
070         */
071        void postHandle(WebRequest request, @Nullable ModelMap model) throws Exception;
072
073        /**
074         * Callback after completion of request processing, that is, after rendering
075         * the view. Will be called on any outcome of handler execution, thus allows
076         * for proper resource cleanup.
077         * <p>Note: Will only be called if this interceptor's {@code preHandle}
078         * method has successfully completed!
079         * @param request the current web request
080         * @param ex exception thrown on handler execution, if any
081         * @throws Exception in case of errors
082         */
083        void afterCompletion(WebRequest request, @Nullable Exception ex) throws Exception;
084
085}