Author: davsclaus Date: Tue Jun 23 08:06:51 2009 New Revision: 787581 URL: http://svn.apache.org/viewvc?rev=787581&view=rev Log: CAMEL-1743: Added converter so validator component can work with InputStream directly.
Added: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorStreamTest.java (with props) camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorStreamWithStreamCachingEnabledTest.java (with props) Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java?rev=787581&r1=787580&r2=787581&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java Tue Jun 23 08:06:51 2009 @@ -27,7 +27,6 @@ import java.lang.reflect.Constructor; import java.nio.ByteBuffer; import java.util.Properties; - import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -44,18 +43,16 @@ import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; +import org.apache.camel.Converter; +import org.apache.camel.Exchange; +import org.apache.camel.util.ObjectHelper; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; - import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; -import org.apache.camel.Converter; -import org.apache.camel.Exchange; -import org.apache.camel.util.ObjectHelper; - /** * A helper class to transform to and from various JAXB types such as {...@link Source} and {...@link Document} @@ -110,8 +107,8 @@ */ public void toResult(Source source, Result result) throws TransformerException { toResult(source, result, defaultOutputProperties()); - } - + } + /** * Converts the given input Source into the required result */ @@ -119,15 +116,15 @@ if (source == null) { return; } - + Transformer transformer = createTransfomer(); if (transformer == null) { throw new TransformerException("Could not create a transformer - JAXP is misconfigured!"); } transformer.setOutputProperties(outputProperties); transformer.transform(source, result); - } - + } + /** * Converts the given byte[] to a Source */ @@ -366,6 +363,15 @@ } @Converter + public DOMSource toDOMSource(InputStream is) throws ParserConfigurationException, IOException, SAXException { + InputSource source = new InputSource(is); + String systemId = source.getSystemId(); + DocumentBuilder builder = createDocumentBuilder(); + Document document = builder.parse(source); + return new DOMSource(document, systemId); + } + + @Converter public DOMSource toDOMSourceFromStream(StreamSource source) throws ParserConfigurationException, IOException, SAXException { DocumentBuilder builder = createDocumentBuilder(); String systemId = source.getSystemId(); @@ -421,7 +427,7 @@ @Converter public Node toDOMNode(Source source) throws TransformerException, ParserConfigurationException, IOException, SAXException { DOMSource domSrc = toDOMSource(source); - return domSrc != null ? domSrc.getNode() : null; + return domSrc != null ? domSrc.getNode() : null; } /** @@ -443,10 +449,10 @@ // If the node is an document, return the root element if (node instanceof Document) { return ((Document) node).getDocumentElement(); - // If the node is an element, just cast it + // If the node is an element, just cast it } else if (node instanceof Element) { return (Element) node; - // Other node types are not handled + // Other node types are not handled } else { throw new TransformerException("Unable to convert DOM node to an Element"); } @@ -543,19 +549,19 @@ // If the node is the document, just cast it if (node instanceof Document) { return (Document) node; - // If the node is an element + // If the node is an element } else if (node instanceof Element) { Element elem = (Element) node; // If this is the root element, return its owner document if (elem.getOwnerDocument().getDocumentElement() == elem) { return elem.getOwnerDocument(); - // else, create a new doc and copy the element inside it + // else, create a new doc and copy the element inside it } else { Document doc = createDocument(); doc.appendChild(doc.importNode(node, true)); return doc; } - // other element types are not handled + // other element types are not handled } else { throw new TransformerException("Unable to convert DOM node to a Document"); } Added: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorStreamTest.java URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorStreamTest.java?rev=787581&view=auto ============================================================================== --- camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorStreamTest.java (added) +++ camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorStreamTest.java Tue Jun 23 08:06:51 2009 @@ -0,0 +1,47 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.itest.jetty; + +import java.io.InputStream; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; + +/** + * @version $Revision$ + */ +public class JettyValidatorStreamTest extends ContextTestSupport { + + public void testValideRequestAsStream() throws Exception { + InputStream inputStream = HttpClient.class.getResourceAsStream("ValidRequest.xml"); + assertNotNull("the inputStream should not be null", inputStream); + String response = HttpClient.send(inputStream); + assertEquals("The response should be ok", response, "<ok/>"); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty:http://localhost:8192/test") + .to("validator:OptimizationRequest.xsd") + .transform(constant("<ok/>")); + } + }; + } +} Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorStreamTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorStreamTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorStreamWithStreamCachingEnabledTest.java URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorStreamWithStreamCachingEnabledTest.java?rev=787581&view=auto ============================================================================== --- camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorStreamWithStreamCachingEnabledTest.java (added) +++ camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorStreamWithStreamCachingEnabledTest.java Tue Jun 23 08:06:51 2009 @@ -0,0 +1,49 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.itest.jetty; + +import java.io.InputStream; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; + +/** + * @version $Revision$ + */ +public class JettyValidatorStreamWithStreamCachingEnabledTest extends ContextTestSupport { + + public void testValideRequestAsStream() throws Exception { + InputStream inputStream = HttpClient.class.getResourceAsStream("ValidRequest.xml"); + assertNotNull("the inputStream should not be null", inputStream); + String response = HttpClient.send(inputStream); + assertEquals("The response should be ok", response, "<ok/>"); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + context.setStreamCaching(true); + + from("jetty:http://localhost:8192/test") + .to("validator:OptimizationRequest.xsd") + .transform(constant("<ok/>")); + } + }; + } +} \ No newline at end of file Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorStreamWithStreamCachingEnabledTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorStreamWithStreamCachingEnabledTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorTest.java URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorTest.java?rev=787581&r1=787580&r2=787581&view=diff ============================================================================== --- camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorTest.java (original) +++ camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/jetty/JettyValidatorTest.java Tue Jun 23 08:06:51 2009 @@ -43,13 +43,13 @@ return new RouteBuilder() { public void configure() { from("jetty:http://localhost:8192/test") - .setBody(body(String.class)) + .convertBodyTo(String.class) .to("log:in") .doTry() .to("validator:OptimizationRequest.xsd") - .setBody(constant("<ok/>")) + .transform(constant("<ok/>")) .doCatch(ValidationException.class) - .setBody(constant("<error/>")) + .transform(constant("<error/>")) .end() .to("log:out"); }