public class SpringBeanRouter extends Router implements org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.config.BeanFactoryPostProcessor
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.MODE_BEST_MATCH, MODE_CUSTOM, MODE_FIRST_MATCH, MODE_LAST_MATCH, MODE_NEXT_MATCH, MODE_RANDOM_MATCH| Constructor and Description |
|---|
SpringBeanRouter()
Constructor.
|
SpringBeanRouter(Context context)
Constructor with a parent context.
|
SpringBeanRouter(Restlet parent)
Constructor with a parent Restlet.
|
| Modifier and Type | Method and 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 via
createFinder(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 Finder |
createFinder(org.springframework.beans.factory.BeanFactory beanFactory,
java.lang.String beanName)
Creates an instance of
SpringBeanFinder. |
protected java.util.Map<java.lang.String,java.lang.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(org.springframework.beans.factory.config.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 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.
|
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, stopcreateFinder, finalize, getApplication, getAuthor, getContext, getDescription, getFinderClass, getLogger, getName, getOwner, handle, handle, handle, isStarted, isStopped, setAuthor, setContext, setDescription, setFinderClass, setName, setOwnerpublic SpringBeanRouter()
public SpringBeanRouter(Context context)
public SpringBeanRouter(Restlet parent)
protected void attachResource(java.lang.String uri,
java.lang.String beanName,
org.springframework.beans.factory.BeanFactory beanFactory)
createFinder(BeanFactory, String).uri - The attachment URI.beanName - The bean name.beanFactory - The Spring bean factory.protected void attachRestlet(java.lang.String uri,
java.lang.String beanName,
org.springframework.beans.factory.BeanFactory beanFactory)
uri - The attachment URI.beanName - The bean name.beanFactory - The Spring bean factory.protected Finder createFinder(org.springframework.beans.factory.BeanFactory beanFactory, java.lang.String beanName)
SpringBeanFinder. This can be overridden
if necessary.beanFactory - The Spring bean factory.beanName - The bean name.attachResource(java.lang.String, java.lang.String, org.springframework.beans.factory.BeanFactory)protected java.util.Map<java.lang.String,java.lang.String> getAttachments()
public boolean isFindingInAncestors()
public void postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
throws org.springframework.beans.BeansException
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.postProcessBeanFactory in interface org.springframework.beans.factory.config.BeanFactoryPostProcessorbeanFactory - The Spring bean factory.org.springframework.beans.BeansExceptionsetAttachments(java.util.Map<java.lang.String, java.lang.String>)protected java.lang.String resolveUri(java.lang.String beanName,
org.springframework.beans.factory.ListableBeanFactory beanFactory)
BeanNameUrlHandlerMapping
.beanName - The bean name to lookup in the bean factory aliases.beanFactory - The Spring bean factory.public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
throws org.springframework.beans.BeansException
setApplicationContext in interface org.springframework.context.ApplicationContextAwareapplicationContext - The context.org.springframework.beans.BeansExceptionpublic void setAttachments(java.util.Map<java.lang.String,java.lang.String> attachments)
attachments - Supplemental explicit mappings.SpringRouterpublic void setFindingInAncestors(boolean findingInAncestors)
findingInAncestors - Search for beans higher up in the BeanFactory hierarchy.Copyright © 2005-2024 Restlet.