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.mvc.method;
018
019import javax.servlet.http.HttpServletRequest;
020import javax.servlet.http.HttpServletResponse;
021
022import org.springframework.core.Ordered;
023import org.springframework.web.method.HandlerMethod;
024import org.springframework.web.servlet.HandlerAdapter;
025import org.springframework.web.servlet.ModelAndView;
026import org.springframework.web.servlet.support.WebContentGenerator;
027
028/**
029 * Abstract base class for {@link HandlerAdapter} implementations that support
030 * handlers of type {@link HandlerMethod}.
031 *
032 * @author Arjen Poutsma
033 * @since 3.1
034 */
035public abstract class AbstractHandlerMethodAdapter extends WebContentGenerator implements HandlerAdapter, Ordered {
036
037        private int order = Ordered.LOWEST_PRECEDENCE;
038
039
040        public AbstractHandlerMethodAdapter() {
041                // no restriction of HTTP methods by default
042                super(false);
043        }
044
045
046        /**
047         * Specify the order value for this HandlerAdapter bean.
048         * <p>The default value is {@code Ordered.LOWEST_PRECEDENCE}, meaning non-ordered.
049         * @see org.springframework.core.Ordered#getOrder()
050         */
051        public void setOrder(int order) {
052                this.order = order;
053        }
054
055        @Override
056        public int getOrder() {
057                return this.order;
058        }
059
060
061        /**
062         * This implementation expects the handler to be an {@link HandlerMethod}.
063         * @param handler the handler instance to check
064         * @return whether or not this adapter can adapt the given handler
065         */
066        @Override
067        public final boolean supports(Object handler) {
068                return (handler instanceof HandlerMethod && supportsInternal((HandlerMethod) handler));
069        }
070
071        /**
072         * Given a handler method, return whether or not this adapter can support it.
073         * @param handlerMethod the handler method to check
074         * @return whether or not this adapter can adapt the given method
075         */
076        protected abstract boolean supportsInternal(HandlerMethod handlerMethod);
077
078        /**
079         * This implementation expects the handler to be an {@link HandlerMethod}.
080         */
081        @Override
082        public final ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
083                        throws Exception {
084
085                return handleInternal(request, response, (HandlerMethod) handler);
086        }
087
088        /**
089         * Use the given handler method to handle the request.
090         * @param request current HTTP request
091         * @param response current HTTP response
092         * @param handlerMethod handler method to use. This object must have previously been passed to the
093         * {@link #supportsInternal(HandlerMethod)} this interface, which must have returned {@code true}.
094         * @return ModelAndView object with the name of the view and the required model data,
095         * or {@code null} if the request has been handled directly
096         * @throws Exception in case of errors
097         */
098        protected abstract ModelAndView handleInternal(HttpServletRequest request,
099                        HttpServletResponse response, HandlerMethod handlerMethod) throws Exception;
100
101        /**
102         * This implementation expects the handler to be an {@link HandlerMethod}.
103         */
104        @Override
105        public final long getLastModified(HttpServletRequest request, Object handler) {
106                return getLastModifiedInternal(request, (HandlerMethod) handler);
107        }
108
109        /**
110         * Same contract as for {@link javax.servlet.http.HttpServlet#getLastModified(HttpServletRequest)}.
111         * @param request current HTTP request
112         * @param handlerMethod handler method to use
113         * @return the lastModified value for the given handler
114         */
115        protected abstract long getLastModifiedInternal(HttpServletRequest request, HandlerMethod handlerMethod);
116
117}