blob: 9defb71365161e54ded13df6200dbf26f667b8e6 [file] [log] [blame]
Dave Borowitz9de65952012-08-13 16:09:45 -07001// Copyright 2012 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.gitiles;
16
Dave Borowitzfc2f00a2014-07-29 17:34:43 -070017import static com.google.common.base.Preconditions.checkArgument;
Dave Borowitz9de65952012-08-13 16:09:45 -070018import static com.google.common.base.Preconditions.checkNotNull;
Shawn Pearcea9b99a12015-02-10 15:35:11 -080019import static com.google.common.base.Preconditions.checkState;
David Pletcherd7bdaf32014-08-27 14:50:32 -070020import static java.nio.charset.StandardCharsets.UTF_8;
Dave Borowitz9de65952012-08-13 16:09:45 -070021
Dave Borowitz9de65952012-08-13 16:09:45 -070022import com.google.common.base.Function;
23import com.google.common.collect.ImmutableList;
24import com.google.common.collect.ImmutableMap;
Shawn Pearcea9b99a12015-02-10 15:35:11 -080025import com.google.common.collect.MapMaker;
Dave Borowitz9de65952012-08-13 16:09:45 -070026import com.google.common.collect.Maps;
Shawn Pearcea9b99a12015-02-10 15:35:11 -080027import com.google.common.hash.Funnels;
28import com.google.common.hash.HashCode;
29import com.google.common.hash.Hasher;
30import com.google.common.hash.Hashing;
31import com.google.common.io.ByteStreams;
Shawn Pearcec4d3fd72015-02-10 14:32:37 -080032import com.google.common.net.HttpHeaders;
Dave Borowitz9de65952012-08-13 16:09:45 -070033import com.google.template.soy.tofu.SoyTofu;
34
35import java.io.File;
36import java.io.IOException;
Shawn Pearcea9b99a12015-02-10 15:35:11 -080037import java.io.InputStream;
Dave Borowitzfc2f00a2014-07-29 17:34:43 -070038import java.io.OutputStream;
Dave Borowitz9de65952012-08-13 16:09:45 -070039import java.net.MalformedURLException;
40import java.net.URL;
41import java.util.List;
42import java.util.Map;
Shawn Pearcea9b99a12015-02-10 15:35:11 -080043import java.util.concurrent.ConcurrentMap;
Dave Borowitz9de65952012-08-13 16:09:45 -070044
Shawn Pearcec4d3fd72015-02-10 14:32:37 -080045import javax.servlet.http.HttpServletRequest;
Dave Borowitz9de65952012-08-13 16:09:45 -070046import javax.servlet.http.HttpServletResponse;
47
Dave Borowitzfc775ad2014-07-30 11:38:53 -070048/**
49 * Renderer for Soy templates used by Gitiles.
50 * <p>
51 * Most callers should not use the methods in this class directly, and instead
52 * use one of the HTML methods in {@link BaseServlet}.
53 */
Dave Borowitz9de65952012-08-13 16:09:45 -070054public abstract class Renderer {
Dave Borowitzfc2f00a2014-07-29 17:34:43 -070055 // Must match .streamingPlaceholder.
56 private static final String PLACEHOLDER = "id=\"STREAMED_OUTPUT_BLOCK\"";
57
Dave Borowitz9de65952012-08-13 16:09:45 -070058 private static final List<String> SOY_FILENAMES = ImmutableList.of(
Dave Borowitz6ec0c872014-01-29 13:59:37 -080059 "BlameDetail.soy",
Dave Borowitz9de65952012-08-13 16:09:45 -070060 "Common.soy",
61 "DiffDetail.soy",
Shawn Pearce374f1842015-02-10 15:36:54 -080062 "Doc.soy",
Dave Borowitz9de65952012-08-13 16:09:45 -070063 "HostIndex.soy",
64 "LogDetail.soy",
65 "ObjectDetail.soy",
66 "PathDetail.soy",
Dave Borowitz209d0aa2012-12-28 14:28:53 -080067 "RefList.soy",
Dave Borowitz9de65952012-08-13 16:09:45 -070068 "RevisionDetail.soy",
69 "RepositoryIndex.soy");
70
71 public static final Map<String, String> STATIC_URL_GLOBALS = ImmutableMap.of(
Andrew Bonventreb33426e2015-09-09 18:28:28 -040072 "gitiles.BASE_CSS_URL", "base.css",
Shawn Pearce374f1842015-02-10 15:36:54 -080073 "gitiles.DOC_CSS_URL", "doc.css",
Dave Borowitzddc0efc2014-04-27 19:12:47 -060074 "gitiles.PRETTIFY_CSS_URL", "prettify/prettify.css");
Dave Borowitz9de65952012-08-13 16:09:45 -070075
Dave Borowitz76bbefd2014-03-11 16:57:45 -070076 protected static class FileUrlMapper implements Function<String, URL> {
77 private final String prefix;
78
79 protected FileUrlMapper() {
80 this("");
Dave Borowitz9de65952012-08-13 16:09:45 -070081 }
Dave Borowitz76bbefd2014-03-11 16:57:45 -070082
83 protected FileUrlMapper(String prefix) {
84 this.prefix = checkNotNull(prefix, "prefix");
85 }
86
87 @Override
88 public URL apply(String filename) {
89 if (filename == null) {
90 return null;
91 }
92 try {
93 return new File(prefix + filename).toURI().toURL();
94 } catch (MalformedURLException e) {
95 throw new IllegalArgumentException(e);
96 }
Dave Borowitz9de65952012-08-13 16:09:45 -070097 }
98 }
99
Shawn Pearcea9b99a12015-02-10 15:35:11 -0800100 protected ImmutableMap<String, URL> templates;
Dave Borowitz9de65952012-08-13 16:09:45 -0700101 protected ImmutableMap<String, String> globals;
Shawn Pearcea9b99a12015-02-10 15:35:11 -0800102 private final ConcurrentMap<String, HashCode> hashes = new MapMaker()
103 .initialCapacity(SOY_FILENAMES.size())
104 .concurrencyLevel(1)
105 .makeMap();
Dave Borowitz9de65952012-08-13 16:09:45 -0700106
107 protected Renderer(Function<String, URL> resourceMapper, Map<String, String> globals,
Dave Borowitz76bbefd2014-03-11 16:57:45 -0700108 String staticPrefix, Iterable<URL> customTemplates, String siteTitle) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700109 checkNotNull(staticPrefix, "staticPrefix");
Shawn Pearcea9b99a12015-02-10 15:35:11 -0800110
111 ImmutableMap.Builder<String, URL> b = ImmutableMap.builder();
112 for (String name : SOY_FILENAMES) {
113 b.put(name, resourceMapper.apply(name));
114 }
115 for (URL u : customTemplates) {
116 b.put(u.toString(), u);
117 }
118 templates = b.build();
Dave Borowitz9de65952012-08-13 16:09:45 -0700119
120 Map<String, String> allGlobals = Maps.newHashMap();
121 for (Map.Entry<String, String> e : STATIC_URL_GLOBALS.entrySet()) {
122 allGlobals.put(e.getKey(), staticPrefix + e.getValue());
123 }
Chad Horohoe2a28d622012-11-12 11:56:59 -0800124 allGlobals.put("gitiles.SITE_TITLE", siteTitle);
Dave Borowitz9de65952012-08-13 16:09:45 -0700125 allGlobals.putAll(globals);
126 this.globals = ImmutableMap.copyOf(allGlobals);
127 }
128
Shawn Pearcea9b99a12015-02-10 15:35:11 -0800129 public HashCode getTemplateHash(String soyFile) {
130 HashCode h = hashes.get(soyFile);
131 if (h == null) {
132 h = computeTemplateHash(soyFile);
133 hashes.put(soyFile, h);
134 }
135 return h;
136 }
137
138 HashCode computeTemplateHash(String soyFile) {
139 URL u = templates.get(soyFile);
140 checkState(u != null, "Missing Soy template %s", soyFile);
141
142 Hasher h = Hashing.sha1().newHasher();
143 try (InputStream is = u.openStream();
144 OutputStream os = Funnels.asOutputStream(h)) {
145 ByteStreams.copy(is, os);
146 } catch (IOException e) {
147 throw new IllegalStateException("Missing Soy template " + soyFile, e);
148 }
149 return h.hash();
150 }
151
Shawn Pearce374f1842015-02-10 15:36:54 -0800152 public String render(String templateName, Map<String, ?> soyData) {
153 return newRenderer(templateName).setData(soyData).render();
154 }
155
Shawn Pearcec4d3fd72015-02-10 14:32:37 -0800156 void render(HttpServletRequest req, HttpServletResponse res,
157 String templateName, Map<String, ?> soyData) throws IOException {
Dave Borowitz9de65952012-08-13 16:09:45 -0700158 res.setContentType("text/html");
159 res.setCharacterEncoding("UTF-8");
David Pletcherd7bdaf32014-08-27 14:50:32 -0700160 byte[] data = newRenderer(templateName).setData(soyData).render().getBytes(UTF_8);
Shawn Pearcec4d3fd72015-02-10 14:32:37 -0800161 if (BaseServlet.acceptsGzipEncoding(req)) {
162 res.setHeader(HttpHeaders.CONTENT_ENCODING, "gzip");
163 data = BaseServlet.gzip(data);
164 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700165 res.setContentLength(data.length);
166 res.getOutputStream().write(data);
167 }
168
Dave Borowitzfc775ad2014-07-30 11:38:53 -0700169 OutputStream renderStreaming(HttpServletResponse res, String templateName, Map<String, ?> soyData)
170 throws IOException {
Dave Borowitzfc2f00a2014-07-29 17:34:43 -0700171 final String html = newRenderer(templateName)
172 .setData(soyData)
173 .render();
174 int id = html.indexOf(PLACEHOLDER);
175 checkArgument(id >= 0, "Template must contain %s", PLACEHOLDER);
176
177 int lt = html.lastIndexOf('<', id);
178 final int gt = html.indexOf('>', id + PLACEHOLDER.length());
179 final OutputStream out = res.getOutputStream();
David Pletcherd7bdaf32014-08-27 14:50:32 -0700180 out.write(html.substring(0, lt).getBytes(UTF_8));
Dave Borowitzfc2f00a2014-07-29 17:34:43 -0700181 out.flush();
182
183 return new OutputStream() {
184 @Override
185 public void write(byte[] b) throws IOException {
186 out.write(b);
187 }
188
189 @Override
190 public void write(byte[] b, int off, int len) throws IOException {
191 out.write(b, off, len);
192 }
193
194 @Override
195 public void write(int b) throws IOException {
196 out.write(b);
197 }
198
199 @Override
200 public void flush() throws IOException {
201 out.flush();
202 }
203
204 @Override
205 public void close() throws IOException {
David Pletcherd7bdaf32014-08-27 14:50:32 -0700206 out.write(html.substring(gt + 1).getBytes(UTF_8));
Dave Borowitzfc2f00a2014-07-29 17:34:43 -0700207 out.close();
208 }
209 };
210 }
211
Dave Borowitz9de65952012-08-13 16:09:45 -0700212 SoyTofu.Renderer newRenderer(String templateName) {
213 return getTofu().newRenderer(templateName);
214 }
215
216 protected abstract SoyTofu getTofu();
217}