cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r888571 - in /cxf/trunk: rt/core/src/main/java/org/apache/cxf/interceptor/ rt/databinding/jaxb/src/main/java/org/apache/cxf/endpoint/dynamic/ systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/
Date Tue, 08 Dec 2009 21:01:16 GMT
Author: dkulp
Date: Tue Dec  8 21:01:15 2009
New Revision: 888571

URL: http://svn.apache.org/viewvc?rev=888571&view=rev
Log:
[CXF-2555] Map custom faults into real exceptions.

Modified:
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/ClientFaultConverter.java
    cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/endpoint/dynamic/TypeClassInitializer.java
    cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/ClientServerMiscTest.java

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/ClientFaultConverter.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/ClientFaultConverter.java?rev=888571&r1=888570&r2=888571&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/ClientFaultConverter.java (original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/ClientFaultConverter.java Tue
Dec  8 21:01:15 2009
@@ -44,6 +44,7 @@
 import org.apache.cxf.helpers.DOMUtils;
 import org.apache.cxf.helpers.XPathUtils;
 import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageUtils;
 import org.apache.cxf.phase.AbstractPhaseInterceptor;
 import org.apache.cxf.phase.Phase;
 import org.apache.cxf.service.Service;
@@ -58,6 +59,7 @@
  * @author Dan Diephouse
  */
 public class ClientFaultConverter extends AbstractPhaseInterceptor<Message> {
+    public static final String DISABLE_FAULT_MAPPING = "disable-fault-mapping";
     private static final Logger LOG = LogUtils.getLogger(ClientFaultConverter.class);
 
     public ClientFaultConverter() {
@@ -70,7 +72,10 @@
     public void handleMessage(Message msg) {
         Fault fault = (Fault) msg.getContent(Exception.class);
 
-        if (fault.getDetail() != null) {
+        if (fault.getDetail() != null 
+            && !MessageUtils.getContextualBoolean(msg,
+                                                 DISABLE_FAULT_MAPPING,
+                                                 false)) {
             processFaultDetail(fault, msg);
             setStackTrace(fault, msg);
         }

Modified: cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/endpoint/dynamic/TypeClassInitializer.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/endpoint/dynamic/TypeClassInitializer.java?rev=888571&r1=888570&r2=888571&view=diff
==============================================================================
--- cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/endpoint/dynamic/TypeClassInitializer.java
(original)
+++ cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/endpoint/dynamic/TypeClassInitializer.java
Tue Dec  8 21:01:15 2009
@@ -27,6 +27,7 @@
 import org.apache.cxf.common.classloader.ClassLoaderUtils;
 import org.apache.cxf.common.i18n.Message;
 import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.common.util.ASMHelper;
 import org.apache.cxf.common.util.PrimitiveUtils;
 import org.apache.cxf.jaxb.JAXBUtils.JType;
 import org.apache.cxf.jaxb.JAXBUtils.Mapping;
@@ -34,17 +35,24 @@
 import org.apache.cxf.jaxb.JAXBUtils.TypeAndAnnotation;
 import org.apache.cxf.service.ServiceModelVisitor;
 import org.apache.cxf.service.factory.ServiceConstructionException;
+import org.apache.cxf.service.model.FaultInfo;
 import org.apache.cxf.service.model.MessagePartInfo;
 import org.apache.cxf.service.model.OperationInfo;
 import org.apache.cxf.service.model.ServiceInfo;
 import org.apache.cxf.service.model.UnwrappedOperationInfo;
 import org.apache.ws.commons.schema.XmlSchemaElement;
+import org.objectweb.asm.ClassWriter;
+import org.objectweb.asm.FieldVisitor;
+import org.objectweb.asm.Label;
+import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.Opcodes;
 
 public class TypeClassInitializer extends ServiceModelVisitor {
     private static final Logger LOG = LogUtils.getL7dLogger(TypeClassInitializer.class);
     
     S2JJAXBModel model;
     boolean allowWrapperOperations;
+    boolean isFault;
     
     public TypeClassInitializer(ServiceInfo serviceInfo, 
                                 S2JJAXBModel model,
@@ -147,10 +155,21 @@
         }
         
         part.setTypeClass(cls);
-        
+        if (isFault) {
+            //need to create an Exception class for this
+            try {
+                part.getMessageInfo().setProperty(Class.class.getName(), createFaultClass(cls));
+            } catch (Throwable t) {
+                //ignore - probably no asm
+            }
+        }
         super.begin(part);
     }
 
+    private Class<?> createFaultClass(Class cls) {
+        return new ExceptionCreator().createExceptionClass(cls);
+    }
+
     private Class getClassByName(JType jType) throws ClassNotFoundException {
         Class cls;
         
@@ -161,5 +180,72 @@
         }
         return cls;
     }
+    public void begin(FaultInfo fault) {
+        isFault = true;
+    }
+    public void end(FaultInfo fault) {
+        isFault = false;
+    }
 
+    
+    private class ExceptionCreator extends ASMHelper {
+        public Class<?> createExceptionClass(Class<?> bean) {
+            String newClassName = bean.getName() + "_Exception";
+            newClassName = newClassName.replaceAll("\\$", ".");
+            newClassName = periodToSlashes(newClassName);
+
+            Class<?> cls = super.findClass(newClassName.replace('/', '.'), bean);
+            if (cls == null) {
+                ClassWriter cw = createClassWriter();
+                cw.visit(Opcodes.V1_5,
+                         Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER,
+                         newClassName,
+                         null,
+                         "java/lang/Exception",
+                         null);
+                
+                FieldVisitor fv;
+                MethodVisitor mv;
+                
+                String beanClassCode = getClassCode(bean);
+                fv = cw.visitField(0, "faultInfo", beanClassCode, null, null);
+                fv.visitEnd();
+                                
+                
+                mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>",
+                                    "(Ljava/lang/String;" + beanClassCode + ")V", null, null);
+                mv.visitCode();
+                mv.visitLabel(new Label());
+                mv.visitVarInsn(Opcodes.ALOAD, 0);
+                mv.visitVarInsn(Opcodes.ALOAD, 1);
+                mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Exception",
+                                   "<init>", "(Ljava/lang/String;)V");
+                mv.visitLabel(new Label());
+                mv.visitVarInsn(Opcodes.ALOAD, 0);
+                mv.visitVarInsn(Opcodes.ALOAD, 2);
+                mv.visitFieldInsn(Opcodes.PUTFIELD, newClassName, "faultInfo", beanClassCode);
+                mv.visitLabel(new Label());
+                mv.visitInsn(Opcodes.RETURN);
+                mv.visitLabel(new Label());
+                mv.visitMaxs(2, 3);
+                mv.visitEnd();
+
+                mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getFaultInfo", 
+                                    "()" + beanClassCode, null, null);
+                mv.visitCode();
+                mv.visitLabel(new Label());
+                mv.visitVarInsn(Opcodes.ALOAD, 0);
+                mv.visitFieldInsn(Opcodes.GETFIELD, newClassName, "faultInfo", beanClassCode);
+                mv.visitInsn(Opcodes.ARETURN);
+                mv.visitLabel(new Label());
+                mv.visitMaxs(1, 1);
+                mv.visitEnd();
+
+                cw.visitEnd();
+                
+                return super.loadClass(bean.getName() + "_Exception", bean, cw.toByteArray());
+            }
+            return cls;
+        }
+    }
 }

Modified: cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/ClientServerMiscTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/ClientServerMiscTest.java?rev=888571&r1=888570&r2=888571&view=diff
==============================================================================
--- cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/ClientServerMiscTest.java
(original)
+++ cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/ClientServerMiscTest.java
Tue Dec  8 21:01:15 2009
@@ -49,13 +49,16 @@
 import org.apache.cxf.anonymous_complex_type.SplitName;
 import org.apache.cxf.anonymous_complex_type.SplitNameResponse.Names;
 import org.apache.cxf.binding.soap.Soap11;
+import org.apache.cxf.binding.soap.SoapFault;
 import org.apache.cxf.common.WSDLConstants;
 import org.apache.cxf.common.util.ASMHelper;
+import org.apache.cxf.endpoint.Client;
 import org.apache.cxf.frontend.ClientProxyFactoryBean;
 import org.apache.cxf.helpers.XMLUtils;
 import org.apache.cxf.helpers.XPathUtils;
 import org.apache.cxf.jaxb_element_test.JaxbElementTest;
 import org.apache.cxf.jaxb_element_test.JaxbElementTest_Service;
+import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
 import org.apache.cxf.ordered_param_holder.ComplexStruct;
 import org.apache.cxf.ordered_param_holder.OrderedParamHolder;
 import org.apache.cxf.ordered_param_holder.OrderedParamHolder_Service;
@@ -802,4 +805,26 @@
                                 doc, XPathConstants.NODE);
         assertNotNull(ct);
     }
+    
+    @Test
+    public void testDynamicClientExceptions() throws Exception {
+        JaxWsDynamicClientFactory dcf = 
+            JaxWsDynamicClientFactory.newInstance();
+        URL wsdlURL = new URL(ServerMisc.DOCLIT_CODEFIRST_URL + "?wsdl");
+        Client client = dcf.createClient(wsdlURL);
+        try {
+            client.invoke("throwException", -2);
+        } catch (Exception ex) {
+            Object o = ex.getClass().getMethod("getFaultInfo").invoke(ex);
+            assertNotNull(o);
+        }
+        
+        try {
+            client.getRequestContext().put("disable-fault-mapping", true);
+            client.invoke("throwException", -2);
+        } catch (SoapFault ex) {
+            //expected
+        }
+       
+    }
 }



Mime
View raw message