You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Rice Yeh <ri...@gmail.com> on 2011/06/02 11:32:42 UTC

How tell get matched response type in an exception mapper?

Hi,
  I write a ExceptionMapper to handle exception. In the ExceptionMapper, I
have a need to know the matched response type to return different response.
For example, if the the matched response type is html, it returns a html
response and if the matched response is  json, it returns json. How do I do
this?

Regards,
Rice

Re: How tell get matched response type in an exception mapper?

Posted by Sergey Beryozkin <sb...@gmail.com>.
have it injected as a  thread-safe Context:

private @Context HttpHeaders headers;

Cheers, Sergey

On Thu, Jun 2, 2011 at 4:46 PM, Rice Yeh <ri...@gmail.com> wrote:
> Hi Sergey,
>  I do not understand your solution. My exception mapper is like the
> following:
>
> public class MyExceptionMapper extends ExceptionMapper<Throwable>
> {
>    public Response toResponse(Throwable throwable) {
>        if (the throwable comes from a matched method provide
> application/json)
>          map throwable to json
>        else if (.... provide text/html)
>          map throwable to html
>        ....
>    }
> }
>
> Where should put the HttpHeaders in MyExceptionMapper?
>
> Regards,
> Rice
>
> On Thu, Jun 2, 2011 at 6:09 PM, Sergey Beryozkin <sb...@gmail.com>wrote:
>
>> Hi Rice
>>
>> On Thu, Jun 2, 2011 at 10:55 AM, Rice Yeh <ri...@gmail.com> wrote:
>> > I have a feeling that the spec of JAX-RS is kind of weak on exception
>> > handling. It regulates a lot of tags for matching a method to handle a
>> > request based on path, consume, provide, ... But once an exception
>> happen,
>> > all these mechanism are gone. I have to judge what kind of content to
>> return
>> > in the method toResonse(Throwable...) based on very little information
>> > available.
>> >
>>
>> You can get @HttpHeaders injected and get the properly sorted list of
>> acceptable media types,
>> thus if you have a single method with @Produces having multiple values
>> then you can use the first *known* value in the list of acceptable
>> media types is the one which is expected by the client.
>> Ex, if you know that @Produces has html/xml/json and you get a sorted
>> list starting with json then it's json. If the lists starts from the
>> type not available on @Produces then you need to skip it.
>>
>> Cheers, Sergey
>>
>> > Rice
>> >
>> > On Thu, Jun 2, 2011 at 5:32 PM, Rice Yeh <ri...@gmail.com> wrote:
>> >
>> >> Hi,
>> >>   I write a ExceptionMapper to handle exception. In the ExceptionMapper,
>> I
>> >> have a need to know the matched response type to return different
>> response.
>> >> For example, if the the matched response type is html, it returns a html
>> >> response and if the matched response is  json, it returns json. How do I
>> do
>> >> this?
>> >>
>> >> Regards,
>> >> Rice
>> >>
>> >
>>
>>
>>
>> --
>> Sergey Beryozkin
>>
>> Application Integration Division of Talend
>> http://sberyozkin.blogspot.com
>>
>



-- 
Sergey Beryozkin

Application Integration Division of Talend
http://sberyozkin.blogspot.com

Re: How tell get matched response type in an exception mapper?

Posted by Rice Yeh <ri...@gmail.com>.
Hi Sergey,
  I do not understand your solution. My exception mapper is like the
following:

public class MyExceptionMapper extends ExceptionMapper<Throwable>
{
    public Response toResponse(Throwable throwable) {
        if (the throwable comes from a matched method provide
application/json)
          map throwable to json
        else if (.... provide text/html)
          map throwable to html
        ....
    }
}

Where should put the HttpHeaders in MyExceptionMapper?

Regards,
Rice

On Thu, Jun 2, 2011 at 6:09 PM, Sergey Beryozkin <sb...@gmail.com>wrote:

> Hi Rice
>
> On Thu, Jun 2, 2011 at 10:55 AM, Rice Yeh <ri...@gmail.com> wrote:
> > I have a feeling that the spec of JAX-RS is kind of weak on exception
> > handling. It regulates a lot of tags for matching a method to handle a
> > request based on path, consume, provide, ... But once an exception
> happen,
> > all these mechanism are gone. I have to judge what kind of content to
> return
> > in the method toResonse(Throwable...) based on very little information
> > available.
> >
>
> You can get @HttpHeaders injected and get the properly sorted list of
> acceptable media types,
> thus if you have a single method with @Produces having multiple values
> then you can use the first *known* value in the list of acceptable
> media types is the one which is expected by the client.
> Ex, if you know that @Produces has html/xml/json and you get a sorted
> list starting with json then it's json. If the lists starts from the
> type not available on @Produces then you need to skip it.
>
> Cheers, Sergey
>
> > Rice
> >
> > On Thu, Jun 2, 2011 at 5:32 PM, Rice Yeh <ri...@gmail.com> wrote:
> >
> >> Hi,
> >>   I write a ExceptionMapper to handle exception. In the ExceptionMapper,
> I
> >> have a need to know the matched response type to return different
> response.
> >> For example, if the the matched response type is html, it returns a html
> >> response and if the matched response is  json, it returns json. How do I
> do
> >> this?
> >>
> >> Regards,
> >> Rice
> >>
> >
>
>
>
> --
> Sergey Beryozkin
>
> Application Integration Division of Talend
> http://sberyozkin.blogspot.com
>

Re: How tell get matched response type in an exception mapper?

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi Rice

On Thu, Jun 2, 2011 at 10:55 AM, Rice Yeh <ri...@gmail.com> wrote:
> I have a feeling that the spec of JAX-RS is kind of weak on exception
> handling. It regulates a lot of tags for matching a method to handle a
> request based on path, consume, provide, ... But once an exception happen,
> all these mechanism are gone. I have to judge what kind of content to return
> in the method toResonse(Throwable...) based on very little information
> available.
>

You can get @HttpHeaders injected and get the properly sorted list of
acceptable media types,
thus if you have a single method with @Produces having multiple values
then you can use the first *known* value in the list of acceptable
media types is the one which is expected by the client.
Ex, if you know that @Produces has html/xml/json and you get a sorted
list starting with json then it's json. If the lists starts from the
type not available on @Produces then you need to skip it.

Cheers, Sergey

> Rice
>
> On Thu, Jun 2, 2011 at 5:32 PM, Rice Yeh <ri...@gmail.com> wrote:
>
>> Hi,
>>   I write a ExceptionMapper to handle exception. In the ExceptionMapper, I
>> have a need to know the matched response type to return different response.
>> For example, if the the matched response type is html, it returns a html
>> response and if the matched response is  json, it returns json. How do I do
>> this?
>>
>> Regards,
>> Rice
>>
>



-- 
Sergey Beryozkin

Application Integration Division of Talend
http://sberyozkin.blogspot.com

Re: How tell get matched response type in an exception mapper?

Posted by Rice Yeh <ri...@gmail.com>.
I have a feeling that the spec of JAX-RS is kind of weak on exception
handling. It regulates a lot of tags for matching a method to handle a
request based on path, consume, provide, ... But once an exception happen,
all these mechanism are gone. I have to judge what kind of content to return
in the method toResonse(Throwable...) based on very little information
available.

Rice

On Thu, Jun 2, 2011 at 5:32 PM, Rice Yeh <ri...@gmail.com> wrote:

> Hi,
>   I write a ExceptionMapper to handle exception. In the ExceptionMapper, I
> have a need to know the matched response type to return different response.
> For example, if the the matched response type is html, it returns a html
> response and if the matched response is  json, it returns json. How do I do
> this?
>
> Regards,
> Rice
>