You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cxf.apache.org by jini <ba...@sk.com> on 2011/03/04 02:16:39 UTC

How can i get my custom exception via dosgi-cxf(RS)?

Hi there.

I'm using the dosgi-cxf(rs) on eclipse 3.6.1.

I have register a exception/responseException Mapper, but i cannot get my
exception.

I got only Invocation Exception resource throws my custom exception ,but it
works when i fix cxf source
code(org.apache.cxf.dosgi.dsw.handlers.ServiceInvocationHandler)

ServiceInvocationHandler.java
public class ServiceInvocationHandler implements InvocationHandler {
    private final static String REMOTE_EXCEPTION_TYPE = "REMOTE";
    private static final Collection OBJECT_METHODS = 
        Arrays.asList(Object.class.getMethods());

    private Map>> exceptionsMap
        = new HashMap>>();
    private Object serviceObject;
    
    public ServiceInvocationHandler(Object serviceObject, Class<?> iType) {
        this.serviceObject = serviceObject;
        introspectType(iType);
    }
    
    public Object invoke(Object proxy, final Method m, Object[] params)
throws Throwable {
        if (OBJECT_METHODS.contains(m)) {
            if (m.getName().equals("equals")) {
                params = new Object[]
{Proxy.getInvocationHandler(params[0])};
            }
            return m.invoke(this, params);
        }

        ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
        try {            
           
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
            final Object[] paramsFinal = params;
            return AccessController.doPrivileged(new
PrivilegedExceptionAction() {
                public Object run() throws Exception {
                    return m.invoke(serviceObject, paramsFinal);
                }
            }); 
        } catch (Throwable ex) {
            Throwable theCause = ex.getCause() == null ? ex : ex.getCause();
            
            List> excTypes = exceptionsMap.get(m);
            if (excTypes != null) {
            	throw ex.getCause().getCause();
//                for (Class<?> type : excTypes) {
//                    if (type.isAssignableFrom(theCause.getClass())) {
//                        throw theCause;
//                    }
//                }
            }
                        
            throw new InvocationTargetException(
                    new ServiceException(REMOTE_EXCEPTION_TYPE, theCause));
        } finally {
            Thread.currentThread().setContextClassLoader(oldCl);
        }
    }

    private void introspectType(Class<?> iType) {
        for (Method m : iType.getDeclaredMethods()) {
            for (Class<?> excType : m.getExceptionTypes()) {
                if (Exception.class.isAssignableFrom(excType)) {
                    List> types = exceptionsMap.get(m);
                    if (types == null) {
                        types = new ArrayList>();
                        exceptionsMap.put(m, types);
                    }
                    types.add(excType);
                }
            }
        }
    }
}


Here my stuff. 

Exception
public class ResourceException extends WebApplicationException {

	/**
	 * @see java.lang.Throwable#getMessage()
	 */
	@Override
	public String getMessage() {
		return message;
	}

	String message;

	/**
	 * @param arg0
	 */
	public ResourceException(Response arg0) {

		super(arg0);
		int read = 1;
		byte buffer[] = new byte[2048];
		InputStream in = (InputStream) this.getResponse().getEntity();
		StringBuffer stringBuffer = new StringBuffer();
		while (read > 0) {
			try {
				read = in.read(buffer);
				stringBuffer.append(buffer);
			} catch (IOException e) {
			}
			System.out.print(new String(buffer));
		}
		message = stringBuffer.toString();
	}

	public ResourceException(int status,String message) {
	
super(Response.status(status).entity(message).type("application/xml").build());
	}
}

Exception Mapper
public class CustomExceptionMapper implements ExceptionMapper {
	/**
	 * @see javax.ws.rs.ext.ExceptionMapper#toResponse(java.lang.Throwable)
	 */
	public Response toResponse(ResourceException arg0) {
		Response response = Response.status(arg0.getResponse().getStatus())
			
.type(MediaType.TEXT_PLAIN).entity(arg0.getResponse().getEntity()).build();
		return response;
	}

}

Response Exception Mapper
public class CustomResponseExceptionMapper implements
ResponseExceptionMapper {

	public ResourceException fromResponse(Response r) {
		return new ResourceException(r);
	}

}

DS
<?xml version="1.0" encoding="UTF-8"?>

     
      
   
   
   
   
   
   
   
   nexcore.platform.foundation.core.exception.CustomExceptionMapper
nexcore.platform.foundation.core.exception.CustomResponseExceptionMapper



Remote-Service
<?xml version="1.0" encoding="UTF-8" ?> 


  
   
  * 
  org.apache.cxf.rs 
  HTTP 
  http://localhost:8080/ncs/fd/acm/applyer
  aegis 
  nexcore.platform.foundation.core.exception.CustomExceptionMapper,
nexcore.platform.foundation.core.exception.CustomResponseExceptionMapper

  
  

  


--
View this message in context: http://cxf.547215.n5.nabble.com/How-can-i-get-my-custom-exception-via-dosgi-cxf-RS-tp3408859p3408859.html
Sent from the cxf-dev mailing list archive at Nabble.com.

Re: How can i get my custom exception via dosgi-cxf(RS)?

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

To show some activity on the DOSGI RI front :-), I created this JIRA:

https://issues.apache.org/jira/browse/DOSGI-91

for this issue be tracked.

In fact, realistically, I can probably look at this issue as well as at the
possibility of reusing the CXF configuration, but at this stage I can only
say that the patch is welcome :-)

Cheers, Sergey

On Fri, Mar 4, 2011 at 1:16 AM, jini <ba...@sk.com> wrote:

> Hi there.
>
> I'm using the dosgi-cxf(rs) on eclipse 3.6.1.
>
> I have register a exception/responseException Mapper, but i cannot get my
> exception.
>
> I got only Invocation Exception resource throws my custom exception ,but it
> works when i fix cxf source
> code(org.apache.cxf.dosgi.dsw.handlers.ServiceInvocationHandler)
>
> ServiceInvocationHandler.java
> public class ServiceInvocationHandler implements InvocationHandler {
>    private final static String REMOTE_EXCEPTION_TYPE = "REMOTE";
>    private static final Collection OBJECT_METHODS =
>        Arrays.asList(Object.class.getMethods());
>
>    private Map>> exceptionsMap
>        = new HashMap>>();
>    private Object serviceObject;
>
>    public ServiceInvocationHandler(Object serviceObject, Class<?> iType) {
>        this.serviceObject = serviceObject;
>        introspectType(iType);
>    }
>
>    public Object invoke(Object proxy, final Method m, Object[] params)
> throws Throwable {
>        if (OBJECT_METHODS.contains(m)) {
>            if (m.getName().equals("equals")) {
>                params = new Object[]
> {Proxy.getInvocationHandler(params[0])};
>            }
>            return m.invoke(this, params);
>        }
>
>        ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
>        try {
>
> Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
>            final Object[] paramsFinal = params;
>            return AccessController.doPrivileged(new
> PrivilegedExceptionAction() {
>                public Object run() throws Exception {
>                    return m.invoke(serviceObject, paramsFinal);
>                }
>            });
>        } catch (Throwable ex) {
>            Throwable theCause = ex.getCause() == null ? ex : ex.getCause();
>
>            List> excTypes = exceptionsMap.get(m);
>            if (excTypes != null) {
>                throw ex.getCause().getCause();
> //                for (Class<?> type : excTypes) {
> //                    if (type.isAssignableFrom(theCause.getClass())) {
> //                        throw theCause;
> //                    }
> //                }
>            }
>
>            throw new InvocationTargetException(
>                    new ServiceException(REMOTE_EXCEPTION_TYPE, theCause));
>        } finally {
>            Thread.currentThread().setContextClassLoader(oldCl);
>        }
>    }
>
>    private void introspectType(Class<?> iType) {
>        for (Method m : iType.getDeclaredMethods()) {
>            for (Class<?> excType : m.getExceptionTypes()) {
>                if (Exception.class.isAssignableFrom(excType)) {
>                    List> types = exceptionsMap.get(m);
>                    if (types == null) {
>                        types = new ArrayList>();
>                        exceptionsMap.put(m, types);
>                    }
>                    types.add(excType);
>                }
>            }
>        }
>    }
> }
>
>
> Here my stuff.
>
> Exception
> public class ResourceException extends WebApplicationException {
>
>        /**
>         * @see java.lang.Throwable#getMessage()
>         */
>        @Override
>        public String getMessage() {
>                return message;
>        }
>
>        String message;
>
>        /**
>         * @param arg0
>         */
>        public ResourceException(Response arg0) {
>
>                super(arg0);
>                int read = 1;
>                byte buffer[] = new byte[2048];
>                InputStream in = (InputStream)
> this.getResponse().getEntity();
>                StringBuffer stringBuffer = new StringBuffer();
>                while (read > 0) {
>                        try {
>                                read = in.read(buffer);
>                                stringBuffer.append(buffer);
>                        } catch (IOException e) {
>                        }
>                        System.out.print(new String(buffer));
>                }
>                message = stringBuffer.toString();
>        }
>
>        public ResourceException(int status,String message) {
>
>
> super(Response.status(status).entity(message).type("application/xml").build());
>        }
> }
>
> Exception Mapper
> public class CustomExceptionMapper implements ExceptionMapper {
>        /**
>         * @see
> javax.ws.rs.ext.ExceptionMapper#toResponse(java.lang.Throwable)
>         */
>        public Response toResponse(ResourceException arg0) {
>                Response response =
> Response.status(arg0.getResponse().getStatus())
>
> .type(MediaType.TEXT_PLAIN).entity(arg0.getResponse().getEntity()).build();
>                return response;
>        }
>
> }
>
> Response Exception Mapper
> public class CustomResponseExceptionMapper implements
> ResponseExceptionMapper {
>
>        public ResourceException fromResponse(Response r) {
>                return new ResourceException(r);
>        }
>
> }
>
> DS
> <?xml version="1.0" encoding="UTF-8"?>
>
>
>
>
>
>
>
>
>
>
>   nexcore.platform.foundation.core.exception.CustomExceptionMapper
> nexcore.platform.foundation.core.exception.CustomResponseExceptionMapper
>
>
>
> Remote-Service
> <?xml version="1.0" encoding="UTF-8" ?>
>
>
>
>
>  *
>  org.apache.cxf.rs
>  HTTP
>  http://localhost:8080/ncs/fd/acm/applyer
>  aegis
>  nexcore.platform.foundation.core.exception.CustomExceptionMapper,
> nexcore.platform.foundation.core.exception.CustomResponseExceptionMapper
>
>
>
>
>
>
>
> --
> View this message in context:
> http://cxf.547215.n5.nabble.com/How-can-i-get-my-custom-exception-via-dosgi-cxf-RS-tp3408859p3408859.html
> Sent from the cxf-dev mailing list archive at Nabble.com.
>



-- 
Sergey Beryozkin

Application Integration Division of Talend <http://www.talend.com>
http://sberyozkin.blogspot.com

Re: How can i get my custom exception via dosgi-cxf(RS)?

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

Forwarding to the users list...

I've checked the DOSGI source and custom ResponseExceptionMappers are not
currently checked on the client side, when the proxy is created and I guess
something else needs to be fixed there, to work it well in the context of
the DOSGI client invocation. That code works well for SOAP clients but not
so well for plain HTTP clients.

What is the type of the actual exception that is being thrown, after you
comment those lines in the invocation handler ? I'm not sure it can work for
a proxy case where only checked exceptions are checked...

thanks, Sergey

[1]
http://cxf.apache.org/apidocs/org/apache/cxf/jaxrs/client/ResponseExceptionMapper.html

On Fri, Mar 4, 2011 at 1:16 AM, jini <ba...@sk.com> wrote:

> Hi there.
>
> I'm using the dosgi-cxf(rs) on eclipse 3.6.1.
>
> I have register a exception/responseException Mapper, but i cannot get my
> exception.
>
> I got only Invocation Exception resource throws my custom exception ,but it
> works when i fix cxf source
> code(org.apache.cxf.dosgi.dsw.handlers.ServiceInvocationHandler)
>
> ServiceInvocationHandler.java
> public class ServiceInvocationHandler implements InvocationHandler {
>    private final static String REMOTE_EXCEPTION_TYPE = "REMOTE";
>    private static final Collection OBJECT_METHODS =
>        Arrays.asList(Object.class.getMethods());
>
>    private Map>> exceptionsMap
>        = new HashMap>>();
>    private Object serviceObject;
>
>    public ServiceInvocationHandler(Object serviceObject, Class<?> iType) {
>        this.serviceObject = serviceObject;
>        introspectType(iType);
>    }
>
>    public Object invoke(Object proxy, final Method m, Object[] params)
> throws Throwable {
>        if (OBJECT_METHODS.contains(m)) {
>            if (m.getName().equals("equals")) {
>                params = new Object[]
> {Proxy.getInvocationHandler(params[0])};
>            }
>            return m.invoke(this, params);
>        }
>
>        ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
>        try {
>
> Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
>            final Object[] paramsFinal = params;
>            return AccessController.doPrivileged(new
> PrivilegedExceptionAction() {
>                public Object run() throws Exception {
>                    return m.invoke(serviceObject, paramsFinal);
>                }
>            });
>        } catch (Throwable ex) {
>            Throwable theCause = ex.getCause() == null ? ex : ex.getCause();
>
>            List> excTypes = exceptionsMap.get(m);
>            if (excTypes != null) {
>                throw ex.getCause().getCause();
> //                for (Class<?> type : excTypes) {
> //                    if (type.isAssignableFrom(theCause.getClass())) {
> //                        throw theCause;
> //                    }
> //                }
>            }
>
>            throw new InvocationTargetException(
>                    new ServiceException(REMOTE_EXCEPTION_TYPE, theCause));
>        } finally {
>            Thread.currentThread().setContextClassLoader(oldCl);
>        }
>    }
>
>    private void introspectType(Class<?> iType) {
>        for (Method m : iType.getDeclaredMethods()) {
>            for (Class<?> excType : m.getExceptionTypes()) {
>                if (Exception.class.isAssignableFrom(excType)) {
>                    List> types = exceptionsMap.get(m);
>                    if (types == null) {
>                        types = new ArrayList>();
>                        exceptionsMap.put(m, types);
>                    }
>                    types.add(excType);
>                }
>            }
>        }
>    }
> }
>
>
> Here my stuff.
>
> Exception
> public class ResourceException extends WebApplicationException {
>
>        /**
>         * @see java.lang.Throwable#getMessage()
>         */
>        @Override
>        public String getMessage() {
>                return message;
>        }
>
>        String message;
>
>        /**
>         * @param arg0
>         */
>        public ResourceException(Response arg0) {
>
>                super(arg0);
>                int read = 1;
>                byte buffer[] = new byte[2048];
>                InputStream in = (InputStream)
> this.getResponse().getEntity();
>                StringBuffer stringBuffer = new StringBuffer();
>                while (read > 0) {
>                        try {
>                                read = in.read(buffer);
>                                stringBuffer.append(buffer);
>                        } catch (IOException e) {
>                        }
>                        System.out.print(new String(buffer));
>                }
>                message = stringBuffer.toString();
>        }
>
>        public ResourceException(int status,String message) {
>
>
> super(Response.status(status).entity(message).type("application/xml").build());
>        }
> }
>
> Exception Mapper
> public class CustomExceptionMapper implements ExceptionMapper {
>        /**
>         * @see
> javax.ws.rs.ext.ExceptionMapper#toResponse(java.lang.Throwable)
>         */
>        public Response toResponse(ResourceException arg0) {
>                Response response =
> Response.status(arg0.getResponse().getStatus())
>
> .type(MediaType.TEXT_PLAIN).entity(arg0.getResponse().getEntity()).build();
>                return response;
>        }
>
> }
>
> Response Exception Mapper
> public class CustomResponseExceptionMapper implements
> ResponseExceptionMapper {
>
>        public ResourceException fromResponse(Response r) {
>                return new ResourceException(r);
>        }
>
> }
>
> DS
> <?xml version="1.0" encoding="UTF-8"?>
>
>
>
>
>
>
>
>
>
>
>   nexcore.platform.foundation.core.exception.CustomExceptionMapper
> nexcore.platform.foundation.core.exception.CustomResponseExceptionMapper
>
>
>
> Remote-Service
> <?xml version="1.0" encoding="UTF-8" ?>
>
>
>
>
>  *
>  org.apache.cxf.rs
>  HTTP
>  http://localhost:8080/ncs/fd/acm/applyer
>  aegis
>  nexcore.platform.foundation.core.exception.CustomExceptionMapper,
> nexcore.platform.foundation.core.exception.CustomResponseExceptionMapper
>
>