CAMEL-6476: Introducing StreamCachingStrategy SPI to make it easier to configure and allow 3rd party to plugin custom strategies. Work in progress.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/046d7492 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/046d7492 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/046d7492 Branch: refs/heads/master Commit: 046d74920ae820177ff9acfb99a2b7d19b169b5a Parents: 225e569 Author: Claus Ibsen <davscl...@apache.org> Authored: Fri Jul 19 14:41:37 2013 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Sat Jul 20 08:40:48 2013 +0200 ---------------------------------------------------------------------- .../converter/stream/CachedOutputStream.java | 2 +- .../converter/stream/InputStreamCache.java | 4 ++ .../stream/MarkableInputStreamCache.java | 55 ++++++++++++++++++++ .../converter/stream/StreamCacheConverter.java | 10 ++-- .../stream/CachedOutputStreamTest.java | 4 +- .../converter/stream/InputStreamCacheTest.java | 41 --------------- .../stream/MarkableInputStreamCacheTest.java | 41 +++++++++++++++ .../stream/StreamCacheConverterTest.java | 6 ++- .../OnExceptionUseOriginalMessageTest.java | 4 +- 9 files changed, 116 insertions(+), 51 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/046d7492/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java b/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java index b25046b..c04d06e 100644 --- a/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java +++ b/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java @@ -180,7 +180,7 @@ public class CachedOutputStream extends OutputStream { if (inMemory) { if (currentStream instanceof ByteArrayOutputStream) { - return new InputStreamCache(((ByteArrayOutputStream) currentStream).toByteArray()); + return new MarkableInputStreamCache(((ByteArrayOutputStream) currentStream).toByteArray()); } else { throw new IllegalStateException("CurrentStream should be an instance of ByteArrayOutputStream but is: " + currentStream.getClass().getName()); } http://git-wip-us.apache.org/repos/asf/camel/blob/046d7492/camel-core/src/main/java/org/apache/camel/converter/stream/InputStreamCache.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/converter/stream/InputStreamCache.java b/camel-core/src/main/java/org/apache/camel/converter/stream/InputStreamCache.java index a6ef5c5..4e596e0 100644 --- a/camel-core/src/main/java/org/apache/camel/converter/stream/InputStreamCache.java +++ b/camel-core/src/main/java/org/apache/camel/converter/stream/InputStreamCache.java @@ -22,6 +22,10 @@ import java.io.OutputStream; import org.apache.camel.StreamCache; +/** + * @deprecated use {@link MarkableInputStreamCache} instead. + */ +@Deprecated public class InputStreamCache extends ByteArrayInputStream implements StreamCache { public InputStreamCache(byte[] data) { http://git-wip-us.apache.org/repos/asf/camel/blob/046d7492/camel-core/src/main/java/org/apache/camel/converter/stream/MarkableInputStreamCache.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/converter/stream/MarkableInputStreamCache.java b/camel-core/src/main/java/org/apache/camel/converter/stream/MarkableInputStreamCache.java new file mode 100644 index 0000000..048786b --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/converter/stream/MarkableInputStreamCache.java @@ -0,0 +1,55 @@ +/** + * 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.converter.stream; + +import java.io.ByteArrayInputStream; +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.camel.StreamCache; +import org.apache.camel.util.IOHelper; + +/** + * A {@link StreamCache} for {@link InputStream} that supports mark. + */ +public final class MarkableInputStreamCache extends FilterInputStream implements StreamCache { + + public MarkableInputStreamCache(byte[] data) { + this(new ByteArrayInputStream(data)); + mark(data.length); + } + + public MarkableInputStreamCache(InputStream in) { + super(in); + } + + @Override + public void reset() { + try { + in.reset(); + } catch (IOException e) { + // ignore + } + } + + @Override + public void writeTo(OutputStream os) throws IOException { + IOHelper.copyAndCloseInput(in, os); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/046d7492/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java b/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java index 9809295..0282565 100644 --- a/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java +++ b/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java @@ -70,9 +70,13 @@ public final class StreamCacheConverter { @Converter public static StreamCache convertToStreamCache(InputStream stream, Exchange exchange) throws IOException { - CachedOutputStream cos = new CachedOutputStream(exchange); - IOHelper.copyAndCloseInput(stream, cos); - return cos.getStreamCache(); + if (stream.markSupported()) { + return new MarkableInputStreamCache(stream); + } else { + CachedOutputStream cos = new CachedOutputStream(exchange); + IOHelper.copyAndCloseInput(stream, cos); + return cos.getStreamCache(); + } } @Converter http://git-wip-us.apache.org/repos/asf/camel/blob/046d7492/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java b/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java index a705e1a..0698100 100644 --- a/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java +++ b/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java @@ -196,7 +196,7 @@ public class CachedOutputStreamTest extends ContextTestSupport { assertEquals("we should have no temp file", files.length, 0); StreamCache cache = cos.getStreamCache(); - assertTrue("Should get the InputStreamCache", cache instanceof InputStreamCache); + assertTrue("Should get the InputStreamCache", cache instanceof MarkableInputStreamCache); String temp = IOConverter.toString((InputStream)cache, null); assertEquals("Cached a wrong file", temp, TEST_STRING); @@ -217,7 +217,7 @@ public class CachedOutputStreamTest extends ContextTestSupport { assertEquals("we should have no temp file", files.length, 0); StreamCache cache = cos.getStreamCache(); - assertTrue("Should get the InputStreamCache", cache instanceof InputStreamCache); + assertTrue("Should get the InputStreamCache", cache instanceof MarkableInputStreamCache); String temp = IOConverter.toString((InputStream)cache, null); assertEquals("Cached a wrong file", temp, TEST_STRING); http://git-wip-us.apache.org/repos/asf/camel/blob/046d7492/camel-core/src/test/java/org/apache/camel/converter/stream/InputStreamCacheTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/converter/stream/InputStreamCacheTest.java b/camel-core/src/test/java/org/apache/camel/converter/stream/InputStreamCacheTest.java deleted file mode 100644 index 5f02265..0000000 --- a/camel-core/src/test/java/org/apache/camel/converter/stream/InputStreamCacheTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/** - * 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.converter.stream; - -import java.io.ByteArrayOutputStream; - -import org.apache.camel.ContextTestSupport; -import org.apache.camel.util.IOHelper; - -/** - * @version - */ -public class InputStreamCacheTest extends ContextTestSupport { - - public void testInputStreamCache() throws Exception { - InputStreamCache cache = new InputStreamCache("<foo>bar</foo>".getBytes()); - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - cache.writeTo(bos); - - String s = context.getTypeConverter().convertTo(String.class, bos); - assertEquals("<foo>bar</foo>", s); - - IOHelper.close(cache, bos); - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/046d7492/camel-core/src/test/java/org/apache/camel/converter/stream/MarkableInputStreamCacheTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/converter/stream/MarkableInputStreamCacheTest.java b/camel-core/src/test/java/org/apache/camel/converter/stream/MarkableInputStreamCacheTest.java new file mode 100644 index 0000000..b8e778a --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/converter/stream/MarkableInputStreamCacheTest.java @@ -0,0 +1,41 @@ +/** + * 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.converter.stream; + +import java.io.ByteArrayOutputStream; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.util.IOHelper; + +/** + * @version + */ +public class MarkableInputStreamCacheTest extends ContextTestSupport { + + public void testInputStreamCache() throws Exception { + MarkableInputStreamCache cache = new MarkableInputStreamCache("<foo>bar</foo>".getBytes()); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + cache.writeTo(bos); + + String s = context.getTypeConverter().convertTo(String.class, bos); + assertEquals("<foo>bar</foo>", s); + + IOHelper.close(cache, bos); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/046d7492/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java b/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java index b792d09..c399dda 100644 --- a/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java +++ b/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java @@ -75,8 +75,10 @@ public class StreamCacheConverterTest extends ContextTestSupport { InputStream is = getTestFileStream(); InputStream cache = (InputStream)StreamCacheConverter.convertToStreamCache(is, exchange); //assert re-readability of the cached InputStream - assertNotNull(IOConverter.toString(cache, null)); - assertNotNull(IOConverter.toString(cache, null)); + String data = IOConverter.toString(cache, null); + cache.reset(); + String data2 = IOConverter.toString(cache, null); + assertEquals(data, data2); } public void testConvertToStreamCacheInputStreamWithFileCache() throws Exception { http://git-wip-us.apache.org/repos/asf/camel/blob/046d7492/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionUseOriginalMessageTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionUseOriginalMessageTest.java b/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionUseOriginalMessageTest.java index 010fe64..25eb746 100644 --- a/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionUseOriginalMessageTest.java +++ b/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionUseOriginalMessageTest.java @@ -21,7 +21,7 @@ import org.apache.camel.Exchange; import org.apache.camel.ExchangePattern; import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.converter.stream.InputStreamCache; +import org.apache.camel.converter.stream.MarkableInputStreamCache; /** * @version @@ -50,7 +50,7 @@ public class OnExceptionUseOriginalMessageTest extends ContextTestSupport { getMockEndpoint("mock:end").expectedMessageCount(1); getMockEndpoint("mock:end").message(0).property(Exchange.EXCEPTION_CAUGHT).isInstanceOf(IllegalArgumentException.class); - InputStreamCache cache = new InputStreamCache(TEST_STRING.getBytes()); + MarkableInputStreamCache cache = new MarkableInputStreamCache(TEST_STRING.getBytes()); template.sendBody("direct:a", cache);