blob: 655b8d27a7d175bef3ac1f8a4e0fe67c5cb9230a [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.collect.ImmutableList;
23import com.google.common.collect.ImmutableMap;
Dave Borowitz9de65952012-08-13 16:09:45 -070024import com.google.common.collect.Maps;
Shawn Pearcea9b99a12015-02-10 15:35:11 -080025import com.google.common.hash.Funnels;
26import com.google.common.hash.HashCode;
27import com.google.common.hash.Hasher;
28import com.google.common.hash.Hashing;
29import com.google.common.io.ByteStreams;
Shawn Pearcec4d3fd72015-02-10 14:32:37 -080030import com.google.common.net.HttpHeaders;
Dave Borowitz9de65952012-08-13 16:09:45 -070031import com.google.template.soy.tofu.SoyTofu;
Dave Borowitz9de65952012-08-13 16:09:45 -070032import java.io.File;
33import java.io.IOException;
Shawn Pearcea9b99a12015-02-10 15:35:11 -080034import java.io.InputStream;
Dave Borowitzfc2f00a2014-07-29 17:34:43 -070035import java.io.OutputStream;
Dave Borowitz9de65952012-08-13 16:09:45 -070036import java.net.MalformedURLException;
37import java.net.URL;
Dave Borowitz9de65952012-08-13 16:09:45 -070038import java.util.Map;
Kurt Alfred Klueverc1f6cfc2018-04-30 20:16:43 -070039import java.util.concurrent.ConcurrentHashMap;
Shawn Pearcea9b99a12015-02-10 15:35:11 -080040import java.util.concurrent.ConcurrentMap;
David Pursehouseb40361f2017-05-30 10:41:53 +090041import java.util.function.Function;
Shawn Pearce6451fa52017-06-29 20:47:05 -070042import java.util.zip.GZIPOutputStream;
Shawn Pearcec4d3fd72015-02-10 14:32:37 -080043import javax.servlet.http.HttpServletRequest;
Dave Borowitz9de65952012-08-13 16:09:45 -070044import javax.servlet.http.HttpServletResponse;
45
Dave Borowitzfc775ad2014-07-30 11:38:53 -070046/**
47 * Renderer for Soy templates used by Gitiles.
Dave Borowitz40255d52016-08-19 16:16:22 -040048 *
49 * <p>Most callers should not use the methods in this class directly, and instead use one of the
50 * HTML methods in {@link BaseServlet}.
Dave Borowitzfc775ad2014-07-30 11:38:53 -070051 */
Dave Borowitz9de65952012-08-13 16:09:45 -070052public abstract class Renderer {
Dave Borowitzfc2f00a2014-07-29 17:34:43 -070053 // Must match .streamingPlaceholder.
54 private static final String PLACEHOLDER = "id=\"STREAMED_OUTPUT_BLOCK\"";
55
Dave Borowitz3b685ab2017-03-09 09:24:54 -050056 private static final ImmutableList<String> SOY_FILENAMES =
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020057 ImmutableList.of(
58 "BlameDetail.soy",
59 "Common.soy",
60 "DiffDetail.soy",
61 "Doc.soy",
62 "HostIndex.soy",
63 "LogDetail.soy",
64 "ObjectDetail.soy",
65 "PathDetail.soy",
66 "RefList.soy",
67 "RevisionDetail.soy",
68 "RepositoryIndex.soy");
Dave Borowitz9de65952012-08-13 16:09:45 -070069
Dave Borowitz3b685ab2017-03-09 09:24:54 -050070 public static final ImmutableMap<String, String> STATIC_URL_GLOBALS =
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020071 ImmutableMap.of(
72 "gitiles.BASE_CSS_URL", "base.css",
73 "gitiles.DOC_CSS_URL", "doc.css",
74 "gitiles.PRETTIFY_CSS_URL", "prettify/prettify.css");
Dave Borowitz9de65952012-08-13 16:09:45 -070075
Dave Borowitzde07eac2016-10-04 09:44:25 -040076 protected static Function<String, URL> fileUrlMapper() {
77 return fileUrlMapper("");
78 }
Dave Borowitz76bbefd2014-03-11 16:57:45 -070079
Dave Borowitzde07eac2016-10-04 09:44:25 -040080 protected static Function<String, URL> fileUrlMapper(String prefix) {
81 checkNotNull(prefix);
82 return filename -> {
Dave Borowitz76bbefd2014-03-11 16:57:45 -070083 if (filename == null) {
84 return null;
85 }
86 try {
87 return new File(prefix + filename).toURI().toURL();
88 } catch (MalformedURLException e) {
89 throw new IllegalArgumentException(e);
90 }
Dave Borowitzde07eac2016-10-04 09:44:25 -040091 };
Dave Borowitz9de65952012-08-13 16:09:45 -070092 }
93
Shawn Pearcea9b99a12015-02-10 15:35:11 -080094 protected ImmutableMap<String, URL> templates;
Dave Borowitz9de65952012-08-13 16:09:45 -070095 protected ImmutableMap<String, String> globals;
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020096 private final ConcurrentMap<String, HashCode> hashes =
Kurt Alfred Klueverc1f6cfc2018-04-30 20:16:43 -070097 new ConcurrentHashMap<>(SOY_FILENAMES.size());
Dave Borowitz9de65952012-08-13 16:09:45 -070098
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020099 protected Renderer(
100 Function<String, URL> resourceMapper,
101 Map<String, String> globals,
102 String staticPrefix,
103 Iterable<URL> customTemplates,
104 String siteTitle) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700105 checkNotNull(staticPrefix, "staticPrefix");
Shawn Pearcea9b99a12015-02-10 15:35:11 -0800106
107 ImmutableMap.Builder<String, URL> b = ImmutableMap.builder();
108 for (String name : SOY_FILENAMES) {
109 b.put(name, resourceMapper.apply(name));
110 }
111 for (URL u : customTemplates) {
112 b.put(u.toString(), u);
113 }
114 templates = b.build();
Dave Borowitz9de65952012-08-13 16:09:45 -0700115
116 Map<String, String> allGlobals = Maps.newHashMap();
117 for (Map.Entry<String, String> e : STATIC_URL_GLOBALS.entrySet()) {
118 allGlobals.put(e.getKey(), staticPrefix + e.getValue());
119 }
Chad Horohoe2a28d622012-11-12 11:56:59 -0800120 allGlobals.put("gitiles.SITE_TITLE", siteTitle);
Dave Borowitz9de65952012-08-13 16:09:45 -0700121 allGlobals.putAll(globals);
122 this.globals = ImmutableMap.copyOf(allGlobals);
123 }
124
Shawn Pearcea9b99a12015-02-10 15:35:11 -0800125 public HashCode getTemplateHash(String soyFile) {
126 HashCode h = hashes.get(soyFile);
127 if (h == null) {
128 h = computeTemplateHash(soyFile);
129 hashes.put(soyFile, h);
130 }
131 return h;
132 }
133
134 HashCode computeTemplateHash(String soyFile) {
135 URL u = templates.get(soyFile);
136 checkState(u != null, "Missing Soy template %s", soyFile);
137
David Pursehoused5914492017-05-31 13:08:22 +0900138 Hasher h = Hashing.murmur3_128().newHasher();
Shawn Pearcea9b99a12015-02-10 15:35:11 -0800139 try (InputStream is = u.openStream();
140 OutputStream os = Funnels.asOutputStream(h)) {
141 ByteStreams.copy(is, os);
142 } catch (IOException e) {
143 throw new IllegalStateException("Missing Soy template " + soyFile, e);
144 }
145 return h.hash();
146 }
147
Shawn Pearce374f1842015-02-10 15:36:54 -0800148 public String render(String templateName, Map<String, ?> soyData) {
149 return newRenderer(templateName).setData(soyData).render();
150 }
151
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200152 void render(
153 HttpServletRequest req, HttpServletResponse res, String templateName, Map<String, ?> soyData)
154 throws IOException {
Dave Borowitz9de65952012-08-13 16:09:45 -0700155 res.setContentType("text/html");
156 res.setCharacterEncoding("UTF-8");
David Pletcherd7bdaf32014-08-27 14:50:32 -0700157 byte[] data = newRenderer(templateName).setData(soyData).render().getBytes(UTF_8);
Shawn Pearcec4d3fd72015-02-10 14:32:37 -0800158 if (BaseServlet.acceptsGzipEncoding(req)) {
Shawn Pearceed3c2d12016-05-30 15:59:02 -0700159 res.addHeader(HttpHeaders.VARY, HttpHeaders.ACCEPT_ENCODING);
Shawn Pearcec4d3fd72015-02-10 14:32:37 -0800160 res.setHeader(HttpHeaders.CONTENT_ENCODING, "gzip");
161 data = BaseServlet.gzip(data);
162 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700163 res.setContentLength(data.length);
164 res.getOutputStream().write(data);
165 }
166
Dave Borowitzfc775ad2014-07-30 11:38:53 -0700167 OutputStream renderStreaming(HttpServletResponse res, String templateName, Map<String, ?> soyData)
168 throws IOException {
Shawn Pearce6451fa52017-06-29 20:47:05 -0700169 return renderStreaming(res, false, templateName, soyData);
170 }
171
172 OutputStream renderStreaming(
173 HttpServletResponse res, boolean gzip, String templateName, Map<String, ?> soyData)
174 throws IOException {
Shawn Pearce4b49d8d2017-06-29 20:02:22 -0700175 String html = newRenderer(templateName).setData(soyData).render();
Dave Borowitzfc2f00a2014-07-29 17:34:43 -0700176 int id = html.indexOf(PLACEHOLDER);
177 checkArgument(id >= 0, "Template must contain %s", PLACEHOLDER);
178
179 int lt = html.lastIndexOf('<', id);
Shawn Pearce4b49d8d2017-06-29 20:02:22 -0700180 int gt = html.indexOf('>', id + PLACEHOLDER.length());
181
Shawn Pearce6451fa52017-06-29 20:47:05 -0700182 OutputStream out = gzip ? new GZIPOutputStream(res.getOutputStream()) : res.getOutputStream();
David Pletcherd7bdaf32014-08-27 14:50:32 -0700183 out.write(html.substring(0, lt).getBytes(UTF_8));
Dave Borowitzfc2f00a2014-07-29 17:34:43 -0700184 out.flush();
185
Shawn Pearce4b49d8d2017-06-29 20:02:22 -0700186 byte[] tail = html.substring(gt + 1).getBytes(UTF_8);
Dave Borowitzfc2f00a2014-07-29 17:34:43 -0700187 return new OutputStream() {
188 @Override
Dave Borowitzfc2f00a2014-07-29 17:34:43 -0700189 public void write(byte[] b, int off, int len) throws IOException {
190 out.write(b, off, len);
191 }
192
193 @Override
194 public void write(int b) throws IOException {
195 out.write(b);
196 }
197
198 @Override
199 public void flush() throws IOException {
200 out.flush();
201 }
202
203 @Override
204 public void close() throws IOException {
Shawn Pearce4b49d8d2017-06-29 20:02:22 -0700205 try (OutputStream o = out) {
206 o.write(tail);
207 }
Dave Borowitzfc2f00a2014-07-29 17:34:43 -0700208 }
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}