You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LogoServlet.java 2.9KB

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