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