James Moger
2015-11-22 ed552ba47c02779c270ffd62841d6d1048dade70
commit | author | age
177256 1 /*
JM 2  * Copyright 2013 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
7bf6e1 16 package com.gitblit.servlet;
177256 17
JM 18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23
cdb2fe 24 import com.google.inject.Inject;
JM 25 import com.google.inject.Singleton;
177256 26 import javax.servlet.ServletContext;
JM 27 import javax.servlet.ServletException;
1b34b0 28 import javax.servlet.http.HttpServlet;
177256 29 import javax.servlet.http.HttpServletRequest;
JM 30 import javax.servlet.http.HttpServletResponse;
31
7bf6e1 32 import com.gitblit.Keys;
db4f6b 33 import com.gitblit.manager.IRuntimeManager;
JM 34
177256 35 /**
JM 36  * Handles requests for logo.png
699e71 37  *
177256 38  * @author James Moger
699e71 39  *
177256 40  */
1b34b0 41 @Singleton
JM 42 public class LogoServlet extends HttpServlet {
699e71 43
177256 44     private static final long serialVersionUID = 1L;
699e71 45
177256 46     private static final long lastModified = System.currentTimeMillis();
JM 47
65d5bb 48     private IRuntimeManager runtimeManager;
cacf8b 49
1b34b0 50     @Inject
JM 51     public LogoServlet(IRuntimeManager runtimeManager) {
52         this.runtimeManager = runtimeManager;
177256 53     }
699e71 54
177256 55     @Override
JM 56     protected long getLastModified(HttpServletRequest req) {
db4f6b 57         File file = runtimeManager.getFileOrFolder(Keys.web.headerLogo, "${baseFolder}/logo.png");
177256 58         if (file.exists()) {
61555b 59             return Math.max(lastModified, file.lastModified());
177256 60         } else {
JM 61             return lastModified;
62         }
63     }
699e71 64
177256 65     @Override
JM 66     protected void doGet(HttpServletRequest request, HttpServletResponse response)
67             throws ServletException, IOException {
68         InputStream is = null;
69         try {
70             String contentType = null;
db4f6b 71             File file = runtimeManager.getFileOrFolder(Keys.web.headerLogo, "${baseFolder}/logo.png");
177256 72             if (file.exists()) {
JM 73                 // custom logo
74                 ServletContext context = request.getSession().getServletContext();
75                 contentType = context.getMimeType(file.getName());
76                 response.setContentLength((int) file.length());
61555b 77                 response.setDateHeader("Last-Modified", Math.max(lastModified, file.lastModified()));
177256 78                 is = new FileInputStream(file);
JM 79             } else {
80                 // default logo
81                 response.setDateHeader("Last-Modified", lastModified);
82                 is = getClass().getResourceAsStream("/logo.png");
699e71 83             }
177256 84             if (contentType == null) {
JM 85                 contentType = "image/png";
86             }
87             response.setContentType(contentType);
a7db57 88             response.setHeader("Cache-Control", "public, max-age=3600, must-revalidate");
177256 89             OutputStream os = response.getOutputStream();
JM 90             byte[] buf = new byte[4096];
91             int bytesRead = is.read(buf);
92             while (bytesRead != -1) {
93                 os.write(buf, 0, bytesRead);
94                 bytesRead = is.read(buf);
95             }
96             os.flush();
97         } catch (Exception e) {
98             e.printStackTrace();
99         } finally {
db4f6b 100             if (is != null) {
fbe265 101                 is.close();
SH 102             }
177256 103         }
JM 104     }
105 }