我正试图将一个简单的重定向添加到Restlets中构建的web应用程序中,事实证明这并不简单。任务很简单:我想将所有丢失的文件从web应用程序重定向到同一个静态文件。
我使用具有以下值的org.restlet.routing.Redirector
(我使用Spring注入):
<bean name="router" class="org.restlet.ext.spring.SpringRouter">
<constructor-arg ref="trackerComponentChildContext" />
<property name="attachments">
<map>
<entry key="/api" value-ref="apiRouter" />
<entry key="/statics" value-ref="staticsDirectory" />
<entry key="/" value-ref="staticsRedirector" />
</map>
</property>
</bean>
<bean id="staticsRedirector" class="ca.uhnresearch.pughlab.tracker.restlets.CustomRedirector">
<constructor-arg ref="trackerComponentChildContext" />
<constructor-arg value="{o}/statics/index.html" />
<constructor-arg value="7" />
</bean>
我可以相对简单地处理文件层次结构,但我只想在同一应用程序中将任何与/api
或/statics
不匹配的内容发送到/statics/index.html
。
Restlet是几乎得到它,它现在似乎确实获得了对正确文件的引用,它只是不太适合它。
我把整件事的工作副本(包括下面蒂埃里的建议)放在:https://github.com/morungos/restlet-spring-static-files.我希望发生的事情类似于下面的等效顺序尝试:
curl http://localhost:8080/statics/**/*
命中相应的/statics/**/*
curl http://localhost:8080
击中主/statics/index.html
curl http://localhost:8080/**/*
击中主/statics/index.html
我对您的问题进行了一些测试,但我不知道如何获得您的消息:-(。也许是因为我没有完整的代码。
事实上,我在SpringRouter
本身的层面上看到了一个问题。我想用attachDefault
而不是attach("/", ...)
/attach("", ...)
来附加重定向器。方法CCD_ 15实际上执行CCD_。
所以我用以下更新做了一些工作:
-
创建自定义
SpringRouter
public class CustomSpringRouter extends SpringRouter { public void setDefaultAttachment(Object route) { if (route instanceof Redirector) { this.attachDefault((Restlet) route); } else { super.setDefaultAttachment(route); } } }
-
创建自定义
Redirector
。我从组件中获得了上下文,而不是子上下文。public class CustomRedirector extends Redirector { public CustomRedirector(Component component, String targetPattern, int mode) { super(component.getContext(), targetPattern, mode); } }
然后我使用以下Spring配置:
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="myComponent" class="org.restlet.ext.spring.SpringComponent">
<property name="defaultTarget" ref="router" />
</bean>
<bean name="router" class="test.CustomSpringRouter">
<property name="attachments">
<map>
<entry key="/api" value-ref="apiRouter" />
<entry key="/statics" value-ref="staticsDirectory" />
</map>
</property>
<property name="defaultAttachment" ref="staticsRedirector" />
</bean>
<bean id="staticsRedirector" class="test.CustomRedirector">
<constructor-arg ref="myComponent" />
<constructor-arg value="{o}/statics/index.html" />
<constructor-arg value="7" />
</bean>
<bean name="apiRouter" class="org.restlet.ext.spring.SpringRouter">
(...)
</bean>
(...)
</beans>
希望它能帮助你,Thierry