You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Sergey Beryozkin <sb...@gmail.com> on 2011/03/04 12:14:02 UTC

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

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
>
>