This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new e6f9ff74495 CAMEL-21371: camel-core - Add converter for Reader to
InputStream (#16051)
e6f9ff74495 is described below
commit e6f9ff74495dbdfca4368365dce92432b4ec4458
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Oct 22 13:56:31 2024 +0200
CAMEL-21371: camel-core - Add converter for Reader to InputStream (#16051)
---
.../camel/jsonpath/JsonPathChoiceReaderTest.java | 69 ++++++++++++++++++++++
.../converter/CamelBaseBulkConverterLoader.java | 9 ++-
.../org/apache/camel/converter/IOConverter.java | 53 +++++++++--------
3 files changed, 106 insertions(+), 25 deletions(-)
diff --git
a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathChoiceReaderTest.java
b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathChoiceReaderTest.java
new file mode 100644
index 00000000000..2e0a0e26c9d
--- /dev/null
+++
b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathChoiceReaderTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.jsonpath;
+
+import java.io.StringReader;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.converter.stream.ReaderCache;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+public class JsonPathChoiceReaderTest extends CamelTestSupport {
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:start")
+ .choice()
+ .when().jsonpath("$[?(@.counter>0)]")
+ .to("mock:positive")
+ .otherwise()
+ .to("mock:zero")
+ .end();
+ }
+ };
+ }
+
+ @Test
+ public void testCounterStringReader() throws Exception {
+ getMockEndpoint("mock:positive").expectedBodiesReceived("{\"counter\":
1}", "{\"counter\": 2}");
+ getMockEndpoint("mock:zero").expectedBodiesReceived("{\"counter\":
0}");
+
+ template.sendBody("direct:start", new StringReader("{\"counter\":
1}"));
+ template.sendBody("direct:start", new StringReader("{\"counter\":
0}"));
+ template.sendBody("direct:start", new StringReader("{\"counter\":
2}"));
+
+ MockEndpoint.assertIsSatisfied(context);
+ }
+
+ @Test
+ public void testCounterReaderCache() throws Exception {
+ getMockEndpoint("mock:positive").expectedBodiesReceived("{\"counter\":
4}", "{\"counter\": 3}");
+ getMockEndpoint("mock:zero").expectedBodiesReceived("{\"counter\":
0}");
+
+ template.sendBody("direct:start", new ReaderCache("{\"counter\": 4}"));
+ template.sendBody("direct:start", new ReaderCache("{\"counter\": 0}"));
+ template.sendBody("direct:start", new ReaderCache("{\"counter\": 3}"));
+
+ MockEndpoint.assertIsSatisfied(context);
+ }
+
+}
diff --git
a/core/camel-base/src/generated/java/org/apache/camel/converter/CamelBaseBulkConverterLoader.java
b/core/camel-base/src/generated/java/org/apache/camel/converter/CamelBaseBulkConverterLoader.java
index f743963ca6f..23466f74d32 100644
---
a/core/camel-base/src/generated/java/org/apache/camel/converter/CamelBaseBulkConverterLoader.java
+++
b/core/camel-base/src/generated/java/org/apache/camel/converter/CamelBaseBulkConverterLoader.java
@@ -41,7 +41,7 @@ public final class CamelBaseBulkConverterLoader implements
TypeConverterLoader,
@Override
public int size() {
- return 129;
+ return 130;
}
@Override
@@ -166,6 +166,9 @@ public final class CamelBaseBulkConverterLoader implements
TypeConverterLoader,
if (value instanceof java.io.BufferedReader) {
return
org.apache.camel.converter.IOConverter.toInputStream((java.io.BufferedReader)
value, exchange);
}
+ if (value instanceof java.io.Reader) {
+ return
org.apache.camel.converter.IOConverter.toInputStream((java.io.Reader) value,
exchange);
+ }
if (value instanceof byte[]) {
return
org.apache.camel.converter.IOConverter.toInputStream((byte[]) value);
}
@@ -559,6 +562,7 @@ public final class CamelBaseBulkConverterLoader implements
TypeConverterLoader,
registry.addConverter(new
TypeConvertible<>(java.lang.StringBuffer.class, java.io.InputStream.class),
this);
registry.addConverter(new
TypeConvertible<>(java.lang.StringBuilder.class, java.io.InputStream.class),
this);
registry.addConverter(new
TypeConvertible<>(java.io.BufferedReader.class, java.io.InputStream.class),
this);
+ registry.addConverter(new TypeConvertible<>(java.io.Reader.class,
java.io.InputStream.class), this);
registry.addConverter(new TypeConvertible<>(byte[].class,
java.io.InputStream.class), this);
registry.addConverter(new
TypeConvertible<>(java.io.ByteArrayOutputStream.class,
java.io.InputStream.class), this);
registry.addConverter(new TypeConvertible<>(java.io.InputStream.class,
java.io.ObjectInput.class), this);
@@ -760,6 +764,9 @@ public final class CamelBaseBulkConverterLoader implements
TypeConverterLoader,
if (from == java.io.BufferedReader.class) {
return this;
}
+ if (from == java.io.Reader.class) {
+ return this;
+ }
if (from == byte[].class) {
return this;
}
diff --git
a/core/camel-base/src/main/java/org/apache/camel/converter/IOConverter.java
b/core/camel-base/src/main/java/org/apache/camel/converter/IOConverter.java
index a83ef509d33..7256877bb7f 100644
--- a/core/camel-base/src/main/java/org/apache/camel/converter/IOConverter.java
+++ b/core/camel-base/src/main/java/org/apache/camel/converter/IOConverter.java
@@ -161,37 +161,42 @@ public final class IOConverter {
}
@Converter(order = 19)
+ public static InputStream toInputStream(Reader buffer, Exchange exchange)
throws IOException {
+ return toInputStream(toString(buffer), exchange);
+ }
+
+ @Converter(order = 20)
public static String toString(byte[] data, Exchange exchange) throws
IOException {
return new String(data, ExchangeHelper.getCharset(exchange));
}
- @Converter(order = 20)
+ @Converter(order = 21)
public static String toString(File file, Exchange exchange) throws
IOException {
return toString(file.toPath(), exchange);
}
- @Converter(order = 21)
+ @Converter(order = 22)
public static String toString(Path file, Exchange exchange) throws
IOException {
return Files.readString(file, ExchangeHelper.getCharset(exchange));
}
- @Converter(order = 22)
+ @Converter(order = 23)
public static byte[] toByteArray(File file) throws IOException {
return toByteArray(file.toPath());
}
- @Converter(order = 23)
+ @Converter(order = 24)
public static byte[] toByteArray(Path file) throws IOException {
return Files.readAllBytes(file);
}
- @Converter(order = 24)
+ @Converter(order = 25)
public static byte[] toByteArray(BufferedReader reader, Exchange exchange)
throws IOException {
String s = toString(reader);
return toByteArray(s, exchange);
}
- @Converter(order = 25)
+ @Converter(order = 26)
public static String toString(URL url, Exchange exchange) throws
IOException {
InputStream is = toInputStream(url);
try {
@@ -201,39 +206,39 @@ public final class IOConverter {
}
}
- @Converter(order = 26)
+ @Converter(order = 27)
public static String toString(BufferedReader reader) throws IOException {
return IOHelper.toString(reader);
}
- @Converter(order = 27)
+ @Converter(order = 28)
public static String toString(Reader reader) throws IOException {
return IOHelper.toString(reader);
}
- @Converter(order = 28)
+ @Converter(order = 29)
public static byte[] toByteArray(Reader reader, Exchange exchange) throws
IOException {
return toByteArray(IOHelper.buffered(reader), exchange);
}
- @Converter(order = 29)
+ @Converter(order = 30)
public static byte[] toByteArray(String value, Exchange exchange) throws
IOException {
return value.getBytes(ExchangeHelper.getCharset(exchange));
}
- @Converter(order = 30)
+ @Converter(order = 31)
public static String toString(InputStream in, Exchange exchange) throws
IOException {
return toString(toReader(in, exchange));
}
- @Converter(order = 31)
+ @Converter(order = 32)
public static InputStream toInputStream(byte[] data) {
// no buffering required as the complete byte input is already passed
// over as a whole
return new ByteArrayInputStream(data);
}
- @Converter(order = 32)
+ @Converter(order = 33)
public static ObjectOutput toObjectOutput(OutputStream stream) throws
IOException {
if (stream instanceof ObjectOutput out) {
return out;
@@ -242,7 +247,7 @@ public final class IOConverter {
}
}
- @Converter(order = 33)
+ @Converter(order = 34)
public static ObjectInput toObjectInput(final InputStream stream, final
Exchange exchange) throws IOException {
if (stream instanceof ObjectInput objectInput) {
return objectInput;
@@ -269,7 +274,7 @@ public final class IOConverter {
}
}
- @Converter(order = 34)
+ @Converter(order = 35)
public static byte[] toBytes(InputStream stream) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
IOHelper.copyAndCloseInput(IOHelper.buffered(stream), bos);
@@ -279,36 +284,36 @@ public final class IOConverter {
return bos.toByteArray();
}
- @Converter(order = 35)
+ @Converter(order = 36)
public static byte[] toByteArray(ByteArrayOutputStream os) {
return os.toByteArray();
}
- @Converter(order = 36)
+ @Converter(order = 37)
public static ByteBuffer covertToByteBuffer(InputStream is) throws
IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
IOHelper.copyAndCloseInput(is, os);
return ByteBuffer.wrap(os.toByteArray());
}
- @Converter(order = 37)
+ @Converter(order = 38)
public static String toString(ByteArrayOutputStream os, Exchange exchange)
throws IOException {
return os.toString(ExchangeHelper.getCharset(exchange));
}
- @Converter(order = 38)
+ @Converter(order = 39)
public static InputStream toInputStream(ByteArrayOutputStream os) {
// no buffering required as the complete byte array input is already
// passed over as a whole
return new ByteArrayInputStream(os.toByteArray());
}
- @Converter(order = 39)
+ @Converter(order = 40)
public static Properties toProperties(File file) throws IOException {
return toProperties(new FileInputStream(file));
}
- @Converter(order = 40)
+ @Converter(order = 41)
public static Properties toProperties(InputStream is) throws IOException {
Properties prop = new Properties();
try {
@@ -319,7 +324,7 @@ public final class IOConverter {
return prop;
}
- @Converter(order = 41)
+ @Converter(order = 42)
public static Properties toProperties(Reader reader) throws IOException {
Properties prop = new Properties();
try {
@@ -330,12 +335,12 @@ public final class IOConverter {
return prop;
}
- @Converter(order = 42)
+ @Converter(order = 43)
public static Path toPath(File file) {
return file.toPath();
}
- @Converter(order = 43)
+ @Converter(order = 44)
public static File toFile(Path path) {
return path.toFile();
}