Hi, This patch changes BeanUtil to give better error messages when the value found in a request can't be converted to the expected type. The result is that instead of the cryptic 'For input string: "xxx"', the error message is now 'Invalid value "xxx" for element yy'.
This error handling is done by wrapping calls to SimpleTypeMapper.getSimpleTypeObject with a private method that catches NumberFormatException and throws an AxisFault instead. It might be possible to make this change in SimpleTypeMapper instead, but that would require changing the signature, since getSimpleTypeObject doesn't have a throws clause. In order to set the correct fault code for the AxisFault, MessageContext.getCurrentMessageContext() is called to get soap version information. It would be more convenient if it was possible to specify the sender/receiver fault code on AxisFault in a version independent way. Regards, Pétur Runólfsson Betware The content of this e-mail, together with any of its attachments, is for the exclusive and confidential use of the named addressee(s) and it may contain legally privileged and confidential information and/or copyrighted material. Any other distribution, use or reproduction without the sender's prior consent is unauthorized and strictly prohibited. If you have by coincidence, mistake or without specific authorization received this e-mail in error, please notify the sender by e-mail immediately, uphold strict confidentiality and neither read, copy, transfer, disseminate, disclose nor otherwise make use of its content in any way and delete the material from your computer. The content of the e-mail and its attachments is the liability of the individual sender, if it does not relate to the affairs of Betware. Betware does not assume any civil or criminal liability should the e-mail or it´s attachments be virus infected.
Index: test/org/apache/axis2/databinding/utils/BeanUtilTest.java =================================================================== --- test/org/apache/axis2/databinding/utils/BeanUtilTest.java (revision 787167) +++ test/org/apache/axis2/databinding/utils/BeanUtilTest.java (working copy) @@ -20,6 +20,9 @@ package org.apache.axis2.databinding.utils; import org.apache.axiom.om.*; +import org.apache.axiom.soap.SOAPFactory; +import org.apache.axis2.AxisFault; +import org.apache.axis2.context.MessageContext; import org.apache.axis2.engine.DefaultObjectSupplier; import org.apache.axis2.engine.ObjectSupplier; @@ -47,7 +50,7 @@ private ObjectSupplier objectSupplier; - private OMFactory omFactory; + private SOAPFactory omFactory; private OMElement omElement; private OMNamespace xsiNamespace; @@ -55,10 +58,20 @@ protected void setUp() throws Exception { objectSupplier = new DefaultObjectSupplier(); - omFactory = OMAbstractFactory.getOMFactory(); + omFactory = OMAbstractFactory.getSOAP12Factory(); xsiNamespace = omFactory.createOMNamespace(Constants.XSI_NAMESPACE, "xsi"); omElement = omFactory.createOMElement(new QName("hello")); + + MessageContext msgContext = new MessageContext(); + msgContext.setEnvelope(omFactory.createSOAPEnvelope()); + + MessageContext.setCurrentMessageContext(msgContext); } + + @Override + protected void tearDown() throws Exception { + MessageContext.setCurrentMessageContext(null); + } public void testProcessObjectAsSimpleType() throws Exception { omElement.setText("World"); @@ -127,7 +140,33 @@ assertTrue(result instanceof OMText); assertEquals("World", ((OMText) result).getText()); } - + + public void testProcessObjectWithWrongType() throws Exception { + omElement.setLocalName("Queensland"); + omElement.setText("Brisbane"); + + try { + BeanUtil.processObject(omElement, int.class, new MultirefHelper(omElement), true, objectSupplier); + } catch (AxisFault e) { + assertEquals(org.apache.axis2.Constants.FAULT_SOAP12_SENDER, e.getFaultCode()); + assertTrue(e.getMessage().contains("Queensland")); + assertTrue(e.getMessage().contains("Brisbane")); + } + } + + public void testDeserializeWithWrongType() throws Exception { + omElement.setLocalName("Queensland"); + omElement.setText("Brisbane"); + + try { + BeanUtil.deserialize(int.class, omElement, objectSupplier, "Queensland"); + } catch (AxisFault e) { + assertEquals(org.apache.axis2.Constants.FAULT_SOAP12_SENDER, e.getFaultCode()); + assertTrue(e.getMessage().contains("Queensland")); + assertTrue(e.getMessage().contains("Brisbane")); + } + } + private OMAttribute createTypeAttribute(String value) { return omFactory.createOMAttribute("type", xsiNamespace, value); } Index: src/org/apache/axis2/databinding/utils/BeanUtil.java =================================================================== --- src/org/apache/axis2/databinding/utils/BeanUtil.java (revision 787167) +++ src/org/apache/axis2/databinding/utils/BeanUtil.java (working copy) @@ -328,7 +328,7 @@ } } else { if (SimpleTypeMapper.isSimpleType(beanClass)) { - return SimpleTypeMapper.getSimpleTypeObject(beanClass, beanElement); + return getSimpleTypeObjectChecked(beanClass, beanElement); } else if ("java.lang.Object".equals(beanClass.getName())){ return beanElement.getFirstOMChild(); } @@ -670,7 +670,7 @@ String value = omElement.getText(); return Base64.decode(value); } else { - return SimpleTypeMapper.getSimpleTypeObject(classType, omElement); + return getSimpleTypeObjectChecked(classType, omElement); } } else if (SimpleTypeMapper.isCollection(classType)) { return SimpleTypeMapper.getArrayList(omElement); @@ -830,4 +830,18 @@ } } + private static Object getSimpleTypeObjectChecked(Class classType, OMElement omElement) throws AxisFault { + try { + return SimpleTypeMapper.getSimpleTypeObject(classType, omElement); + } catch (NumberFormatException e) { + MessageContext msgContext = MessageContext.getCurrentMessageContext(); + QName faultCode = msgContext != null ? msgContext.getEnvelope().getVersion().getSenderFaultCode() : null; + + throw new AxisFault( + "Invalid value \"" + omElement.getText() + "\" for element " + omElement.getLocalName(), + faultCode, + e); + } + } + }