001/*
002 * Copyright 2012-2017 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.servlet.view;
018
019import java.io.IOException;
020import java.io.InputStreamReader;
021import java.io.Reader;
022import java.util.Locale;
023import java.util.Map;
024
025import javax.servlet.http.HttpServletRequest;
026import javax.servlet.http.HttpServletResponse;
027
028import com.samskivert.mustache.Mustache.Compiler;
029import com.samskivert.mustache.Template;
030
031import org.springframework.core.io.Resource;
032import org.springframework.web.servlet.View;
033import org.springframework.web.servlet.view.AbstractTemplateView;
034
035/**
036 * Spring MVC {@link View} using the Mustache template engine.
037 *
038 * @author Brian Clozel
039 * @author Dave Syer
040 * @author Phillip Webb
041 * @since 2.0.0
042 */
043public class MustacheView extends AbstractTemplateView {
044
045        private Compiler compiler;
046
047        private String charset;
048
049        /**
050         * Set the Mustache compiler to be used by this view.
051         * <p>
052         * Typically this property is not set directly. Instead a single {@link Compiler} is
053         * expected in the Spring application context which is used to compile Mustache
054         * templates.
055         * @param compiler the Mustache compiler
056         */
057        public void setCompiler(Compiler compiler) {
058                this.compiler = compiler;
059        }
060
061        /**
062         * Set the charset used for reading Mustache template files.
063         * @param charset the charset to use for reading template files
064         */
065        public void setCharset(String charset) {
066                this.charset = charset;
067        }
068
069        @Override
070        public boolean checkResource(Locale locale) throws Exception {
071                Resource resource = getApplicationContext().getResource(this.getUrl());
072                return (resource != null && resource.exists());
073        }
074
075        @Override
076        protected void renderMergedTemplateModel(Map<String, Object> model,
077                        HttpServletRequest request, HttpServletResponse response) throws Exception {
078                Template template = createTemplate(
079                                getApplicationContext().getResource(this.getUrl()));
080                if (template != null) {
081                        template.execute(model, response.getWriter());
082                }
083        }
084
085        private Template createTemplate(Resource resource) throws IOException {
086                try (Reader reader = getReader(resource)) {
087                        return this.compiler.compile(reader);
088                }
089        }
090
091        private Reader getReader(Resource resource) throws IOException {
092                if (this.charset != null) {
093                        return new InputStreamReader(resource.getInputStream(), this.charset);
094                }
095                return new InputStreamReader(resource.getInputStream());
096        }
097
098}