Package org.restlet.ext.spring
Class SpringBeanRouter
- java.lang.Object
-
- org.restlet.Restlet
-
- org.restlet.routing.Router
-
- org.restlet.ext.spring.SpringBeanRouter
-
- All Implemented Interfaces:
org.restlet.Uniform
,org.springframework.beans.factory.Aware
,org.springframework.beans.factory.config.BeanFactoryPostProcessor
,org.springframework.context.ApplicationContextAware
public class SpringBeanRouter extends org.restlet.routing.Router implements org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.config.BeanFactoryPostProcessor
RestletRouter
which behaves like Spring'sBeanNameUrlHandlerMapping
. It takes every bean of typeServerResource
orRestlet
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
-
-
Constructor Summary
Constructors Constructor Description SpringBeanRouter()
Constructor.SpringBeanRouter(org.restlet.Context context)
Constructor with a parent context.SpringBeanRouter(org.restlet.Restlet parent)
Constructor with a parent Restlet.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
attachResource(java.lang.String uri, java.lang.String beanName, org.springframework.beans.factory.BeanFactory beanFactory)
Attaches the named resource bean at the given URI, creating a finder for it viacreateFinder(BeanFactory, String)
.protected void
attachRestlet(java.lang.String uri, java.lang.String beanName, org.springframework.beans.factory.BeanFactory beanFactory)
Attaches the named restlet bean directly at the given URI.protected org.restlet.resource.Finder
createFinder(org.springframework.beans.factory.BeanFactory beanFactory, java.lang.String beanName)
Creates an instance ofSpringBeanFinder
.protected java.util.Map<java.lang.String,java.lang.String>
getAttachments()
Returns supplemental explicit mappingsboolean
isFindingInAncestors()
Returns true if bean names will be searched for higher up in the BeanFactory hierarchy.void
postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
Attaches allServerResource
andRestlet
beans found in the surrounding bean factory for whichresolveUri(java.lang.String, org.springframework.beans.factory.ListableBeanFactory)
finds a usable URI.protected java.lang.String
resolveUri(java.lang.String beanName, org.springframework.beans.factory.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(org.springframework.context.ApplicationContext applicationContext)
Sets the Spring application context.void
setAttachments(java.util.Map<java.lang.String,java.lang.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
-
-
-
-
Method Detail
-
attachResource
protected void attachResource(java.lang.String uri, java.lang.String beanName, org.springframework.beans.factory.BeanFactory beanFactory)
Attaches the named resource bean at the given URI, creating a finder for it viacreateFinder(BeanFactory, String)
.- Parameters:
uri
- The attachment URI.beanName
- The bean name.beanFactory
- The Spring bean factory.
-
attachRestlet
protected void attachRestlet(java.lang.String uri, java.lang.String beanName, org.springframework.beans.factory.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 org.restlet.resource.Finder createFinder(org.springframework.beans.factory.BeanFactory beanFactory, java.lang.String beanName)
Creates an instance ofSpringBeanFinder
. 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 java.util.Map<java.lang.String,java.lang.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(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory) throws org.springframework.beans.BeansException
Attaches allServerResource
andRestlet
beans found in the surrounding bean factory for whichresolveUri(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 interfaceorg.springframework.beans.factory.config.BeanFactoryPostProcessor
- Parameters:
beanFactory
- The Spring bean factory.- Throws:
org.springframework.beans.BeansException
- See Also:
setAttachments(java.util.Map<java.lang.String, java.lang.String>)
-
resolveUri
protected java.lang.String resolveUri(java.lang.String beanName, org.springframework.beans.factory.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 ofBeanNameUrlHandlerMapping
.- 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(org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException
Sets the Spring application context.- Specified by:
setApplicationContext
in interfaceorg.springframework.context.ApplicationContextAware
- Parameters:
applicationContext
- The context.- Throws:
org.springframework.beans.BeansException
-
setAttachments
public void setAttachments(java.util.Map<java.lang.String,java.lang.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.
-
-