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.

This exception comes from original CXF source code;
java.lang.reflect.UndeclaredThrowableException
       at $Proxy42.retrieveResourceList(Unknown Source)
       at
nexcore.platform.foundation.accesscontrol.consumer.ACMActivator.testTemp(ACMActivator.java:111)
       at
nexcore.platform.foundation.accesscontrol.consumer.ACMActivator$3.addingService(ACMActivator.java:102)
       at
org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:896)
       at
org.osgi.util.tracker.AbstractTracked.trackAdding(AbstractTracked.java:261)
       at
org.osgi.util.tracker.AbstractTracked.track(AbstractTracked.java:233)
      at
org.osgi.util.tracker.ServiceTracker$Tracked.serviceChanged(ServiceTracker.java:840)
       at
org.eclipse.osgi.internal.serviceregistry.FilteredServiceListener.serviceChanged(FilteredServiceListener.java:104)
       at
org.eclipse.osgi.framework.internal.core.BundleContextImpl.dispatchEvent(BundleContextImpl.java:933)
       at
org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:227)
       at
org.eclipse.osgi.framework.eventmgr.ListenerQueue.dispatchEventSynchronous(ListenerQueue.java:149)
       at
org.eclipse.osgi.internal.serviceregistry.ServiceRegistry.publishServiceEventPrivileged(ServiceRegistry.java:756)
       at
org.eclipse.osgi.internal.serviceregistry.ServiceRegistry.publishServiceEvent(ServiceRegistry.java:711)
       at
org.eclipse.osgi.internal.serviceregistry.ServiceRegistrationImpl.register(ServiceRegistrationImpl.java:130)
       at
org.eclipse.osgi.internal.serviceregistry.ServiceRegistry.registerService(ServiceRegistry.java:206)
       at
org.eclipse.osgi.framework.internal.core.BundleContextImpl.registerService(BundleContextImpl.java:507)
       at
org.eclipse.osgi.framework.internal.core.BundleContextImpl.registerService(BundleContextImpl.java:525)
       at
org.apache.cxf.dosgi.dsw.service.RemoteServiceAdminCore.proxifyMatchingInterface(RemoteServiceAdminCore.java:440)
       at
org.apache.cxf.dosgi.dsw.service.RemoteServiceAdminCore.importService(RemoteServiceAdminCore.java:396)
       at
org.apache.cxf.dosgi.dsw.service.RemoteServiceAdminInstance$2.run(RemoteServiceAdminInstance.java:131)
       at
org.apache.cxf.dosgi.dsw.service.RemoteServiceAdminInstance$2.run(RemoteServiceAdminInstance.java:124)
       at java.security.AccessController.doPrivileged(Native Method)
       at
org.apache.cxf.dosgi.dsw.service.RemoteServiceAdminInstance.importService(RemoteServiceAdminInstance.java:124)
       at
org.apache.cxf.dosgi.topologymanager.TopologyManagerImport.importService(TopologyManagerImport.java:308)
       at
org.apache.cxf.dosgi.topologymanager.TopologyManagerImport.importAllServicesStrategy(TopologyManagerImport.java:239)
       at
org.apache.cxf.dosgi.topologymanager.TopologyManagerImport.access$300(TopologyManagerImport.java:41)
       at
org.apache.cxf.dosgi.topologymanager.TopologyManagerImport$1.run(TopologyManagerImport.java:197)
       at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
       at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
       at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.reflect.InvocationTargetException
       at
org.apache.cxf.dosgi.dsw.handlers.ServiceInvocationHandler.invoke(ServiceInvocationHandler.java:82)
       ... 30 more
Caused by: org.osgi.framework.ServiceException: REMOTE
       at
org.apache.cxf.dosgi.dsw.handlers.ServiceInvocationHandler.invoke(ServiceInvocationHandler.java:83)
       ... 30 more
Caused by: java.lang.reflect.InvocationTargetException
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:597)
       at
org.apache.cxf.dosgi.dsw.handlers.ServiceInvocationHandler$1.run(ServiceInvocationHandler.java:65)
       at java.security.AccessController.doPrivileged(Native Method)
       at
org.apache.cxf.dosgi.dsw.handlers.ServiceInvocationHandler.invoke(ServiceInvocationHandler.java:63)
       ... 30 more
Caused by: javax.ws.rs.WebApplicationException
       at
org.apache.cxf.jaxrs.client.AbstractClient.reportNoMessageHandler(AbstractClient.java:487)
       at
org.apache.cxf.jaxrs.client.AbstractClient.readBody(AbstractClient.java:446)
      at
org.apache.cxf.jaxrs.client.ClientProxyImpl.handleResponse(ClientProxyImpl.java:463)
       at
org.apache.cxf.jaxrs.client.ClientProxyImpl.doChainedInvocation(ClientProxyImpl.java:445)
       at
org.apache.cxf.jaxrs.client.ClientProxyImpl.invoke(ClientProxyImpl.java:177)
       at $Proxy41.retrieveResourceList(Unknown Source)
       ... 37 more


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
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&nbsp; &nbsp;&nbsp; 
&nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp;
&nbsp; &nbsp;
&nbsp; &nbsp;
&nbsp; &nbsp;
&nbsp; &nbsp;
&nbsp; &nbsp;
&nbsp; &nbsp;
&nbsp;
&nbsp;nexcore.platform.foundation.core.exception.CustomExceptionMapper
nexcore.platform.foundation.core.exception.CustomResponseExceptionMapper



Remote-Service
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt; 
&nbsp; 
&nbsp; &nbsp;
&nbsp; *&nbsp;
&nbsp; org.apache.cxf.rs&nbsp;
&nbsp; HTTP&nbsp;
&nbsp;  http://localhost:8080/ncs/fd/acm/applyer
http://localhost:8080/ncs/fd/acm/applyer 
&nbsp; aegis&nbsp;
&nbsp; nexcore.platform.foundation.core.exception.CustomExceptionMapper,
nexcore.platform.foundation.core.exception.CustomResponseExceptionMapper
&nbsp; 
&nbsp; 
&nbsp; 


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

Reply via email to