001/*
002 * Copyright 2002-2018 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.servlet;
018
019import javax.servlet.http.HttpServletRequest;
020import javax.servlet.http.HttpServletResponse;
021
022import org.springframework.lang.Nullable;
023
024/**
025 * MVC framework SPI, allowing parameterization of the core MVC workflow.
026 *
027 * <p>Interface that must be implemented for each handler type to handle a request.
028 * This interface is used to allow the {@link DispatcherServlet} to be indefinitely
029 * extensible. The {@code DispatcherServlet} accesses all installed handlers through
030 * this interface, meaning that it does not contain code specific to any handler type.
031 *
032 * <p>Note that a handler can be of type {@code Object}. This is to enable
033 * handlers from other frameworks to be integrated with this framework without
034 * custom coding, as well as to allow for annotation-driven handler objects that
035 * do not obey any specific Java interface.
036 *
037 * <p>This interface is not intended for application developers. It is available
038 * to handlers who want to develop their own web workflow.
039 *
040 * <p>Note: {@code HandlerAdapter} implementors may implement the {@link
041 * org.springframework.core.Ordered} interface to be able to specify a sorting
042 * order (and thus a priority) for getting applied by the {@code DispatcherServlet}.
043 * Non-Ordered instances get treated as lowest priority.
044 *
045 * @author Rod Johnson
046 * @author Juergen Hoeller
047 * @see org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
048 * @see org.springframework.web.servlet.handler.SimpleServletHandlerAdapter
049 */
050public interface HandlerAdapter {
051
052        /**
053         * Given a handler instance, return whether or not this {@code HandlerAdapter}
054         * can support it. Typical HandlerAdapters will base the decision on the handler
055         * type. HandlerAdapters will usually only support one handler type each.
056         * <p>A typical implementation:
057         * <p>{@code
058         * return (handler instanceof MyHandler);
059         * }
060         * @param handler the handler object to check
061         * @return whether or not this object can use the given handler
062         */
063        boolean supports(Object handler);
064
065        /**
066         * Use the given handler to handle this request.
067         * The workflow that is required may vary widely.
068         * @param request current HTTP request
069         * @param response current HTTP response
070         * @param handler the handler to use. This object must have previously been passed
071         * to the {@code supports} method of this interface, which must have
072         * returned {@code true}.
073         * @throws Exception in case of errors
074         * @return a ModelAndView object with the name of the view and the required
075         * model data, or {@code null} if the request has been handled directly
076         */
077        @Nullable
078        ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;
079
080        /**
081         * Same contract as for HttpServlet's {@code getLastModified} method.
082         * Can simply return -1 if there's no support in the handler class.
083         * @param request current HTTP request
084         * @param handler the handler to use
085         * @return the lastModified value for the given handler
086         * @see javax.servlet.http.HttpServlet#getLastModified
087         * @see org.springframework.web.servlet.mvc.LastModified#getLastModified
088         */
089        long getLastModified(HttpServletRequest request, Object handler);
090
091}