質問

I have a Restlet (v2.1.1) component that is using a ServerResource to process HTTP GET requests.

I would like to put filters and/or routers into the component so that they can do some processing before the request gets to the ServerResource.

I have been searching the Internet for an example of doing this, as well as reading the "Restlet in Action" book. I have discovered something interesting:

There are plenty of examples of how to set up a ServerResource within a component.

There are plenty of examples of how to create and set up filters and routers.

Unfortunately, search as I might, through the book and on the Internet, I cannot find an example of using both!

According to the book and the tutorials on the Internet, we should be able to create a component, set up a ServerResource in the component, and use a filter to preprocess requests that go to the ServerResource. None of the documentation anywhere seems to tell us exactly how to do this.

Am I misreading everything? Is there really no way to put filters or routers in components with ServerResources? Or have I missed some document somewhere that provides a real example of how to do this?

Could someone please either provide a simple example or provide a link to an example of doing this?

Thanks...

役に立ちましたか?

解決

You need to attach the filter to the router and then attach the ServerResource to the filter using the method setNext(Class<? extends ServerResource> targetClass):

Filter myFilter = new MyFilter(getContext());
myFilter.setNext(MyServerResource.class);
router.attach("/test", myFilter);

Now you can preprocess using the filter's beforeHandle(Request request, Response response) method. If you return CONTINUE in this method, the filter will pass the request to the ServerResource.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top