/** * 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(); } }
public void testSerialization() throws Exception { StringSource expected = new StringSource(expectedBody, "mySystemID", "utf-8"); expected.setPublicId("myPublicId"); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutputStream output = new ObjectOutputStream(buffer); output.writeObject(expected); output.close(); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); Object object = in.readObject(); assertTrue("is a StringSource", object instanceof StringSource); StringSource actual = (StringSource) object; assertEquals("source.text", expected.getPublicId(), actual.getPublicId()); assertEquals("source.text", expected.getSystemId(), actual.getSystemId()); assertEquals("source.text", expected.getEncoding(), actual.getEncoding()); assertEquals("source.text", expected.getText(), actual.getText()); String value = converter.convertTo(String.class, actual); assertEquals("text value of StringSource", expectedBody, value); }
public void testLogBodyStreamStringSourceDisabled() throws Exception { context.getProperties().put(Exchange.LOG_DEBUG_BODY_STREAMS, "false"); StringSource body = new StringSource("<?xml version=\"1.0\"?><person><name>Claus</name></person>"); MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(1); template.sendBody("direct:start", body); assertMockEndpointsSatisfied(); // should be logged anyway TraceExchangeFormatter myFormatter = context.getRegistry().lookupByNameAndType("logFormatter", TraceExchangeFormatter.class); String msg = myFormatter.getMessage(); assertTrue(msg.endsWith("Body: <?xml version=\"1.0\"?><person><name>Claus</name></person>]")); }
public void testLogBodyStreamStringSourceDisabledByDefault() throws Exception { context.getProperties().remove(Exchange.LOG_DEBUG_BODY_STREAMS); StringSource body = new StringSource("<?xml version=\"1.0\"?><person><name>Claus</name></person>"); MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(1); template.sendBody("direct:start", body); assertMockEndpointsSatisfied(); // should be logged anyway TraceExchangeFormatter myFormatter = context.getRegistry().lookupByNameAndType("logFormatter", TraceExchangeFormatter.class); String msg = myFormatter.getMessage(); assertTrue(msg.endsWith("Body: <?xml version=\"1.0\"?><person><name>Claus</name></person>]")); }
public void testLogBodyStreamStringSourceEnabled() throws Exception { context.getProperties().put(Exchange.LOG_DEBUG_BODY_STREAMS, "true"); StringSource body = new StringSource("<?xml version=\"1.0\"?><person><name>Claus</name></person>"); MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(1); template.sendBody("direct:start", body); assertMockEndpointsSatisfied(); // should be logged anyway TraceExchangeFormatter myFormatter = context.getRegistry().lookupByNameAndType("logFormatter", TraceExchangeFormatter.class); String msg = myFormatter.getMessage(); assertTrue(msg.endsWith("Body: <?xml version=\"1.0\"?><person><name>Claus</name></person>]")); }
@Test public void testLondonWithStringSourceAsObject() throws Exception { MockEndpoint mock = getMockEndpoint("mock:london"); mock.expectedMessageCount(1); mock.message(0).body(String.class).contains("James"); Source source = new StringSource("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<person user=\"james\">\n" + " <firstName>James</firstName>\n" + " <lastName>Strachan</lastName>\n" + " <city>London</city>\n" + "</person>"); assertNotNull(source); template.sendBody("direct:object", source); assertMockEndpointsSatisfied(); }
@Test public void testLondonWithStringSourceAsBytes() throws Exception { MockEndpoint mock = getMockEndpoint("mock:london"); mock.expectedMessageCount(1); mock.message(0).body(String.class).contains("James"); Source source = new StringSource("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<person user=\"james\">\n" + " <firstName>James</firstName>\n" + " <lastName>Strachan</lastName>\n" + " <city>London</city>\n" + "</person>"); assertNotNull(source); template.sendBody("direct:bytes", source); assertMockEndpointsSatisfied(); }
@Test public void testLondonWithStringSourceAsDefault() throws Exception { MockEndpoint mock = getMockEndpoint("mock:london"); mock.expectedMessageCount(1); mock.message(0).body(String.class).contains("James"); Source source = new StringSource("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<person user=\"james\">\n" + " <firstName>James</firstName>\n" + " <lastName>Strachan</lastName>\n" + " <city>London</city>\n" + "</person>"); assertNotNull(source); template.sendBody("direct:default", source); assertMockEndpointsSatisfied(); }
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws TransformerConfigurationException { from("seda:xslttest") .setHeader("testheader", el("hello")) .setBody(el("header:${in.headers.testheader}")) .to("mock:Before") .setBody(el("<cats><cat id=\"1\"/><cat id=\"2\"/></cats>")) .process(XsltBuilder.xslt(new StringSource(xslt))) .setBody(el("header:${in.headers.testheader}")) .to("mock:After"); } }; }
@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); }
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); } }
public void testStringSourceMessage() throws Exception { MockEndpoint mock = getMockEndpoint("mock:valid"); mock.expectedMessageCount(1); String xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>" + "<user xmlns=\"http://foo.com/bar\">" + " <id>1</id>" + " <username>davsclaus</username>" + "</user>"; template.sendBody("direct:start", new StringSource(xml)); assertMockEndpointsSatisfied(); }
public void testSourceCache() throws Exception { String input = "<A>a</A>"; MockEndpoint mock = getMockEndpoint("mock:resulta"); mock.expectedBodiesReceived(input); mock = getMockEndpoint("mock:resultb"); mock.expectedBodiesReceived(input); StringSource ss = new StringSource(input); SAXSource saxSource = new SAXSource(SAXSource.sourceToInputSource(ss)); template.sendBody("direct:start", saxSource); assertMockEndpointsSatisfied(); }
public void testSendStringSource() throws Exception { x.expectedMessageCount(1); y.expectedMessageCount(1); sendBody("direct:start", new StringSource("<message>xx</message>")); sendBody("direct:start", new StringSource("<message>yy</message>")); assertMockEndpointsSatisfied(); }
@Override protected void setUp() throws Exception { super.setUp(); File file = new File("src/test/resources/org/apache/camel/processor/ValidatingProcessor.xsd"); String body = context.getTypeConverter().convertTo(String.class, file); validating = new ValidatingProcessor(); validating.setSchemaSource(new StringSource(body)); // loading scheme can be forced so lets try it validating.loadSchema(); }
public Object evaluate(Exchange exchange) { try { LOG.debug("Evaluation: {} for exchange: {}", expression, exchange); if (resultType != null) { if (resultType.equals(String.class)) { return evaluateAsString(exchange); } else if (resultType.isAssignableFrom(Collection.class)) { return evaluateAsList(exchange); } else if (resultType.isAssignableFrom(Node.class)) { return evaluateAsDOM(exchange); } else { throw new IllegalArgumentException("ResultType: " + resultType.getCanonicalName() + " not supported"); } } switch (resultsFormat) { case Bytes: return evaluateAsBytes(exchange); case BytesSource: return evaluateAsBytesSource(exchange); case DOM: return evaluateAsDOM(exchange); case List: return evaluateAsList(exchange); case StringSource: return evaluateAsStringSource(exchange); case String: default: return evaluateAsString(exchange); } } catch (Exception e) { throw new RuntimeExpressionException(e); } }
public Object evaluateAsStringSource(Exchange exchange) throws Exception { LOG.debug("evaluateAsString: {} for exchange: {}", expression, exchange); initialize(exchange); String text = evaluateAsString(exchange); return new StringSource(text); }
@Before public void setUp() throws Exception { super.setUp(); exchange = new DefaultExchange(context); evaluator = new XPathEvaluator(); doc = evaluator.setSource(new StringSource(CONTENT)); }
@Test public void consumeStockQuoteWebserviceWithCamelStringSourceInput() throws Exception { Object result = template.requestBody("direct:stockQuoteWebservice", new StringSource(xmlRequestForGoogleStockQuote)); assertNotNull(result); assertTrue(result instanceof Source); }
@Test(timeout = 5000) public void consumeStockQuoteWebserviceWithCamelStringSourceInput() throws Exception { Object result = template.requestBody("direct:stockQuoteWebservice", new StringSource(xmlRequestForGoogleStockQuote)); assertNotNull(result); assertTrue(result instanceof Source); }
/** * Assumes that the exchange's in-body can be retrieved by <code>exchange.getIn().getBody(String.class)</code> and then unmarshals this body into a jaxb object (using * {@link JAXBConstants#JAXB_ContextPath}). The jaxb object is then processed by the responder that has beed registered for the jaxb object's class. * * @see #addResponder(Class, AbstractResponder) */ @Override public void process(Exchange exchange) throws Exception // NOPMD by al on 5/9/13 12:48 PM { final String requestStr = exchange.getIn().getBody(String.class); log.info("Received request: " + requestStr); final JAXBElement<?> jaxbElement = (JAXBElement<?>)jaxbContext.createUnmarshaller().unmarshal(new StringSource(requestStr)); final Object xmlRequest = jaxbElement.getValue(); getResponderForRequest(xmlRequest).process(exchange); }
protected IT retrieveRequest(String requestStr) throws JAXBException { @SuppressWarnings("unchecked") final JAXBElement<IT> jaxbElement = (JAXBElement<IT>)jaxbContext.createUnmarshaller().unmarshal(new StringSource(requestStr)); final IT xmlRequest = jaxbElement.getValue(); return xmlRequest; }
@Test public void consumeStockQuoteWebserviceWithCamelStringSourceInput() throws Exception { Object result = template.requestBody("direct:stockQuoteWebservice", new StringSource(xmlRequestForGoogleStockQuote)); Assert.assertNotNull(result); Assert.assertTrue(result instanceof Source); }
@Converter public static StreamCache convertToStreamCache(StringSource source) { //no need to do stream caching for a StringSource return null; }
/** * Converts the given String to a Source */ @Converter public StringSource toStringSource(String data) { return new StringSource(data); }
/** * Converts the given String to a Source */ @Converter public Source toSource(String data) { return new StringSource(data); }
@Converter public StreamSource toStreamSourceFromDOM(DOMSource source, Exchange exchange) throws TransformerException { String result = toString(source, exchange); return new StringSource(result); }
@Converter public StreamSource toStreamSourceFromStAX(StAXSource source, Exchange exchange) throws TransformerException { String result = toString(source, exchange); return new StringSource(result); }
public XQueryBuilder asStringSource() { setResultsFormat(ResultFormat.StringSource); return this; }
/** * Converts a Spring-WS {@link org.springframework.xml.transform.StringSource} * to a Camel {@link org.apache.camel.converter.jaxp.StringSource} */ @Converter public static StringSource toStringSourceFromSpring(org.springframework.xml.transform.StringSource springStringSource) { return new StringSource(springStringSource.toString()); }
/** * Converts a Camel {@link org.apache.camel.converter.jaxp.StringSource} * to a Spring-WS {@link org.springframework.xml.transform.StringSource} */ @Converter public static org.springframework.xml.transform.StringSource toStringSourceFromCamel(StringSource camelStringSource) { return new org.springframework.xml.transform.StringSource(camelStringSource.getText()); }
@Converter public GPathResult fromStringSource(StringSource input) throws IOException, SAXException, ParserConfigurationException { return fromString(input.getText()); }
protected <T> T unmarshalFragment(String responseXML, Class<T> fragmentClass) throws JAXBException { Unmarshaller unmarshaller = JAXBContext.newInstance(fragmentClass).createUnmarshaller(); JAXBElement<T> jaxbElement = unmarshaller.unmarshal(new StringSource(responseXML), fragmentClass); return jaxbElement.getValue(); }