KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > handler > AbstractHandlerMapping


1 /*
2  * Copyright 2002-2007 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.handler;
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20
21 import org.springframework.beans.BeansException;
22 import org.springframework.core.Ordered;
23 import org.springframework.web.context.request.WebRequestInterceptor;
24 import org.springframework.web.context.support.WebApplicationObjectSupport;
25 import org.springframework.web.servlet.HandlerExecutionChain;
26 import org.springframework.web.servlet.HandlerInterceptor;
27 import org.springframework.web.servlet.HandlerMapping;
28
29 /**
30  * Abstract base class for {@link org.springframework.web.servlet.HandlerMapping}
31  * implementations. Supports ordering, a default handler, and handler interceptors.
32  *
33  * <p>Note: This base class does <i>not</i> support exposure of the
34  * {@link #PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE}. Support for this attribute
35  * is up to concrete subclasses, typically based on request URL mappings.
36  *
37  * @author Juergen Hoeller
38  * @since 07.04.2003
39  * @see #getHandlerInternal
40  * @see #setDefaultHandler
41  * @see #setInterceptors
42  * @see org.springframework.web.servlet.HandlerInterceptor
43  */

44 public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
45     implements HandlerMapping, Ordered {
46
47     private int order = Integer.MAX_VALUE; // default: same as non-Ordered
48

49     private Object JavaDoc defaultHandler;
50
51     private Object JavaDoc[] interceptors;
52
53     private HandlerInterceptor[] adaptedInterceptors;
54
55
56     public final void setOrder(int order) {
57       this.order = order;
58     }
59
60     public final int getOrder() {
61       return this.order;
62     }
63
64     /**
65      * Set the default handler for this handler mapping.
66      * This handler will be returned if no specific mapping was found.
67      * <p>Default is <code>null</code>, indicating no default handler.
68      */

69     public void setDefaultHandler(Object JavaDoc defaultHandler) {
70         this.defaultHandler = defaultHandler;
71     }
72
73     /**
74      * Return the default handler for this handler mapping,
75      * or <code>null</code> if none.
76      */

77     public Object JavaDoc getDefaultHandler() {
78         return this.defaultHandler;
79     }
80
81     /**
82      * Set the interceptors to apply for all handlers mapped by this handler mapping.
83      * <p>Supported interceptor types are HandlerInterceptor and WebRequestInterceptor.
84      * @param interceptors array of handler interceptors, or <code>null</code> if none
85      * @see #adaptInterceptor
86      * @see org.springframework.web.servlet.HandlerInterceptor
87      * @see org.springframework.web.context.request.WebRequestInterceptor
88      */

89     public final void setInterceptors(Object JavaDoc[] interceptors) {
90         this.interceptors = interceptors;
91     }
92
93
94     /**
95      * Calls the <code>initInterceptors()</code> method.
96      * @see #initInterceptors()
97      */

98     protected void initApplicationContext() throws BeansException {
99         initInterceptors();
100     }
101
102     /**
103      * Initialize the specified interceptors, adapting them where necessary.
104      * @see #setInterceptors
105      * @see #adaptInterceptor
106      */

107     protected void initInterceptors() {
108         if (this.interceptors != null) {
109             this.adaptedInterceptors = new HandlerInterceptor[this.interceptors.length];
110             for (int i = 0; i < this.interceptors.length; i++) {
111                 if (this.interceptors[i] == null) {
112                     throw new IllegalArgumentException JavaDoc("Entry number " + i + " in interceptors array is null");
113                 }
114                 this.adaptedInterceptors[i] = adaptInterceptor(this.interceptors[i]);
115             }
116         }
117     }
118
119     /**
120      * Adapt the given interceptor object to the HandlerInterceptor interface.
121      * <p>Supported interceptor types are HandlerInterceptor and WebRequestInterceptor.
122      * Each given WebRequestInterceptor will be wrapped in a WebRequestHandlerInterceptorAdapter.
123      * Can be overridden in subclasses.
124      * @param interceptor the specified interceptor object
125      * @return the interceptor wrapped as HandlerInterceptor
126      * @see org.springframework.web.servlet.HandlerInterceptor
127      * @see org.springframework.web.context.request.WebRequestInterceptor
128      * @see WebRequestHandlerInterceptorAdapter
129      */

130     protected HandlerInterceptor adaptInterceptor(Object JavaDoc interceptor) {
131         if (interceptor instanceof HandlerInterceptor) {
132             return (HandlerInterceptor) interceptor;
133         }
134         else if (interceptor instanceof WebRequestInterceptor) {
135             return new WebRequestHandlerInterceptorAdapter((WebRequestInterceptor) interceptor);
136         }
137         else {
138             throw new IllegalArgumentException JavaDoc("Interceptor type not supported: " + interceptor.getClass().getName());
139         }
140     }
141
142     /**
143      * Return the adapted interceptors as HandlerInterceptor array.
144      * @return the array of HandlerInterceptors, or <code>null</code> if none
145      */

146     protected final HandlerInterceptor[] getAdaptedInterceptors() {
147         return this.adaptedInterceptors;
148     }
149
150
151     /**
152      * Look up a handler for the given request, falling back to the default
153      * handler if no specific one is found.
154      * @param request current HTTP request
155      * @return the corresponding handler instance, or the default handler
156      * @see #getHandlerInternal
157      */

158     public final HandlerExecutionChain getHandler(HttpServletRequest JavaDoc request) throws Exception JavaDoc {
159         Object JavaDoc handler = getHandlerInternal(request);
160         if (handler == null) {
161             handler = getDefaultHandler();
162         }
163         if (handler == null) {
164             return null;
165         }
166         // Bean name or resolved handler?
167
if (handler instanceof String JavaDoc) {
168             String JavaDoc handlerName = (String JavaDoc) handler;
169             handler = getApplicationContext().getBean(handlerName);
170         }
171         return getHandlerExecutionChain(handler, request);
172     }
173
174     /**
175      * Look up a handler for the given request, returning <code>null</code> if no
176      * specific one is found. This method is called by <code>getHandler<code>;
177      * a <code>null</code> return value will lead to the default handler, if one is set.
178      * @param request current HTTP request
179      * @return the corresponding handler instance, or <code>null</code> if none found
180      * @throws Exception if there is an internal error
181      */

182     protected abstract Object JavaDoc getHandlerInternal(HttpServletRequest JavaDoc request) throws Exception JavaDoc;
183
184     /**
185      * Build a HandlerExecutionChain for the given handler, including applicable interceptors.
186      * <p>The default implementation simply builds a standard HandlerExecutionChain with
187      * the given handler and this handler mapping's common interceptors. Subclasses may
188      * override this in order to extend/rearrange the list of interceptors.
189      * @param handler the resolved handler instance (never <code>null</code>)
190      * @param request current HTTP request
191      * @return the HandlerExecutionChain (never <code>null</code>)
192      * @see #getAdaptedInterceptors()
193      */

194     protected HandlerExecutionChain getHandlerExecutionChain(Object JavaDoc handler, HttpServletRequest JavaDoc request) {
195         return new HandlerExecutionChain(handler, getAdaptedInterceptors());
196     }
197
198 }
199
Popular Tags