Need an example of placing a filter in the Restlet component

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

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

I searched the Internet for an example of this, and also read the book "Restlet in Action". I found something interesting:

There are many examples of how to configure a server source in a component.

There are many examples of creating and configuring filters and routers.

Unfortunately, searching, as I could, through a book and on the Internet, cannot find an example of using both!

According to the book and tutorials on the Internet, we should be able to create a component, configure ServerResource in the component, and use a filter to pre-process requests that go to ServerResource. None of the documentation anywhere tells us how to do this.

Am I misunderstanding everything? Can't I put filters or routers in components with ServerResources? Or am I somewhere missing a document that gives a real example of how to do this?

Can someone ask for a simple example or provide a link to an example of this?

Thanks...

+4
source share
1 answer

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

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

Now you can do the preprocessing using the beforeHandle(Request request, Response response) filter. If you return CONTINUE in this method, the filter will pass the request to the server source.

+6
source

All Articles