Skip to content

Commit

Permalink
Add session object to reactive view
Browse files Browse the repository at this point in the history
  • Loading branch information
ebussieres committed May 18, 2020
1 parent f73afd4 commit 876606d
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/src/orchid/resources/changelog/v3_1_4.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- Set proxyBeanMethods to false (#507)
- Add access to Spring Beans/request/session and response when using Pebble with WebFlux (#512)
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public String beans() {
return "beans";
}

@RequestMapping("/response.action")
public String response() {
return "responseObject";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ctx path:{{request.contextPath}}
ctx path:{{request.contextPath}}{{request.path}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
response:{{response.status}}{{response.statusCode}}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
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 static final String SESSION_VARIABLE_NAME = "session";

private PebbleEngine pebbleEngine;
private String templateName;
Expand All @@ -48,7 +51,7 @@ protected Mono<Void> renderInternal(Map<String, Object> 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);
Expand All @@ -57,8 +60,11 @@ protected Mono<Void> renderInternal(Map<String, Object> renderAttributes,
return exchange.getResponse().writeWith(Flux.just(dataBuffer));
}

private void addVariablesToModel(Map<String, Object> model) {
private void addVariablesToModel(Map<String, Object> 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());
model.put(SESSION_VARIABLE_NAME, exchange.getSession());
}

private Charset getCharset(@Nullable MediaType mediaType) {
Expand Down

0 comments on commit 876606d

Please sign in to comment.