Search in sources :

Example 26 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException in project camel by apache.

the class Soap11DataFormatAdapter 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
     * SOAPFaultException is created.
     * 
     * @param fault Soap fault
     * @return created Exception
     */
private Exception createExceptionFromFault(Fault fault) {
    String message = fault.getFaultstring();
    Detail faultDetail = fault.getDetail();
    if (faultDetail == null || faultDetail.getAny().size() == 0) {
        try {
            return new SOAPFaultException(SOAPFactory.newInstance().createFault(message, fault.getFaultcode()));
        } 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 : SOAPException(javax.xml.soap.SOAPException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) JAXBElement(javax.xml.bind.JAXBElement) Detail(org.xmlsoap.schemas.soap.envelope.Detail) RuntimeCamelException(org.apache.camel.RuntimeCamelException) SOAPException(javax.xml.soap.SOAPException) IOException(java.io.IOException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException)

Example 27 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException 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 28 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException 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 29 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException in project camel by apache.

the class SoapJaxbDataFormat method createContentFromObject.

/**
     * Create body content from a non Exception object. If the inputObject is a
     * BeanInvocation the following should be considered: The first parameter
     * will be used for the SOAP body. BeanInvocations with more than one
     * parameter are not supported. So the interface should be in doc lit bare
     * style.
     * 
     * @param inputObject
     *            object to be put into the SOAP body
     * @param soapAction
     *            for name resolution
     * @param headerElements
     *            in/out parameter used to capture header content if present
     *            
     * @return JAXBElement for the body content
     */
protected List<Object> createContentFromObject(final Object inputObject, String soapAction, List<Object> headerElements) {
    List<Object> bodyParts = new ArrayList<Object>();
    List<Object> headerParts = new ArrayList<Object>();
    if (inputObject instanceof BeanInvocation) {
        BeanInvocation bi = (BeanInvocation) inputObject;
        Annotation[][] annotations = bi.getMethod().getParameterAnnotations();
        List<WebParam> webParams = new ArrayList<WebParam>();
        for (Annotation[] singleParameterAnnotations : annotations) {
            for (Annotation annotation : singleParameterAnnotations) {
                if (annotation instanceof WebParam) {
                    webParams.add((WebParam) annotation);
                }
            }
        }
        if (webParams.size() > 0) {
            if (webParams.size() == bi.getArgs().length) {
                int index = -1;
                for (Object o : bi.getArgs()) {
                    if (webParams.get(++index).header()) {
                        headerParts.add(o);
                    } else {
                        bodyParts.add(o);
                    }
                }
            } else {
                throw new RuntimeCamelException("The number of bean invocation parameters does not " + "match the number of parameters annotated with @WebParam for the method [ " + bi.getMethod().getName() + "].");
            }
        } else {
            // try to map all objects for the body
            for (Object o : bi.getArgs()) {
                bodyParts.add(o);
            }
        }
    } else {
        bodyParts.add(inputObject);
    }
    List<Object> bodyElements = new ArrayList<Object>();
    for (Object bodyObj : bodyParts) {
        QName name = elementNameStrategy.findQNameForSoapActionOrType(soapAction, bodyObj.getClass());
        if (name == null) {
            LOG.warn("Could not find QName for class " + bodyObj.getClass().getName());
            continue;
        } else {
            bodyElements.add(getElement(bodyObj, name));
        }
    }
    for (Object headerObj : headerParts) {
        QName name = elementNameStrategy.findQNameForSoapActionOrType(soapAction, headerObj.getClass());
        if (name == null) {
            LOG.warn("Could not find QName for class " + headerObj.getClass().getName());
            continue;
        } else {
            JAXBElement<?> headerElem = getElement(headerObj, name);
            if (null != headerElem) {
                headerElements.add(headerElem);
            }
        }
    }
    return bodyElements;
}
Also used : QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) BeanInvocation(org.apache.camel.component.bean.BeanInvocation) Annotation(java.lang.annotation.Annotation) WebParam(javax.jws.WebParam) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Example 30 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException in project camel by apache.

the class ICalDataFormatAutoConfiguration method configureICalDataFormatFactory.

@Bean(name = "ical-dataformat-factory")
@ConditionalOnClass(CamelContext.class)
@ConditionalOnMissingBean(ICalDataFormat.class)
public DataFormatFactory configureICalDataFormatFactory(final CamelContext camelContext, final ICalDataFormatConfiguration configuration) {
    return new DataFormatFactory() {

        public DataFormat newInstance() {
            ICalDataFormat dataformat = new ICalDataFormat();
            if (CamelContextAware.class.isAssignableFrom(ICalDataFormat.class)) {
                CamelContextAware contextAware = CamelContextAware.class.cast(dataformat);
                if (contextAware != null) {
                    contextAware.setCamelContext(camelContext);
                }
            }
            try {
                Map<String, Object> parameters = new HashMap<>();
                IntrospectionSupport.getProperties(configuration, parameters, null, false);
                IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), dataformat, parameters);
            } catch (Exception e) {
                throw new RuntimeCamelException(e);
            }
            return dataformat;
        }
    };
}
Also used : DataFormatFactory(org.apache.camel.spi.DataFormatFactory) CamelContextAware(org.apache.camel.CamelContextAware) HashMap(java.util.HashMap) ICalDataFormat(org.apache.camel.component.ical.ICalDataFormat) RuntimeCamelException(org.apache.camel.RuntimeCamelException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

RuntimeCamelException (org.apache.camel.RuntimeCamelException)196 HashMap (java.util.HashMap)52 CamelContextAware (org.apache.camel.CamelContextAware)45 DataFormatFactory (org.apache.camel.spi.DataFormatFactory)45 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)45 ConditionalOnClass (org.springframework.boot.autoconfigure.condition.ConditionalOnClass)45 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)45 Bean (org.springframework.context.annotation.Bean)45 IOException (java.io.IOException)36 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)32 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)11 Exchange (org.apache.camel.Exchange)11 InputStream (java.io.InputStream)9 GeneralSecurityException (java.security.GeneralSecurityException)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 TimeoutException (java.util.concurrent.TimeoutException)7 QName (javax.xml.namespace.QName)7 Message (org.apache.camel.Message)7 Method (java.lang.reflect.Method)6