Java 类org.apache.camel.StringSource 实例源码

项目:Camel    文件:XmlConverter.java   
/**
 * 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();
    }
}
项目:Camel    文件:StringSourceTest.java   
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);
}
项目:Camel    文件:LogDebugBodyStreamsTest.java   
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>]"));
}
项目:Camel    文件:LogDebugBodyStreamsTest.java   
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>]"));
}
项目:Camel    文件:LogDebugBodyStreamsTest.java   
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>]"));
}
项目:Camel    文件:JmsXMLRouteTest.java   
@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();
}
项目:Camel    文件:JmsXMLRouteTest.java   
@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();
}
项目:Camel    文件:JmsXMLRouteTest.java   
@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();
}
项目:Camel    文件:XsltLosesHeaderTest.java   
@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");
        }
    };
}
项目:Camel    文件:XmlConverter.java   
@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);
}
项目:Camel    文件:StreamCachingInterceptorTest.java   
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);
    }        
}
项目:Camel    文件:ValidatingProcessorTest.java   
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();
}
项目:Camel    文件:MultiCastParallelAndStreamCachingTest.java   
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();
}
项目:Camel    文件:StreamSourceContentBasedRouterTest.java   
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();
}
项目:Camel    文件:ValidatingProcessorFromSourceTest.java   
@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();
}
项目:Camel    文件:XQueryBuilder.java   
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);
    }
}
项目:Camel    文件:XQueryBuilder.java   
public Object evaluateAsStringSource(Exchange exchange) throws Exception {
    LOG.debug("evaluateAsString: {} for exchange: {}", expression, exchange);
    initialize(exchange);

    String text = evaluateAsString(exchange);
    return new StringSource(text);
}
项目:Camel    文件:SaxonConverterTest.java   
@Before
public void setUp() throws Exception {
    super.setUp();
    exchange = new DefaultExchange(context);
    evaluator = new XPathEvaluator();
    doc = evaluator.setSource(new StringSource(CONTENT));
}
项目:Camel    文件:ProducerLocalRouteTest.java   
@Test
public void consumeStockQuoteWebserviceWithCamelStringSourceInput() throws Exception {
    Object result = template.requestBody("direct:stockQuoteWebservice", new StringSource(xmlRequestForGoogleStockQuote));

    assertNotNull(result);
    assertTrue(result instanceof Source);
}
项目:Camel    文件:ProducerRemoteRouteTest.java   
@Test(timeout = 5000)
public void consumeStockQuoteWebserviceWithCamelStringSourceInput() throws Exception {
    Object result = template.requestBody("direct:stockQuoteWebservice", new StringSource(xmlRequestForGoogleStockQuote));

    assertNotNull(result);
    assertTrue(result instanceof Source);
}
项目:metasfresh    文件:SwitchResponder.java   
/**
 * 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);
}
项目:metasfresh    文件:AbstractResponder.java   
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;
}
项目:wildfly-camel    文件:SpringWsIntegrationTest.java   
@Test
public void consumeStockQuoteWebserviceWithCamelStringSourceInput() throws Exception {
    Object result = template.requestBody("direct:stockQuoteWebservice", new StringSource(xmlRequestForGoogleStockQuote));

    Assert.assertNotNull(result);
    Assert.assertTrue(result instanceof Source);
}
项目:Camel    文件:StreamCacheConverter.java   
@Converter
public static StreamCache convertToStreamCache(StringSource source) {
    //no need to do stream caching for a StringSource
    return null;
}
项目:Camel    文件:XmlConverter.java   
/**
 * Converts the given String to a Source
 */
@Converter
public StringSource toStringSource(String data) {
    return new StringSource(data);
}
项目:Camel    文件:XmlConverter.java   
/**
 * Converts the given String to a Source
 */
@Converter
public Source toSource(String data) {
    return new StringSource(data);
}
项目:Camel    文件:XmlConverter.java   
@Converter
public StreamSource toStreamSourceFromDOM(DOMSource source, Exchange exchange) throws TransformerException {
    String result = toString(source, exchange);
    return new StringSource(result);
}
项目:Camel    文件:XmlConverter.java   
@Converter
public StreamSource toStreamSourceFromStAX(StAXSource source, Exchange exchange) throws TransformerException {
    String result = toString(source, exchange);
    return new StringSource(result);
}
项目:Camel    文件:XQueryBuilder.java   
public XQueryBuilder asStringSource() {
    setResultsFormat(ResultFormat.StringSource);
    return this;
}
项目:Camel    文件:StringSourceConverter.java   
/**
 * 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());
}
项目:Camel    文件:StringSourceConverter.java   
/**
 * 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());
}
项目:Camel    文件:GPathResultConverter.java   
@Converter
public GPathResult fromStringSource(StringSource input) throws IOException, SAXException, ParserConfigurationException {
    return fromString(input.getText());
}
项目:cleverbus    文件:AbstractOperationRouteTest.java   
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();
}