Search in sources :

Example 21 with StringReader

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);
        }
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) InputStream(java.io.InputStream) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) StringReader(java.io.StringReader) Ehcache(net.sf.ehcache.Ehcache) File(java.io.File)

Example 22 with StringReader

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;
}
Also used : CamelContext(org.apache.camel.CamelContext) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) HashMap(java.util.HashMap) Map(java.util.Map)

Example 23 with StringReader

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"));
}
Also used : InputSource(org.xml.sax.InputSource) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) PurchaseOrder(org.apache.camel.dataformat.jibx.model.PurchaseOrder) Test(org.junit.Test)

Example 24 with StringReader

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"));
}
Also used : InputSource(org.xml.sax.InputSource) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) PurchaseOrder(org.apache.camel.dataformat.jibx.model.PurchaseOrder) Test(org.junit.Test)

Example 25 with StringReader

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"));
}
Also used : InputSource(org.xml.sax.InputSource) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) PurchaseOrder(org.apache.camel.dataformat.jibx.model.PurchaseOrder) Test(org.junit.Test)

Aggregations

StringReader (java.io.StringReader)3695 Test (org.junit.Test)837 IOException (java.io.IOException)488 Reader (java.io.Reader)402 InputSource (org.xml.sax.InputSource)369 TokenStream (org.apache.lucene.analysis.TokenStream)298 BufferedReader (java.io.BufferedReader)277 Tokenizer (org.apache.lucene.analysis.Tokenizer)238 StringWriter (java.io.StringWriter)226 ArrayList (java.util.ArrayList)225 JSONReader (com.alibaba.fastjson.JSONReader)195 Document (org.w3c.dom.Document)181 DocumentBuilder (javax.xml.parsers.DocumentBuilder)165 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)141 Map (java.util.Map)139 ParserResult (org.jabref.logic.importer.ParserResult)130 MockTokenizer (org.apache.lucene.analysis.MockTokenizer)120 HashMap (java.util.HashMap)117 Element (org.w3c.dom.Element)108 StreamSource (javax.xml.transform.stream.StreamSource)106