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