users@jersey.java.net

[Jersey] Re: Custom handlers

From: Pavel Bucek <pavel.bucek_at_oracle.com>
Date: Fri, 05 Aug 2011 10:36:27 +0200

On 8/5/11 10:21 AM, Jose Antonio Illescas Del Olmo wrote:
> I try this, but... this is a "jersey" feature.
well, this is Jersey user list..
>
> Any "jax-rs" (annotated) alternative?
nope, specification don't contain any general pre/post processing
(AFAIK, you can check it by yourself [1]), every implementation solves
this differently now.

>
> Future jax-rs 2.0 include this filters?
not exactly these filters, but I know that there is a plan for
interceptors/handlers which you can use to achieve what you've described.

Regards,
Pavel

[1] http://download.oracle.com/otndocs/jcp/jaxrs-1.1-mrel-eval-oth-JSpec/
>
> Thank you
>
> On 04/08/2011 16:51, Pavel Bucek wrote:
>> Hello,
>>
>> you can define ContainerRequest/Response filter - it should do
>> exactly what you need; see
>>
>> http://jersey.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/spi/container/ContainerRequestFilter.html
>>
>> http://jersey.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/spi/container/ContainerResponseFilter.html
>>
>>
>> you might want to look at LoggingFilter as sample for your own one:
>>
>> http://jersey.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/container/filter/LoggingFilter.html
>>
>> (source:
>> http://java.net/projects/jersey/sources/svn/content/trunk/jersey/jersey-server/src/main/java/com/sun/jersey/api/container/filter/LoggingFilter.java?rev=5224)
>>
>> Regards,
>> Pavel
>>
>> On 8/4/11 12:28 PM, Jose Antonio Illescas Del Olmo wrote:
>>> Can I define custom handlers?
>>>
>>>
>>> example:
>>>
>>> public class MyInvocationHandler {
>>>
>>> public Response process(Annotation[] annotations, MediaType
>>> mediaType, InvocationContex context) {
>>>
>>> // put your pre-process here: logs, security,
>>> transactions...
>>>
>>> context.invoke(); // this method calls to my found
>>> REST service method
>>>
>>> // put your post-process here: redirect/forwards,
>>> Reponse modifications...
>>> }
>>>
>>> and defining my hadler on my REST service (or use a
>>> META-INF/services file to configure)
>>>
>>> @Path("hello")
>>> public class HelloWorld {
>>>
>>> @GET
>>> @Produces("text/xml")
>>> @Handle(MyInvocationHandler)
>>> public String sayHello() {
>>> return "<message>Hello world</message>";
>>> }
>>>
>>>
>>
>>
>
>