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