Skip to content

Commit

Permalink
#512: Add beans access for reactive app
Browse files Browse the repository at this point in the history
  • Loading branch information
ebussieres committed May 17, 2020
1 parent ba51c35 commit f73afd4
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ class PebbleServletWebConfiguration extends AbstractPebbleConfiguration {
@ConditionalOnMissingBean(name = "pebbleViewResolver")
PebbleViewResolver pebbleViewResolver(PebbleProperties properties,
PebbleEngine pebbleEngine) {
PebbleViewResolver pvr = new PebbleViewResolver();
PebbleViewResolver pvr = new PebbleViewResolver(pebbleEngine);
properties.applyToMvcViewResolver(pvr);

pvr.setPebbleEngine(pebbleEngine);
if (pebbleEngine.getLoader() instanceof ClasspathLoader) {
// classpathloader doesn't like leading slashes in paths
pvr.setPrefix(this.stripLeadingSlash(properties.getPrefix()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public String extensions() {
return "extensions";
}

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.mitchellbosecke.pebble.boot;

import org.springframework.stereotype.Component;

@Component
public class Foo {

public String value = "bar";
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,16 @@ void testAdditionalExtensions() throws Exception {

assertThat(result).isEqualTo("Hola Boot! Tested!");
}

@Test
void testBeansAccess() throws Exception {
String result = this.client.get().uri("/beans.action").exchange()
.expectStatus().isOk()
.expectHeader().contentTypeCompatibleWith(MediaType.TEXT_HTML)
.expectBody(String.class)
.returnResult().getResponseBody();

assertThat(result).isEqualTo("beans:bar");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,12 @@ void testAdditionalExtensions() throws Exception {
.andExpect(content().string("Hola Boot! Tested!"));
}

@Test
void testBeansAccess() throws Exception {
this.mockMvc.perform(get("/beans.action"))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML))
.andExpect(content().string("beans:bar"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
beans:{{beans.foo.value}}
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package com.mitchellbosecke.pebble.spring.reactive;

import static java.util.Optional.ofNullable;

import com.mitchellbosecke.pebble.PebbleEngine;
import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.spring.context.Beans;
import com.mitchellbosecke.pebble.template.PebbleTemplate;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Locale;
import java.util.Map;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
Expand All @@ -12,20 +20,13 @@
import org.springframework.util.MimeType;
import org.springframework.web.reactive.result.view.AbstractUrlBasedView;
import org.springframework.web.server.ServerWebExchange;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Locale;
import java.util.Map;

import static java.util.Optional.ofNullable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public class PebbleReactiveView extends AbstractUrlBasedView {

private static final String BEANS_VARIABLE_NAME = "beans";

private PebbleEngine pebbleEngine;
private String templateName;

Expand All @@ -47,6 +48,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.evaluateTemplate(renderAttributes, locale, writer);
} catch (Exception ex) {
DataBufferUtils.release(dataBuffer);
Expand All @@ -55,6 +57,10 @@ protected Mono<Void> renderInternal(Map<String, Object> renderAttributes,
return exchange.getResponse().writeWith(Flux.just(dataBuffer));
}

private void addVariablesToModel(Map<String, Object> model) {
model.put(BEANS_VARIABLE_NAME, new Beans(this.getApplicationContext()));
}

private Charset getCharset(@Nullable MediaType mediaType) {
return ofNullable(mediaType)
.map(MimeType::getCharset)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mitchellbosecke.pebble.spring.reactive;

import com.mitchellbosecke.pebble.PebbleEngine;

import org.springframework.web.reactive.result.view.AbstractUrlBasedView;
import org.springframework.web.reactive.result.view.UrlBasedViewResolver;

Expand All @@ -27,8 +26,4 @@ protected AbstractUrlBasedView createView(String viewName) {
protected Class<?> requiredViewClass() {
return PebbleReactiveView.class;
}

public PebbleEngine getPebbleEngine() {
return this.pebbleEngine;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@

import com.mitchellbosecke.pebble.PebbleEngine;
import com.mitchellbosecke.pebble.loader.Loader;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
import org.springframework.web.servlet.view.AbstractUrlBasedView;

public class PebbleViewResolver extends AbstractTemplateViewResolver implements InitializingBean {

private String characterEncoding = "UTF-8";
private PebbleEngine pebbleEngine;
private final PebbleEngine pebbleEngine;

public PebbleViewResolver() {
public PebbleViewResolver(PebbleEngine pebbleEngine) {
this.pebbleEngine = pebbleEngine;
this.setViewClass(this.requiredViewClass());
}

Expand All @@ -34,11 +33,6 @@ public void setCharacterEncoding(String characterEncoding) {
this.characterEncoding = characterEncoding;
}

@Required
public void setPebbleEngine(PebbleEngine pebbleEngine) {
this.pebbleEngine = pebbleEngine;
}

@Override
protected AbstractUrlBasedView buildView(String viewName) throws Exception {
PebbleView view = (PebbleView) super.buildView(viewName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ public Loader<?> templateLoader() {

@Bean
public ViewResolver viewResolver(PebbleEngine pebbleEngine) {
PebbleViewResolver viewResolver = new PebbleViewResolver();
PebbleViewResolver viewResolver = new PebbleViewResolver(pebbleEngine);
viewResolver.setPrefix("com/mitchellbosecke/pebble/spring/template/");
viewResolver.setSuffix(".html");
viewResolver.setPebbleEngine(pebbleEngine);
viewResolver.setContentType("text/html");
return viewResolver;
}
Expand Down

0 comments on commit f73afd4

Please sign in to comment.