Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NPE if Qute template can't be found #2434

Merged
merged 1 commit into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"));
}
}