| 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 | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 17 | import static com.google.common.base.MoreObjects.firstNonNull; |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 18 | import static com.google.gitiles.FormatType.DEFAULT; |
| 19 | import static com.google.gitiles.FormatType.HTML; |
| 20 | import static com.google.gitiles.FormatType.JSON; |
| 21 | import static com.google.gitiles.FormatType.TEXT; |
| David Pletcher | d7bdaf3 | 2014-08-27 14:50:32 -0700 | [diff] [blame] | 22 | import static java.nio.charset.StandardCharsets.UTF_8; |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 23 | import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 24 | import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 25 | import static javax.servlet.http.HttpServletResponse.SC_OK; |
| Shawn Pearce | c4d3fd7 | 2015-02-10 14:32:37 -0800 | [diff] [blame] | 26 | import static org.eclipse.jgit.util.HttpSupport.ENCODING_GZIP; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 27 | |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 28 | import com.google.common.base.Joiner; |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame] | 29 | import com.google.common.base.Strings; |
| Dave Borowitz | 5427146 | 2013-11-11 11:43:11 -0800 | [diff] [blame] | 30 | import com.google.common.collect.ImmutableMap; |
| 31 | import com.google.common.collect.Maps; |
| 32 | import com.google.common.net.HttpHeaders; |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 33 | import com.google.gitiles.GitilesRequestFailureException.FailureReason; |
| Dave Borowitz | 5427146 | 2013-11-11 11:43:11 -0800 | [diff] [blame] | 34 | import com.google.gson.FieldNamingPolicy; |
| 35 | import com.google.gson.GsonBuilder; |
| Dave Borowitz | ae171e6 | 2017-05-11 17:51:37 -0400 | [diff] [blame] | 36 | import java.io.BufferedWriter; |
| Shawn Pearce | c4d3fd7 | 2015-02-10 14:32:37 -0800 | [diff] [blame] | 37 | import java.io.ByteArrayOutputStream; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 38 | import java.io.IOException; |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 39 | import java.io.OutputStream; |
| Dave Borowitz | 673d198 | 2014-05-02 12:30:49 -0700 | [diff] [blame] | 40 | import java.io.OutputStreamWriter; |
| 41 | import java.io.Writer; |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 42 | import java.lang.reflect.Type; |
| David Pursehouse | c0037b8 | 2018-03-16 13:56:20 +0900 | [diff] [blame] | 43 | import java.time.Instant; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 44 | import java.util.Map; |
| David Pursehouse | 7a7f547 | 2016-10-14 09:59:20 +0900 | [diff] [blame] | 45 | import java.util.Optional; |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 46 | import java.util.regex.Pattern; |
| Shawn Pearce | c4d3fd7 | 2015-02-10 14:32:37 -0800 | [diff] [blame] | 47 | import java.util.zip.GZIPOutputStream; |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 48 | import javax.servlet.ServletException; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 49 | import javax.servlet.http.HttpServlet; |
| 50 | import javax.servlet.http.HttpServletRequest; |
| 51 | import javax.servlet.http.HttpServletResponse; |
| 52 | |
| 53 | /** Base servlet class for Gitiles servlets that serve Soy templates. */ |
| 54 | public abstract class BaseServlet extends HttpServlet { |
| Chad Horohoe | ad23f14 | 2012-11-12 09:45:39 -0800 | [diff] [blame] | 55 | private static final long serialVersionUID = 1L; |
| Dave Borowitz | c99d0bb | 2014-07-31 15:39:39 -0700 | [diff] [blame] | 56 | private static final String DATA_ATTRIBUTE = BaseServlet.class.getName() + "/Data"; |
| 57 | private static final String STREAMING_ATTRIBUTE = BaseServlet.class.getName() + "/Streaming"; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 58 | |
| 59 | static void setNotCacheable(HttpServletResponse res) { |
| 60 | res.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, max-age=0, must-revalidate"); |
| 61 | res.setHeader(HttpHeaders.PRAGMA, "no-cache"); |
| Dave Borowitz | 70d8454 | 2015-08-16 15:08:35 -0400 | [diff] [blame] | 62 | res.setHeader(HttpHeaders.EXPIRES, "Mon, 01 Jan 1990 00:00:00 GMT"); |
| David Pursehouse | c0037b8 | 2018-03-16 13:56:20 +0900 | [diff] [blame] | 63 | res.setDateHeader(HttpHeaders.DATE, Instant.now().toEpochMilli()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | public static BaseServlet notFoundServlet() { |
| Dave Borowitz | 8d6d687 | 2014-03-16 15:18:14 -0700 | [diff] [blame] | 67 | return new BaseServlet(null, null) { |
| Chad Horohoe | ad23f14 | 2012-11-12 09:45:39 -0800 | [diff] [blame] | 68 | private static final long serialVersionUID = 1L; |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 69 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 70 | @Override |
| 71 | public void service(HttpServletRequest req, HttpServletResponse res) { |
| 72 | res.setStatus(SC_NOT_FOUND); |
| 73 | } |
| 74 | }; |
| 75 | } |
| 76 | |
| 77 | public static Map<String, String> menuEntry(String text, String url) { |
| 78 | if (url != null) { |
| 79 | return ImmutableMap.of("text", text, "url", url); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 80 | } |
| David Pursehouse | b3b630f | 2016-06-15 21:51:18 +0900 | [diff] [blame] | 81 | return ImmutableMap.of("text", text); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| Dave Borowitz | c99d0bb | 2014-07-31 15:39:39 -0700 | [diff] [blame] | 84 | public static boolean isStreamingResponse(HttpServletRequest req) { |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 85 | return firstNonNull((Boolean) req.getAttribute(STREAMING_ATTRIBUTE), false); |
| Dave Borowitz | c99d0bb | 2014-07-31 15:39:39 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 88 | protected static ArchiveFormat getArchiveFormat(GitilesAccess access) throws IOException { |
| 89 | return ArchiveFormat.getDefault(access.getConfig()); |
| 90 | } |
| 91 | |
| Dave Borowitz | 113c935 | 2013-03-27 18:13:41 -0400 | [diff] [blame] | 92 | /** |
| 93 | * Put a value into a request's Soy data map. |
| 94 | * |
| 95 | * @param req in-progress request. |
| 96 | * @param key key. |
| 97 | * @param value Soy data value. |
| 98 | */ |
| 99 | public static void putSoyData(HttpServletRequest req, String key, Object value) { |
| 100 | getData(req).put(key, value); |
| 101 | } |
| 102 | |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 103 | @Override |
| 104 | protected void doGet(HttpServletRequest req, HttpServletResponse res) |
| 105 | throws IOException, ServletException { |
| Shawn Pearce | 10e68e6 | 2016-01-02 09:37:58 -0800 | [diff] [blame] | 106 | Optional<FormatType> format = getFormat(req); |
| 107 | if (!format.isPresent()) { |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 108 | res.sendError(SC_BAD_REQUEST); |
| 109 | return; |
| 110 | } |
| Shawn Pearce | 10e68e6 | 2016-01-02 09:37:58 -0800 | [diff] [blame] | 111 | switch (format.get()) { |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 112 | case HTML: |
| 113 | doGetHtml(req, res); |
| 114 | break; |
| 115 | case TEXT: |
| 116 | doGetText(req, res); |
| 117 | break; |
| 118 | case JSON: |
| 119 | doGetJson(req, res); |
| 120 | break; |
| David Pursehouse | cb91aaf | 2016-06-15 22:05:24 +0900 | [diff] [blame] | 121 | case DEFAULT: |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 122 | default: |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 123 | throw new GitilesRequestFailureException(FailureReason.UNSUPPORTED_RESPONSE_FORMAT); |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| Shawn Pearce | 10e68e6 | 2016-01-02 09:37:58 -0800 | [diff] [blame] | 127 | protected Optional<FormatType> getFormat(HttpServletRequest req) { |
| 128 | Optional<FormatType> format = FormatType.getFormatType(req); |
| 129 | if (format.isPresent() && format.get() == DEFAULT) { |
| 130 | return Optional.of(getDefaultFormat(req)); |
| 131 | } |
| 132 | return format; |
| 133 | } |
| 134 | |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 135 | /** |
| 136 | * @param req in-progress request. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 137 | * @return the default {@link FormatType} used when {@code ?format=} is not specified. |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 138 | */ |
| 139 | protected FormatType getDefaultFormat(HttpServletRequest req) { |
| 140 | return HTML; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Handle a GET request when the requested format type was HTML. |
| 145 | * |
| 146 | * @param req in-progress request. |
| 147 | * @param res in-progress response. |
| David Pursehouse | 3863a1e | 2019-06-01 15:34:47 +0900 | [diff] [blame] | 148 | * @throws IOException if there was an error rendering the result. |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 149 | */ |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 150 | protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 151 | throw new GitilesRequestFailureException(FailureReason.UNSUPPORTED_RESPONSE_FORMAT); |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Handle a GET request when the requested format type was plain text. |
| 156 | * |
| 157 | * @param req in-progress request. |
| 158 | * @param res in-progress response. |
| David Pursehouse | 3863a1e | 2019-06-01 15:34:47 +0900 | [diff] [blame] | 159 | * @throws IOException if there was an error rendering the result. |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 160 | */ |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 161 | protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 162 | throw new GitilesRequestFailureException(FailureReason.UNSUPPORTED_RESPONSE_FORMAT); |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Handle a GET request when the requested format type was JSON. |
| 167 | * |
| 168 | * @param req in-progress request. |
| 169 | * @param res in-progress response. |
| David Pursehouse | 3863a1e | 2019-06-01 15:34:47 +0900 | [diff] [blame] | 170 | * @throws IOException if there was an error rendering the result. |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 171 | */ |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 172 | protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 173 | throw new GitilesRequestFailureException(FailureReason.UNSUPPORTED_RESPONSE_FORMAT); |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 176 | protected static Map<String, Object> getData(HttpServletRequest req) { |
| 177 | @SuppressWarnings("unchecked") |
| 178 | Map<String, Object> data = (Map<String, Object>) req.getAttribute(DATA_ATTRIBUTE); |
| 179 | if (data == null) { |
| 180 | data = Maps.newHashMap(); |
| 181 | req.setAttribute(DATA_ATTRIBUTE, data); |
| 182 | } |
| 183 | return data; |
| 184 | } |
| 185 | |
| 186 | protected final Renderer renderer; |
| Dave Borowitz | 8d6d687 | 2014-03-16 15:18:14 -0700 | [diff] [blame] | 187 | private final GitilesAccess.Factory accessFactory; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 188 | |
| Dave Borowitz | 8d6d687 | 2014-03-16 15:18:14 -0700 | [diff] [blame] | 189 | protected BaseServlet(Renderer renderer, GitilesAccess.Factory accessFactory) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 190 | this.renderer = renderer; |
| Dave Borowitz | 8d6d687 | 2014-03-16 15:18:14 -0700 | [diff] [blame] | 191 | this.accessFactory = accessFactory; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /** |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 195 | * Render data to HTML using Soy. |
| 196 | * |
| 197 | * @param req in-progress request. |
| 198 | * @param res in-progress response. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 199 | * @param templateName Soy template name; must be in one of the template files defined in {@link |
| 200 | * Renderer}. |
| Dave Borowitz | 33d4fda | 2013-10-22 16:40:20 -0700 | [diff] [blame] | 201 | * @param soyData data for Soy. |
| 202 | * @throws IOException an error occurred during rendering. |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 203 | */ |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 204 | protected void renderHtml( |
| 205 | HttpServletRequest req, HttpServletResponse res, String templateName, Map<String, ?> soyData) |
| 206 | throws IOException { |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 207 | renderer.renderHtml(req, res, templateName, startHtmlResponse(req, res, soyData)); |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Start a streaming HTML response with header and footer rendered by Soy. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 212 | * |
| 213 | * <p>A streaming template includes the special template {@code gitiles.streamingPlaceholder} at |
| 214 | * the point where data is to be streamed. The template before and after this placeholder is |
| 215 | * rendered using the provided data map. |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 216 | * |
| 217 | * @param req in-progress request. |
| 218 | * @param res in-progress response. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 219 | * @param templateName Soy template name; must be in one of the template files defined in {@link |
| 220 | * Renderer}. |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 221 | * @param soyData data for Soy. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 222 | * @return output stream to render to. The portion of the template before the placeholder is |
| 223 | * already written and flushed; the portion after is written only on calling {@code close()}. |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 224 | * @throws IOException an error occurred during rendering the header. |
| 225 | */ |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 226 | protected OutputStream startRenderStreamingHtml( |
| 227 | HttpServletRequest req, HttpServletResponse res, String templateName, Map<String, ?> soyData) |
| 228 | throws IOException { |
| Dave Borowitz | c99d0bb | 2014-07-31 15:39:39 -0700 | [diff] [blame] | 229 | req.setAttribute(STREAMING_ATTRIBUTE, true); |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 230 | return renderer.renderHtmlStreaming( |
| 231 | res, false, templateName, startHtmlResponse(req, res, soyData)); |
| Shawn Pearce | 6451fa5 | 2017-06-29 20:47:05 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Start a compressed, streaming HTML response with header and footer rendered by Soy. |
| 236 | * |
| 237 | * <p>A streaming template includes the special template {@code gitiles.streamingPlaceholder} at |
| 238 | * the point where data is to be streamed. The template before and after this placeholder is |
| 239 | * rendered using the provided data map. |
| 240 | * |
| 241 | * <p>The response will be gzip compressed (if the user agent supports it) to reduce bandwidth. |
| 242 | * This may delay rendering in the browser. |
| 243 | * |
| 244 | * @param req in-progress request. |
| 245 | * @param res in-progress response. |
| 246 | * @param templateName Soy template name; must be in one of the template files defined in {@link |
| 247 | * Renderer}. |
| 248 | * @param soyData data for Soy. |
| 249 | * @return output stream to render to. The portion of the template before the placeholder is |
| 250 | * already written and flushed; the portion after is written only on calling {@code close()}. |
| 251 | * @throws IOException an error occurred during rendering the header. |
| 252 | */ |
| 253 | protected OutputStream startRenderCompressedStreamingHtml( |
| David Pursehouse | f39cadc | 2017-07-07 08:47:51 +0900 | [diff] [blame] | 254 | HttpServletRequest req, HttpServletResponse res, String templateName, Map<String, ?> soyData) |
| Shawn Pearce | 6451fa5 | 2017-06-29 20:47:05 -0700 | [diff] [blame] | 255 | throws IOException { |
| 256 | req.setAttribute(STREAMING_ATTRIBUTE, true); |
| 257 | boolean gzip = false; |
| 258 | if (acceptsGzipEncoding(req)) { |
| 259 | res.addHeader(HttpHeaders.VARY, HttpHeaders.ACCEPT_ENCODING); |
| 260 | res.setHeader(HttpHeaders.CONTENT_ENCODING, "gzip"); |
| 261 | gzip = true; |
| 262 | } |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 263 | return renderer.renderHtmlStreaming( |
| 264 | res, gzip, templateName, startHtmlResponse(req, res, soyData)); |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 267 | private Map<String, ?> startHtmlResponse( |
| 268 | HttpServletRequest req, HttpServletResponse res, Map<String, ?> soyData) throws IOException { |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 269 | res.setContentType(FormatType.HTML.getMimeType()); |
| David Pletcher | d7bdaf3 | 2014-08-27 14:50:32 -0700 | [diff] [blame] | 270 | res.setCharacterEncoding(UTF_8.name()); |
| Andrew Bonventre | cc3418b | 2016-12-01 20:18:37 -0800 | [diff] [blame] | 271 | setCacheHeaders(req, res); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 272 | |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 273 | Map<String, Object> allData = getData(req); |
| Dave Borowitz | 76bbefd | 2014-03-11 16:57:45 -0700 | [diff] [blame] | 274 | |
| Björn Pedersen | bc0eaaa | 2016-03-29 15:30:29 +0200 | [diff] [blame] | 275 | // for backwards compatibility, first try to access the old customHeader config var, |
| 276 | // then read the new customVariant variable. |
| 277 | GitilesConfig.putVariant(getAccess(req).getConfig(), "customHeader", "customVariant", allData); |
| 278 | GitilesConfig.putVariant(getAccess(req).getConfig(), "customVariant", "customVariant", allData); |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 279 | allData.putAll(soyData); |
| 280 | GitilesView view = ViewFilter.getView(req); |
| 281 | if (!allData.containsKey("repositoryName") && view.getRepositoryName() != null) { |
| 282 | allData.put("repositoryName", view.getRepositoryName()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 283 | } |
| Shawn Pearce | 5c34e09 | 2017-06-29 21:18:30 -0700 | [diff] [blame] | 284 | if (!allData.containsKey("breadcrumbs") && view.getRepositoryName() != null) { |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 285 | allData.put("breadcrumbs", view.getBreadcrumbs()); |
| 286 | } |
| 287 | |
| 288 | res.setStatus(HttpServletResponse.SC_OK); |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 289 | return allData; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 292 | /** |
| 293 | * Render data to JSON using GSON. |
| 294 | * |
| 295 | * @param req in-progress request. |
| 296 | * @param res in-progress response. |
| Dave Borowitz | 876b981 | 2015-09-16 15:17:58 -0400 | [diff] [blame] | 297 | * @param src @see com.google.gson.Gson#toJson(Object, Type, Appendable) |
| 298 | * @param typeOfSrc @see com.google.gson.Gson#toJson(Object, Type, Appendable) |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 299 | */ |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 300 | protected void renderJson( |
| 301 | HttpServletRequest req, HttpServletResponse res, Object src, Type typeOfSrc) |
| 302 | throws IOException { |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 303 | setApiHeaders(req, res, JSON); |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 304 | res.setStatus(SC_OK); |
| Shawn Pearce | c4d3fd7 | 2015-02-10 14:32:37 -0800 | [diff] [blame] | 305 | try (Writer writer = newWriter(req, res)) { |
| 306 | newGsonBuilder(req).create().toJson(src, typeOfSrc, writer); |
| 307 | writer.write('\n'); |
| 308 | } |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| Dave Borowitz | 438c528 | 2014-07-09 20:15:34 -0700 | [diff] [blame] | 311 | @SuppressWarnings("unused") // Used in subclasses. |
| 312 | protected GsonBuilder newGsonBuilder(HttpServletRequest req) throws IOException { |
| 313 | return new GsonBuilder() |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 314 | .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) |
| 315 | .setPrettyPrinting() |
| 316 | .generateNonExecutableJson(); |
| Dave Borowitz | 438c528 | 2014-07-09 20:15:34 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 319 | /** |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame] | 320 | * @see #startRenderText(HttpServletRequest, HttpServletResponse) |
| 321 | * @param req in-progress request. |
| 322 | * @param res in-progress response. |
| 323 | * @param contentType contentType to set. |
| 324 | * @return the response's writer. |
| 325 | */ |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 326 | protected Writer startRenderText( |
| 327 | HttpServletRequest req, HttpServletResponse res, String contentType) throws IOException { |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 328 | setApiHeaders(req, res, contentType); |
| Shawn Pearce | c4d3fd7 | 2015-02-10 14:32:37 -0800 | [diff] [blame] | 329 | return newWriter(req, res); |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /** |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 333 | * Prepare the response to render plain text. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 334 | * |
| 335 | * <p>Unlike {@link #renderHtml(HttpServletRequest, HttpServletResponse, String, Map)} and {@link |
| 336 | * #renderJson(HttpServletRequest, HttpServletResponse, Object, Type)}, which assume the data to |
| 337 | * render is already completely prepared, this method does not write any data, only headers, and |
| 338 | * returns the response's ready-to-use writer. |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 339 | * |
| 340 | * @param req in-progress request. |
| 341 | * @param res in-progress response. |
| 342 | * @return the response's writer. |
| 343 | */ |
| Dave Borowitz | 673d198 | 2014-05-02 12:30:49 -0700 | [diff] [blame] | 344 | protected Writer startRenderText(HttpServletRequest req, HttpServletResponse res) |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 345 | throws IOException { |
| Dave Borowitz | 673d198 | 2014-05-02 12:30:49 -0700 | [diff] [blame] | 346 | return startRenderText(req, res, TEXT.getMimeType()); |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 349 | /** |
| 350 | * Render an error as plain text. |
| 351 | * |
| 352 | * @param req in-progress request. |
| 353 | * @param res in-progress response. |
| 354 | * @param statusCode HTTP status code. |
| 355 | * @param message full message text. |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 356 | * @throws IOException |
| 357 | */ |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 358 | protected void renderTextError( |
| 359 | HttpServletRequest req, HttpServletResponse res, int statusCode, String message) |
| 360 | throws IOException { |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 361 | res.setStatus(statusCode); |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 362 | setApiHeaders(req, res, TEXT); |
| Andrew Bonventre | cc3418b | 2016-12-01 20:18:37 -0800 | [diff] [blame] | 363 | setCacheHeaders(req, res); |
| Shawn Pearce | c4d3fd7 | 2015-02-10 14:32:37 -0800 | [diff] [blame] | 364 | try (Writer out = newWriter(req, res)) { |
| 365 | out.write(message); |
| 366 | } |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| Dave Borowitz | 8d6d687 | 2014-03-16 15:18:14 -0700 | [diff] [blame] | 369 | protected GitilesAccess getAccess(HttpServletRequest req) { |
| Shawn Pearce | db394cc | 2017-07-01 11:45:20 -0700 | [diff] [blame] | 370 | return GitilesAccess.getAccess(req, accessFactory); |
| Dave Borowitz | 8d6d687 | 2014-03-16 15:18:14 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| Andrew Bonventre | cc3418b | 2016-12-01 20:18:37 -0800 | [diff] [blame] | 373 | protected void setCacheHeaders(HttpServletRequest req, HttpServletResponse res) { |
| David Pursehouse | ccaa85d | 2017-05-30 10:47:27 +0900 | [diff] [blame] | 374 | if (Strings.nullToEmpty(req.getHeader(HttpHeaders.PRAGMA)).equalsIgnoreCase("no-cache") |
| 375 | || Strings.nullToEmpty(req.getHeader(HttpHeaders.CACHE_CONTROL)) |
| Andrew Bonventre | cc3418b | 2016-12-01 20:18:37 -0800 | [diff] [blame] | 376 | .equalsIgnoreCase("no-cache")) { |
| 377 | setNotCacheable(res); |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | GitilesView view = ViewFilter.getView(req); |
| 382 | Revision rev = view.getRevision(); |
| 383 | if (rev.nameIsId()) { |
| David Pursehouse | ccaa85d | 2017-05-30 10:47:27 +0900 | [diff] [blame] | 384 | res.setHeader( |
| 385 | HttpHeaders.CACHE_CONTROL, "private, max-age=7200, stale-while-revalidate=604800"); |
| Andrew Bonventre | cc3418b | 2016-12-01 20:18:37 -0800 | [diff] [blame] | 386 | return; |
| 387 | } |
| 388 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 389 | setNotCacheable(res); |
| 390 | } |
| Dave Borowitz | 0c94476 | 2013-04-04 11:01:42 -0700 | [diff] [blame] | 391 | |
| David Pursehouse | ccaa85d | 2017-05-30 10:47:27 +0900 | [diff] [blame] | 392 | protected void setApiHeaders(HttpServletRequest req, HttpServletResponse res, String contentType) |
| 393 | throws IOException { |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame] | 394 | if (!Strings.isNullOrEmpty(contentType)) { |
| 395 | res.setContentType(contentType); |
| 396 | } |
| David Pletcher | d7bdaf3 | 2014-08-27 14:50:32 -0700 | [diff] [blame] | 397 | res.setCharacterEncoding(UTF_8.name()); |
| Dave Borowitz | 0c94476 | 2013-04-04 11:01:42 -0700 | [diff] [blame] | 398 | res.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment"); |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 399 | |
| 400 | GitilesAccess access = getAccess(req); |
| 401 | String[] allowOrigin = access.getConfig().getStringList("gitiles", null, "allowOriginRegex"); |
| 402 | |
| 403 | if (allowOrigin.length > 0) { |
| 404 | String origin = req.getHeader(HttpHeaders.ORIGIN); |
| 405 | Pattern allowOriginPattern = Pattern.compile(Joiner.on("|").join(allowOrigin)); |
| 406 | |
| 407 | if (!Strings.isNullOrEmpty(origin) && allowOriginPattern.matcher(origin).matches()) { |
| 408 | res.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, origin); |
| AJ Ross | 066d93c | 2016-08-23 18:12:46 -0700 | [diff] [blame] | 409 | res.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "X-Requested-With"); |
| 410 | res.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); |
| 411 | res.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "GET"); |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 412 | } |
| 413 | } else { |
| 414 | res.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*"); |
| 415 | } |
| Andrew Bonventre | cc3418b | 2016-12-01 20:18:37 -0800 | [diff] [blame] | 416 | setCacheHeaders(req, res); |
| Dave Borowitz | 0c94476 | 2013-04-04 11:01:42 -0700 | [diff] [blame] | 417 | } |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 418 | |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 419 | protected void setApiHeaders(HttpServletRequest req, HttpServletResponse res, FormatType type) |
| 420 | throws IOException { |
| 421 | setApiHeaders(req, res, type.getMimeType()); |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| David Pursehouse | ccaa85d | 2017-05-30 10:47:27 +0900 | [diff] [blame] | 424 | protected void setDownloadHeaders( |
| 425 | HttpServletRequest req, HttpServletResponse res, String filename, String contentType) { |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 426 | res.setContentType(contentType); |
| 427 | res.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename); |
| Andrew Bonventre | cc3418b | 2016-12-01 20:18:37 -0800 | [diff] [blame] | 428 | setCacheHeaders(req, res); |
| Dave Borowitz | 6d9bc5a | 2013-06-19 09:12:52 -0700 | [diff] [blame] | 429 | } |
| Dave Borowitz | 673d198 | 2014-05-02 12:30:49 -0700 | [diff] [blame] | 430 | |
| Dave Borowitz | 29914cb | 2014-08-20 14:37:57 -0700 | [diff] [blame] | 431 | protected static Writer newWriter(OutputStream os, HttpServletResponse res) throws IOException { |
| Dave Borowitz | ae171e6 | 2017-05-11 17:51:37 -0400 | [diff] [blame] | 432 | // StreamEncoder#write(int) is wasteful with its allocations, and we don't have much control |
| 433 | // over whether library code calls that variant as opposed to the saner write(char[], int, int). |
| 434 | // Protect against this by buffering. |
| 435 | return new BufferedWriter(new OutputStreamWriter(os, res.getCharacterEncoding())); |
| Dave Borowitz | 673d198 | 2014-05-02 12:30:49 -0700 | [diff] [blame] | 436 | } |
| Dave Borowitz | bf72ab7 | 2014-09-17 16:15:19 -0700 | [diff] [blame] | 437 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 438 | private Writer newWriter(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Shawn Pearce | c4d3fd7 | 2015-02-10 14:32:37 -0800 | [diff] [blame] | 439 | OutputStream out; |
| 440 | if (acceptsGzipEncoding(req)) { |
| Shawn Pearce | ed3c2d1 | 2016-05-30 15:59:02 -0700 | [diff] [blame] | 441 | res.addHeader(HttpHeaders.VARY, HttpHeaders.ACCEPT_ENCODING); |
| Shawn Pearce | c4d3fd7 | 2015-02-10 14:32:37 -0800 | [diff] [blame] | 442 | res.setHeader(HttpHeaders.CONTENT_ENCODING, "gzip"); |
| 443 | out = new GZIPOutputStream(res.getOutputStream()); |
| 444 | } else { |
| 445 | out = res.getOutputStream(); |
| 446 | } |
| 447 | return newWriter(out, res); |
| 448 | } |
| 449 | |
| 450 | protected static boolean acceptsGzipEncoding(HttpServletRequest req) { |
| 451 | String accepts = req.getHeader(HttpHeaders.ACCEPT_ENCODING); |
| 452 | if (accepts == null) { |
| 453 | return false; |
| 454 | } |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 455 | for (int b = 0; b < accepts.length(); ) { |
| Shawn Pearce | c4d3fd7 | 2015-02-10 14:32:37 -0800 | [diff] [blame] | 456 | int comma = accepts.indexOf(',', b); |
| 457 | int e = 0 <= comma ? comma : accepts.length(); |
| 458 | String term = accepts.substring(b, e).trim(); |
| 459 | if (term.equals(ENCODING_GZIP)) { |
| 460 | return true; |
| 461 | } |
| 462 | b = e + 1; |
| 463 | } |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | protected static byte[] gzip(byte[] raw) throws IOException { |
| 468 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 469 | try (GZIPOutputStream gz = new GZIPOutputStream(out)) { |
| 470 | gz.write(raw); |
| 471 | } |
| 472 | return out.toByteArray(); |
| Dave Borowitz | bf72ab7 | 2014-09-17 16:15:19 -0700 | [diff] [blame] | 473 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 474 | } |