Search in sources :

Example 1 with ExpectedBodyTypeException

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

the class XsltBuilderTest method testFailNullBody.

public void testFailNullBody() throws Exception {
    URL styleSheet = getClass().getResource("example.xsl");
    XsltBuilder builder = XsltBuilder.xslt(styleSheet);
    builder.setFailOnNullBody(true);
    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setBody(null);
    try {
        builder.process(exchange);
        fail("Should thrown an exception");
    } catch (ExpectedBodyTypeException e) {
    // expected
    }
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) ExpectedBodyTypeException(org.apache.camel.ExpectedBodyTypeException) URL(java.net.URL)

Example 2 with ExpectedBodyTypeException

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

the class XsltBuilderTest method testNullBodyDefault.

public void testNullBodyDefault() throws Exception {
    URL styleSheet = getClass().getResource("example.xsl");
    XsltBuilder builder = XsltBuilder.xslt(styleSheet);
    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setBody(null);
    try {
        builder.process(exchange);
        fail("Should thrown an exception");
    } catch (ExpectedBodyTypeException e) {
    // expected
    }
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) ExpectedBodyTypeException(org.apache.camel.ExpectedBodyTypeException) URL(java.net.URL)

Example 3 with ExpectedBodyTypeException

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

the class XsltBuilder method getSource.

/**
     * Converts the inbound body to a {@link Source}, if the body is <b>not</b> already a {@link Source}.
     * <p/>
     * This implementation will prefer to source in the following order:
     * <ul>
     *   <li>StAX - If StAX is allowed</li>
     *   <li>SAX - SAX as 2nd choice</li>
     *   <li>Stream - Stream as 3rd choice</li>
     *   <li>DOM - DOM as 4th choice</li>
     * </ul>
     */
protected Source getSource(Exchange exchange, Object body) {
    // body may already be a source
    if (body instanceof Source) {
        return (Source) body;
    }
    Source source = null;
    if (body != null) {
        if (isAllowStAX()) {
            // try StAX if enabled
            source = exchange.getContext().getTypeConverter().tryConvertTo(StAXSource.class, exchange, body);
        }
        if (source == null) {
            // then try SAX
            source = exchange.getContext().getTypeConverter().tryConvertTo(SAXSource.class, exchange, body);
            tryAddEntityResolver((SAXSource) source);
        }
        if (source == null) {
            // then try stream
            source = exchange.getContext().getTypeConverter().tryConvertTo(StreamSource.class, exchange, body);
        }
        if (source == null) {
            // and fallback to DOM
            source = exchange.getContext().getTypeConverter().tryConvertTo(DOMSource.class, exchange, body);
        }
        // now we just put the call of source converter at last
        if (source == null) {
            TypeConverter tc = exchange.getContext().getTypeConverterRegistry().lookup(Source.class, body.getClass());
            if (tc != null) {
                source = tc.convertTo(Source.class, exchange, body);
            }
        }
    }
    if (source == null) {
        if (isFailOnNullBody()) {
            throw new ExpectedBodyTypeException(exchange, Source.class);
        } else {
            try {
                source = converter.toDOMSource(converter.createDocument());
            } catch (ParserConfigurationException e) {
                throw new RuntimeTransformException(e);
            }
        }
    }
    return source;
}
Also used : TypeConverter(org.apache.camel.TypeConverter) DOMSource(javax.xml.transform.dom.DOMSource) ExpectedBodyTypeException(org.apache.camel.ExpectedBodyTypeException) StAX2SAXSource(org.apache.camel.converter.jaxp.StAX2SAXSource) SAXSource(javax.xml.transform.sax.SAXSource) StreamSource(javax.xml.transform.stream.StreamSource) StAXSource(javax.xml.transform.stax.StAXSource) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) RuntimeTransformException(org.apache.camel.RuntimeTransformException) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) StAX2SAXSource(org.apache.camel.converter.jaxp.StAX2SAXSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource)

Aggregations

ExpectedBodyTypeException (org.apache.camel.ExpectedBodyTypeException)3 URL (java.net.URL)2 Exchange (org.apache.camel.Exchange)2 DefaultExchange (org.apache.camel.impl.DefaultExchange)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Source (javax.xml.transform.Source)1 DOMSource (javax.xml.transform.dom.DOMSource)1 SAXSource (javax.xml.transform.sax.SAXSource)1 StAXSource (javax.xml.transform.stax.StAXSource)1 StreamSource (javax.xml.transform.stream.StreamSource)1 RuntimeTransformException (org.apache.camel.RuntimeTransformException)1 TypeConverter (org.apache.camel.TypeConverter)1 StAX2SAXSource (org.apache.camel.converter.jaxp.StAX2SAXSource)1