use of java.io.StringReader in project camel by apache.
the class CacheBasedXPathReplacer method process.
public void process(Exchange exchange) throws Exception {
String cacheKey = key.evaluate(exchange, String.class);
if (isValid(cacheManager, cacheName, cacheKey)) {
Ehcache cache = cacheManager.getCache(cacheName);
if (LOG.isDebugEnabled()) {
LOG.debug("Replacing XPath value {} in Message with value stored against key {} in CacheName {}", new Object[] { xpath, cacheKey, cacheName });
}
exchange.getIn().setHeader(CacheConstants.CACHE_KEY, cacheKey);
Object body = exchange.getIn().getBody();
InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class, body);
Document document;
try {
document = exchange.getContext().getTypeConverter().convertTo(Document.class, exchange, is);
} finally {
IOHelper.close(is, "is", LOG);
}
InputStream cis = exchange.getContext().getTypeConverter().convertTo(InputStream.class, cache.get(cacheKey).getObjectValue());
try {
Document cacheValueDocument = exchange.getContext().getTypeConverter().convertTo(Document.class, exchange, cis);
// Create/setup the Transformer
XmlConverter xmlConverter = new XmlConverter();
String xslString = IOConverter.toString(new File("./src/main/resources/xpathreplacer.xsl"), exchange);
xslString = xslString.replace("##match_token##", xpath);
Source xslSource = xmlConverter.toStreamSource(new StringReader(xslString));
TransformerFactory transformerFactory = xmlConverter.createTransformerFactory();
Transformer transformer = transformerFactory.newTransformer(xslSource);
DOMSource source = xmlConverter.toDOMSource(document);
DOMResult result = new DOMResult();
transformer.setParameter("cacheValue", cacheValueDocument);
transformer.transform(source, result);
// DOMSource can be converted to byte[] by camel type converter mechanism
DOMSource dom = new DOMSource(result.getNode());
exchange.getIn().setBody(dom, byte[].class);
} finally {
IOHelper.close(cis, "cis", LOG);
}
}
}
use of java.io.StringReader in project camel by apache.
the class AbstractGoogleDriveTestSupport method createCamelContext.
@Override
protected CamelContext createCamelContext() throws Exception {
final InputStream in = getClass().getResourceAsStream(TEST_OPTIONS_PROPERTIES);
if (in == null) {
throw new IOException(TEST_OPTIONS_PROPERTIES + " could not be found");
}
final StringBuilder builder = new StringBuilder();
final BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line).append(LINE_SEPARATOR);
}
propertyText = builder.toString();
final Properties properties = new Properties();
try {
properties.load(new StringReader(propertyText));
} catch (IOException e) {
throw new IOException(String.format("%s could not be loaded: %s", TEST_OPTIONS_PROPERTIES, e.getMessage()), e);
}
//
// // cache test properties
// refreshToken = properties.getProperty(REFRESH_TOKEN_PROPERTY);
// testFolderId = properties.getProperty("testFolderId");
// testFileId = properties.getProperty("testFileId");
// testUserId = properties.getProperty("testUserId");
//
Map<String, Object> options = new HashMap<String, Object>();
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
options.put(entry.getKey().toString(), entry.getValue());
}
final GoogleDriveConfiguration configuration = new GoogleDriveConfiguration();
IntrospectionSupport.setProperties(configuration, options);
// add GoogleDriveComponent to Camel context
final CamelContext context = super.createCamelContext();
final GoogleDriveComponent component = new GoogleDriveComponent(context);
component.setConfiguration(configuration);
context.addComponent("google-drive", component);
return context;
}
use of java.io.StringReader in project camel by apache.
the class JibxDataFormatMarshallTest method testMarshall.
@Test
public void testMarshall() throws InterruptedException, ParserConfigurationException, IOException, SAXException {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
PurchaseOrder purchaseOrder = new PurchaseOrder();
String name = "foo";
purchaseOrder.setName(name);
double price = 49;
purchaseOrder.setPrice(price);
double amount = 3;
purchaseOrder.setAmount(amount);
template.sendBody("direct:start", purchaseOrder);
assertMockEndpointsSatisfied();
String body = mock.getReceivedExchanges().get(0).getIn().getBody(String.class);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Element root = builder.parse(new InputSource(new StringReader(body))).getDocumentElement();
assertEquals(name, root.getAttribute("name"));
assertEquals(price + "", root.getAttribute("price"));
assertEquals(amount + "", root.getAttribute("amount"));
}
use of java.io.StringReader in project camel by apache.
the class JibxDataFormatMarshallWithBindingNameTest method testMarshall.
@Test
public void testMarshall() throws InterruptedException, ParserConfigurationException, IOException, SAXException {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
PurchaseOrder purchaseOrder = new PurchaseOrder();
String name = "foo";
purchaseOrder.setName(name);
double price = 49;
purchaseOrder.setPrice(price);
double amount = 3;
purchaseOrder.setAmount(amount);
template.sendBody("direct:start", purchaseOrder);
assertMockEndpointsSatisfied();
String body = mock.getReceivedExchanges().get(0).getIn().getBody(String.class);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Element root = builder.parse(new InputSource(new StringReader(body))).getDocumentElement();
assertEquals(name, root.getAttribute("name"));
assertEquals(price + "", root.getAttribute("price"));
assertEquals(amount + "", root.getAttribute("amount"));
}
use of java.io.StringReader in project camel by apache.
the class JibxDataFormatSpringDslTest method testMarshall.
@Test
public void testMarshall() throws InterruptedException, ParserConfigurationException, IOException, SAXException {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
PurchaseOrder purchaseOrder = new PurchaseOrder();
String name = "foo";
purchaseOrder.setName(name);
double price = 49;
purchaseOrder.setPrice(price);
double amount = 3;
purchaseOrder.setAmount(amount);
template.sendBody("direct:marshall", purchaseOrder);
assertMockEndpointsSatisfied();
String body = mock.getReceivedExchanges().get(0).getIn().getBody(String.class);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Element root = builder.parse(new InputSource(new StringReader(body))).getDocumentElement();
assertEquals(name, root.getAttribute("name"));
assertEquals(price + "", root.getAttribute("price"));
assertEquals(amount + "", root.getAttribute("amount"));
}
Aggregations