View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.web.servlet.mvc.method;
18  
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
21  
22  import org.springframework.core.Ordered;
23  import org.springframework.web.method.HandlerMethod;
24  import org.springframework.web.servlet.HandlerAdapter;
25  import org.springframework.web.servlet.ModelAndView;
26  import org.springframework.web.servlet.support.WebContentGenerator;
27  
28  /**
29   * Abstract base class for {@link HandlerAdapter} implementations that support
30   * handlers of type {@link HandlerMethod}.
31   *
32   * @author Arjen Poutsma
33   * @since 3.1
34   */
35  public abstract class AbstractHandlerMethodAdapter extends WebContentGenerator implements HandlerAdapter, Ordered {
36  
37  	private int order = Ordered.LOWEST_PRECEDENCE;
38  
39  
40  	public AbstractHandlerMethodAdapter() {
41  		// no restriction of HTTP methods by default
42  		super(false);
43  	}
44  
45  
46  	/**
47  	 * Specify the order value for this HandlerAdapter bean.
48  	 * <p>Default value is {@code Integer.MAX_VALUE}, meaning that it's non-ordered.
49  	 * @see org.springframework.core.Ordered#getOrder()
50  	 */
51  	public void setOrder(int order) {
52  		this.order = order;
53  	}
54  
55  	@Override
56  	public int getOrder() {
57  		return this.order;
58  	}
59  
60  
61  	/**
62  	 * This implementation expects the handler to be an {@link HandlerMethod}.
63  	 * @param handler the handler instance to check
64  	 * @return whether or not this adapter can adapt the given handler
65  	 */
66  	@Override
67  	public final boolean supports(Object handler) {
68  		return (handler instanceof HandlerMethod && supportsInternal((HandlerMethod) handler));
69  	}
70  
71  	/**
72  	 * Given a handler method, return whether or not this adapter can support it.
73  	 * @param handlerMethod the handler method to check
74  	 * @return whether or not this adapter can adapt the given method
75  	 */
76  	protected abstract boolean supportsInternal(HandlerMethod handlerMethod);
77  
78  	/**
79  	 * This implementation expects the handler to be an {@link HandlerMethod}.
80  	 */
81  	@Override
82  	public final ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
83  			throws Exception {
84  
85  		return handleInternal(request, response, (HandlerMethod) handler);
86  	}
87  
88  	/**
89  	 * Use the given handler method to handle the request.
90  	 * @param request current HTTP request
91  	 * @param response current HTTP response
92  	 * @param handlerMethod handler method to use. This object must have previously been passed to the
93  	 * {@link #supportsInternal(HandlerMethod)} this interface, which must have returned {@code true}.
94  	 * @return ModelAndView object with the name of the view and the required model data,
95  	 * or {@code null} if the request has been handled directly
96  	 * @throws Exception in case of errors
97  	 */
98  	protected abstract ModelAndView handleInternal(HttpServletRequest request,
99  			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 }