Example usage for javax.xml.soap SOAPFault getFaultCodeAsName

List of usage examples for javax.xml.soap SOAPFault getFaultCodeAsName

Introduction

In this page you can find the example usage for javax.xml.soap SOAPFault getFaultCodeAsName.

Prototype

public Name getFaultCodeAsName();

Source Link

Document

Gets the mandatory SOAP 1.1 fault code for this SOAPFault object as a SAAJ Name object.

Usage

From source file:it.cnr.icar.eric.common.SOAPMessenger.java

/**
 * Convert SOAPFault back to RegistryException (if possible)
 * @param fault SOAPFault/*from  www.  jav a  2  s  . c om*/
 * @return RegistryException
 */
RegistryException createRegistryException(SOAPFault fault) {
    RegistryException result = null;

    //is this message too generic?
    String unknownError = resourceBundle.getString("message.unknown");

    if (log.isDebugEnabled()) {
        log.debug(fault.toString());
    }

    String exceptionName = null;
    if (fault.getFaultCode().startsWith(BindingUtility.SOAP_FAULT_PREFIX)) {
        // Old style faultcode value, skip prefix and colon
        exceptionName = fault.getFaultCode().substring(BindingUtility.SOAP_FAULT_PREFIX.length() + 1);
    } else if ( // TODO: SAAJ 1.3 has introduced preferred QName interfaces
    fault.getFaultCodeAsName().getURI().equals(BindingUtility.SOAP_FAULT_PREFIX)) {
        // New style
        exceptionName = fault.getFaultCodeAsName().getLocalName();
    }

    if (null == exceptionName) {
        // not a recognized ebXML fault
        result = new RegistryException(unknownError);
    } else {
        // ebXML fault
        String exceptionMessage = fault.getFaultString();
        unknownError = resourceBundle.getString("message.exception",
                new String[] { exceptionName, exceptionMessage });

        /*
           Detail detail = fault.getDetail();
           Iterator iter = detail.getDetailEntries();
           int i=0;
           while (iter.hasNext()) {
        DetailEntry detailEntry = (DetailEntry)iter.next();
        unknownError += " detailEntry[" + i++ + "] = " + detailEntry.toString();
           }
         **/

        //TODO: get and reconstruct Stacktrace
        try {

            Class<?> exceptionClass = null;
            //exceptionClass = Class.forName("it.cnr.icar.eric.common.exceptions." + exceptionName);
            exceptionClass = Class.forName(exceptionName);

            if (RegistryException.class.isAssignableFrom(exceptionClass)) {
                //Exception is a RegistryException. Reconstitute it as a RegistryException

                // NPE has null message..
                if (exceptionMessage != null) {
                    @SuppressWarnings("rawtypes")
                    Class[] parameterDefinition = { String.class };
                    Constructor<?> exceptionConstructor = exceptionClass.getConstructor(parameterDefinition);
                    Object[] parameters = { exceptionMessage };
                    result = (RegistryException) exceptionConstructor.newInstance(parameters);
                } else {
                    @SuppressWarnings("rawtypes")
                    Class[] parameterDefinition = {};
                    Constructor<?> exceptionConstructor = exceptionClass.getConstructor(parameterDefinition);
                    Object[] parameters = {};
                    result = (RegistryException) exceptionConstructor.newInstance(parameters);
                }
            } else {
                //Exception is not a RegistryException.

                //Make it a RegistryException with exceptionMessage
                //In future make it a nested Throwable of a RegistryException
                // NPE has null message..
                result = new RegistryException(unknownError);
            }
        } catch (ClassNotFoundException e) {
            //could happen with non-eric server?
            result = new RegistryException(unknownError, e);
        } catch (NoSuchMethodException e) {
            //should not happen
            result = new RegistryException(unknownError, e);
        } catch (IllegalAccessException e) {
            //happens when?
            result = new RegistryException(unknownError, e);
        } catch (InvocationTargetException e) {
            //happens when?
            result = new RegistryException(unknownError, e);
        } catch (InstantiationException e) {
            //happens when trying to instantiate Interface
            result = new RegistryException(unknownError, e);
        }
    }
    return result;
}