Author: amilas Date: Thu Apr 29 05:29:06 2010 New Revision: 939200 URL: http://svn.apache.org/viewvc?rev=939200&view=rev Log: commiting the patch AXIS2-4408. This handles the exception properly
Modified: axis/axis2/java/core/trunk/modules/adb/src/org/apache/axis2/rpc/receivers/RPCMessageReceiver.java axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/description/java2wsdl/DefaultSchemaGenerator.java Modified: axis/axis2/java/core/trunk/modules/adb/src/org/apache/axis2/rpc/receivers/RPCMessageReceiver.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/adb/src/org/apache/axis2/rpc/receivers/RPCMessageReceiver.java?rev=939200&r1=939199&r2=939200&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/adb/src/org/apache/axis2/rpc/receivers/RPCMessageReceiver.java (original) +++ axis/axis2/java/core/trunk/modules/adb/src/org/apache/axis2/rpc/receivers/RPCMessageReceiver.java Thu Apr 29 05:29:06 2010 @@ -25,9 +25,14 @@ package org.apache.axis2.rpc.receivers; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMNamespace; +import org.apache.axiom.om.OMAbstractFactory; +import org.apache.axiom.om.impl.builder.StAXOMBuilder; +import org.apache.axiom.om.impl.llom.factory.OMXMLBuilderFactory; import org.apache.axiom.soap.SOAPEnvelope; import org.apache.axiom.soap.SOAPFactory; import org.apache.axis2.AxisFault; +import org.apache.axis2.util.StreamWrapper; +import org.apache.axis2.databinding.utils.BeanUtil; import org.apache.axis2.context.MessageContext; import org.apache.axis2.description.AxisMessage; import org.apache.axis2.description.AxisOperation; @@ -35,11 +40,14 @@ import org.apache.axis2.description.Axis import org.apache.axis2.description.Parameter; import org.apache.axis2.description.WSDL2Constants; import org.apache.axis2.description.java2wsdl.Java2WSDLConstants; +import org.apache.axis2.description.java2wsdl.TypeTable; import org.apache.axis2.receivers.AbstractInOutMessageReceiver; import org.apache.axis2.wsdl.WSDLConstants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamReader; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -154,6 +162,40 @@ public class RPCMessageReceiver extends log.debug(msg, cause); throw (AxisFault)cause; } + + Class[] exceptionTypes = method.getExceptionTypes(); + for (Class exceptionType : exceptionTypes){ + if (exceptionType.getName().equals(cause.getClass().getName())){ + // this is an bussiness logic exception so handle it properly + String partQName = getSimpleClassName(exceptionType); + TypeTable typeTable = inMessage.getAxisService().getTypeTable(); + QName elementQName = typeTable.getQNamefortheType(partQName); + SOAPFactory fac = getSOAPFactory(inMessage); + OMElement exceptionElement = fac.createOMElement(elementQName); + + if (exceptionType.getName().equals(Exception.class.getName())){ + // this is an exception class. so create a element by hand and add the message + OMElement innterExceptionElement = fac.createOMElement(elementQName); + OMElement messageElement = fac.createOMElement("Message", inMessage.getAxisService().getTargetNamespace(), null); + messageElement.setText(cause.getMessage()); + + innterExceptionElement.addChild(messageElement); + exceptionElement.addChild(innterExceptionElement); + } else { + // if it is a normal bussiness exception we need to generate the schema assuming it is a pojo + XMLStreamReader xr = BeanUtil.getPullParser(cause, + elementQName, typeTable, true, false); + StAXOMBuilder stAXOMBuilder = new StAXOMBuilder(OMAbstractFactory.getOMFactory(), new StreamWrapper(xr)); + OMElement documentElement = stAXOMBuilder.getDocumentElement(); + exceptionElement.addChild(documentElement); + } + + AxisFault axisFault = new AxisFault(cause.getMessage()); + axisFault.setDetail(exceptionElement); + throw axisFault; + } + } + log.error(msg, e); throw new AxisFault(msg, e); } catch(RuntimeException e) { @@ -166,4 +208,14 @@ public class RPCMessageReceiver extends throw AxisFault.makeFault(e); } } + + private String getSimpleClassName(Class type) { + String simpleClassName = type.getName(); + int idx = simpleClassName.lastIndexOf('.'); + if (idx != -1 && idx < (simpleClassName.length() - 1)) { + simpleClassName = simpleClassName.substring(idx + 1); + } + + return simpleClassName.replace('$', '_'); + } } Modified: axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/description/java2wsdl/DefaultSchemaGenerator.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/description/java2wsdl/DefaultSchemaGenerator.java?rev=939200&r1=939199&r2=939200&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/description/java2wsdl/DefaultSchemaGenerator.java (original) +++ axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/description/java2wsdl/DefaultSchemaGenerator.java Thu Apr 29 05:29:06 2010 @@ -417,29 +417,6 @@ public class DefaultSchemaGenerator impl XmlSchemaComplexType methodSchemaType; XmlSchemaSequence sequence; if (jMethod.getExceptionTypes().length > 0) { - if (!generateBaseException) { - if (typeTable.getComplexSchemaType(Exception.class.getName()) != null) { - - } else { - sequence = new XmlSchemaSequence(); - XmlSchema xmlSchema = getXmlSchema(schemaTargetNameSpace); - QName elementName = new QName(schemaTargetNameSpace, - "Exception", - schema_namespace_prefix); - XmlSchemaComplexType complexType = new XmlSchemaComplexType(xmlSchema); - complexType.setName("Exception"); - xmlSchema.getItems().add(complexType); - xmlSchema.getElements().add(elementName, complexType); - typeTable.addComplexSchema(Exception.class.getName(), elementName); - QName schemaTypeName = TypeTable.ANY_TYPE; - addContentToMethodSchemaType(sequence, - schemaTypeName, - "Exception", - false); - complexType.setParticle(sequence); - } - generateBaseException = true; - } for (Class<?> extype : jMethod.getExceptionTypes()) { if (AxisFault.class.getName().equals(extype.getName())) { continue; @@ -450,6 +427,9 @@ public class DefaultSchemaGenerator impl new QName(this.schemaTargetNameSpace, partQname, this.schema_namespace_prefix); sequence = new XmlSchemaSequence(); if (Exception.class.getName().equals(extype.getName())) { + if (typeTable.getComplexSchemaType(Exception.class.getName()) == null) { + generateComplexTypeforException(); + } QName schemaTypeName = typeTable.getComplexSchemaType(Exception.class.getName()); addContentToMethodSchemaType(sequence, schemaTypeName, @@ -478,6 +458,20 @@ public class DefaultSchemaGenerator impl } } + private void generateComplexTypeforException() { + XmlSchemaSequence sequence = new XmlSchemaSequence(); + XmlSchema xmlSchema = getXmlSchema(schemaTargetNameSpace); + QName elementName = new QName(schemaTargetNameSpace, "Exception", schema_namespace_prefix); + XmlSchemaComplexType complexType = new XmlSchemaComplexType(xmlSchema); + complexType.setName("Exception"); + xmlSchema.getItems().add(complexType); + xmlSchema.getElements().add(elementName, complexType); + typeTable.addComplexSchema(Exception.class.getName(), elementName); + QName schemaTypeName = new QName(Java2WSDLConstants.URI_2001_SCHEMA_XSD, "string"); + addContentToMethodSchemaType(sequence, schemaTypeName, "Message", false); + complexType.setParticle(sequence); + } + /** * Generate schema construct for given type * @@ -511,10 +505,11 @@ public class DefaultSchemaGenerator impl eltOuter.setQName(schemaTypeName); Class<?> sup = javaType.getSuperclass(); - if ((sup != null) && ("java.lang.Object".compareTo(sup.getName()) != 0) && - (getQualifiedName(sup.getPackage()).indexOf("org.apache.axis2") < 0) - && (getQualifiedName(sup.getPackage()).indexOf("java.util") < 0)) - { + if ((sup != null) + && (!"java.lang.Object".equals(sup.getName())) + && (!"java.lang.Exception".equals(sup.getName())) + && !getQualifiedName(sup.getPackage()).startsWith("org.apache.axis2") + && !getQualifiedName(sup.getPackage()).startsWith("java.util")){ String superClassName = sup.getName(); String superclassname = getSimpleClassName(sup); String tgtNamespace;