001/* 002 * Copyright 2012-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.web.reactive.result.view; 018 019import java.io.IOException; 020import java.io.InputStreamReader; 021import java.io.OutputStreamWriter; 022import java.io.Reader; 023import java.io.Writer; 024import java.nio.charset.Charset; 025import java.util.Locale; 026import java.util.Map; 027import java.util.Optional; 028 029import com.samskivert.mustache.Mustache.Compiler; 030import com.samskivert.mustache.Template; 031import reactor.core.publisher.Flux; 032import reactor.core.publisher.Mono; 033 034import org.springframework.core.io.Resource; 035import org.springframework.core.io.buffer.DataBuffer; 036import org.springframework.core.io.buffer.DataBufferUtils; 037import org.springframework.http.MediaType; 038import org.springframework.web.reactive.result.view.AbstractUrlBasedView; 039import org.springframework.web.reactive.result.view.View; 040import org.springframework.web.server.ServerWebExchange; 041 042/** 043 * Spring WebFlux {@link View} using the Mustache template engine. 044 * 045 * @author Brian Clozel 046 * @since 2.0.0 047 */ 048public class MustacheView extends AbstractUrlBasedView { 049 050 private Compiler compiler; 051 052 private String charset; 053 054 /** 055 * Set the JMustache compiler to be used by this view. Typically this property is not 056 * set directly. Instead a single {@link Compiler} is expected in the Spring 057 * application context which is used to compile Mustache templates. 058 * @param compiler the Mustache compiler 059 */ 060 public void setCompiler(Compiler compiler) { 061 this.compiler = compiler; 062 } 063 064 /** 065 * Set the charset used for reading Mustache template files. 066 * @param charset the charset to use for reading template files 067 */ 068 public void setCharset(String charset) { 069 this.charset = charset; 070 } 071 072 @Override 073 public boolean checkResourceExists(Locale locale) throws Exception { 074 return resolveResource() != null; 075 } 076 077 @Override 078 protected Mono<Void> renderInternal(Map<String, Object> model, MediaType contentType, 079 ServerWebExchange exchange) { 080 Resource resource = resolveResource(); 081 if (resource == null) { 082 return Mono.error(new IllegalStateException( 083 "Could not find Mustache template with URL [" + getUrl() + "]")); 084 } 085 DataBuffer dataBuffer = exchange.getResponse().bufferFactory().allocateBuffer(); 086 try (Reader reader = getReader(resource)) { 087 Template template = this.compiler.compile(reader); 088 Charset charset = getCharset(contentType).orElse(getDefaultCharset()); 089 try (Writer writer = new OutputStreamWriter(dataBuffer.asOutputStream(), 090 charset)) { 091 template.execute(model, writer); 092 writer.flush(); 093 } 094 } 095 catch (Exception ex) { 096 DataBufferUtils.release(dataBuffer); 097 return Mono.error(ex); 098 } 099 return exchange.getResponse().writeWith(Flux.just(dataBuffer)); 100 } 101 102 private Resource resolveResource() { 103 Resource resource = getApplicationContext().getResource(getUrl()); 104 if (resource == null || !resource.exists()) { 105 return null; 106 } 107 return resource; 108 } 109 110 private Reader getReader(Resource resource) throws IOException { 111 if (this.charset != null) { 112 return new InputStreamReader(resource.getInputStream(), this.charset); 113 } 114 return new InputStreamReader(resource.getInputStream()); 115 } 116 117 private Optional<Charset> getCharset(MediaType mediaType) { 118 return Optional.ofNullable((mediaType != null) ? mediaType.getCharset() : null); 119 } 120 121}