Simon Harrer
2013-07-18 fbe265fa787e4be9cb63c6bae2ef30b9400d9afc
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  */
16 package com.gitblit;
17
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
24 import javax.servlet.ServletContext;
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServlet;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 /**
31  * Handles requests for logo.png
32  * 
33  * @author James Moger
34  * 
35  */
36 public class LogoServlet extends HttpServlet {
37     
38     private static final long serialVersionUID = 1L;
39     
40     private static final long lastModified = System.currentTimeMillis();
41
42     public LogoServlet() {
43         super();
44     }
45     
46     @Override
47     protected long getLastModified(HttpServletRequest req) {
48         File file = GitBlit.getFileOrFolder(Keys.web.headerLogo, "${baseFolder}/logo.png");
49         if (file.exists()) {
61555b 50             return Math.max(lastModified, file.lastModified());
177256 51         } else {
JM 52             return lastModified;
53         }
54     }
55     
56     @Override
57     protected void doGet(HttpServletRequest request, HttpServletResponse response)
58             throws ServletException, IOException {
59         InputStream is = null;
60         try {
61             String contentType = null;
62             File file = GitBlit.getFileOrFolder(Keys.web.headerLogo, "${baseFolder}/logo.png");
63             if (file.exists()) {
64                 // custom logo
65                 ServletContext context = request.getSession().getServletContext();
66                 contentType = context.getMimeType(file.getName());
67                 response.setContentLength((int) file.length());
61555b 68                 response.setDateHeader("Last-Modified", Math.max(lastModified, file.lastModified()));
177256 69                 is = new FileInputStream(file);
JM 70             } else {
71                 // default logo
72                 response.setDateHeader("Last-Modified", lastModified);
73                 is = getClass().getResourceAsStream("/logo.png");
74             }            
75             if (contentType == null) {
76                 contentType = "image/png";
77             }
78             response.setContentType(contentType);
79             OutputStream os = response.getOutputStream();
80             byte[] buf = new byte[4096];
81             int bytesRead = is.read(buf);
82             while (bytesRead != -1) {
83                 os.write(buf, 0, bytesRead);
84                 bytesRead = is.read(buf);
85             }
86             os.flush();
87         } catch (Exception e) {
88             e.printStackTrace();
89         } finally {
fbe265 90             if(is != null) {
SH 91                 is.close();
92             }
177256 93         }
JM 94     }
95 }