You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2009/12/08 23:45:52 UTC

svn commit: r888613 - in /cxf/branches/2.2.x-fixes: ./ 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/

Author: dkulp
Date: Tue Dec  8 22:45:51 2009
New Revision: 888613

URL: http://svn.apache.org/viewvc?rev=888613&view=rev
Log:
Merged revisions 888571 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r888571 | dkulp | 2009-12-08 16:01:15 -0500 (Tue, 08 Dec 2009) | 1 line
  
  [CXF-2555] Map custom faults into real exceptions.
........

Modified:
    cxf/branches/2.2.x-fixes/   (props changed)
    cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/ClientFaultConverter.java
    cxf/branches/2.2.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/endpoint/dynamic/TypeClassInitializer.java
    cxf/branches/2.2.x-fixes/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/ClientServerMiscTest.java

Propchange: cxf/branches/2.2.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/ClientFaultConverter.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/ClientFaultConverter.java?rev=888613&r1=888612&r2=888613&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/ClientFaultConverter.java (original)
+++ cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/ClientFaultConverter.java Tue Dec  8 22:45:51 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/branches/2.2.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/endpoint/dynamic/TypeClassInitializer.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/endpoint/dynamic/TypeClassInitializer.java?rev=888613&r1=888612&r2=888613&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/endpoint/dynamic/TypeClassInitializer.java (original)
+++ cxf/branches/2.2.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/endpoint/dynamic/TypeClassInitializer.java Tue Dec  8 22:45:51 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/branches/2.2.x-fixes/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/ClientServerMiscTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/ClientServerMiscTest.java?rev=888613&r1=888612&r2=888613&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/ClientServerMiscTest.java (original)
+++ cxf/branches/2.2.x-fixes/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/ClientServerMiscTest.java Tue Dec  8 22:45:51 2009
@@ -49,12 +49,15 @@
 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.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;
@@ -716,4 +719,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
+        }
+       
+    }
 }