我有两个要求
答案 0 :(得分:0)
为Restlet中的请求配置执行链非常灵活。
要为所有服务器资源创建全局过滤器,只需在Restlet应用程序中的路由器前添加过滤器:
public class MyApplication extends Application {
public Restlet createInboundRoot() {
Router router = new Router(getContext());
(...)
router.attach("/test", TestServerResource.class);
router.attach("/", RootServerResource.class);
return new MyFilter(getContext(), router);
}
}
此过滤器的内容可能是这样的:
public class MyGlobalFilter extends Filter {
public MyGlobalFilter(Context context, Restlet next) {
super(context, next);
}
(...)
@Override
protected void afterHandle(Request request, Response response) {
(...)
}
@Override
protected int beforeHandle(Request request, Response response) {
(...)
}
}
关于Restlet中的前置和后置过滤器,只需在过滤器类中分别实现beforeHandle
和afterHandle
方法。
public class MyGlobalFilter extends Filter {
(...)
@Override
protected void afterHandle(Request request, Response response) {
(...)
}
@Override
protected int beforeHandle(Request request, Response response) {
(...)
}
}
要应用此类过滤器,Restlet中没有相应的注释。在应用程序类的createInboundRoot
方法中定义执行链(路由)时,需要添加它们。您可以注意到,子路由器只能用于对服务器资源的子集应用过滤器。
public class MyApplication extends Application {
public Restlet createInboundRoot() {
Router rootRouter = new Router(getContext());
(...)
Router subRouter = new Router(getContext());
subRouter.attach("", TestsServerResource.class);
subRouter.attach("{id}", TestServerResource.class);
MyFilter myFilter = new MyFilter(getContext(), subRouter);
rootRouter.attach("/test", myFilter)
.setMatchingMode(Template.MODE_STARTS_WITH);
return rootRouter;
}
}
希望它可以帮到你, 亨利