From 7dc9120440adab0c193c00b379bd450863a742e6 Mon Sep 17 00:00:00 2001 From: Eric Bussieres Date: Sun, 17 May 2020 20:52:41 -0400 Subject: [PATCH] Add request and response object to reactive view --- docs/src/orchid/resources/changelog/v3_1_4.md | 3 ++- .../mitchellbosecke/pebble/boot/Controllers.java | 5 +++++ .../pebble/boot/autoconfigure/ReactiveAppTest.java | 13 ++++++++++++- .../pebble/boot/autoconfigure/ServletAppTest.java | 7 +++++++ .../src/test/resources/templates/contextPath.pebble | 2 +- .../test/resources/templates/responseObject.pebble | 1 + .../pebble/spring/reactive/PebbleReactiveView.java | 8 ++++++-- 7 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 pebble-spring/pebble-spring-boot-starter/src/test/resources/templates/responseObject.pebble diff --git a/docs/src/orchid/resources/changelog/v3_1_4.md b/docs/src/orchid/resources/changelog/v3_1_4.md index 4b6ba977a..96a1ca245 100644 --- a/docs/src/orchid/resources/changelog/v3_1_4.md +++ b/docs/src/orchid/resources/changelog/v3_1_4.md @@ -5,4 +5,5 @@ version: '3.1.4' - Slice filter: Use collection size when toIndex is greater than collection size (#504) - Adjust spring boot doc (#509) - Build with jdk14 (#508) -- Set proxyBeanMethods to false (#507) \ No newline at end of file +- Set proxyBeanMethods to false (#507) +- Add access to Spring Beans/request and response when using Pebble with WebFlux (#512) \ No newline at end of file diff --git a/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/Controllers.java b/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/Controllers.java index 7dda78d98..795b843de 100644 --- a/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/Controllers.java +++ b/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/Controllers.java @@ -31,4 +31,9 @@ public String beans() { return "beans"; } + @RequestMapping("/response.action") + public String response() { + return "responseObject"; + } + } \ No newline at end of file diff --git a/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/ReactiveAppTest.java b/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/ReactiveAppTest.java index c58fd63da..60d6a8b26 100644 --- a/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/ReactiveAppTest.java +++ b/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/ReactiveAppTest.java @@ -36,7 +36,7 @@ void testRequestAccess() throws Exception { .expectBody(String.class) .returnResult().getResponseBody(); - assertThat(result).isEqualTo("ctx path:"); + assertThat(result).isEqualTo("ctx path:/contextPath.action"); } @Test @@ -85,5 +85,16 @@ void testBeansAccess() throws Exception { assertThat(result).isEqualTo("beans:bar"); } + + @Test + void testResponseAccess() throws Exception { + String result = this.client.get().uri("/response.action").exchange() + .expectStatus().isOk() + .expectHeader().contentTypeCompatibleWith(MediaType.TEXT_HTML) + .expectBody(String.class) + .returnResult().getResponseBody(); + + assertThat(result).isEqualTo("response:200 OK"); + } } diff --git a/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/ServletAppTest.java b/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/ServletAppTest.java index 67ebf0be3..b4bdeb587 100644 --- a/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/ServletAppTest.java +++ b/pebble-spring/pebble-spring-boot-starter/src/test/java/com/mitchellbosecke/pebble/boot/autoconfigure/ServletAppTest.java @@ -78,4 +78,11 @@ void testBeansAccess() throws Exception { .andExpect(content().string("beans:bar")); } + @Test + void testResponseAccess() throws Exception { + this.mockMvc.perform(get("/response.action")) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML)) + .andExpect(content().string("response:200")); + } } diff --git a/pebble-spring/pebble-spring-boot-starter/src/test/resources/templates/contextPath.pebble b/pebble-spring/pebble-spring-boot-starter/src/test/resources/templates/contextPath.pebble index 6b2af7875..bf5fc36c5 100644 --- a/pebble-spring/pebble-spring-boot-starter/src/test/resources/templates/contextPath.pebble +++ b/pebble-spring/pebble-spring-boot-starter/src/test/resources/templates/contextPath.pebble @@ -1 +1 @@ -ctx path:{{request.contextPath}} \ No newline at end of file +ctx path:{{request.contextPath}}{{request.path}} \ No newline at end of file diff --git a/pebble-spring/pebble-spring-boot-starter/src/test/resources/templates/responseObject.pebble b/pebble-spring/pebble-spring-boot-starter/src/test/resources/templates/responseObject.pebble new file mode 100644 index 000000000..1b11aa7d5 --- /dev/null +++ b/pebble-spring/pebble-spring-boot-starter/src/test/resources/templates/responseObject.pebble @@ -0,0 +1 @@ +response:{{response.status}}{{response.statusCode}} \ No newline at end of file diff --git a/pebble-spring/pebble-spring5/src/main/java/com/mitchellbosecke/pebble/spring/reactive/PebbleReactiveView.java b/pebble-spring/pebble-spring5/src/main/java/com/mitchellbosecke/pebble/spring/reactive/PebbleReactiveView.java index 9a4edc469..ff33f7fce 100644 --- a/pebble-spring/pebble-spring5/src/main/java/com/mitchellbosecke/pebble/spring/reactive/PebbleReactiveView.java +++ b/pebble-spring/pebble-spring5/src/main/java/com/mitchellbosecke/pebble/spring/reactive/PebbleReactiveView.java @@ -26,6 +26,8 @@ public class PebbleReactiveView extends AbstractUrlBasedView { private static final String BEANS_VARIABLE_NAME = "beans"; + private static final String REQUEST_VARIABLE_NAME = "request"; + private static final String RESPONSE_VARIABLE_NAME = "response"; private PebbleEngine pebbleEngine; private String templateName; @@ -48,7 +50,7 @@ protected Mono renderInternal(Map renderAttributes, try { Charset charset = this.getCharset(contentType); Writer writer = new OutputStreamWriter(dataBuffer.asOutputStream(), charset); - this.addVariablesToModel(renderAttributes); + this.addVariablesToModel(renderAttributes, exchange); this.evaluateTemplate(renderAttributes, locale, writer); } catch (Exception ex) { DataBufferUtils.release(dataBuffer); @@ -57,8 +59,10 @@ protected Mono renderInternal(Map renderAttributes, return exchange.getResponse().writeWith(Flux.just(dataBuffer)); } - private void addVariablesToModel(Map model) { + private void addVariablesToModel(Map model, ServerWebExchange exchange) { model.put(BEANS_VARIABLE_NAME, new Beans(this.getApplicationContext())); + model.put(REQUEST_VARIABLE_NAME, exchange.getRequest()); + model.put(RESPONSE_VARIABLE_NAME, exchange.getResponse()); } private Charset getCharset(@Nullable MediaType mediaType) {