This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-3.11.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.11.x by this push: new ec2f00f fix(dataformat): conversion to ByteBuffer (#6016) ec2f00f is described below commit ec2f00f17b6feee23833ad6487f25e8aa359632d Author: Pasquale Congiusti <pasquale.congiu...@gmail.com> AuthorDate: Fri Sep 3 07:09:26 2021 +0200 fix(dataformat): conversion to ByteBuffer (#6016) * fix(platform-http): use mandatory convert with Exchange object Ref CAMEL-16906 * fix(jackson): conversion to ByteBuffer Ref CAMEL-16906 --- .../jackson/converter/JacksonTypeConverters.java | 4 ++ .../converter/JacksonConversionsBufferTest.java | 64 ++++++++++++++++++++++ .../http/vertx/VertxPlatformHttpSupport.java | 2 +- 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java index cd0649d..0751d2c 100644 --- a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java +++ b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java @@ -19,6 +19,7 @@ package org.apache.camel.component.jackson.converter; import java.io.File; import java.io.InputStream; import java.io.Reader; +import java.nio.ByteBuffer; import java.util.Map; import java.util.Set; @@ -107,6 +108,9 @@ public final class JacksonTypeConverters { } else if (byte[].class.isAssignableFrom(type)) { byte[] out = mapper.writeValueAsBytes(value); return type.cast(out); + } else if (ByteBuffer.class.isAssignableFrom(type)) { + byte[] out = mapper.writeValueAsBytes(value); + return type.cast(ByteBuffer.wrap(out)); } else if (mapper.canSerialize(type) && !Enum.class.isAssignableFrom(type)) { // if the source value type is readable by the mapper then use // its read operation diff --git a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/converter/JacksonConversionsBufferTest.java b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/converter/JacksonConversionsBufferTest.java new file mode 100644 index 0000000..e91f69c --- /dev/null +++ b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/converter/JacksonConversionsBufferTest.java @@ -0,0 +1,64 @@ +/* + * 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.component.jackson.converter; + +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.jackson.JacksonConstants; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class JacksonConversionsBufferTest extends CamelTestSupport { + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext context = super.createCamelContext(); + // enable jackson type converter by setting this property on + // CamelContext + context.getGlobalOptions().put(JacksonConstants.ENABLE_TYPE_CONVERTER, "true"); + return context; + } + + @Test + public void shouldConvertMapToByteBuffer() { + String name = "someName"; + Map<String, String> pojoAsMap = new HashMap<>(); + pojoAsMap.put("name", name); + + ByteBuffer testByteBuffer = (ByteBuffer) template.requestBody("direct:test", pojoAsMap); + + assertEquals("{\"name\":\"someName\"}", StandardCharsets.UTF_8.decode(testByteBuffer).toString()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:test").convertBodyTo(ByteBuffer.class); + } + }; + } + +} diff --git a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java index 97cd2a1..fab52af 100644 --- a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java +++ b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java @@ -180,7 +180,7 @@ public final class VertxPlatformHttpSupport { response.end((Buffer) body); } else { final TypeConverter tc = camelExchange.getContext().getTypeConverter(); - final ByteBuffer bb = tc.mandatoryConvertTo(ByteBuffer.class, body); + final ByteBuffer bb = tc.mandatoryConvertTo(ByteBuffer.class, camelExchange, body); final Buffer b = Buffer.buffer(bb.capacity()); b.setBytes(0, bb);