blob: dd08353784f06ceff69bfdea917c8249d64e2186 [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;
Jakub Vrana21e70b72019-05-20 16:33:54 +020029import com.google.common.html.types.LegacyConversions;
Shawn Pearcea9b99a12015-02-10 15:35:11 -080030import com.google.common.io.ByteStreams;
Shawn Pearcec4d3fd72015-02-10 14:32:37 -080031import com.google.common.net.HttpHeaders;
David Pursehouse28726042019-06-27 09:09:30 +090032import com.google.template.soy.jbcsrc.api.SoySauce;
Dave Borowitz9de65952012-08-13 16:09:45 -070033import java.io.File;
34import java.io.IOException;
Shawn Pearcea9b99a12015-02-10 15:35:11 -080035import java.io.InputStream;
Dave Borowitzfc2f00a2014-07-29 17:34:43 -070036import java.io.OutputStream;
Dave Borowitz9de65952012-08-13 16:09:45 -070037import java.net.MalformedURLException;
38import java.net.URL;
Dave Borowitz9de65952012-08-13 16:09:45 -070039import java.util.Map;
Kamil Musin34d50082022-12-09 15:48:47 +010040import java.util.Optional;
Kurt Alfred Klueverc1f6cfc2018-04-30 20:16:43 -070041import java.util.concurrent.ConcurrentHashMap;
Shawn Pearcea9b99a12015-02-10 15:35:11 -080042import java.util.concurrent.ConcurrentMap;
David Pursehouseb40361f2017-05-30 10:41:53 +090043import java.util.function.Function;
Shawn Pearce6451fa52017-06-29 20:47:05 -070044import java.util.zip.GZIPOutputStream;
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.
Dave Borowitz40255d52016-08-19 16:16:22 -040050 *
51 * <p>Most callers should not use the methods in this class directly, and instead use one of the
52 * HTML methods in {@link BaseServlet}.
Dave Borowitzfc775ad2014-07-30 11:38:53 -070053 */
Dave Borowitz9de65952012-08-13 16:09:45 -070054public abstract class Renderer {
Dave Borowitzfc2f00a2014-07-29 17:34:43 -070055 // Must match .streamingPlaceholder.
Sven Selberg958f5bf2022-01-31 10:33:24 +010056 private static final String PLACEHOLDER = "id=\"STREAMED-OUTPUT-BLOCK\"";
Dave Borowitzfc2f00a2014-07-29 17:34:43 -070057
Dave Borowitz3b685ab2017-03-09 09:24:54 -050058 private static final ImmutableList<String> SOY_FILENAMES =
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020059 ImmutableList.of(
60 "BlameDetail.soy",
61 "Common.soy",
62 "DiffDetail.soy",
63 "Doc.soy",
Alon Bar-Levcf9e71d2019-01-23 15:23:19 +020064 "Error.soy",
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020065 "HostIndex.soy",
66 "LogDetail.soy",
67 "ObjectDetail.soy",
68 "PathDetail.soy",
69 "RefList.soy",
70 "RevisionDetail.soy",
71 "RepositoryIndex.soy");
Dave Borowitz9de65952012-08-13 16:09:45 -070072
Dave Borowitz3b685ab2017-03-09 09:24:54 -050073 public static final ImmutableMap<String, String> STATIC_URL_GLOBALS =
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020074 ImmutableMap.of(
75 "gitiles.BASE_CSS_URL", "base.css",
76 "gitiles.DOC_CSS_URL", "doc.css",
77 "gitiles.PRETTIFY_CSS_URL", "prettify/prettify.css");
Dave Borowitz9de65952012-08-13 16:09:45 -070078
Dave Borowitzde07eac2016-10-04 09:44:25 -040079 protected static Function<String, URL> fileUrlMapper() {
80 return fileUrlMapper("");
81 }
Dave Borowitz76bbefd2014-03-11 16:57:45 -070082
Dave Borowitzde07eac2016-10-04 09:44:25 -040083 protected static Function<String, URL> fileUrlMapper(String prefix) {
84 checkNotNull(prefix);
85 return filename -> {
Dave Borowitz76bbefd2014-03-11 16:57:45 -070086 if (filename == null) {
87 return null;
88 }
89 try {
90 return new File(prefix + filename).toURI().toURL();
91 } catch (MalformedURLException e) {
92 throw new IllegalArgumentException(e);
93 }
Dave Borowitzde07eac2016-10-04 09:44:25 -040094 };
Dave Borowitz9de65952012-08-13 16:09:45 -070095 }
96
Shawn Pearcea9b99a12015-02-10 15:35:11 -080097 protected ImmutableMap<String, URL> templates;
Dave Borowitz9de65952012-08-13 16:09:45 -070098 protected ImmutableMap<String, String> globals;
Jesse Costello-Goodf65ff3b2021-05-17 13:18:09 -070099 protected final String siteTitle;
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200100 private final ConcurrentMap<String, HashCode> hashes =
Kurt Alfred Klueverc1f6cfc2018-04-30 20:16:43 -0700101 new ConcurrentHashMap<>(SOY_FILENAMES.size());
Dave Borowitz9de65952012-08-13 16:09:45 -0700102
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200103 protected Renderer(
104 Function<String, URL> resourceMapper,
105 Map<String, String> globals,
106 String staticPrefix,
107 Iterable<URL> customTemplates,
108 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 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700124 allGlobals.putAll(globals);
125 this.globals = ImmutableMap.copyOf(allGlobals);
Jesse Costello-Goodf65ff3b2021-05-17 13:18:09 -0700126 this.siteTitle = siteTitle;
Dave Borowitz9de65952012-08-13 16:09:45 -0700127 }
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
David Pursehoused5914492017-05-31 13:08:22 +0900142 Hasher h = Hashing.murmur3_128().newHasher();
Shawn Pearcea9b99a12015-02-10 15:35:11 -0800143 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
David Pursehouse28726042019-06-27 09:09:30 +0900152 void renderHtml(
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200153 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 Pursehouse28726042019-06-27 09:09:30 +0900157 byte[] data =
Kamil Musin34d50082022-12-09 15:48:47 +0100158 newRenderer(templateName, Optional.of(req))
159 .setData(soyData)
160 .renderHtml()
161 .get()
162 .toString()
163 .getBytes(UTF_8);
Shawn Pearcec4d3fd72015-02-10 14:32:37 -0800164 if (BaseServlet.acceptsGzipEncoding(req)) {
Shawn Pearceed3c2d12016-05-30 15:59:02 -0700165 res.addHeader(HttpHeaders.VARY, HttpHeaders.ACCEPT_ENCODING);
Shawn Pearcec4d3fd72015-02-10 14:32:37 -0800166 res.setHeader(HttpHeaders.CONTENT_ENCODING, "gzip");
167 data = BaseServlet.gzip(data);
168 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700169 res.setContentLength(data.length);
170 res.getOutputStream().write(data);
171 }
172
David Pursehouse28726042019-06-27 09:09:30 +0900173 OutputStream renderHtmlStreaming(
Kamil Musin34d50082022-12-09 15:48:47 +0100174 HttpServletRequest req, HttpServletResponse res, String templateName, Map<String, ?> soyData)
175 throws IOException {
176 return renderHtmlStreaming(req, res, false, templateName, soyData);
Shawn Pearce6451fa52017-06-29 20:47:05 -0700177 }
178
David Pursehouse28726042019-06-27 09:09:30 +0900179 OutputStream renderHtmlStreaming(
Kamil Musin34d50082022-12-09 15:48:47 +0100180 HttpServletRequest req,
181 HttpServletResponse res,
182 boolean gzip,
183 String templateName,
184 Map<String, ?> soyData)
Shawn Pearce6451fa52017-06-29 20:47:05 -0700185 throws IOException {
Kamil Musin34d50082022-12-09 15:48:47 +0100186 String html =
187 newRenderer(templateName, Optional.of(req)).setData(soyData).renderHtml().get().toString();
Dave Borowitzfc2f00a2014-07-29 17:34:43 -0700188 int id = html.indexOf(PLACEHOLDER);
189 checkArgument(id >= 0, "Template must contain %s", PLACEHOLDER);
190
191 int lt = html.lastIndexOf('<', id);
Shawn Pearce4b49d8d2017-06-29 20:02:22 -0700192 int gt = html.indexOf('>', id + PLACEHOLDER.length());
193
Shawn Pearce6451fa52017-06-29 20:47:05 -0700194 OutputStream out = gzip ? new GZIPOutputStream(res.getOutputStream()) : res.getOutputStream();
David Pletcherd7bdaf32014-08-27 14:50:32 -0700195 out.write(html.substring(0, lt).getBytes(UTF_8));
Dave Borowitzfc2f00a2014-07-29 17:34:43 -0700196 out.flush();
197
Shawn Pearce4b49d8d2017-06-29 20:02:22 -0700198 byte[] tail = html.substring(gt + 1).getBytes(UTF_8);
Dave Borowitzfc2f00a2014-07-29 17:34:43 -0700199 return new OutputStream() {
200 @Override
Dave Borowitzfc2f00a2014-07-29 17:34:43 -0700201 public void write(byte[] b, int off, int len) throws IOException {
202 out.write(b, off, len);
203 }
204
205 @Override
206 public void write(int b) throws IOException {
207 out.write(b);
208 }
209
210 @Override
211 public void flush() throws IOException {
212 out.flush();
213 }
214
215 @Override
216 public void close() throws IOException {
Shawn Pearce4b49d8d2017-06-29 20:02:22 -0700217 try (OutputStream o = out) {
218 o.write(tail);
219 }
Dave Borowitzfc2f00a2014-07-29 17:34:43 -0700220 }
221 };
222 }
223
David Pursehouse28726042019-06-27 09:09:30 +0900224 SoySauce.Renderer newRenderer(String templateName) {
Kamil Musin34d50082022-12-09 15:48:47 +0100225 return newRenderer(templateName, Optional.empty());
226 }
227
228 SoySauce.Renderer newRenderer(String templateName, Optional<HttpServletRequest> req) {
Jakub Vrana21e70b72019-05-20 16:33:54 +0200229 ImmutableMap.Builder<String, Object> staticUrls = ImmutableMap.builder();
230 for (String key : STATIC_URL_GLOBALS.keySet()) {
231 staticUrls.put(
232 key.replaceFirst("^gitiles\\.", ""),
233 LegacyConversions.riskilyAssumeTrustedResourceUrl(globals.get(key)));
234 }
Kamil Musin34d50082022-12-09 15:48:47 +0100235 ImmutableMap.Builder<String, Object> ij =
236 ImmutableMap.<String, Object>builder()
237 .put("staticUrls", staticUrls.build())
238 .put("SITE_TITLE", siteTitle);
239 Optional<String> nonce = req.map((r) -> (String) r.getAttribute("nonce"));
Matthias Sohn30ea2672023-09-30 22:18:32 +0200240 if (nonce.isPresent()) {
Jonathan Nieder97076682022-12-12 15:54:33 -0800241 ij.put("csp_nonce", nonce.get());
Kamil Musin34d50082022-12-09 15:48:47 +0100242 }
243 return getSauce().renderTemplate(templateName).setIj(ij.build());
Dave Borowitz9de65952012-08-13 16:09:45 -0700244 }
245
David Pursehouse28726042019-06-27 09:09:30 +0900246 protected abstract SoySauce getSauce();
Jesse Costello-Good6f62c5d2021-02-02 12:05:06 -0800247
248 /**
249 * Give a resource URL of a soy template file, returns the import path for use in a Soy import
250 * statement.
251 */
252 protected String toSoySrcPath(URL templateUrl) {
253 String filePath = templateUrl.getPath();
254 String fileName = filePath.substring(filePath.lastIndexOf('/') + 1);
255 return "com/google/gitiles/templates/" + fileName;
256 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700257}