Skip to content

Commit

Permalink
Fix NPE if Qute template can't be found
Browse files Browse the repository at this point in the history
Fixes #2410
  • Loading branch information
jamesnetherton committed Apr 1, 2021
1 parent 928ab99 commit f0b5e21
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.quarkus.qute.EngineBuilder;
import io.quarkus.qute.ReflectionValueResolver;
import io.quarkus.qute.Template;
import io.quarkus.qute.TemplateException;
import io.quarkus.qute.TemplateInstance;
import io.quarkus.qute.TemplateLocator.TemplateLocation;
import io.quarkus.qute.Variant;
Expand Down Expand Up @@ -187,9 +188,15 @@ protected void onExchange(Exchange exchange) throws Exception {
Engine engine = getQuteEngine();
if (content == null) {
template = engine.getTemplate(path);
if (template == null) {
throw new TemplateException("Unable to parse Qute template from path: " + path);
}
} else {
// This is the first time to parse the content
template = engine.parse(content);
if (template == null) {
throw new TemplateException("Unable to parse Qute template");
}
}
instance = template.instance();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,47 @@
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.camel.CamelExecutionException;
import org.apache.camel.ProducerTemplate;
import org.jboss.logging.Logger;
import org.apache.camel.component.qute.QuteConstants;

@Path("/qute")
@ApplicationScoped
public class QuteResource {

private static final Logger LOG = Logger.getLogger(QuteResource.class);

@Inject
ProducerTemplate producerTemplate;

@Path("/get/{name}")
@Path("/template")
@POST
@Produces(MediaType.TEXT_PLAIN)
public String getTemplateFromHeader(String content) throws Exception {
return producerTemplate.requestBodyAndHeader("qute:hello?allowTemplateFromHeader=true", "World",
QuteConstants.QUTE_TEMPLATE, content, String.class);
}

@Path("/template/{name}")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String get(@PathParam("name") String name) throws Exception {
public String getTemplateFromPath(@PathParam("name") String name) throws Exception {
return producerTemplate.requestBody("direct:test", name, String.class);
}

@Path("/template/invalid/path")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getInvalidTemplatePath() throws Exception {
try {
producerTemplate.requestBody("qute:invalid-path", "test", String.class);
return null;
} catch (CamelExecutionException e) {
return e.getCause().getMessage();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,25 @@
class QuteTest {

@Test
public void test() {
RestAssured.when().get("/qute/get/World").then().body(is("Hello World"));
public void testTemplate() {
RestAssured.get("/qute/template/World")
.then()
.body(is("Hello World"));
}

@Test
public void testInvalidTemplatePath() {
RestAssured.get("/qute/template/invalid/path")
.then()
.body(is("Unable to parse Qute template from path: invalid-path"));
}

@Test
public void tesTemplateContentFromHeader() {
RestAssured.given()
.body("Hello {body}")
.post("/qute/template")
.then()
.body(is("Hello World"));
}
}

0 comments on commit f0b5e21

Please sign in to comment.