Line data Source code
1 : // Copyright (C) 2009 The Android Open Source Project 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.gerrit.httpd.gitweb; 16 : 17 : import static com.google.gerrit.common.FileUtil.lastModified; 18 : import static java.nio.charset.StandardCharsets.UTF_8; 19 : 20 : import com.google.gerrit.httpd.HtmlDomUtil; 21 : import com.google.gerrit.server.config.GitwebCgiConfig; 22 : import com.google.gerrit.server.config.SitePaths; 23 : import com.google.gerrit.util.http.CacheHeaders; 24 : import com.google.gerrit.util.http.RequestUtil; 25 : import com.google.inject.Inject; 26 : import com.google.inject.Singleton; 27 : import java.io.IOException; 28 : import java.nio.file.Path; 29 : import java.util.concurrent.TimeUnit; 30 : import javax.servlet.ServletOutputStream; 31 : import javax.servlet.http.HttpServlet; 32 : import javax.servlet.http.HttpServletRequest; 33 : import javax.servlet.http.HttpServletResponse; 34 : 35 : abstract class GitwebCssServlet extends HttpServlet { 36 : private static final long serialVersionUID = 1L; 37 : 38 : @Singleton 39 : static class Site extends GitwebCssServlet { 40 : private static final long serialVersionUID = 1L; 41 : 42 : @Inject 43 : Site(SitePaths paths) throws IOException { 44 0 : super(paths.site_css); 45 0 : } 46 : } 47 : 48 : @Singleton 49 : static class Default extends GitwebCssServlet { 50 : private static final long serialVersionUID = 1L; 51 : 52 : @Inject 53 : Default(GitwebCgiConfig gwcc) throws IOException { 54 0 : super(gwcc.getGitwebCss()); 55 0 : } 56 : } 57 : 58 : private final long modified; 59 : private final byte[] raw_css; 60 : private final byte[] gz_css; 61 : 62 0 : GitwebCssServlet(Path src) throws IOException { 63 0 : if (src != null) { 64 0 : final Path dir = src.getParent(); 65 0 : final String name = src.getFileName().toString(); 66 0 : final String raw = HtmlDomUtil.readFile(dir, name); 67 0 : if (raw != null) { 68 0 : modified = lastModified(src); 69 0 : raw_css = raw.getBytes(UTF_8); 70 0 : gz_css = HtmlDomUtil.compress(raw_css); 71 : } else { 72 0 : modified = -1L; 73 0 : raw_css = null; 74 0 : gz_css = null; 75 : } 76 0 : } else { 77 0 : modified = -1; 78 0 : raw_css = null; 79 0 : gz_css = null; 80 : } 81 0 : } 82 : 83 : @Override 84 : protected long getLastModified(HttpServletRequest req) { 85 0 : return modified; 86 : } 87 : 88 : @Override 89 : protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws IOException { 90 0 : if (raw_css != null) { 91 0 : rsp.setContentType("text/css"); 92 0 : rsp.setCharacterEncoding(UTF_8.name()); 93 : final byte[] toSend; 94 0 : if (RequestUtil.acceptsGzipEncoding(req)) { 95 0 : rsp.setHeader("Content-Encoding", "gzip"); 96 0 : toSend = gz_css; 97 : } else { 98 0 : toSend = raw_css; 99 : } 100 0 : rsp.setContentLength(toSend.length); 101 0 : rsp.setDateHeader("Last-Modified", modified); 102 0 : CacheHeaders.setCacheable(req, rsp, 5, TimeUnit.MINUTES); 103 : 104 0 : try (ServletOutputStream os = rsp.getOutputStream()) { 105 0 : os.write(toSend); 106 : } 107 0 : } else { 108 0 : CacheHeaders.setNotCacheable(rsp); 109 0 : rsp.sendError(HttpServletResponse.SC_NOT_FOUND); 110 : } 111 0 : } 112 : }