001/*
002 * Copyright 2002-2012 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.ui.ModelMap;
020
021/**
022 * Interface for general web request interception. Allows for being applied
023 * to Servlet request as well as Portlet request environments, by building
024 * 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 * <p><b>NOTE:</b> While this interceptor is applied to the entire request processing
043 * in a Servlet environment, it is by default only applied to the <i>render</i> phase
044 * in a Portlet environment, preparing and rendering a Portlet view. To apply
045 * WebRequestInterceptors to the <i>action</i> phase as well, set the HandlerMapping's
046 * "applyWebRequestInterceptorsToRenderPhaseOnly" flag to "false". Alternatively,
047 * consider using the Portlet-specific HandlerInterceptor mechanism for such needs.
048 *
049 * @author Juergen Hoeller
050 * @since 2.0
051 * @see ServletWebRequest
052 * @see org.springframework.web.servlet.DispatcherServlet
053 * @see org.springframework.web.servlet.handler.AbstractHandlerMapping#setInterceptors
054 * @see org.springframework.web.servlet.HandlerInterceptor
055 * @see org.springframework.web.portlet.context.PortletWebRequest
056 * @see org.springframework.web.portlet.DispatcherPortlet
057 * @see org.springframework.web.portlet.handler.AbstractHandlerMapping#setInterceptors
058 * @see org.springframework.web.portlet.handler.AbstractHandlerMapping#setApplyWebRequestInterceptorsToRenderPhaseOnly
059 * @see org.springframework.web.portlet.HandlerInterceptor
060 */
061public interface WebRequestInterceptor {
062
063        /**
064         * Intercept the execution of a request handler <i>before</i> its invocation.
065         * <p>Allows for preparing context resources (such as a Hibernate Session)
066         * and expose them as request attributes or as thread-local objects.
067         * @param request the current web request
068         * @throws Exception in case of errors
069         */
070        void preHandle(WebRequest request) throws Exception;
071
072        /**
073         * Intercept the execution of a request handler <i>after</i> its successful
074         * invocation, right before view rendering (if any).
075         * <p>Allows for modifying context resources after successful handler
076         * execution (for example, flushing a Hibernate Session).
077         * @param request the current web request
078         * @param model the map of model objects that will be exposed to the view
079         * (may be {@code null}). Can be used to analyze the exposed model
080         * and/or to add further model attributes, if desired.
081         * @throws Exception in case of errors
082         */
083        void postHandle(WebRequest request, ModelMap model) throws Exception;
084
085        /**
086         * Callback after completion of request processing, that is, after rendering
087         * the view. Will be called on any outcome of handler execution, thus allows
088         * for proper resource cleanup.
089         * <p>Note: Will only be called if this interceptor's {@code preHandle}
090         * method has successfully completed!
091         * @param request the current web request
092         * @param ex exception thrown on handler execution, if any
093         * @throws Exception in case of errors
094         */
095        void afterCompletion(WebRequest request, Exception ex) throws Exception;
096
097}