/** * Return the handler methods for the given mapping name. * @param mappingName the mapping name * @return a list of matching HandlerMethod's or {@code null}; the returned * list will never be modified and is safe to iterate. * @see #setHandlerMethodMappingNamingStrategy */ @Nullable public List<HandlerMethod> getHandlerMethodsForMappingName(String mappingName) { return this.mappingRegistry.getHandlerMethodsByMappingName(mappingName); }
protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception { List<Match> matches = new ArrayList<>(); List<T> directPathMatches = this.mappingRegistry.getMappingsByUrl(lookupPath); if (directPathMatches != null) { addMatchingMappings(directPathMatches, matches, request); addMatchingMappings(this.mappingRegistry.getMappings().keySet(), matches, request); return handleNoMatch(this.mappingRegistry.getMappings().keySet(), lookupPath, request);
/** * Return a (read-only) map with all mappings and HandlerMethod's. */ public Map<T, HandlerMethod> getHandlerMethods() { this.mappingRegistry.acquireReadLock(); try { return Collections.unmodifiableMap(this.mappingRegistry.getMappings()); } finally { this.mappingRegistry.releaseReadLock(); } }
List<T> directPathMatches = this.mappingRegistry.getMappingsByUrl(lookupPath); if (directPathMatches != null) { addMatchingMappings(directPathMatches, matches, request);
/** * Return the handler methods for the given mapping name. * @param mappingName the mapping name * @return a list of matching HandlerMethod's or {@code null}; the returned * list will never be modified and is safe to iterate. * @see #setHandlerMethodMappingNamingStrategy */ public List<HandlerMethod> getHandlerMethodsForMappingName(String mappingName) { return this.mappingRegistry.getHandlerMethodsByMappingName(mappingName); }
/** * Un-register the given mapping. * <p>This method may be invoked at runtime after initialization has completed. * @param mapping the mapping to unregister */ public void unregisterMapping(T mapping) { this.mappingRegistry.unregister(mapping); }
/** * Return a (read-only) map with all mappings and HandlerMethod's. */ public Map<T, HandlerMethod> getHandlerMethods() { this.mappingRegistry.acquireReadLock(); try { return Collections.unmodifiableMap(this.mappingRegistry.getMappings()); } finally { this.mappingRegistry.releaseReadLock(); } }
/** * Look up a handler method for the given request. */ @Override protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception { String lookupPath = getUrlPathHelper().getLookupPathForRequest(request); this.mappingRegistry.acquireReadLock(); try { HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request); return (handlerMethod != null ? handlerMethod.createWithResolvedBean() : null); } finally { this.mappingRegistry.releaseReadLock(); } }
protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception { List<Match> matches = new ArrayList<>(); List<T> directPathMatches = this.mappingRegistry.getMappingsByUrl(lookupPath); if (directPathMatches != null) { addMatchingMappings(directPathMatches, matches, request); addMatchingMappings(this.mappingRegistry.getMappings().keySet(), matches, request); return handleNoMatch(this.mappingRegistry.getMappings().keySet(), lookupPath, request);
@Override protected CorsConfiguration getCorsConfiguration(Object handler, HttpServletRequest request) { CorsConfiguration corsConfig = super.getCorsConfiguration(handler, request); if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; if (handlerMethod.equals(PREFLIGHT_AMBIGUOUS_MATCH)) { return AbstractHandlerMethodMapping.ALLOW_CORS_CONFIG; } else { CorsConfiguration corsConfigFromMethod = this.mappingRegistry.getCorsConfiguration(handlerMethod); corsConfig = (corsConfig != null ? corsConfig.combine(corsConfigFromMethod) : corsConfigFromMethod); } } return corsConfig; }
/** * Register the given mapping. * <p>This method may be invoked at runtime after initialization has completed. * @param mapping the mapping for the handler method * @param handler the handler * @param method the method */ public void registerMapping(T mapping, Object handler, Method method) { if (logger.isTraceEnabled()) { logger.trace("Register \"" + mapping + "\" to " + method.toGenericString()); } this.mappingRegistry.register(mapping, handler, method); }
private void addMatchingMappings(Collection<T> mappings, List<Match> matches, HttpServletRequest request) { for (T mapping : mappings) { T match = getMatchingMapping(mapping, request); if (match != null) { matches.add(new Match(match, this.mappingRegistry.getMappings().get(mapping))); } } }
/** * Register a handler method and its unique mapping. Invoked at startup for * each detected handler method. * @param handler the bean name of the handler or the handler instance * @param method the method to register * @param mapping the mapping conditions associated with the handler method * @throws IllegalStateException if another method was already registered * under the same mapping */ protected void registerHandlerMethod(Object handler, Method method, T mapping) { this.mappingRegistry.register(mapping, handler, method); }
/** * Un-register the given mapping. * <p>This method may be invoked at runtime after initialization has completed. * @param mapping the mapping to unregister */ public void unregisterMapping(T mapping) { if (logger.isTraceEnabled()) { logger.trace("Unregister mapping \"" + mapping + "\""); } this.mappingRegistry.unregister(mapping); }
/** * Return a (read-only) map with all mappings and HandlerMethod's. */ public Map<T, HandlerMethod> getHandlerMethods() { this.mappingRegistry.acquireReadLock(); try { return Collections.unmodifiableMap(this.mappingRegistry.getMappings()); } finally { this.mappingRegistry.releaseReadLock(); } }
@Test public void registerMappingWithSameMethodAndTwoHandlerInstances() throws Exception { String key1 = "foo"; String key2 = "bar"; MyHandler handler1 = new MyHandler(); MyHandler handler2 = new MyHandler(); HandlerMethod handlerMethod1 = new HandlerMethod(handler1, this.method1); HandlerMethod handlerMethod2 = new HandlerMethod(handler2, this.method1); this.mapping.registerMapping(key1, handler1, this.method1); this.mapping.registerMapping(key2, handler2, this.method1); // Direct URL lookup List<String> directUrlMatches = this.mapping.getMappingRegistry().getMappingsByUrl(key1); assertNotNull(directUrlMatches); assertEquals(1, directUrlMatches.size()); assertEquals(key1, directUrlMatches.get(0)); // Mapping name lookup String name = this.method1.getName(); List<HandlerMethod> handlerMethods = this.mapping.getMappingRegistry().getHandlerMethodsByMappingName(name); assertNotNull(handlerMethods); assertEquals(2, handlerMethods.size()); assertEquals(handlerMethod1, handlerMethods.get(0)); assertEquals(handlerMethod2, handlerMethods.get(1)); // CORS lookup CorsConfiguration config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod1); assertNotNull(config); assertEquals("http://" + handler1.hashCode() + name, config.getAllowedOrigins().get(0)); config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod2); assertNotNull(config); assertEquals("http://" + handler2.hashCode() + name, config.getAllowedOrigins().get(0)); }
List<String> directUrlMatches = this.mapping.getMappingRegistry().getMappingsByUrl(key1); assertNotNull(directUrlMatches); assertEquals(1, directUrlMatches.size()); List<HandlerMethod> handlerMethods = this.mapping.getMappingRegistry().getHandlerMethodsByMappingName(name1); assertNotNull(handlerMethods); assertEquals(1, handlerMethods.size()); handlerMethods = this.mapping.getMappingRegistry().getHandlerMethodsByMappingName(name2); assertNotNull(handlerMethods); assertEquals(1, handlerMethods.size()); CorsConfiguration config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod1); assertNotNull(config); assertEquals("http://" + handler.hashCode() + name1, config.getAllowedOrigins().get(0)); config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod2); assertNotNull(config); assertEquals("http://" + handler.hashCode() + name2, config.getAllowedOrigins().get(0));
/** * Look up a handler method for the given request. */ @Override protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception { String lookupPath = getUrlPathHelper().getLookupPathForRequest(request); this.mappingRegistry.acquireReadLock(); try { HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request); return (handlerMethod != null ? handlerMethod.createWithResolvedBean() : null); } finally { this.mappingRegistry.releaseReadLock(); } }
@Test public void getCorsConfigWithBeanNameHandler() throws Exception { String key = "foo"; String beanName = "handler1"; StaticWebApplicationContext context = new StaticWebApplicationContext(); context.registerSingleton(beanName, MyHandler.class); this.mapping.setApplicationContext(context); this.mapping.registerMapping(key, beanName, this.method1); HandlerMethod handlerMethod = this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key)); CorsConfiguration config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod); assertNotNull(config); assertEquals("http://" + beanName.hashCode() + this.method1.getName(), config.getAllowedOrigins().get(0)); }
/** * Register the given mapping. * <p>This method may be invoked at runtime after initialization has completed. * @param mapping the mapping for the handler method * @param handler the handler * @param method the method */ public void registerMapping(T mapping, Object handler, Method method) { if (logger.isTraceEnabled()) { logger.trace("Register \"" + mapping + "\" to " + method.toGenericString()); } this.mappingRegistry.register(mapping, handler, method); }