Skip to content

Commit

Permalink
Add a named producer for the current HttpServerRequest
Browse files Browse the repository at this point in the history
- i.e. make it accesible from qute templates
- resolves #17353
  • Loading branch information
mkouba committed May 26, 2021
1 parent e6d7287 commit d71cff0
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/src/main/asciidoc/qute-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,8 @@ For the expression `inject:foo.price` the implementation class of the injected b

NOTE: A `ValueResolver` is also generated for all beans annotated with `@Named` so that it's possible to access its properties without reflection.

TIP: If your application serves link:http-reference[HTTP requests] you can also inject the current `io.vertx.core.http.HttpServerRequest` via the `inject` namespace, e.g. `{inject:request.getParam('foo')}`.

[[typesafe_expressions]]
=== Type-safe Expressions

Expand Down
2 changes: 1 addition & 1 deletion extensions/qute/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-http-deployment</artifactId>
<artifactId>quarkus-vertx-web-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.quarkus.qute.deployment.currentrequest;

import static org.junit.jupiter.api.Assertions.fail;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.qute.TemplateException;
import io.quarkus.test.QuarkusUnitTest;

public class CurrentRequestDisabledTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addAsResource(new StringAsset(
"quarkus.arc.exclude-types=io.quarkus.vertx.http.runtime.CurrentRequestProducer"),
"application.properties")
.addAsResource(new StringAsset(
"Hello {inject:request.getParam('name')}!"),
"templates/request.txt"))
.setExpectedException(TemplateException.class);

@Test
public void testCurrentRequest() {
fail();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.quarkus.qute.deployment.currentrequest;

import static io.restassured.RestAssured.when;

import javax.inject.Inject;

import org.hamcrest.Matchers;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.qute.Template;
import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.vertx.web.Route;

public class CurrentRequestTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClass(MyRoute.class)
.addAsResource(new StringAsset(
"Hello {inject:request.getParam('name')}!"),
"templates/request.txt"));

@Test
public void testCurrentRequest() {
when().get("/current-request?name=Joe").then().body(Matchers.is("Hello Joe!"));
}

public static class MyRoute {

@Inject
Template request;

@Route
String currentRequest() {
return request.render();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import io.quarkus.vertx.core.deployment.EventLoopCountBuildItem;
import io.quarkus.vertx.http.deployment.devmode.HttpRemoteDevClientProvider;
import io.quarkus.vertx.http.deployment.devmode.NotFoundPageDisplayableEndpointBuildItem;
import io.quarkus.vertx.http.runtime.CurrentRequestProducer;
import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
import io.quarkus.vertx.http.runtime.HttpBuildTimeConfig;
import io.quarkus.vertx.http.runtime.HttpConfiguration;
Expand Down Expand Up @@ -97,6 +98,7 @@ AdditionalBeanBuildItem additionalBeans() {
.setUnremovable()
.addBeanClass(RouterProducer.class)
.addBeanClass(CurrentVertxRequest.class)
.addBeanClass(CurrentRequestProducer.class)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.quarkus.vertx.http.runtime;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Named;
import javax.inject.Singleton;

import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.web.RoutingContext;

/**
* Note that we intentionally put the producer in a separate class so that it's possible to exclude the bean if a naming
* conflict exists.
*/
@Singleton
public class CurrentRequestProducer {

@Named("request")
@Produces
@RequestScoped
public HttpServerRequest getCurrentRequest(RoutingContext rc) {
return rc.request();
}

}

0 comments on commit d71cff0

Please sign in to comment.