org.resthub.web.springmvc.router
Class RouterHandlerMapping

java.lang.Object
  extended by org.springframework.context.support.ApplicationObjectSupport
      extended by org.springframework.web.context.support.WebApplicationObjectSupport
          extended by org.springframework.web.servlet.handler.AbstractHandlerMapping
              extended by org.resthub.web.springmvc.router.RouterHandlerMapping
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.context.ApplicationContextAware, org.springframework.core.Ordered, org.springframework.web.context.ServletContextAware, org.springframework.web.servlet.HandlerMapping

public class RouterHandlerMapping
extends org.springframework.web.servlet.handler.AbstractHandlerMapping

Implementation of the HandlerMapping interface that maps handlers based on HTTP routes defined in a route configuration file.

RouterHandlerMapping is not the default HandlerMapping registered in DispatcherServlet in SpringMVC. You need to declare and configure it in your DispatcherServlet context, by adding a RouterHandlerMapping bean explicitly. RouterHandlerMapping needs the name of the route configuration file (available in the application classpath); it also allows for registering custom interceptors:

 <bean
 class="org.resthub.web.springmvc.router.RouterHandlerMapping">
 <property name="routeFiles"> 
 <list>
   <value>routes.conf</value>
   <value>addroutes.conf</value>
 </list>
 </property>
 <property
 name="servletPrefix" value="/myservlet" /> <property
 name="interceptors" > ... </property> </bean>
 

Annotated controllers should be marked with the Controller stereotype at the type level. This is not strictly necessary because the methodeInvoker will try to map the Controller.invoker anyway using the current ApplicationContext. The RequestMapping is not taken into account here.

RouterHandlerMapping loads routes configuration from a file for route configuration syntax (the Router implementation is adapted from Play! Framework http://www.playframework.org/documentation/1.0.3/routes#syntax). Example:

 GET /home PageController.showPage(id:'home') GET
 /page/{id} PageController.showPage POST /customer/{<[0-9]+>customerid}
 CustomerController.createCustomer
 

The RouterHandlerAdapter is responsible for choosing and invoking the right controller method, as mapped by this HandlerMapping.

Author:
Brian Clozel
See Also:
AbstractHandlerMapping

Field Summary
 
Fields inherited from interface org.springframework.web.servlet.HandlerMapping
BEST_MATCHING_PATTERN_ATTRIBUTE, INTROSPECT_TYPE_LEVEL_MAPPING, PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, URI_TEMPLATE_VARIABLES_ATTRIBUTE
 
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
 
Constructor Summary
RouterHandlerMapping()
           
 
Method Summary
protected  Object getHandlerInternal(javax.servlet.http.HttpServletRequest request)
          Resolves a HandlerMethod (of type RouterHandler) given the current HTTP request, using the Router instance.
 List<String> getRouteFiles()
          Routes configuration Files names< Injected by bean configuration (in servlet.xml)
 String getServletPrefix()
          Servlet Prefix to be added in front of all routes Injected by bean configuration (in servlet.xml)
protected  void initApplicationContext()
          Inits Routes from route configuration file
 void reloadRoutesConfiguration()
          Reload routes configuration at runtime.
 void setRouteFiles(List<String> routeFiles)
           
 void setServletPrefix(String servletPrefix)
           
 
Methods inherited from class org.springframework.web.servlet.handler.AbstractHandlerMapping
adaptInterceptor, detectMappedInterceptors, extendInterceptors, getAdaptedInterceptors, getDefaultHandler, getHandler, getHandlerExecutionChain, getMappedInterceptors, getOrder, getPathMatcher, getUrlPathHelper, initInterceptors, setAlwaysUseFullPath, setDefaultHandler, setInterceptors, setOrder, setPathMatcher, setUrlDecode, setUrlPathHelper
 
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
 
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, requiredContextClass, setApplicationContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RouterHandlerMapping

public RouterHandlerMapping()
Method Detail

getServletPrefix

public String getServletPrefix()
Servlet Prefix to be added in front of all routes Injected by bean configuration (in servlet.xml)


setServletPrefix

public void setServletPrefix(String servletPrefix)

getRouteFiles

public List<String> getRouteFiles()
Routes configuration Files names< Injected by bean configuration (in servlet.xml)


setRouteFiles

public void setRouteFiles(List<String> routeFiles)

reloadRoutesConfiguration

public void reloadRoutesConfiguration()
Reload routes configuration at runtime. No-op if configuration files didn't change since last reload.


initApplicationContext

protected void initApplicationContext()
                               throws org.springframework.beans.BeansException
Inits Routes from route configuration file

Overrides:
initApplicationContext in class org.springframework.web.servlet.handler.AbstractHandlerMapping
Throws:
org.springframework.beans.BeansException

getHandlerInternal

protected Object getHandlerInternal(javax.servlet.http.HttpServletRequest request)
                             throws Exception
Resolves a HandlerMethod (of type RouterHandler) given the current HTTP request, using the Router instance.

Specified by:
getHandlerInternal in class org.springframework.web.servlet.handler.AbstractHandlerMapping
Parameters:
request - the HTTP Servlet request
Returns:
a RouterHandler, containing matching route + wrapped request
Throws:
Exception


Copyright © 2010-2012. All Rights Reserved.