This is an automated email from the ASF dual-hosted git repository. ppalaga pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
The following commit(s) were added to refs/heads/main by this push: new d54c3abec1 Velocity $foreach.index, $foreach.count and $foreach.hasNext do not work in native mode fix #5080 d54c3abec1 is described below commit d54c3abec1d488ddd4b3e5991f084cd2d982281e Author: Peter Palaga <ppal...@redhat.com> AuthorDate: Wed Jul 12 11:45:43 2023 +0200 Velocity $foreach.index, $foreach.count and $foreach.hasNext do not work in native mode fix #5080 --- .../velocity/deployment/VelocityProcessor.java | 19 ++++++++++++------- .../camel/quarkus/component/velocity/it/Person.java | 5 +++++ .../component/velocity/it/VelocityResource.java | 21 +++++++++++++++++++++ .../velocity/src/main/resources/template/foreach.vm | 19 +++++++++++++++++++ .../quarkus/component/velocity/it/VelocityTest.java | 17 +++++++++++++++++ 5 files changed, 74 insertions(+), 7 deletions(-) diff --git a/extensions/velocity/deployment/src/main/java/org/apache/camel/quarkus/component/velocity/deployment/VelocityProcessor.java b/extensions/velocity/deployment/src/main/java/org/apache/camel/quarkus/component/velocity/deployment/VelocityProcessor.java index 816b866c3e..308b834f19 100644 --- a/extensions/velocity/deployment/src/main/java/org/apache/camel/quarkus/component/velocity/deployment/VelocityProcessor.java +++ b/extensions/velocity/deployment/src/main/java/org/apache/camel/quarkus/component/velocity/deployment/VelocityProcessor.java @@ -19,6 +19,7 @@ package org.apache.camel.quarkus.component.velocity.deployment; import java.util.ArrayList; import java.util.TreeMap; +import io.quarkus.deployment.annotations.BuildProducer; import io.quarkus.deployment.annotations.BuildStep; import io.quarkus.deployment.builditem.CombinedIndexBuildItem; import io.quarkus.deployment.builditem.FeatureBuildItem; @@ -26,6 +27,7 @@ import io.quarkus.deployment.builditem.IndexDependencyBuildItem; import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem; import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; import org.apache.camel.component.velocity.CamelVelocityClasspathResourceLoader; +import org.apache.velocity.runtime.directive.ForeachScope; import org.jboss.jandex.IndexView; import static java.util.stream.Collectors.toCollection; @@ -47,7 +49,9 @@ class VelocityProcessor { } @BuildStep - ReflectiveClassBuildItem registerForReflection(CombinedIndexBuildItem combinedIndex) { + void reflectiveClass( + BuildProducer<ReflectiveClassBuildItem> reflectiveClass, + CombinedIndexBuildItem combinedIndex) { IndexView index = combinedIndex.getIndex(); ArrayList<String> dtos = index.getKnownClasses().stream().map(ci -> ci.name().toString()) @@ -56,13 +60,14 @@ class VelocityProcessor { .collect(toCollection(ArrayList::new)); dtos.add(CamelVelocityClasspathResourceLoader.class.getName()); + reflectiveClass.produce(ReflectiveClassBuildItem.builder(dtos.toArray(new String[dtos.size()])).build()); - return ReflectiveClassBuildItem.builder(dtos.toArray(new String[dtos.size()])).build(); - } - - @BuildStep - ReflectiveClassBuildItem registerForReflectionWithMethods() { - return ReflectiveClassBuildItem.builder(TreeMap.class.getName()).methods().build(); + reflectiveClass.produce( + ReflectiveClassBuildItem.builder( + TreeMap.class.getName(), + ForeachScope.class.getName()) + .methods() + .build()); } @BuildStep diff --git a/integration-tests/velocity/src/main/java/org/apache/camel/quarkus/component/velocity/it/Person.java b/integration-tests/velocity/src/main/java/org/apache/camel/quarkus/component/velocity/it/Person.java index 9ba60b665f..a01c7e7261 100644 --- a/integration-tests/velocity/src/main/java/org/apache/camel/quarkus/component/velocity/it/Person.java +++ b/integration-tests/velocity/src/main/java/org/apache/camel/quarkus/component/velocity/it/Person.java @@ -24,6 +24,11 @@ public class Person { private String name; private String country; + public static Person fromString(String str) { + String[] fields = str.split(","); + return new Person(fields[0], fields[1]); + } + public Person() { } diff --git a/integration-tests/velocity/src/main/java/org/apache/camel/quarkus/component/velocity/it/VelocityResource.java b/integration-tests/velocity/src/main/java/org/apache/camel/quarkus/component/velocity/it/VelocityResource.java index 79f0da783d..faac091151 100644 --- a/integration-tests/velocity/src/main/java/org/apache/camel/quarkus/component/velocity/it/VelocityResource.java +++ b/integration-tests/velocity/src/main/java/org/apache/camel/quarkus/component/velocity/it/VelocityResource.java @@ -18,8 +18,10 @@ package org.apache.camel.quarkus.component.velocity.it; import java.net.URI; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import java.util.stream.Stream; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; @@ -166,6 +168,25 @@ public class VelocityResource { .build(); } + @Path("/list") + @POST + @Consumes(MediaType.TEXT_PLAIN) + @Produces(MediaType.TEXT_PLAIN) + public Response list(String payload, @QueryParam("template") String template) throws Exception { + final List<Person> persons = Stream.of(payload.split(";")) + .map(Person::fromString) + .collect(Collectors.toList()); + + final String response = producerTemplate.requestBody("velocity:" + template, + persons, + String.class); + LOG.infof("Got response from velocity: %s", response); + return Response + .created(new URI("https://camel.apache.org/")) + .entity(response) + .build(); + } + @Path("/dynamicTemplate") @POST @Consumes(MediaType.TEXT_PLAIN) diff --git a/integration-tests/velocity/src/main/resources/template/foreach.vm b/integration-tests/velocity/src/main/resources/template/foreach.vm new file mode 100644 index 0000000000..c348f03012 --- /dev/null +++ b/integration-tests/velocity/src/main/resources/template/foreach.vm @@ -0,0 +1,19 @@ +#* + * 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. + *# +#foreach( $person in $body ) +- $person, $foreach.index, $foreach.count, $foreach.hasNext +#end \ No newline at end of file diff --git a/integration-tests/velocity/src/test/java/org/apache/camel/quarkus/component/velocity/it/VelocityTest.java b/integration-tests/velocity/src/test/java/org/apache/camel/quarkus/component/velocity/it/VelocityTest.java index 3eed5bdc8b..1045de04c9 100644 --- a/integration-tests/velocity/src/test/java/org/apache/camel/quarkus/component/velocity/it/VelocityTest.java +++ b/integration-tests/velocity/src/test/java/org/apache/camel/quarkus/component/velocity/it/VelocityTest.java @@ -75,6 +75,23 @@ class VelocityTest { .body(equalTo(MSG)); } + @Test + public void forEach() { + RestAssured.given() + .queryParam("template", "//template/foreach.vm") + .contentType(ContentType.TEXT) + .body("Joe,US;Paul,UK") + .post("/velocity/list") + .then() + .statusCode(201) + .body(equalTo( + """ + + - Person{name='Joe', country='US'}, 0, 1, true + - Person{name='Paul', country='UK'}, 1, 2, false + """)); + } + @Test public void testTemplateViaClasspathWithProperties() { //class loader is forbidden by properties, response should fail