Line data Source code
1 : // Copyright (C) 2010 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 : 19 : import com.google.common.io.ByteStreams; 20 : import com.google.gerrit.server.config.GitwebCgiConfig; 21 : import com.google.gerrit.util.http.CacheHeaders; 22 : import com.google.inject.Inject; 23 : import com.google.inject.Singleton; 24 : import java.io.IOException; 25 : import java.io.InputStream; 26 : import java.nio.file.Files; 27 : import java.nio.file.NoSuchFileException; 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 : @Singleton 36 : class GitwebJavaScriptServlet extends HttpServlet { 37 : private static final long serialVersionUID = 1L; 38 : 39 : private final long modified; 40 : private final byte[] raw; 41 : 42 : @Inject 43 0 : GitwebJavaScriptServlet(GitwebCgiConfig gitwebCgiConfig) throws IOException { 44 : byte[] png; 45 0 : Path src = gitwebCgiConfig.getGitwebJs(); 46 0 : if (src != null) { 47 0 : try (InputStream in = Files.newInputStream(src)) { 48 0 : png = ByteStreams.toByteArray(in); 49 0 : } catch (NoSuchFileException e) { 50 0 : png = null; 51 0 : } 52 0 : modified = lastModified(src); 53 : } else { 54 0 : modified = -1; 55 0 : png = null; 56 : } 57 0 : raw = png; 58 0 : } 59 : 60 : @Override 61 : protected long getLastModified(HttpServletRequest req) { 62 0 : return modified; 63 : } 64 : 65 : @Override 66 : protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws IOException { 67 0 : if (raw != null) { 68 0 : rsp.setContentType("text/javascript"); 69 0 : rsp.setContentLength(raw.length); 70 0 : rsp.setDateHeader("Last-Modified", modified); 71 0 : CacheHeaders.setCacheable(req, rsp, 5, TimeUnit.MINUTES); 72 : 73 0 : try (ServletOutputStream os = rsp.getOutputStream()) { 74 0 : os.write(raw); 75 : } 76 : } else { 77 0 : CacheHeaders.setNotCacheable(rsp); 78 0 : rsp.sendError(HttpServletResponse.SC_NOT_FOUND); 79 : } 80 0 : } 81 : }