use of javax.xml.transform.stream.StreamSource in project camel by apache.
the class CacheInputStreamInDeadLetterIssue520Test method testSendingSource.
public void testSendingSource() throws Exception {
StreamSource message = new StreamSource(new StringReader("<hello>Willem</hello>"));
sendingMessage(message);
}
use of javax.xml.transform.stream.StreamSource in project camel by apache.
the class ValidatingProcessor method doProcess.
protected void doProcess(Exchange exchange) throws Exception {
Schema schema;
if (isUseSharedSchema()) {
schema = getSchema();
} else {
schema = createSchema();
}
Validator validator = schema.newValidator();
// the underlying input stream, which we need to close to avoid locking files or other resources
Source source = null;
InputStream is = null;
try {
Result result = null;
// only convert to input stream if really needed
if (isInputStreamNeeded(exchange)) {
is = getContentToValidate(exchange, InputStream.class);
if (is != null) {
source = getSource(exchange, is);
}
} else {
Object content = getContentToValidate(exchange);
if (content != null) {
source = getSource(exchange, content);
}
}
if (shouldUseHeader()) {
if (source == null && isFailOnNullHeader()) {
throw new NoXmlHeaderValidationException(exchange, headerName);
}
} else {
if (source == null && isFailOnNullBody()) {
throw new NoXmlBodyValidationException(exchange);
}
}
//CAMEL-7036 We don't need to set the result if the source is an instance of StreamSource
if (source instanceof DOMSource) {
result = new DOMResult();
} else if (source instanceof SAXSource) {
result = new SAXResult();
} else if (source instanceof StAXSource || source instanceof StreamSource) {
result = null;
}
if (source != null) {
// create a new errorHandler and set it on the validator
// must be a local instance to avoid problems with concurrency (to be
// thread safe)
ValidatorErrorHandler handler = errorHandler.getClass().newInstance();
validator.setErrorHandler(handler);
try {
LOG.trace("Validating {}", source);
validator.validate(source, result);
handler.handleErrors(exchange, schema, result);
} catch (SAXParseException e) {
// can be thrown for non well formed XML
throw new SchemaValidationException(exchange, schema, Collections.singletonList(e), Collections.<SAXParseException>emptyList(), Collections.<SAXParseException>emptyList());
}
}
} finally {
IOHelper.close(is);
}
}
use of javax.xml.transform.stream.StreamSource in project camel by apache.
the class StreamCachingInterceptorTest method testConvertStreamSourceWithRouteOnlyStreamCaching.
public void testConvertStreamSourceWithRouteOnlyStreamCaching() throws Exception {
b.expectedMessageCount(1);
StreamSource message = new StreamSource(new StringReader(MESSAGE));
template.sendBody("direct:b", message);
assertMockEndpointsSatisfied();
assertTrue(b.assertExchangeReceived(0).getIn().getBody() instanceof StreamCache);
assertEquals(b.assertExchangeReceived(0).getIn().getBody(String.class), MESSAGE);
}
use of javax.xml.transform.stream.StreamSource in project camel by apache.
the class XmlConverter method toStreamSourceFromSAX.
@Converter
public StreamSource toStreamSourceFromSAX(SAXSource source, Exchange exchange) throws TransformerException {
InputSource inputSource = source.getInputSource();
if (inputSource != null) {
if (inputSource.getCharacterStream() != null) {
return new StreamSource(inputSource.getCharacterStream());
}
if (inputSource.getByteStream() != null) {
return new StreamSource(inputSource.getByteStream());
}
}
String result = toString(source, exchange);
return new StringSource(result);
}
use of javax.xml.transform.stream.StreamSource in project camel by apache.
the class XmlConverterTest method testToStreamSourceByFile.
public void testToStreamSourceByFile() throws Exception {
XmlConverter conv = new XmlConverter();
File file = new File("org/apache/camel/converter/stream/test.xml");
StreamSource source = conv.toStreamSource(file);
StreamSource out = conv.toStreamSource(source, null);
assertSame(source, out);
}
Aggregations