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 2016/05/14 07:47:35 UTC

Error handling in out interceptor in jaxrs

Hi,
  I write a outward interceptor and put it after JAXRSOutInterceptor to
clean up some resouces. When cleaning up the resources, there might be some
exceptions happening. Then I directly set the HttpServletResponse's headers
and content to send back an appropriate response. This works even there is
a IllegalStateException thrown when later cxf's jaxrs implementation try
flush http headers to the HttpServletResponse like below. Any more
appropriate way to achieve my goal without this exception?

2016-05-13T16:58:36.514 [qtp1980982961-89] WARN
 o.a.cxf.phase.PhaseInterceptorChain - Interceptor for {
http://rs.security.xs/}Resource has thrown exception, unwinding now

java.lang.IllegalStateException: WRITER

at org.eclipse.jetty.server.Response.getOutputStream(Response.java:906)
~[na:na]

at
org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(AbstractHTTPDestination.java:585)
[cxf-rt-transports-http-2.7.11.jar:2.7.11]

at
org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStream.close(AbstractHTTPDestination.java:743)
~[cxf-rt-transports-http-2.7.11.jar:2.7.11]

Re: Error handling in out interceptor in jaxrs

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

If the exceptions thrown by either CXF JAX-RS implementation itself or 
by JAX-RS filters or code were not caught by the matching exception 
mappers then it would be a major issue :-). I believe it has been 
thoroughly tested.

I believe what you are talking about here are the exceptions thrown 
outside of the JAX-RS chain, from the custom CXF interceptors.
The JAX-RS impl does not have to catch them but AFAIK it has been supported.

Tell me please what exception is thrown, from which interceptor, and why 
do think this exception has not been mapped (you referred to some Jetty 
issue earlier)

Cheers, Sergey
On 16/05/16 17:21, Rice Yeh wrote:t
> Hi Sergey,
>    "the exceptions thrown from either in or out chains are expected to be
> caught by the registered mappers". I tried it, cxf's jaxrs does not follows
> what you said.
>
> Rice
>
> On Mon, May 16, 2016 at 4:26 PM, Sergey Beryozkin <sb...@gmail.com>
> wrote:
>
>> Hi Rice
>>
>> After JAXRSOutIterceptor has finished its work, the response has already
>> been written out, so I guess trying to do some extra response work after
>> that can not work, may be I'm missing something ?
>>
>> FYI, the exceptions thrown from either in or out chains are expected to be
>> caught by the registered mappers, the exceptions escaped from in/out chains
>> will still be tried in the default JAX-RS out fault interceptor.
>>
>> Cheers, Sergey
>>
>> On 16/05/16 05:58, Rice Yeh wrote:
>>
>>> In cxf jaxrs implementation, javax.ws.rs.ex.ExceptionMapper can work
>>> because JAXRSInInterceptor.handleFault(...) will invokde it.
>>> But JAXRSOutInterceptor.handleFault(...)  does not do as much as
>>> JASRSInInterceptor does.  That is ExceptionMapper only works during IN
>>> interceptor chain. My exception happens during OUT interceptor chain, That
>>> is, I need to to use serlvlet api to consturct my response.
>>>
>>> Rice
>>>
>>> On Mon, May 16, 2016 at 11:16 AM, Rice Yeh <ri...@gmail.com> wrote:
>>>
>>> Hi,
>>>>     I do release my resources in a interceptor. Just when error happens
>>>> when
>>>> releasing resources in my intereceptor, I like to construct my response.
>>>> But since the intereceptor is after jaxrs, I cannot access jaxrs api but
>>>> servlet api. So I construct my response with HttpServletResponse
>>>> directly.
>>>> How do I construct my response in a more cxf-friendly way?
>>>> I try to use message.setContent(Response.class, ...) but it does not
>>>> work.
>>>>
>>>> On Sun, May 15, 2016 at 10:13 PM, Andrei Shakirin <as...@talend.com>
>>>> wrote:
>>>>
>>>> Hi Rice,
>>>>>
>>>>> The error said that you try to use Jetty Response in Writer mode
>>>>> (getWriter()), although it was originally opened in stream mode
>>>>> (getOutputStream()).
>>>>> Generally I am not sure that setting headers and content directly in
>>>>> Response is a good idea - this makes your code fragile and dependent on
>>>>> internal CXF implementation.
>>>>>
>>>>> Why don't release your resources in JX-RS Filter (
>>>>> http://cxf.apache.org/docs/jax-rs-filters.html) or Interceptor (
>>>>> http://cxf.apache.org/docs/jax-rs-basics.html#JAX-RSBasics-Interceptors
>>>>> )?
>>>>>
>>>>> Regards,
>>>>> Andrei.
>>>>>
>>>>> -----Original Message-----
>>>>>> From: Rice Yeh [mailto:riceyeh@gmail.com]
>>>>>> Sent: Samstag, 14. Mai 2016 09:48
>>>>>> To: users@cxf.apache.org
>>>>>> Subject: Error handling in out interceptor in jaxrs
>>>>>>
>>>>>> Hi,
>>>>>>     I write a outward interceptor and put it after JAXRSOutInterceptor
>>>>>> to
>>>>>>
>>>>> clean
>>>>>
>>>>>> up some resouces. When cleaning up the resources, there might be some
>>>>>> exceptions happening. Then I directly set the HttpServletResponse's
>>>>>>
>>>>> headers
>>>>>
>>>>>> and content to send back an appropriate response. This works even there
>>>>>>
>>>>> is
>>>>>
>>>>>> a IllegalStateException thrown when later cxf's jaxrs implementation
>>>>>>
>>>>> try flush
>>>>>
>>>>>> http headers to the HttpServletResponse like below. Any more
>>>>>> appropriate
>>>>>> way to achieve my goal without this exception?
>>>>>>
>>>>>> 2016-05-13T16:58:36.514 [qtp1980982961-89] WARN
>>>>>> o.a.cxf.phase.PhaseInterceptorChain - Interceptor for {
>>>>>> http://rs.security.xs/}Resource has thrown exception, unwinding now
>>>>>>
>>>>>> java.lang.IllegalStateException: WRITER
>>>>>>
>>>>>> at org.eclipse.jetty.server.Response.getOutputStream(Response.java:906)
>>>>>> ~[na:na]
>>>>>>
>>>>>> at
>>>>>>
>>>>>>
>>>>> org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(Abstra
>>>>>
>>>>>> ctHTTPDestination.java:585)
>>>>>> [cxf-rt-transports-http-2.7.11.jar:2.7.11]
>>>>>>
>>>>>> at
>>>>>> org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStre
>>>>>> am.close(AbstractHTTPDestination.java:743)
>>>>>> ~[cxf-rt-transports-http-2.7.11.jar:2.7.11]
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>
>> --
>> Sergey Beryozkin
>>
>> Talend Community Coders
>> http://coders.talend.com/
>>
>


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Re: Error handling in out interceptor in jaxrs

Posted by Rice Yeh <ri...@gmail.com>.
Hi Sergey,
  "the exceptions thrown from either in or out chains are expected to be
caught by the registered mappers". I tried it, cxf's jaxrs does not follows
what you said.

Rice

On Mon, May 16, 2016 at 4:26 PM, Sergey Beryozkin <sb...@gmail.com>
wrote:

> Hi Rice
>
> After JAXRSOutIterceptor has finished its work, the response has already
> been written out, so I guess trying to do some extra response work after
> that can not work, may be I'm missing something ?
>
> FYI, the exceptions thrown from either in or out chains are expected to be
> caught by the registered mappers, the exceptions escaped from in/out chains
> will still be tried in the default JAX-RS out fault interceptor.
>
> Cheers, Sergey
>
> On 16/05/16 05:58, Rice Yeh wrote:
>
>> In cxf jaxrs implementation, javax.ws.rs.ex.ExceptionMapper can work
>> because JAXRSInInterceptor.handleFault(...) will invokde it.
>> But JAXRSOutInterceptor.handleFault(...)  does not do as much as
>> JASRSInInterceptor does.  That is ExceptionMapper only works during IN
>> interceptor chain. My exception happens during OUT interceptor chain, That
>> is, I need to to use serlvlet api to consturct my response.
>>
>> Rice
>>
>> On Mon, May 16, 2016 at 11:16 AM, Rice Yeh <ri...@gmail.com> wrote:
>>
>> Hi,
>>>    I do release my resources in a interceptor. Just when error happens
>>> when
>>> releasing resources in my intereceptor, I like to construct my response.
>>> But since the intereceptor is after jaxrs, I cannot access jaxrs api but
>>> servlet api. So I construct my response with HttpServletResponse
>>> directly.
>>> How do I construct my response in a more cxf-friendly way?
>>> I try to use message.setContent(Response.class, ...) but it does not
>>> work.
>>>
>>> On Sun, May 15, 2016 at 10:13 PM, Andrei Shakirin <as...@talend.com>
>>> wrote:
>>>
>>> Hi Rice,
>>>>
>>>> The error said that you try to use Jetty Response in Writer mode
>>>> (getWriter()), although it was originally opened in stream mode
>>>> (getOutputStream()).
>>>> Generally I am not sure that setting headers and content directly in
>>>> Response is a good idea - this makes your code fragile and dependent on
>>>> internal CXF implementation.
>>>>
>>>> Why don't release your resources in JX-RS Filter (
>>>> http://cxf.apache.org/docs/jax-rs-filters.html) or Interceptor (
>>>> http://cxf.apache.org/docs/jax-rs-basics.html#JAX-RSBasics-Interceptors
>>>> )?
>>>>
>>>> Regards,
>>>> Andrei.
>>>>
>>>> -----Original Message-----
>>>>> From: Rice Yeh [mailto:riceyeh@gmail.com]
>>>>> Sent: Samstag, 14. Mai 2016 09:48
>>>>> To: users@cxf.apache.org
>>>>> Subject: Error handling in out interceptor in jaxrs
>>>>>
>>>>> Hi,
>>>>>    I write a outward interceptor and put it after JAXRSOutInterceptor
>>>>> to
>>>>>
>>>> clean
>>>>
>>>>> up some resouces. When cleaning up the resources, there might be some
>>>>> exceptions happening. Then I directly set the HttpServletResponse's
>>>>>
>>>> headers
>>>>
>>>>> and content to send back an appropriate response. This works even there
>>>>>
>>>> is
>>>>
>>>>> a IllegalStateException thrown when later cxf's jaxrs implementation
>>>>>
>>>> try flush
>>>>
>>>>> http headers to the HttpServletResponse like below. Any more
>>>>> appropriate
>>>>> way to achieve my goal without this exception?
>>>>>
>>>>> 2016-05-13T16:58:36.514 [qtp1980982961-89] WARN
>>>>> o.a.cxf.phase.PhaseInterceptorChain - Interceptor for {
>>>>> http://rs.security.xs/}Resource has thrown exception, unwinding now
>>>>>
>>>>> java.lang.IllegalStateException: WRITER
>>>>>
>>>>> at org.eclipse.jetty.server.Response.getOutputStream(Response.java:906)
>>>>> ~[na:na]
>>>>>
>>>>> at
>>>>>
>>>>>
>>>> org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(Abstra
>>>>
>>>>> ctHTTPDestination.java:585)
>>>>> [cxf-rt-transports-http-2.7.11.jar:2.7.11]
>>>>>
>>>>> at
>>>>> org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStre
>>>>> am.close(AbstractHTTPDestination.java:743)
>>>>> ~[cxf-rt-transports-http-2.7.11.jar:2.7.11]
>>>>>
>>>>
>>>>
>>>
>>>
>>
>
> --
> Sergey Beryozkin
>
> Talend Community Coders
> http://coders.talend.com/
>

Re: Error handling in out interceptor in jaxrs

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

After JAXRSOutIterceptor has finished its work, the response has already 
been written out, so I guess trying to do some extra response work after 
that can not work, may be I'm missing something ?

FYI, the exceptions thrown from either in or out chains are expected to 
be caught by the registered mappers, the exceptions escaped from in/out 
chains will still be tried in the default JAX-RS out fault interceptor.

Cheers, Sergey
On 16/05/16 05:58, Rice Yeh wrote:
> In cxf jaxrs implementation, javax.ws.rs.ex.ExceptionMapper can work
> because JAXRSInInterceptor.handleFault(...) will invokde it.
> But JAXRSOutInterceptor.handleFault(...)  does not do as much as
> JASRSInInterceptor does.  That is ExceptionMapper only works during IN
> interceptor chain. My exception happens during OUT interceptor chain, That
> is, I need to to use serlvlet api to consturct my response.
>
> Rice
>
> On Mon, May 16, 2016 at 11:16 AM, Rice Yeh <ri...@gmail.com> wrote:
>
>> Hi,
>>    I do release my resources in a interceptor. Just when error happens when
>> releasing resources in my intereceptor, I like to construct my response.
>> But since the intereceptor is after jaxrs, I cannot access jaxrs api but
>> servlet api. So I construct my response with HttpServletResponse directly.
>> How do I construct my response in a more cxf-friendly way?
>> I try to use message.setContent(Response.class, ...) but it does not work.
>>
>> On Sun, May 15, 2016 at 10:13 PM, Andrei Shakirin <as...@talend.com>
>> wrote:
>>
>>> Hi Rice,
>>>
>>> The error said that you try to use Jetty Response in Writer mode
>>> (getWriter()), although it was originally opened in stream mode
>>> (getOutputStream()).
>>> Generally I am not sure that setting headers and content directly in
>>> Response is a good idea - this makes your code fragile and dependent on
>>> internal CXF implementation.
>>>
>>> Why don't release your resources in JX-RS Filter (
>>> http://cxf.apache.org/docs/jax-rs-filters.html) or Interceptor (
>>> http://cxf.apache.org/docs/jax-rs-basics.html#JAX-RSBasics-Interceptors)?
>>>
>>> Regards,
>>> Andrei.
>>>
>>>> -----Original Message-----
>>>> From: Rice Yeh [mailto:riceyeh@gmail.com]
>>>> Sent: Samstag, 14. Mai 2016 09:48
>>>> To: users@cxf.apache.org
>>>> Subject: Error handling in out interceptor in jaxrs
>>>>
>>>> Hi,
>>>>    I write a outward interceptor and put it after JAXRSOutInterceptor to
>>> clean
>>>> up some resouces. When cleaning up the resources, there might be some
>>>> exceptions happening. Then I directly set the HttpServletResponse's
>>> headers
>>>> and content to send back an appropriate response. This works even there
>>> is
>>>> a IllegalStateException thrown when later cxf's jaxrs implementation
>>> try flush
>>>> http headers to the HttpServletResponse like below. Any more appropriate
>>>> way to achieve my goal without this exception?
>>>>
>>>> 2016-05-13T16:58:36.514 [qtp1980982961-89] WARN
>>>> o.a.cxf.phase.PhaseInterceptorChain - Interceptor for {
>>>> http://rs.security.xs/}Resource has thrown exception, unwinding now
>>>>
>>>> java.lang.IllegalStateException: WRITER
>>>>
>>>> at org.eclipse.jetty.server.Response.getOutputStream(Response.java:906)
>>>> ~[na:na]
>>>>
>>>> at
>>>>
>>> org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(Abstra
>>>> ctHTTPDestination.java:585)
>>>> [cxf-rt-transports-http-2.7.11.jar:2.7.11]
>>>>
>>>> at
>>>> org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStre
>>>> am.close(AbstractHTTPDestination.java:743)
>>>> ~[cxf-rt-transports-http-2.7.11.jar:2.7.11]
>>>
>>
>>
>


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Re: Error handling in out interceptor in jaxrs

Posted by Rice Yeh <ri...@gmail.com>.
In cxf jaxrs implementation, javax.ws.rs.ex.ExceptionMapper can work
because JAXRSInInterceptor.handleFault(...) will invokde it.
But JAXRSOutInterceptor.handleFault(...)  does not do as much as
JASRSInInterceptor does.  That is ExceptionMapper only works during IN
interceptor chain. My exception happens during OUT interceptor chain, That
is, I need to to use serlvlet api to consturct my response.

Rice

On Mon, May 16, 2016 at 11:16 AM, Rice Yeh <ri...@gmail.com> wrote:

> Hi,
>   I do release my resources in a interceptor. Just when error happens when
> releasing resources in my intereceptor, I like to construct my response.
> But since the intereceptor is after jaxrs, I cannot access jaxrs api but
> servlet api. So I construct my response with HttpServletResponse directly.
> How do I construct my response in a more cxf-friendly way?
> I try to use message.setContent(Response.class, ...) but it does not work.
>
> On Sun, May 15, 2016 at 10:13 PM, Andrei Shakirin <as...@talend.com>
> wrote:
>
>> Hi Rice,
>>
>> The error said that you try to use Jetty Response in Writer mode
>> (getWriter()), although it was originally opened in stream mode
>> (getOutputStream()).
>> Generally I am not sure that setting headers and content directly in
>> Response is a good idea - this makes your code fragile and dependent on
>> internal CXF implementation.
>>
>> Why don't release your resources in JX-RS Filter (
>> http://cxf.apache.org/docs/jax-rs-filters.html) or Interceptor (
>> http://cxf.apache.org/docs/jax-rs-basics.html#JAX-RSBasics-Interceptors)?
>>
>> Regards,
>> Andrei.
>>
>> > -----Original Message-----
>> > From: Rice Yeh [mailto:riceyeh@gmail.com]
>> > Sent: Samstag, 14. Mai 2016 09:48
>> > To: users@cxf.apache.org
>> > Subject: Error handling in out interceptor in jaxrs
>> >
>> > Hi,
>> >   I write a outward interceptor and put it after JAXRSOutInterceptor to
>> clean
>> > up some resouces. When cleaning up the resources, there might be some
>> > exceptions happening. Then I directly set the HttpServletResponse's
>> headers
>> > and content to send back an appropriate response. This works even there
>> is
>> > a IllegalStateException thrown when later cxf's jaxrs implementation
>> try flush
>> > http headers to the HttpServletResponse like below. Any more appropriate
>> > way to achieve my goal without this exception?
>> >
>> > 2016-05-13T16:58:36.514 [qtp1980982961-89] WARN
>> > o.a.cxf.phase.PhaseInterceptorChain - Interceptor for {
>> > http://rs.security.xs/}Resource has thrown exception, unwinding now
>> >
>> > java.lang.IllegalStateException: WRITER
>> >
>> > at org.eclipse.jetty.server.Response.getOutputStream(Response.java:906)
>> > ~[na:na]
>> >
>> > at
>> >
>> org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(Abstra
>> > ctHTTPDestination.java:585)
>> > [cxf-rt-transports-http-2.7.11.jar:2.7.11]
>> >
>> > at
>> > org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStre
>> > am.close(AbstractHTTPDestination.java:743)
>> > ~[cxf-rt-transports-http-2.7.11.jar:2.7.11]
>>
>
>

Re: Error handling in out interceptor in jaxrs

Posted by Rice Yeh <ri...@gmail.com>.
Hi,
  I do release my resources in a interceptor. Just when error happens when
releasing resources in my intereceptor, I like to construct my response.
But since the intereceptor is after jaxrs, I cannot access jaxrs api but
servlet api. So I construct my response with HttpServletResponse directly.
How do I construct my response in a more cxf-friendly way?
I try to use message.setContent(Response.class, ...) but it does not work.

On Sun, May 15, 2016 at 10:13 PM, Andrei Shakirin <as...@talend.com>
wrote:

> Hi Rice,
>
> The error said that you try to use Jetty Response in Writer mode
> (getWriter()), although it was originally opened in stream mode
> (getOutputStream()).
> Generally I am not sure that setting headers and content directly in
> Response is a good idea - this makes your code fragile and dependent on
> internal CXF implementation.
>
> Why don't release your resources in JX-RS Filter (
> http://cxf.apache.org/docs/jax-rs-filters.html) or Interceptor (
> http://cxf.apache.org/docs/jax-rs-basics.html#JAX-RSBasics-Interceptors)?
>
> Regards,
> Andrei.
>
> > -----Original Message-----
> > From: Rice Yeh [mailto:riceyeh@gmail.com]
> > Sent: Samstag, 14. Mai 2016 09:48
> > To: users@cxf.apache.org
> > Subject: Error handling in out interceptor in jaxrs
> >
> > Hi,
> >   I write a outward interceptor and put it after JAXRSOutInterceptor to
> clean
> > up some resouces. When cleaning up the resources, there might be some
> > exceptions happening. Then I directly set the HttpServletResponse's
> headers
> > and content to send back an appropriate response. This works even there
> is
> > a IllegalStateException thrown when later cxf's jaxrs implementation try
> flush
> > http headers to the HttpServletResponse like below. Any more appropriate
> > way to achieve my goal without this exception?
> >
> > 2016-05-13T16:58:36.514 [qtp1980982961-89] WARN
> > o.a.cxf.phase.PhaseInterceptorChain - Interceptor for {
> > http://rs.security.xs/}Resource has thrown exception, unwinding now
> >
> > java.lang.IllegalStateException: WRITER
> >
> > at org.eclipse.jetty.server.Response.getOutputStream(Response.java:906)
> > ~[na:na]
> >
> > at
> > org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(Abstra
> > ctHTTPDestination.java:585)
> > [cxf-rt-transports-http-2.7.11.jar:2.7.11]
> >
> > at
> > org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStre
> > am.close(AbstractHTTPDestination.java:743)
> > ~[cxf-rt-transports-http-2.7.11.jar:2.7.11]
>

RE: Error handling in out interceptor in jaxrs

Posted by Andrei Shakirin <as...@talend.com>.
Hi Rice,

The error said that you try to use Jetty Response in Writer mode (getWriter()), although it was originally opened in stream mode (getOutputStream()).
Generally I am not sure that setting headers and content directly in Response is a good idea - this makes your code fragile and dependent on internal CXF implementation.

Why don't release your resources in JX-RS Filter (http://cxf.apache.org/docs/jax-rs-filters.html) or Interceptor (http://cxf.apache.org/docs/jax-rs-basics.html#JAX-RSBasics-Interceptors)?

Regards,
Andrei.

> -----Original Message-----
> From: Rice Yeh [mailto:riceyeh@gmail.com]
> Sent: Samstag, 14. Mai 2016 09:48
> To: users@cxf.apache.org
> Subject: Error handling in out interceptor in jaxrs
> 
> Hi,
>   I write a outward interceptor and put it after JAXRSOutInterceptor to clean
> up some resouces. When cleaning up the resources, there might be some
> exceptions happening. Then I directly set the HttpServletResponse's headers
> and content to send back an appropriate response. This works even there is
> a IllegalStateException thrown when later cxf's jaxrs implementation try flush
> http headers to the HttpServletResponse like below. Any more appropriate
> way to achieve my goal without this exception?
> 
> 2016-05-13T16:58:36.514 [qtp1980982961-89] WARN
> o.a.cxf.phase.PhaseInterceptorChain - Interceptor for {
> http://rs.security.xs/}Resource has thrown exception, unwinding now
> 
> java.lang.IllegalStateException: WRITER
> 
> at org.eclipse.jetty.server.Response.getOutputStream(Response.java:906)
> ~[na:na]
> 
> at
> org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(Abstra
> ctHTTPDestination.java:585)
> [cxf-rt-transports-http-2.7.11.jar:2.7.11]
> 
> at
> org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStre
> am.close(AbstractHTTPDestination.java:743)
> ~[cxf-rt-transports-http-2.7.11.jar:2.7.11]