| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 17 | import static com.google.common.base.Preconditions.checkArgument; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkState; |
| David Pletcher | d7bdaf3 | 2014-08-27 14:50:32 -0700 | [diff] [blame] | 20 | import static java.nio.charset.StandardCharsets.UTF_8; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 21 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
| 23 | import com.google.common.collect.ImmutableMap; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 24 | import com.google.common.collect.Maps; |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 25 | import com.google.common.hash.Funnels; |
| 26 | import com.google.common.hash.HashCode; |
| 27 | import com.google.common.hash.Hasher; |
| 28 | import com.google.common.hash.Hashing; |
| Jakub Vrana | 21e70b7 | 2019-05-20 16:33:54 +0200 | [diff] [blame] | 29 | import com.google.common.html.types.LegacyConversions; |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 30 | import com.google.common.io.ByteStreams; |
| Shawn Pearce | c4d3fd7 | 2015-02-10 14:32:37 -0800 | [diff] [blame] | 31 | import com.google.common.net.HttpHeaders; |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 32 | import com.google.template.soy.jbcsrc.api.SoySauce; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 33 | import java.io.File; |
| 34 | import java.io.IOException; |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 35 | import java.io.InputStream; |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 36 | import java.io.OutputStream; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 37 | import java.net.MalformedURLException; |
| 38 | import java.net.URL; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 39 | import java.util.Map; |
| Kurt Alfred Kluever | c1f6cfc | 2018-04-30 20:16:43 -0700 | [diff] [blame] | 40 | import java.util.concurrent.ConcurrentHashMap; |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 41 | import java.util.concurrent.ConcurrentMap; |
| David Pursehouse | b40361f | 2017-05-30 10:41:53 +0900 | [diff] [blame] | 42 | import java.util.function.Function; |
| Shawn Pearce | 6451fa5 | 2017-06-29 20:47:05 -0700 | [diff] [blame] | 43 | import java.util.zip.GZIPOutputStream; |
| Shawn Pearce | c4d3fd7 | 2015-02-10 14:32:37 -0800 | [diff] [blame] | 44 | import javax.servlet.http.HttpServletRequest; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 45 | import javax.servlet.http.HttpServletResponse; |
| 46 | |
| Dave Borowitz | fc775ad | 2014-07-30 11:38:53 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Renderer for Soy templates used by Gitiles. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 49 | * |
| 50 | * <p>Most callers should not use the methods in this class directly, and instead use one of the |
| 51 | * HTML methods in {@link BaseServlet}. |
| Dave Borowitz | fc775ad | 2014-07-30 11:38:53 -0700 | [diff] [blame] | 52 | */ |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 53 | public abstract class Renderer { |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 54 | // Must match .streamingPlaceholder. |
| 55 | private static final String PLACEHOLDER = "id=\"STREAMED_OUTPUT_BLOCK\""; |
| 56 | |
| Dave Borowitz | 3b685ab | 2017-03-09 09:24:54 -0500 | [diff] [blame] | 57 | private static final ImmutableList<String> SOY_FILENAMES = |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 58 | ImmutableList.of( |
| 59 | "BlameDetail.soy", |
| 60 | "Common.soy", |
| 61 | "DiffDetail.soy", |
| 62 | "Doc.soy", |
| Alon Bar-Lev | cf9e71d | 2019-01-23 15:23:19 +0200 | [diff] [blame] | 63 | "Error.soy", |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 64 | "HostIndex.soy", |
| 65 | "LogDetail.soy", |
| 66 | "ObjectDetail.soy", |
| 67 | "PathDetail.soy", |
| 68 | "RefList.soy", |
| 69 | "RevisionDetail.soy", |
| 70 | "RepositoryIndex.soy"); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 71 | |
| Dave Borowitz | 3b685ab | 2017-03-09 09:24:54 -0500 | [diff] [blame] | 72 | public static final ImmutableMap<String, String> STATIC_URL_GLOBALS = |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 73 | ImmutableMap.of( |
| 74 | "gitiles.BASE_CSS_URL", "base.css", |
| 75 | "gitiles.DOC_CSS_URL", "doc.css", |
| 76 | "gitiles.PRETTIFY_CSS_URL", "prettify/prettify.css"); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 77 | |
| Dave Borowitz | de07eac | 2016-10-04 09:44:25 -0400 | [diff] [blame] | 78 | protected static Function<String, URL> fileUrlMapper() { |
| 79 | return fileUrlMapper(""); |
| 80 | } |
| Dave Borowitz | 76bbefd | 2014-03-11 16:57:45 -0700 | [diff] [blame] | 81 | |
| Dave Borowitz | de07eac | 2016-10-04 09:44:25 -0400 | [diff] [blame] | 82 | protected static Function<String, URL> fileUrlMapper(String prefix) { |
| 83 | checkNotNull(prefix); |
| 84 | return filename -> { |
| Dave Borowitz | 76bbefd | 2014-03-11 16:57:45 -0700 | [diff] [blame] | 85 | if (filename == null) { |
| 86 | return null; |
| 87 | } |
| 88 | try { |
| 89 | return new File(prefix + filename).toURI().toURL(); |
| 90 | } catch (MalformedURLException e) { |
| 91 | throw new IllegalArgumentException(e); |
| 92 | } |
| Dave Borowitz | de07eac | 2016-10-04 09:44:25 -0400 | [diff] [blame] | 93 | }; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 96 | protected ImmutableMap<String, URL> templates; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 97 | protected ImmutableMap<String, String> globals; |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 98 | private final ConcurrentMap<String, HashCode> hashes = |
| Kurt Alfred Kluever | c1f6cfc | 2018-04-30 20:16:43 -0700 | [diff] [blame] | 99 | new ConcurrentHashMap<>(SOY_FILENAMES.size()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 100 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 101 | protected Renderer( |
| 102 | Function<String, URL> resourceMapper, |
| 103 | Map<String, String> globals, |
| 104 | String staticPrefix, |
| 105 | Iterable<URL> customTemplates, |
| 106 | String siteTitle) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 107 | checkNotNull(staticPrefix, "staticPrefix"); |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 108 | |
| 109 | ImmutableMap.Builder<String, URL> b = ImmutableMap.builder(); |
| 110 | for (String name : SOY_FILENAMES) { |
| 111 | b.put(name, resourceMapper.apply(name)); |
| 112 | } |
| 113 | for (URL u : customTemplates) { |
| 114 | b.put(u.toString(), u); |
| 115 | } |
| 116 | templates = b.build(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 117 | |
| 118 | Map<String, String> allGlobals = Maps.newHashMap(); |
| 119 | for (Map.Entry<String, String> e : STATIC_URL_GLOBALS.entrySet()) { |
| 120 | allGlobals.put(e.getKey(), staticPrefix + e.getValue()); |
| 121 | } |
| Chad Horohoe | 2a28d62 | 2012-11-12 11:56:59 -0800 | [diff] [blame] | 122 | allGlobals.put("gitiles.SITE_TITLE", siteTitle); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 123 | allGlobals.putAll(globals); |
| 124 | this.globals = ImmutableMap.copyOf(allGlobals); |
| 125 | } |
| 126 | |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 127 | public HashCode getTemplateHash(String soyFile) { |
| 128 | HashCode h = hashes.get(soyFile); |
| 129 | if (h == null) { |
| 130 | h = computeTemplateHash(soyFile); |
| 131 | hashes.put(soyFile, h); |
| 132 | } |
| 133 | return h; |
| 134 | } |
| 135 | |
| 136 | HashCode computeTemplateHash(String soyFile) { |
| 137 | URL u = templates.get(soyFile); |
| 138 | checkState(u != null, "Missing Soy template %s", soyFile); |
| 139 | |
| David Pursehouse | d591449 | 2017-05-31 13:08:22 +0900 | [diff] [blame] | 140 | Hasher h = Hashing.murmur3_128().newHasher(); |
| Shawn Pearce | a9b99a1 | 2015-02-10 15:35:11 -0800 | [diff] [blame] | 141 | try (InputStream is = u.openStream(); |
| 142 | OutputStream os = Funnels.asOutputStream(h)) { |
| 143 | ByteStreams.copy(is, os); |
| 144 | } catch (IOException e) { |
| 145 | throw new IllegalStateException("Missing Soy template " + soyFile, e); |
| 146 | } |
| 147 | return h.hash(); |
| 148 | } |
| 149 | |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 150 | public String renderHtml(String templateName, Map<String, ?> soyData) { |
| 151 | return newRenderer(templateName).setData(soyData).renderHtml().get().toString(); |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 154 | void renderHtml( |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 155 | HttpServletRequest req, HttpServletResponse res, String templateName, Map<String, ?> soyData) |
| 156 | throws IOException { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 157 | res.setContentType("text/html"); |
| 158 | res.setCharacterEncoding("UTF-8"); |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 159 | byte[] data = |
| 160 | newRenderer(templateName).setData(soyData).renderHtml().get().toString().getBytes(UTF_8); |
| Shawn Pearce | c4d3fd7 | 2015-02-10 14:32:37 -0800 | [diff] [blame] | 161 | if (BaseServlet.acceptsGzipEncoding(req)) { |
| Shawn Pearce | ed3c2d1 | 2016-05-30 15:59:02 -0700 | [diff] [blame] | 162 | res.addHeader(HttpHeaders.VARY, HttpHeaders.ACCEPT_ENCODING); |
| Shawn Pearce | c4d3fd7 | 2015-02-10 14:32:37 -0800 | [diff] [blame] | 163 | res.setHeader(HttpHeaders.CONTENT_ENCODING, "gzip"); |
| 164 | data = BaseServlet.gzip(data); |
| 165 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 166 | res.setContentLength(data.length); |
| 167 | res.getOutputStream().write(data); |
| 168 | } |
| 169 | |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 170 | OutputStream renderHtmlStreaming( |
| 171 | HttpServletResponse res, String templateName, Map<String, ?> soyData) throws IOException { |
| 172 | return renderHtmlStreaming(res, false, templateName, soyData); |
| Shawn Pearce | 6451fa5 | 2017-06-29 20:47:05 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 175 | OutputStream renderHtmlStreaming( |
| Shawn Pearce | 6451fa5 | 2017-06-29 20:47:05 -0700 | [diff] [blame] | 176 | HttpServletResponse res, boolean gzip, String templateName, Map<String, ?> soyData) |
| 177 | throws IOException { |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 178 | String html = newRenderer(templateName).setData(soyData).renderHtml().get().toString(); |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 179 | int id = html.indexOf(PLACEHOLDER); |
| 180 | checkArgument(id >= 0, "Template must contain %s", PLACEHOLDER); |
| 181 | |
| 182 | int lt = html.lastIndexOf('<', id); |
| Shawn Pearce | 4b49d8d | 2017-06-29 20:02:22 -0700 | [diff] [blame] | 183 | int gt = html.indexOf('>', id + PLACEHOLDER.length()); |
| 184 | |
| Shawn Pearce | 6451fa5 | 2017-06-29 20:47:05 -0700 | [diff] [blame] | 185 | OutputStream out = gzip ? new GZIPOutputStream(res.getOutputStream()) : res.getOutputStream(); |
| David Pletcher | d7bdaf3 | 2014-08-27 14:50:32 -0700 | [diff] [blame] | 186 | out.write(html.substring(0, lt).getBytes(UTF_8)); |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 187 | out.flush(); |
| 188 | |
| Shawn Pearce | 4b49d8d | 2017-06-29 20:02:22 -0700 | [diff] [blame] | 189 | byte[] tail = html.substring(gt + 1).getBytes(UTF_8); |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 190 | return new OutputStream() { |
| 191 | @Override |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 192 | public void write(byte[] b, int off, int len) throws IOException { |
| 193 | out.write(b, off, len); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public void write(int b) throws IOException { |
| 198 | out.write(b); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public void flush() throws IOException { |
| 203 | out.flush(); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void close() throws IOException { |
| Shawn Pearce | 4b49d8d | 2017-06-29 20:02:22 -0700 | [diff] [blame] | 208 | try (OutputStream o = out) { |
| 209 | o.write(tail); |
| 210 | } |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 211 | } |
| 212 | }; |
| 213 | } |
| 214 | |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 215 | SoySauce.Renderer newRenderer(String templateName) { |
| Jakub Vrana | 21e70b7 | 2019-05-20 16:33:54 +0200 | [diff] [blame] | 216 | ImmutableMap.Builder<String, Object> staticUrls = ImmutableMap.builder(); |
| 217 | for (String key : STATIC_URL_GLOBALS.keySet()) { |
| 218 | staticUrls.put( |
| 219 | key.replaceFirst("^gitiles\\.", ""), |
| 220 | LegacyConversions.riskilyAssumeTrustedResourceUrl(globals.get(key))); |
| 221 | } |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 222 | return getSauce() |
| 223 | .renderTemplate(templateName) |
| 224 | .setIj(ImmutableMap.of("staticUrls", staticUrls.build())); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 227 | protected abstract SoySauce getSauce(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 228 | } |