1 16 17 package org.springframework.web.servlet.handler; 18 19 import java.util.Collections ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import javax.servlet.http.HttpServletRequest ; 25 26 import org.springframework.beans.BeansException; 27 import org.springframework.util.AntPathMatcher; 28 import org.springframework.util.Assert; 29 import org.springframework.util.PathMatcher; 30 import org.springframework.web.servlet.HandlerMapping; 31 import org.springframework.web.util.UrlPathHelper; 32 33 54 public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping { 55 56 private UrlPathHelper urlPathHelper = new UrlPathHelper(); 57 58 private PathMatcher pathMatcher = new AntPathMatcher(); 59 60 private Object rootHandler; 61 62 private boolean lazyInitHandlers = false; 63 64 private final Map handlerMap = new HashMap (); 65 66 67 74 public void setAlwaysUseFullPath(boolean alwaysUseFullPath) { 75 this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath); 76 } 77 78 88 public void setUrlDecode(boolean urlDecode) { 89 this.urlPathHelper.setUrlDecode(urlDecode); 90 } 91 92 99 public void setUrlPathHelper(UrlPathHelper urlPathHelper) { 100 this.urlPathHelper = urlPathHelper; 101 } 102 103 108 public void setPathMatcher(PathMatcher pathMatcher) { 109 Assert.notNull(pathMatcher, "PathMatcher must not be null"); 110 this.pathMatcher = pathMatcher; 111 } 112 113 118 public void setRootHandler(Object rootHandler) { 119 this.rootHandler = rootHandler; 120 } 121 122 126 public Object getRootHandler() { 127 return this.rootHandler; 128 } 129 130 140 public void setLazyInitHandlers(boolean lazyInitHandlers) { 141 this.lazyInitHandlers = lazyInitHandlers; 142 } 143 144 145 150 protected Object getHandlerInternal(HttpServletRequest request) throws Exception { 151 String lookupPath = this.urlPathHelper.getLookupPathForRequest(request); 152 if (logger.isDebugEnabled()) { 153 logger.debug("Looking up handler for [" + lookupPath + "]"); 154 } 155 Object handler = lookupHandler(lookupPath, request); 156 if (handler == null) { 157 if ("/".equals(lookupPath)) { 160 handler = getRootHandler(); 161 } 162 if (handler == null) { 163 handler = getDefaultHandler(); 164 } 165 if (handler != null) { 166 exposePathWithinMapping(lookupPath, request); 167 } 168 } 169 return handler; 170 } 171 172 185 protected Object lookupHandler(String urlPath, HttpServletRequest request) { 186 Object handler = this.handlerMap.get(urlPath); 188 if (handler != null) { 189 exposePathWithinMapping(urlPath, request); 190 return handler; 191 } 192 String bestPathMatch = null; 194 for (Iterator it = this.handlerMap.keySet().iterator(); it.hasNext();) { 195 String registeredPath = (String ) it.next(); 196 if (this.pathMatcher.match(registeredPath, urlPath) && 197 (bestPathMatch == null || bestPathMatch.length() <= registeredPath.length())) { 198 bestPathMatch = registeredPath; 199 } 200 } 201 if (bestPathMatch != null) { 202 handler = this.handlerMap.get(bestPathMatch); 203 exposePathWithinMapping(this.pathMatcher.extractPathWithinPattern(bestPathMatch, urlPath), request); 204 } 205 return handler; 206 } 207 208 214 protected void exposePathWithinMapping(String pathWithinMapping, HttpServletRequest request) { 215 request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, pathWithinMapping); 216 } 217 218 219 226 protected void registerHandler(String [] urlPaths, String beanName) throws BeansException, IllegalStateException { 227 Assert.notNull(urlPaths, "URL path array must not be null"); 228 for (int j = 0; j < urlPaths.length; j++) { 229 registerHandler(urlPaths[j], beanName); 230 } 231 } 232 233 241 protected void registerHandler(String urlPath, Object handler) throws BeansException, IllegalStateException { 242 Assert.notNull(urlPath, "URL path must not be null"); 243 Assert.notNull(handler, "Handler object must not be null"); 244 245 Object mappedHandler = this.handlerMap.get(urlPath); 246 if (mappedHandler != null) { 247 throw new IllegalStateException ( 248 "Cannot map handler [" + handler + "] to URL path [" + urlPath + 249 "]: There is already handler [" + mappedHandler + "] mapped."); 250 } 251 252 if (!this.lazyInitHandlers && handler instanceof String ) { 254 String handlerName = (String ) handler; 255 if (getApplicationContext().isSingleton(handlerName)) { 256 handler = getApplicationContext().getBean(handlerName); 257 } 258 } 259 260 if (urlPath.equals("/")) { 261 if (logger.isDebugEnabled()) { 262 logger.debug("Root mapping to handler [" + handler + "]"); 263 } 264 setRootHandler(handler); 265 } 266 else if (urlPath.equals("/*")) { 267 if (logger.isDebugEnabled()) { 268 logger.debug("Default mapping to handler [" + handler + "]"); 269 } 270 setDefaultHandler(handler); 271 } 272 else { 273 this.handlerMap.put(urlPath, handler); 274 if (logger.isDebugEnabled()) { 275 logger.debug("Mapped URL path [" + urlPath + "] onto handler [" + handler + "]"); 276 } 277 } 278 } 279 280 281 287 public final Map getHandlerMap() { 288 return Collections.unmodifiableMap(this.handlerMap); 289 } 290 291 } 292 | Popular Tags |