Skip to content

Commit

Permalink
Velocity $foreach.index, $foreach.count and $foreach.hasNext do not w…
Browse files Browse the repository at this point in the history
…ork in native mode fix #5080
  • Loading branch information
ppalaga committed Jul 12, 2023
1 parent 9faa615 commit d54c3ab
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
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;
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;
Expand All @@ -47,7 +49,9 @@ NativeImageResourceBuildItem initResources() {
}

@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())
Expand All @@ -56,13 +60,14 @@ ReflectiveClassBuildItem registerForReflection(CombinedIndexBuildItem combinedIn
.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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

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;
Expand Down Expand Up @@ -166,6 +168,25 @@ public Response templateViaHeader(String message, @QueryParam("body") String bod
.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)
Expand Down
19 changes: 19 additions & 0 deletions integration-tests/velocity/src/main/resources/template/foreach.vm
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ public void testTemplateViaClasspath() {
.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
Expand Down

0 comments on commit d54c3ab

Please sign in to comment.