Allow to use tarfile data format in XML marshal/unmarshal
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/fd0eee43 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/fd0eee43 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/fd0eee43 Branch: refs/heads/master Commit: fd0eee438a1182204a8ce2a03b17620d95a95b8a Parents: 40f3870 Author: Antoine DESSAIGNE <antoine.dessai...@gmail.com> Authored: Tue Apr 26 17:11:52 2016 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue Apr 26 19:13:12 2016 +0200 ---------------------------------------------------------------------- .../apache/camel/model/MarshalDefinition.java | 8 +- .../apache/camel/model/UnmarshalDefinition.java | 6 +- .../apache/camel/model/dataformat/jaxb.index | 1 + .../tarfile/SpringTarFileDataFormatTest.java | 168 +++++++++++++++++++ .../tarfile/TarFileDataFormatTest.java | 47 +----- .../camel/dataformat/tarfile/TarUtils.java | 71 ++++++++ .../tarfile/SpringTarFileDataFormatTest.xml | 82 +++++++++ 7 files changed, 333 insertions(+), 50 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/fd0eee43/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java b/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java index 706f6e3..be08866 100644 --- a/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java @@ -48,6 +48,7 @@ import org.apache.camel.model.dataformat.SerializationDataFormat; import org.apache.camel.model.dataformat.SoapJaxbDataFormat; import org.apache.camel.model.dataformat.StringDataFormat; import org.apache.camel.model.dataformat.SyslogDataFormat; +import org.apache.camel.model.dataformat.TarFileDataFormat; import org.apache.camel.model.dataformat.TidyMarkupDataFormat; import org.apache.camel.model.dataformat.UniVocityCsvDataFormat; import org.apache.camel.model.dataformat.UniVocityFixedWidthDataFormat; @@ -67,7 +68,7 @@ import org.apache.camel.spi.RouteContext; /** * Marshals data into a specified format for transmission over a transport or component * - * @version + * @version */ @Metadata(label = "eip,transformation") @XmlRootElement(name = "marshal") @@ -102,6 +103,7 @@ public class MarshalDefinition extends NoOutputDefinition<MarshalDefinition> { @XmlElement(required = false, name = "soapjaxb", type = SoapJaxbDataFormat.class), @XmlElement(required = false, name = "string", type = StringDataFormat.class), @XmlElement(required = false, name = "syslog", type = SyslogDataFormat.class), + @XmlElement(required = false, name = "tarfile", type = TarFileDataFormat.class), @XmlElement(required = false, name = "tidyMarkup", type = TidyMarkupDataFormat.class), @XmlElement(required = false, name = "univocity-csv", type = UniVocityCsvDataFormat.class), @XmlElement(required = false, name = "univocity-fixed", type = UniVocityFixedWidthDataFormat.class), @@ -135,7 +137,7 @@ public class MarshalDefinition extends NoOutputDefinition<MarshalDefinition> { public String toString() { return "Marshal[" + description() + "]"; } - + protected String description() { return dataFormatType != null ? dataFormatType.toString() : "ref:" + ref; } @@ -144,7 +146,7 @@ public class MarshalDefinition extends NoOutputDefinition<MarshalDefinition> { public String getLabel() { return "marshal[" + description() + "]"; } - + public String getRef() { return ref; } http://git-wip-us.apache.org/repos/asf/camel/blob/fd0eee43/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java b/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java index 9b95f23..c4af19b 100644 --- a/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java @@ -48,6 +48,7 @@ import org.apache.camel.model.dataformat.SerializationDataFormat; import org.apache.camel.model.dataformat.SoapJaxbDataFormat; import org.apache.camel.model.dataformat.StringDataFormat; import org.apache.camel.model.dataformat.SyslogDataFormat; +import org.apache.camel.model.dataformat.TarFileDataFormat; import org.apache.camel.model.dataformat.TidyMarkupDataFormat; import org.apache.camel.model.dataformat.UniVocityCsvDataFormat; import org.apache.camel.model.dataformat.UniVocityFixedWidthDataFormat; @@ -67,7 +68,7 @@ import org.apache.camel.spi.RouteContext; /** * Converts the message data received from the wire into a format that Apache Camel processors can consume * - * @version + * @version */ @Metadata(label = "eip,transformation") @XmlRootElement(name = "unmarshal") @@ -102,6 +103,7 @@ public class UnmarshalDefinition extends NoOutputDefinition<UnmarshalDefinition> @XmlElement(required = false, name = "soapjaxb", type = SoapJaxbDataFormat.class), @XmlElement(required = false, name = "string", type = StringDataFormat.class), @XmlElement(required = false, name = "syslog", type = SyslogDataFormat.class), + @XmlElement(required = false, name = "tarfile", type = TarFileDataFormat.class), @XmlElement(required = false, name = "tidyMarkup", type = TidyMarkupDataFormat.class), @XmlElement(required = false, name = "univocity-csv", type = UniVocityCsvDataFormat.class), @XmlElement(required = false, name = "univocity-fixed", type = UniVocityFixedWidthDataFormat.class), @@ -135,7 +137,7 @@ public class UnmarshalDefinition extends NoOutputDefinition<UnmarshalDefinition> public String toString() { return "Unmarshal[" + description() + "]"; } - + protected String description() { if (dataFormatType != null) { return dataFormatType.toString(); http://git-wip-us.apache.org/repos/asf/camel/blob/fd0eee43/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index ---------------------------------------------------------------------- diff --git a/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index b/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index index 74a63d7..a8b348c 100644 --- a/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index +++ b/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index @@ -41,6 +41,7 @@ SerializationDataFormat SoapJaxbDataFormat StringDataFormat SyslogDataFormat +TarFileDataFormat TidyMarkupDataFormat UniVocityCsvDataFormat UniVocityFixedWidthDataFormat http://git-wip-us.apache.org/repos/asf/camel/blob/fd0eee43/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/SpringTarFileDataFormatTest.java ---------------------------------------------------------------------- diff --git a/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/SpringTarFileDataFormatTest.java b/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/SpringTarFileDataFormatTest.java new file mode 100644 index 0000000..90f3e02 --- /dev/null +++ b/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/SpringTarFileDataFormatTest.java @@ -0,0 +1,168 @@ +/** + * 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.dataformat.tarfile; + +import java.io.File; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.NotifyBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.spring.CamelSpringTestSupport; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Collections.singletonList; +import static org.apache.camel.Exchange.FILE_NAME; +import static org.apache.camel.dataformat.tarfile.TarUtils.TEXT; +import static org.apache.camel.dataformat.tarfile.TarUtils.getBytes; +import static org.apache.camel.dataformat.tarfile.TarUtils.getTaredText; + +public class SpringTarFileDataFormatTest extends CamelSpringTestSupport { + private static final File TEST_DIR = new File("target/springtar"); + + @Test + public void testTarWithoutFileName() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:tar"); + mock.expectedMessageCount(1); + + template.sendBody("direct:tar", TEXT); + + assertMockEndpointsSatisfied(); + + Exchange exchange = mock.getReceivedExchanges().get(0); + assertEquals(exchange.getIn().getMessageId() + ".tar", exchange.getIn().getHeader(FILE_NAME)); + assertArrayEquals(getTaredText(exchange.getIn().getMessageId()), (byte[]) exchange.getIn().getBody()); + } + + + @Test + public void testTarWithFileName() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:tar"); + mock.expectedMessageCount(1); + mock.expectedHeaderReceived(FILE_NAME, "poem.txt.tar"); + + template.sendBodyAndHeader("direct:tar", TEXT, FILE_NAME, "poem.txt"); + + assertMockEndpointsSatisfied(); + + Exchange exchange = mock.getReceivedExchanges().get(0); + assertArrayEquals(getTaredText("poem.txt"), (byte[]) exchange.getIn().getBody()); + } + + @Test + public void testUntar() throws Exception { + getMockEndpoint("mock:untar").expectedBodiesReceived(TEXT); + getMockEndpoint("mock:untar").expectedHeaderReceived(FILE_NAME, "file"); + + template.sendBody("direct:untar", getTaredText("file")); + + assertMockEndpointsSatisfied(); + } + + @Test + public void testTarAndUntar() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:tarAndUntar"); + mock.expectedMessageCount(1); + + template.sendBody("direct:tarAndUntar", TEXT); + + assertMockEndpointsSatisfied(); + + Exchange exchange = mock.getReceivedExchanges().get(0); + assertEquals(exchange.getIn().getMessageId(), exchange.getIn().getHeader(FILE_NAME)); + assertArrayEquals(TEXT.getBytes(UTF_8), (byte[]) exchange.getIn().getBody()); + } + + @Test + public void testTarToFileWithoutFileName() throws Exception { + NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create(); + + String[] files = TEST_DIR.list(); + assertTrue(files == null || files.length == 0); + + MockEndpoint mock = getMockEndpoint("mock:intercepted"); + mock.expectedMessageCount(1); + + template.sendBody("direct:tarToFile", TEXT); + + assertMockEndpointsSatisfied(); + + // use builder to ensure the exchange is fully done before we check for file exists + assertTrue(notify.matches(5, TimeUnit.SECONDS)); + + Exchange exchange = mock.getReceivedExchanges().get(0); + File file = new File(TEST_DIR, exchange.getIn().getMessageId() + ".tar"); + assertTrue(file.exists()); + assertArrayEquals(getTaredText(exchange.getIn().getMessageId()), getBytes(file)); + } + + @Test + public void testTarToFileWithFileName() throws Exception { + NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create(); + + MockEndpoint mock = getMockEndpoint("mock:tarToFile"); + mock.expectedMessageCount(1); + + File file = new File(TEST_DIR, "poem.txt.tar"); + assertFalse(file.exists()); + + template.sendBodyAndHeader("direct:tarToFile", TEXT, FILE_NAME, "poem.txt"); + + // just make sure the file is created + mock.assertIsSatisfied(); + + // use builder to ensure the exchange is fully done before we check for file exists + assertTrue(notify.matches(5, TimeUnit.SECONDS)); + + assertTrue(file.exists()); + assertArrayEquals(getTaredText("poem.txt"), getBytes(file)); + } + + @Test + public void testDslTar() throws Exception { + getMockEndpoint("mock:dslTar").expectedBodiesReceived(singletonList(getTaredText("poem.txt"))); + getMockEndpoint("mock:dslTar").expectedHeaderReceived(FILE_NAME, "poem.txt.tar"); + + template.sendBodyAndHeader("direct:dslTar", TEXT, FILE_NAME, "poem.txt"); + + assertMockEndpointsSatisfied(); + } + + + @Test + public void testDslUntar() throws Exception { + getMockEndpoint("mock:dslUntar").expectedBodiesReceived(TEXT); + getMockEndpoint("mock:dslUntar").expectedHeaderReceived(FILE_NAME, "test.txt"); + + template.sendBody("direct:dslUntar", getTaredText("test.txt")); + + assertMockEndpointsSatisfied(); + } + + @Override + public void setUp() throws Exception { + deleteDirectory(TEST_DIR); + super.setUp(); + } + + @Override + protected ClassPathXmlApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/dataformat/tarfile/SpringTarFileDataFormatTest.xml"); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/fd0eee43/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/TarFileDataFormatTest.java ---------------------------------------------------------------------- diff --git a/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/TarFileDataFormatTest.java b/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/TarFileDataFormatTest.java index fa9bba8..1ae7a58 100644 --- a/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/TarFileDataFormatTest.java +++ b/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/TarFileDataFormatTest.java @@ -16,11 +16,7 @@ */ package org.apache.camel.dataformat.tarfile; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; import java.util.concurrent.TimeUnit; import org.apache.camel.Exchange; @@ -28,30 +24,18 @@ import org.apache.camel.builder.NotifyBuilder; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.test.junit4.CamelTestSupport; -import org.apache.camel.util.IOHelper; import org.apache.camel.util.ObjectHelper; -import org.apache.commons.compress.archivers.tar.TarArchiveEntry; -import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; import org.junit.Test; import static org.apache.camel.Exchange.FILE_NAME; +import static org.apache.camel.dataformat.tarfile.TarUtils.*; +import static org.apache.camel.dataformat.tarfile.TarUtils.getTaredText; /** * Unit tests for {@link TarFileDataFormat}. */ public class TarFileDataFormatTest extends CamelTestSupport { - private static final String TEXT = "The Masque of Queen Bersabe (excerpt) \n" - + "by: Algernon Charles Swinburne \n\n" - + "My lips kissed dumb the word of Ah \n" - + "Sighed on strange lips grown sick thereby. \n" - + "God wrought to me my royal bed; \n" - + "The inner work thereof was red, \n" - + "The outer work was ivory. \n" - + "My mouth's heat was the heat of flame \n" - + "For lust towards the kings that came \n" - + "With horsemen riding royally."; - private static final File TEST_DIR = new File("target/tar"); @Test @@ -195,31 +179,4 @@ public class TarFileDataFormatTest extends CamelTestSupport { } }; } - - private static byte[] getTaredText(String entryName) throws IOException { - ByteArrayInputStream bais = new ByteArrayInputStream(TEXT.getBytes("UTF-8")); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - TarArchiveOutputStream tos = new TarArchiveOutputStream(baos); - try { - TarArchiveEntry entry = new TarArchiveEntry(entryName); - entry.setSize(bais.available()); - tos.putArchiveEntry(entry); - IOHelper.copy(bais, tos); - } finally { - tos.closeArchiveEntry(); - IOHelper.close(bais, tos); - } - return baos.toByteArray(); - } - - private static byte[] getBytes(File file) throws IOException { - FileInputStream fis = new FileInputStream(file); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try { - IOHelper.copy(fis, baos); - } finally { - IOHelper.close(fis, baos); - } - return baos.toByteArray(); - } } http://git-wip-us.apache.org/repos/asf/camel/blob/fd0eee43/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/TarUtils.java ---------------------------------------------------------------------- diff --git a/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/TarUtils.java b/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/TarUtils.java new file mode 100644 index 0000000..aded0f5 --- /dev/null +++ b/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/TarUtils.java @@ -0,0 +1,71 @@ +/** + * 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.dataformat.tarfile; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +import org.apache.camel.util.IOHelper; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; + +final class TarUtils { + static final String TEXT = "The Masque of Queen Bersabe (excerpt) \n" + + "by: Algernon Charles Swinburne \n\n" + + "My lips kissed dumb the word of Ah \n" + + "Sighed on strange lips grown sick thereby. \n" + + "God wrought to me my royal bed; \n" + + "The inner work thereof was red, \n" + + "The outer work was ivory. \n" + + "My mouth's heat was the heat of flame \n" + + "For lust towards the kings that came \n" + + "With horsemen riding royally."; + + static byte[] getTaredText(String entryName) throws IOException { + ByteArrayInputStream bais = new ByteArrayInputStream(TEXT.getBytes("UTF-8")); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + TarArchiveOutputStream tos = new TarArchiveOutputStream(baos); + try { + TarArchiveEntry entry = new TarArchiveEntry(entryName); + entry.setSize(bais.available()); + tos.putArchiveEntry(entry); + IOHelper.copy(bais, tos); + } finally { + tos.closeArchiveEntry(); + IOHelper.close(bais, tos); + } + return baos.toByteArray(); + } + + static byte[] getBytes(File file) throws IOException { + FileInputStream fis = new FileInputStream(file); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try { + IOHelper.copy(fis, baos); + } finally { + IOHelper.close(fis, baos); + } + return baos.toByteArray(); + } + + private TarUtils() { + // Prevent instantiation + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/fd0eee43/components/camel-tarfile/src/test/resources/org/apache/camel/dataformat/tarfile/SpringTarFileDataFormatTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-tarfile/src/test/resources/org/apache/camel/dataformat/tarfile/SpringTarFileDataFormatTest.xml b/components/camel-tarfile/src/test/resources/org/apache/camel/dataformat/tarfile/SpringTarFileDataFormatTest.xml new file mode 100644 index 0000000..0908b6f --- /dev/null +++ b/components/camel-tarfile/src/test/resources/org/apache/camel/dataformat/tarfile/SpringTarFileDataFormatTest.xml @@ -0,0 +1,82 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <interceptSendToEndpoint uri="file:*"> + <to uri="mock:intercepted"/> + </interceptSendToEndpoint> + + <route> + <from uri="direct:tar"/> + <marshal> + <tarfile/> + </marshal> + <to uri="mock:tar"/> + </route> + + <route> + <from uri="direct:untar"/> + <unmarshal> + <tarfile /> + </unmarshal> + <to uri="mock:untar" /> + </route> + + <route> + <from uri="direct:tarAndUntar" /> + <marshal> + <tarfile/> + </marshal> + <unmarshal> + <tarfile/> + </unmarshal> + <to uri="mock:tarAndUntar"/> + </route> + + <route> + <from uri="direct:tarToFile"/> + <marshal> + <tarfile /> + </marshal> + <to uri="file:target/springtar"/> + <to uri="mock:tarToFile" /> + </route> + + <route> + <from uri="direct:dslTar"/> + <marshal> + <tarfile/> + </marshal> + <to uri="mock:dslTar"/> + </route> + + <route> + <from uri="direct:dslUntar"/> + <unmarshal> + <tarfile/> + </unmarshal> + <to uri="mock:dslUntar"/> + </route> + </camelContext> +</beans>