Search in sources :

Example 1 with Fault

use of org.w3._2003._05.soap_envelope.Fault in project camel by apache.

the class Soap12DataFormatAdapter method doUnmarshal.

@Override
public Object doUnmarshal(Exchange exchange, InputStream stream, Object rootObject) throws IOException {
    if (rootObject.getClass() != Envelope.class) {
        throw new RuntimeCamelException("Expected Soap Envelope but got " + rootObject.getClass());
    }
    Envelope envelope = (Envelope) rootObject;
    Header header = envelope.getHeader();
    if (header != null) {
        List<Object> returnHeaders;
        List<Object> anyHeaderElements = envelope.getHeader().getAny();
        if (null != anyHeaderElements && !(getDataFormat().isIgnoreUnmarshalledHeaders())) {
            if (getDataFormat().isIgnoreJAXBElement()) {
                returnHeaders = new ArrayList<Object>();
                for (Object headerEl : anyHeaderElements) {
                    returnHeaders.add(JAXBIntrospector.getValue(headerEl));
                }
            } else {
                returnHeaders = anyHeaderElements;
            }
            exchange.getOut().setHeader(SoapJaxbDataFormat.SOAP_UNMARSHALLED_HEADER_LIST, returnHeaders);
        }
    }
    List<Object> anyElement = envelope.getBody().getAny();
    if (anyElement.size() == 0) {
        // No parameter so return null
        return null;
    }
    Object payloadEl = anyElement.get(0);
    Object payload = JAXBIntrospector.getValue(payloadEl);
    if (payload instanceof Fault) {
        Exception exception = createExceptionFromFault((Fault) payload);
        exchange.setException(exception);
        return null;
    } else {
        return getDataFormat().isIgnoreJAXBElement() ? payload : payloadEl;
    }
}
Also used : Header(org.w3._2003._05.soap_envelope.Header) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Fault(org.w3._2003._05.soap_envelope.Fault) WebFault(javax.xml.ws.WebFault) Envelope(org.w3._2003._05.soap_envelope.Envelope) SOAPException(javax.xml.soap.SOAPException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException)

Example 2 with Fault

use of org.w3._2003._05.soap_envelope.Fault in project camel by apache.

the class Soap12DataFormatAdapter method createExceptionFromFault.

/**
     * Creates an exception and eventually an embedded bean that contains the
     * fault detail. The exception class is determined by using the
     * elementNameStrategy. The qName of the fault detail should match the
     * WebFault annotation of the Exception class. If no fault detail is set
     * a {@link javax.xml.ws.soap.SOAPFaultException} is created.
     * 
     * @param fault Soap fault
     * @return created Exception
     */
private Exception createExceptionFromFault(Fault fault) {
    StringBuilder sb = new StringBuilder();
    for (Reasontext text : fault.getReason().getText()) {
        sb.append(text.getValue());
    }
    String message = sb.toString();
    Detail faultDetail = fault.getDetail();
    if (faultDetail == null || faultDetail.getAny().size() == 0) {
        try {
            return new SOAPFaultException(SOAPFactory.newInstance().createFault(message, fault.getCode().getValue()));
        } catch (SOAPException e) {
            throw new RuntimeCamelException(e);
        }
    }
    JAXBElement<?> detailEl = (JAXBElement<?>) faultDetail.getAny().get(0);
    Class<? extends Exception> exceptionClass = getDataFormat().getElementNameStrategy().findExceptionForFaultName(detailEl.getName());
    Constructor<? extends Exception> messageConstructor;
    Constructor<? extends Exception> constructor;
    try {
        messageConstructor = exceptionClass.getConstructor(String.class);
        Object detail = JAXBIntrospector.getValue(detailEl);
        try {
            constructor = exceptionClass.getConstructor(String.class, detail.getClass());
            return constructor.newInstance(message, detail);
        } catch (NoSuchMethodException e) {
            return messageConstructor.newInstance(message);
        }
    } catch (Exception e) {
        throw new RuntimeCamelException(e);
    }
}
Also used : Reasontext(org.w3._2003._05.soap_envelope.Reasontext) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) JAXBElement(javax.xml.bind.JAXBElement) SOAPException(javax.xml.soap.SOAPException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) SOAPException(javax.xml.soap.SOAPException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Detail(org.w3._2003._05.soap_envelope.Detail)

Example 3 with Fault

use of org.w3._2003._05.soap_envelope.Fault in project camel by apache.

the class Soap12DataFormatAdapter method createFaultFromException.

/**
     * Creates a SOAP fault from the exception and populates the message as well
     * as the detail. The detail object is read from the method getFaultInfo of
     * the throwable if present
     * 
     * @param exception the cause exception
     * @return SOAP fault from given Throwable
     */
@SuppressWarnings("unchecked")
private JAXBElement<Fault> createFaultFromException(final Throwable exception) {
    WebFault webFault = exception.getClass().getAnnotation(WebFault.class);
    if (webFault == null || webFault.targetNamespace() == null) {
        throw new RuntimeException("The exception " + exception.getClass().getName() + " needs to have an WebFault annotation with name and targetNamespace", exception);
    }
    QName name = new QName(webFault.targetNamespace(), webFault.name());
    Object faultObject;
    try {
        Method method = exception.getClass().getMethod("getFaultInfo");
        faultObject = method.invoke(exception);
    } catch (Exception e) {
        throw new RuntimeCamelException("Exception while trying to get fault details", e);
    }
    Fault fault = new Fault();
    Faultcode code = new Faultcode();
    code.setValue(FAULT_CODE_SERVER);
    fault.setCode(code);
    Reasontext text = new Reasontext();
    text.setValue(exception.getMessage());
    text.setLang("en");
    fault.setReason(new Faultreason().withText(text));
    Detail detailEl = new ObjectFactory().createDetail();
    @SuppressWarnings("rawtypes") JAXBElement<?> faultDetailContent = new JAXBElement(name, faultObject.getClass(), faultObject);
    detailEl.getAny().add(faultDetailContent);
    fault.setDetail(detailEl);
    return new ObjectFactory().createFault(fault);
}
Also used : Reasontext(org.w3._2003._05.soap_envelope.Reasontext) QName(javax.xml.namespace.QName) Fault(org.w3._2003._05.soap_envelope.Fault) WebFault(javax.xml.ws.WebFault) Method(java.lang.reflect.Method) JAXBElement(javax.xml.bind.JAXBElement) SOAPException(javax.xml.soap.SOAPException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) WebFault(javax.xml.ws.WebFault) Faultcode(org.w3._2003._05.soap_envelope.Faultcode) ObjectFactory(org.w3._2003._05.soap_envelope.ObjectFactory) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Faultreason(org.w3._2003._05.soap_envelope.Faultreason) Detail(org.w3._2003._05.soap_envelope.Detail)

Aggregations

IOException (java.io.IOException)3 SOAPException (javax.xml.soap.SOAPException)3 SOAPFaultException (javax.xml.ws.soap.SOAPFaultException)3 RuntimeCamelException (org.apache.camel.RuntimeCamelException)3 JAXBElement (javax.xml.bind.JAXBElement)2 WebFault (javax.xml.ws.WebFault)2 Detail (org.w3._2003._05.soap_envelope.Detail)2 Fault (org.w3._2003._05.soap_envelope.Fault)2 Reasontext (org.w3._2003._05.soap_envelope.Reasontext)2 Method (java.lang.reflect.Method)1 QName (javax.xml.namespace.QName)1 Envelope (org.w3._2003._05.soap_envelope.Envelope)1 Faultcode (org.w3._2003._05.soap_envelope.Faultcode)1 Faultreason (org.w3._2003._05.soap_envelope.Faultreason)1 Header (org.w3._2003._05.soap_envelope.Header)1 ObjectFactory (org.w3._2003._05.soap_envelope.ObjectFactory)1