1 16 17 package org.springframework.web.servlet.handler; 18 19 import javax.servlet.http.HttpServletRequest ; 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 44 public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport 45 implements HandlerMapping, Ordered { 46 47 private int order = Integer.MAX_VALUE; 49 private Object defaultHandler; 50 51 private Object [] 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 69 public void setDefaultHandler(Object defaultHandler) { 70 this.defaultHandler = defaultHandler; 71 } 72 73 77 public Object getDefaultHandler() { 78 return this.defaultHandler; 79 } 80 81 89 public final void setInterceptors(Object [] interceptors) { 90 this.interceptors = interceptors; 91 } 92 93 94 98 protected void initApplicationContext() throws BeansException { 99 initInterceptors(); 100 } 101 102 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 ("Entry number " + i + " in interceptors array is null"); 113 } 114 this.adaptedInterceptors[i] = adaptInterceptor(this.interceptors[i]); 115 } 116 } 117 } 118 119 130 protected HandlerInterceptor adaptInterceptor(Object 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 ("Interceptor type not supported: " + interceptor.getClass().getName()); 139 } 140 } 141 142 146 protected final HandlerInterceptor[] getAdaptedInterceptors() { 147 return this.adaptedInterceptors; 148 } 149 150 151 158 public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception { 159 Object handler = getHandlerInternal(request); 160 if (handler == null) { 161 handler = getDefaultHandler(); 162 } 163 if (handler == null) { 164 return null; 165 } 166 if (handler instanceof String ) { 168 String handlerName = (String ) handler; 169 handler = getApplicationContext().getBean(handlerName); 170 } 171 return getHandlerExecutionChain(handler, request); 172 } 173 174 182 protected abstract Object getHandlerInternal(HttpServletRequest request) throws Exception ; 183 184 194 protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request) { 195 return new HandlerExecutionChain(handler, getAdaptedInterceptors()); 196 } 197 198 } 199 | Popular Tags |