cxf-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sergey Beryozkin <sberyoz...@gmail.com>
Subject Re: How can i get my custom exception via dosgi-cxf(RS)?
Date Mon, 14 Mar 2011 22:14:28 GMT
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 <baramnemse@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

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message