Restlet Framework 2.2 Snapshot
OSGi Environments

org.restlet.ext.spring
Class SpringBeanRouter

java.lang.Object
  extended by org.restlet.Restlet
      extended by org.restlet.routing.Router
          extended by org.restlet.ext.spring.SpringBeanRouter
All Implemented Interfaces:
Uniform, Aware, BeanFactoryPostProcessor, ApplicationContextAware

public class SpringBeanRouter
extends Router
implements ApplicationContextAware, BeanFactoryPostProcessor

Restlet Router which behaves like Spring's BeanNameUrlHandlerMapping. It takes every bean of type ServerResource or Restlet defined in a particular context and examines its aliases (generally speaking, its name and id). If one of the aliases begins with a forward slash, the resource will be attached to that URI.

Example:

 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
    <!-- Singleton instance of this class -->
    <bean name="router" class="org.restlet.ext.spring.SpringBeanRouter"/>
 
    <!-- Prototype beans for the resources -->
    <bean name="/studies" id="studiesResource" autowire="byName" scope="prototype" class="edu.northwestern.myapp.StudiesResource" >
       <property name="studyDao" ref="studyDao"/>
    </bean>
 
    <bean name="/studies/{study-identifier}/template" id="templateResource" autowire="byName" scope="prototype" class="edu.northwestern.myapp.TemplateResource" />
 
    <!-- Singleton bean for a restlet -->
    <bean name="/studies/{study-identifier}/files" id="filesResource" autowire="byName" class="edu.northwestern.myapp.MyDirectory" />
 </beans>
 
This will route two resources and one restlet: "/studies", "/studies/{study-identifier}/template", and "/studies/{study-identifier}/files" to the corresponding beans. N.b.: Resources must be scoped prototype, since a new instance must be created for each request. Restlets may be singletons (this class will only ever load one instance for each). Concurrency note: instances of this class or its subclasses can be invoked by several threads at the same time and therefore must be thread-safe. You should be especially careful when storing state in member variables.

Author:
Rhett Sutphin, James Maki

Field Summary
 
Fields inherited from class org.restlet.routing.Router
MODE_BEST_MATCH, MODE_CUSTOM, MODE_FIRST_MATCH, MODE_LAST_MATCH, MODE_NEXT_MATCH, MODE_RANDOM_MATCH
 
Constructor Summary
SpringBeanRouter()
          Constructor.
SpringBeanRouter(Context context)
          Constructor with a parent context.
SpringBeanRouter(Restlet parent)
          Constructor with a parent Restlet.
 
Method Summary
protected  void attachResource(String uri, String beanName, BeanFactory beanFactory)
          Attaches the named resource bean at the given URI, creating a finder for it via createFinder(BeanFactory, String).
protected  void attachRestlet(String uri, String beanName, BeanFactory beanFactory)
          Attaches the named restlet bean directly at the given URI.
protected  Finder createFinder(BeanFactory beanFactory, String beanName)
          Creates an instance of SpringBeanFinder.
protected  Map<String,String> getAttachments()
          Returns supplemental explicit mappings
 boolean isFindingInAncestors()
          Returns true if bean names will be searched for higher up in the BeanFactory hierarchy.
 void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
          Attaches all ServerResource and Restlet beans found in the surrounding bean factory for which resolveUri(java.lang.String, org.springframework.beans.factory.ListableBeanFactory) finds a usable URI.
protected  String resolveUri(String beanName, ListableBeanFactory beanFactory)
          Uses this first alias for this bean that starts with '/' and is not mapped in the explicit attachments to another bean.
 void setApplicationContext(ApplicationContext applicationContext)
          Sets the Spring application context.
 void setAttachments(Map<String,String> attachments)
          Sets an explicit mapping of URI templates to bean IDs to use in addition to the usual bean name mapping behavior.
 void setFindingInAncestors(boolean findingInAncestors)
          Sets if bean names will be searched for higher up in the BeanFactory hierarchy.
 
Methods inherited from class org.restlet.routing.Router
attach, attach, attach, attach, attach, attach, attachDefault, attachDefault, createRoute, createRoute, detach, detach, doHandle, getCustom, getDefaultMatchingMode, getDefaultMatchingQuery, getDefaultRoute, getMatchingMode, getMaxAttempts, getNext, getRequiredScore, getRetryDelay, getRoutes, getRoutingMode, handle, logRoute, redirectPermanent, redirectSeeOther, redirectTemporary, setDefaultMatchingMode, setDefaultMatchingQuery, setDefaultRoute, setMaxAttempts, setRequiredScore, setRetryDelay, setRoutes, setRoutingMode, start, stop
 
Methods inherited from class org.restlet.Restlet
createFinder, finalize, getApplication, getAuthor, getContext, getDescription, getFinderClass, getLogger, getName, getOwner, handle, handle, handle, isStarted, isStopped, setAuthor, setContext, setDescription, setFinderClass, setName, setOwner
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SpringBeanRouter

public SpringBeanRouter()
Constructor.


SpringBeanRouter

public SpringBeanRouter(Context context)
Constructor with a parent context.


SpringBeanRouter

public SpringBeanRouter(Restlet parent)
Constructor with a parent Restlet.

Method Detail

attachResource

protected void attachResource(String uri,
                              String beanName,
                              BeanFactory beanFactory)
Attaches the named resource bean at the given URI, creating a finder for it via createFinder(BeanFactory, String).

Parameters:
uri - The attachment URI.
beanName - The bean name.
beanFactory - The Spring bean factory.

attachRestlet

protected void attachRestlet(String uri,
                             String beanName,
                             BeanFactory beanFactory)
Attaches the named restlet bean directly at the given URI.

Parameters:
uri - The attachment URI.
beanName - The bean name.
beanFactory - The Spring bean factory.

createFinder

protected Finder createFinder(BeanFactory beanFactory,
                              String beanName)
Creates an instance of SpringBeanFinder. This can be overridden if necessary.

Parameters:
beanFactory - The Spring bean factory.
beanName - The bean name.
See Also:
attachResource(java.lang.String, java.lang.String, org.springframework.beans.factory.BeanFactory)

getAttachments

protected Map<String,String> getAttachments()
Returns supplemental explicit mappings

Returns:
Supplemental explicit mappings

isFindingInAncestors

public boolean isFindingInAncestors()
Returns true if bean names will be searched for higher up in the BeanFactory hierarchy. Default is true.

Returns:
True if bean names will be searched for higher up in the BeanFactory hierarchy.

postProcessBeanFactory

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
                            throws BeansException
Attaches all ServerResource and Restlet beans found in the surrounding bean factory for which resolveUri(java.lang.String, org.springframework.beans.factory.ListableBeanFactory) finds a usable URI. Also attaches everything explicitly routed in the attachments property.

Specified by:
postProcessBeanFactory in interface BeanFactoryPostProcessor
Parameters:
beanFactory - The Spring bean factory.
Throws:
BeansException
See Also:
setAttachments(java.util.Map)

resolveUri

protected String resolveUri(String beanName,
                            ListableBeanFactory beanFactory)
Uses this first alias for this bean that starts with '/' and is not mapped in the explicit attachments to another bean. This mimics the behavior of BeanNameUrlHandlerMapping .

Parameters:
beanName - The bean name to lookup in the bean factory aliases.
beanFactory - The Spring bean factory.
Returns:
The alias URI.

setApplicationContext

public void setApplicationContext(ApplicationContext applicationContext)
                           throws BeansException
Sets the Spring application context.

Specified by:
setApplicationContext in interface ApplicationContextAware
Parameters:
applicationContext - The context.
Throws:
BeansException

setAttachments

public void setAttachments(Map<String,String> attachments)
Sets an explicit mapping of URI templates to bean IDs to use in addition to the usual bean name mapping behavior. If a URI template appears in both this mapping and as a bean name, the bean it is mapped to here is the one that will be used.

Parameters:
attachments - Supplemental explicit mappings.
See Also:
SpringRouter

setFindingInAncestors

public void setFindingInAncestors(boolean findingInAncestors)
Sets if bean names will be searched for higher up in the BeanFactory hierarchy.

Parameters:
findingInAncestors - Search for beans higher up in the BeanFactory hierarchy.

Restlet Framework 2.2 Snapshot
OSGi Environments

Copyright © 2005-2014 Restlet.