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