You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by nikosdim <ni...@gmail.com> on 2014/01/16 15:54:23 UTC

Custom Response from handleFault [JAX-RS]

Hi

I have an inbound interceptor were in a case of an exception I throw a new
Fault(e) exception. 
What I want to achieve is to return a custom Response to the client from the
handleFault method of my interceptor.

I ve tried this but with no luck:

 public class ESMAPISecurityInterceptor extends 
                AbstractPhaseInterceptor<Message> { 

        public ESMAPISecurityInterceptor(String phase) { 
                super(phase); 
        } 

        @Override 
        public void handleMessage(Message message) throws Fault { 
                try { 
                        Class<?> c = Class.forName("an.invalid.class.name"); 
                } catch (ClassNotFoundException e) { 
                        throw new Fault(e);
                } 
        } 

        @Override 
        public void handleFault(Message message) { 
                 Response response = Response
				.status(Status.INTERNAL_SERVER_ERROR)
				.type(MediaType.APPLICATION_JSON)
				.entity("error message").build();
		message.setContent(Response.class, response);
		message.getExchange().put(Response.class, response);
        } 
} 

Is this possible? Is there any other way that I could return a custom
message back to the client from the handleFault() method? 

Thanks



--
View this message in context: http://cxf.547215.n5.nabble.com/Custom-Response-from-handleFault-JAX-RS-tp5738651.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Custom Response from handleFault [JAX-RS]

Posted by Sergey Beryozkin <sb...@gmail.com>.
My fault, sorry about wasting your time.
I've checked the source, the exceptions escaped from the cxf 
interceptors can be properly mapped only in 3.0.0-milestone2 due to be 
release very shortly.

Can you try a trunk snapshot please just to double check ?

I'm wondering though why you are not seeing it working in 
3.0.0.milestone1, hmm, the miliestone2 does test this scenario and so 
3.0.0.milestone1 though the fix in m1 is temporary.

Have a look at 
http://svn.apache.org/repos/asf/cxf/tags/cxf-3.0.0-milestone1/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java, 
handleFault()

I'm not sure why it does not work given PRE_LOGICAL is after UNMARSHALL 
which is where JAXRSInInterceptor sits.
Please download m1 source and debug if you are interested, as I said it 
absolutely must work in m2

Sergey

On 22/01/14 10:32, nikosdim wrote:
> I also changed my xml file
>
> I deleted the
>
>          <cxf:bus>
>                  <cxf:inInterceptors>
>                          <ref bean="MyLoginInInterceptor" />
>                  </cxf:inInterceptors>
>                  <cxf:outInterceptors>
>                          <ref bean="MyLoginOutInterceptor" />
>                  </cxf:outInterceptors>
>          </cxf:bus>
>
> and added this under the <jaxrs:server> tag
>
> 		<jaxrs:inInterceptors>
> 			<ref bean="MyLoginInInterceptor" />
> 		</jaxrs:inInterceptors>
> 		<jaxrs:outInterceptors>
> 			<ref bean="MyLoginOutInterceptor" />
> 		</jaxrs:outInterceptors>
>
>
> but still the same problem. My exception mapper is never invoked and
> WebApplicationException is thrown as normal.
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Custom-Response-from-handleFault-JAX-RS-tp5738651p5738955.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>


-- 
Sergey Beryozkin

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

Blog: http://sberyozkin.blogspot.com

Re: Custom Response from handleFault [JAX-RS]

Posted by nikosdim <ni...@gmail.com>.
I also changed my xml file 

I deleted the

        <cxf:bus>
                <cxf:inInterceptors>
                        <ref bean="MyLoginInInterceptor" />        
                </cxf:inInterceptors>
                <cxf:outInterceptors>
                        <ref bean="MyLoginOutInterceptor" />
                </cxf:outInterceptors>
        </cxf:bus>

and added this under the <jaxrs:server> tag

		<jaxrs:inInterceptors>
			<ref bean="MyLoginInInterceptor" />
		</jaxrs:inInterceptors>
		<jaxrs:outInterceptors>
			<ref bean="MyLoginOutInterceptor" />
		</jaxrs:outInterceptors>


but still the same problem. My exception mapper is never invoked and
WebApplicationException is thrown as normal.



--
View this message in context: http://cxf.547215.n5.nabble.com/Custom-Response-from-handleFault-JAX-RS-tp5738651p5738955.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Custom Response from handleFault [JAX-RS]

Posted by nikosdim <ni...@gmail.com>.
Hi Sergey

I am just throwing a WebApplicationException in my inbound interceptor. See
below how exactly is my code.

-----------------------------------------------------------------

import javax.ws.rs.WebApplicationException;

public class MyLoginInInterceptor extends AbstractPhaseInterceptor<Message>
{ 

        public MyLoginInInterceptor() { 
                super(Phase.PRE_LOGICAL); 
        } 

        public void handleMessage(Message message) throws Fault { 
                //other code
                throw new WebApplicationException(); 
        } 
}
-----------------------------------------------------------------
import javax.ws.rs.ext.ExceptionMapper;

public class MyExceptionMapper implements
ExceptionMapper<WebApplicationException>{ 
        @Override 
        public Response toResponse(WebApplicationException exception) { 
                Response response = Response 
                                .status(Status.INTERNAL_SERVER_ERROR) 
                                .type(MediaType.APPLICATION_JSON) 
                                .entity("{\"message\":\"Please contact
system administrators.\"}").build(); 
                return response; 
        } 
} 

-----------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans>
	<import resource="classpath:META-INF/cxf/cxf.xml" />

	<bean class="com.ba.oasis.interceptors.MyLoginInInterceptor"
id="MyLoginInInterceptor"/>
	<bean class="com.ba.oasis.interceptors.MyLoginOutInterceptor"
id="MyLoginOutInterceptor"/>

	<cxf:bus>
		<cxf:inInterceptors>
			<ref bean="MyLoginInInterceptor" />	
		</cxf:inInterceptors>
		<cxf:outInterceptors>
			<ref bean="MyLoginOutInterceptor" />
		</cxf:outInterceptors>
	</cxf:bus>

	<jaxrs:server address="/" id="services">
		<jaxrs:providers>
			<bean class="com.ba.oasis.exceptions.MyExceptionMapper"/>
			<bean class="org.apache.cxf.jaxrs.ext.search.SearchContextProvider" />
			<bean class="com.ba.oasis.utils.MyJAXBProvider" />
			<bean class="com.ba.oasis.utils.MyJSONProvider" id="JsonpProvider">
				<property name="ignoreNamespaces" value="true" />
				<property name="dropRootElement" value="false" />
				<property name="supportUnwrapped" value="true" />
			</bean>
		</jaxrs:providers>
		<jaxrs:serviceBeans>
			<bean class="com.ba.oasis.resources.HpsimManagedObjectsResource" />
		</jaxrs:serviceBeans>

		<jaxrs:extensionMappings>
			<entry key="json" value="application/json" />
			<entry key="xml" value="application/xml" />
			<entry key="jsonp" value="application/javascript" />
		</jaxrs:extensionMappings>
		
		<jaxrs:features>
			<cxf:logging />
		</jaxrs:features>
		<jaxrs:properties>
			<entry key="ignore.matrix.parameters" value="true" />
			<entry key="search.lax.property.match" value="true" />
			
		</jaxrs:properties>
	</jaxrs:server>
</beans>


To be honest I didn't understand what you mean by 
"if you throw a custom WebApplicationException with Response containing 
some text then the mappers will be ignored by default - do you do it in 
your code ?" 

That's why I am listing my code. 


Do you see any problem in my configuration?






--
View this message in context: http://cxf.547215.n5.nabble.com/Custom-Response-from-handleFault-JAX-RS-tp5738651p5738914.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Custom Response from handleFault [JAX-RS]

Posted by Sergey Beryozkin <sb...@gmail.com>.
On 17/01/14 16:44, nikosdim wrote:
> I have CXF 3.0.0-milestone1.
>
> I deleted the property map.cxf.interceptor.fault from my xml file.
>
> Still does not invoking my exception mapper.

Works for me as expected. I have a test where a custom in interceptor at 
PRE_INVOKE throws WebApplicationException and it is mapped to a default 
WebApplicationException.
I changed the interceptor to run at PRE_LOGICAL and registered a custom 
mapper and all works as expected.

I can think of only one possible reason it does not work for you:
- in CXF 3.0.0 a rather problematic spec text is enforced by default (to 
get past some of early TCK tests):
if you throw a custom WebApplicationException with Response containing 
some text then the mappers will be ignored by default - do you do it in 
your code ?

Sergey

>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Custom-Response-from-handleFault-JAX-RS-tp5738651p5738726.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>


-- 
Sergey Beryozkin

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

Blog: http://sberyozkin.blogspot.com

Re: Custom Response from handleFault [JAX-RS]

Posted by nikosdim <ni...@gmail.com>.
I have CXF 3.0.0-milestone1. 

I deleted the property map.cxf.interceptor.fault from my xml file. 

Still does not invoking my exception mapper. 



--
View this message in context: http://cxf.547215.n5.nabble.com/Custom-Response-from-handleFault-JAX-RS-tp5738651p5738726.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Custom Response from handleFault [JAX-RS]

Posted by Sergey Beryozkin <sb...@gmail.com>.
Sorry, did you mean CXF 3.0.0-milestone1 or CXF 2.7.8 ? You set the 
property which Andrey referred to in scope of working with CXF 2.7.8...

Sergey
On 17/01/14 16:29, nikosdim wrote:
> I am throwing the WebApplicationException() in my handleMessage method. I am
> not throwing a Fault.
>
> My Interceptor
>
> public class MyLoginInInterceptor extends AbstractPhaseInterceptor<Message>
> {
>
> 	public MyLoginInInterceptor() {
> 		super(Phase.PRE_LOGICAL);
> 	}
>
> 	public void handleMessage(Message message) throws Fault {
> 		
> 		throw new WebApplicationException();
> 	}
> }
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Custom-Response-from-handleFault-JAX-RS-tp5738651p5738717.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>


Re: Custom Response from handleFault [JAX-RS]

Posted by nikosdim <ni...@gmail.com>.
I am throwing the WebApplicationException() in my handleMessage method. I am
not throwing a Fault.

My Interceptor

public class MyLoginInInterceptor extends AbstractPhaseInterceptor<Message>
{

	public MyLoginInInterceptor() {
		super(Phase.PRE_LOGICAL);
	}

	public void handleMessage(Message message) throws Fault {
		
		throw new WebApplicationException();
	}
}



--
View this message in context: http://cxf.547215.n5.nabble.com/Custom-Response-from-handleFault-JAX-RS-tp5738651p5738717.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Custom Response from handleFault [JAX-RS]

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi, do you still throw a Fault from your interceptor ? Or 
WebApplicationException ?

Cheers, Sergey
On 17/01/14 16:17, nikosdim wrote:
> Hi Andrei
>
> I updated to cxf 3.0.0 and I am trying to make exception mapping to work but
> not luck until now.
>
> My exception mapper:
>
> public class MyExceptionMapper implements
> ExceptionMapper<WebApplicationException>{
> 	@Override
> 	public Response toResponse(WebApplicationException exception) {
> 		Response response = Response
> 				.status(Status.INTERNAL_SERVER_ERROR)
> 				.type(MediaType.APPLICATION_JSON)
> 				.entity("{\"message\":\"Please contact system
> administrators.\"}").build();
> 		return response;
> 	}
> }
>
>
> My inbound interceptor is in PRE_LOGICAL phase which is after the UNMARSHAL
> phase (but it should not matter since it is version 3.0.0) and in the
> interceptor I have "throw new WebApplicationException();"
>
> in my xml configuration file I have registered my interceptor
>
> 	<cxf:bus>
> 		<cxf:inInterceptors>
> 			<bean class="com.ba.oasis.interceptors.MyLoginInInterceptor"
> id="MyLoginInInterceptor"/>
> 		</cxf:inInterceptors>
> 		<cxf:outInterceptors>
> 		</cxf:outInterceptors>
> 	</cxf:bus>
>
> and also the mapper in the providers
>
> 	<jaxrs:server address="/" id="services">
> 		<jaxrs:serviceBeans>
> 			<bean class="com.ba.oasis.resources.HpsimManagedObjectsResource" />
> 		</jaxrs:serviceBeans>
> 		<jaxrs:providers>
> 			<bean class="org.apache.cxf.jaxrs.ext.search.SearchContextProvider" />
> 			<bean class="com.ba.oasis.utils.MyJAXBProvider" />
> 			<bean class="com.ba.oasis.utils.MyJSONProvider" id="JsonpProvider">
> 				<property name="ignoreNamespaces" value="true" />
> 				<property name="dropRootElement" value="false" />
> 				<property name="supportUnwrapped" value="true" />
> 			</bean>
> 			<bean class="com.ba.oasis.exceptions.MyExceptionMapper"/>
> 		</jaxrs:providers>
> 		<jaxrs:properties>
> 			<entry key="ignore.matrix.parameters" value="true" />
> 			<entry key="search.lax.property.match" value="true" />
> 			<entry key="map.cxf.interceptor.fault" value="true" />
> 		</jaxrs:properties>
> 	</jaxrs:server>
>
> When I am calling my service it never gets to the  "toResponse()" of my
> mapper. What am I doing wrong?
>
> Thanks
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Custom-Response-from-handleFault-JAX-RS-tp5738651p5738714.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>



RE: Custom Response from handleFault [JAX-RS]

Posted by nikosdim <ni...@gmail.com>.
Hi Andrei

I updated to cxf 3.0.0 and I am trying to make exception mapping to work but
not luck until now. 

My exception mapper:

public class MyExceptionMapper implements
ExceptionMapper<WebApplicationException>{
	@Override
	public Response toResponse(WebApplicationException exception) {
		Response response = Response
				.status(Status.INTERNAL_SERVER_ERROR)
				.type(MediaType.APPLICATION_JSON)
				.entity("{\"message\":\"Please contact system
administrators.\"}").build();
		return response;
	}
}


My inbound interceptor is in PRE_LOGICAL phase which is after the UNMARSHAL
phase (but it should not matter since it is version 3.0.0) and in the
interceptor I have "throw new WebApplicationException();"

in my xml configuration file I have registered my interceptor 

	<cxf:bus>
		<cxf:inInterceptors>
			<bean class="com.ba.oasis.interceptors.MyLoginInInterceptor"
id="MyLoginInInterceptor"/>
		</cxf:inInterceptors>
		<cxf:outInterceptors>
		</cxf:outInterceptors>
	</cxf:bus>

and also the mapper in the providers

	<jaxrs:server address="/" id="services">
		<jaxrs:serviceBeans>
			<bean class="com.ba.oasis.resources.HpsimManagedObjectsResource" />
		</jaxrs:serviceBeans>
		<jaxrs:providers>
			<bean class="org.apache.cxf.jaxrs.ext.search.SearchContextProvider" />
			<bean class="com.ba.oasis.utils.MyJAXBProvider" />
			<bean class="com.ba.oasis.utils.MyJSONProvider" id="JsonpProvider">
				<property name="ignoreNamespaces" value="true" />
				<property name="dropRootElement" value="false" />
				<property name="supportUnwrapped" value="true" />
			</bean>
			<bean class="com.ba.oasis.exceptions.MyExceptionMapper"/>
		</jaxrs:providers>
		<jaxrs:properties>
			<entry key="ignore.matrix.parameters" value="true" />
			<entry key="search.lax.property.match" value="true" />
			<entry key="map.cxf.interceptor.fault" value="true" />
		</jaxrs:properties>
	</jaxrs:server>

When I am calling my service it never gets to the  "toResponse()" of my
mapper. What am I doing wrong?

Thanks



--
View this message in context: http://cxf.547215.n5.nabble.com/Custom-Response-from-handleFault-JAX-RS-tp5738651p5738714.html
Sent from the cxf-user mailing list archive at Nabble.com.

RE: Custom Response from handleFault [JAX-RS]

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

For CXF 2.7.8:
If your interceptor is called after JAXRSInInterceptor (Phase.UNMARSHAL) for inbound chain and before JAXRSOutInterceptor (Phase.MARSHAL) for outbound chain, you can use normal exception mapper to map exception thrown form interceptor to response (http://cxf.apache.org/docs/jax-rs-basics.html#JAX-RSBasics-MappingexceptionsthrownfromCXFinterceptors). It is necessary to set "map.cxf.interceptor.fault" contextual property to true.

For CXF 3.0.0 exceptions from all interceptors will go through exception mapper by default.

In exeption mapper you will be able to build custom response for the exception.

Regards,
Andrei.

> -----Original Message-----
> From: nikosdim [mailto:nick.dimizas@gmail.com]
> Sent: Donnerstag, 16. Januar 2014 15:54
> To: users@cxf.apache.org
> Subject: Custom Response from handleFault [JAX-RS]
> 
> Hi
> 
> I have an inbound interceptor were in a case of an exception I throw a new
> Fault(e) exception.
> What I want to achieve is to return a custom Response to the client from the
> handleFault method of my interceptor.
> 
> I ve tried this but with no luck:
> 
>  public class ESMAPISecurityInterceptor extends
>                 AbstractPhaseInterceptor<Message> {
> 
>         public ESMAPISecurityInterceptor(String phase) {
>                 super(phase);
>         }
> 
>         @Override
>         public void handleMessage(Message message) throws Fault {
>                 try {
>                         Class<?> c = Class.forName("an.invalid.class.name");
>                 } catch (ClassNotFoundException e) {
>                         throw new Fault(e);
>                 }
>         }
> 
>         @Override
>         public void handleFault(Message message) {
>                  Response response = Response
> 				.status(Status.INTERNAL_SERVER_ERROR)
> 				.type(MediaType.APPLICATION_JSON)
> 				.entity("error message").build();
> 		message.setContent(Response.class, response);
> 		message.getExchange().put(Response.class, response);
>         }
> }
> 
> Is this possible? Is there any other way that I could return a custom message
> back to the client from the handleFault() method?
> 
> Thanks
> 
> 
> 
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Custom-
> Response-from-handleFault-JAX-RS-tp5738651.html
> Sent from the cxf-user mailing list archive at Nabble.com.