Search in sources :

Example 1 with BytesSource

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

the class XmlSignatureProcessor method getSchema.

protected Schema getSchema(Message message) throws SAXException, XmlSignatureException, IOException {
    String schemaResourceUri = getSchemaResourceUri(message);
    if (schemaResourceUri == null || schemaResourceUri.isEmpty()) {
        return null;
    }
    InputStream is = ResourceHelper.resolveResourceAsInputStream(getConfiguration().getCamelContext().getClassResolver(), schemaResourceUri);
    if (is == null) {
        throw new XmlSignatureException("XML Signature component is wrongly configured: No XML schema found for specified schema resource URI " + schemaResourceUri);
    }
    byte[] bytes = null;
    try {
        bytes = IOConverter.toBytes(is);
    } finally {
        // and make sure to close the input stream after the schema has been loaded
        IOHelper.close(is);
    }
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schemaFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    schemaFactory.setResourceResolver(new DefaultLSResourceResolver(getConfiguration().getCamelContext(), getConfiguration().getSchemaResourceUri()));
    LOG.debug("Instantiating schema for validation");
    return schemaFactory.newSchema(new BytesSource(bytes));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) BytesSource(org.apache.camel.BytesSource) XmlSignatureException(org.apache.camel.component.xmlsecurity.api.XmlSignatureException) InputStream(java.io.InputStream) DefaultLSResourceResolver(org.apache.camel.component.validator.DefaultLSResourceResolver)

Example 2 with BytesSource

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

the class StreamCachingInterceptorTest method testNoConversionForOtherXmlSourceTypes.

public void testNoConversionForOtherXmlSourceTypes() throws Exception {
    a.expectedMessageCount(3);
    send(converter.toDOMSource(MESSAGE));
    send(new StringSource(MESSAGE));
    send(new BytesSource(MESSAGE.getBytes()));
    assertMockEndpointsSatisfied();
    for (Exchange exchange : a.getExchanges()) {
        assertFalse(exchange.getIn().getHeader(BODY_TYPE, Class.class).toString() + " shouldn't have been converted to StreamCache", exchange.getIn().getBody() instanceof StreamCache);
    }
}
Also used : Exchange(org.apache.camel.Exchange) BytesSource(org.apache.camel.BytesSource) StreamCache(org.apache.camel.StreamCache) StringSource(org.apache.camel.StringSource)

Example 3 with BytesSource

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

the class XmlConverter method toString.

/**
     * Converts the given input Source into text
     */
@Converter
public String toString(Source source, Exchange exchange) throws TransformerException {
    if (source == null) {
        return null;
    } else if (source instanceof StringSource) {
        return ((StringSource) source).getText();
    } else if (source instanceof BytesSource) {
        return new String(((BytesSource) source).getData());
    } else {
        StringWriter buffer = new StringWriter();
        if (exchange != null) {
            // check the camelContext properties first
            Properties properties = ObjectHelper.getCamelPropertiesWithPrefix(OUTPUT_PROPERTIES_PREFIX, exchange.getContext());
            if (properties.size() > 0) {
                toResult(source, new StreamResult(buffer), properties);
                return buffer.toString();
            }
        }
        // using the old way to deal with it
        toResult(source, new StreamResult(buffer));
        return buffer.toString();
    }
}
Also used : BytesSource(org.apache.camel.BytesSource) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StringSource(org.apache.camel.StringSource) Properties(java.util.Properties) Converter(org.apache.camel.Converter)

Example 4 with BytesSource

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

the class XmlConverterTest method testToBytesSource.

public void testToBytesSource() throws Exception {
    XmlConverter conv = new XmlConverter();
    BytesSource bs = conv.toBytesSource("<foo>bar</foo>".getBytes());
    assertNotNull(bs);
    assertEquals("<foo>bar</foo>", new String(bs.getData()));
}
Also used : BytesSource(org.apache.camel.BytesSource)

Example 5 with BytesSource

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

the class BytesSourceTest method testBytesSourceCtr.

public void testBytesSourceCtr() {
    BytesSource bs = new BytesSource("foo".getBytes());
    assertNotNull(bs.getData());
    assertEquals("BytesSource[foo]", bs.toString());
    assertNull(bs.getSystemId());
    assertNotNull(bs.getInputStream());
    assertNotNull(bs.getReader());
}
Also used : BytesSource(org.apache.camel.BytesSource)

Aggregations

BytesSource (org.apache.camel.BytesSource)9 StringSource (org.apache.camel.StringSource)3 InputStream (java.io.InputStream)2 Properties (java.util.Properties)2 StreamResult (javax.xml.transform.stream.StreamResult)2 Converter (org.apache.camel.Converter)2 StreamCache (org.apache.camel.StreamCache)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Reader (java.io.Reader)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 Source (javax.xml.transform.Source)1 SchemaFactory (javax.xml.validation.SchemaFactory)1 Exchange (org.apache.camel.Exchange)1 WrappedFile (org.apache.camel.WrappedFile)1 DefaultLSResourceResolver (org.apache.camel.component.validator.DefaultLSResourceResolver)1 XmlSignatureException (org.apache.camel.component.xmlsecurity.api.XmlSignatureException)1