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 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.servlet;
  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 com.google.inject.Inject;
  23. import com.google.inject.Singleton;
  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. import com.gitblit.Keys;
  30. import com.gitblit.manager.IRuntimeManager;
  31. /**
  32. * Handles requests for logo.png
  33. *
  34. * @author James Moger
  35. *
  36. */
  37. @Singleton
  38. public class LogoServlet extends HttpServlet {
  39. private static final long serialVersionUID = 1L;
  40. private static final long lastModified = System.currentTimeMillis();
  41. private IRuntimeManager runtimeManager;
  42. @Inject
  43. public LogoServlet(IRuntimeManager runtimeManager) {
  44. this.runtimeManager = runtimeManager;
  45. }
  46. @Override
  47. protected long getLastModified(HttpServletRequest req) {
  48. File file = runtimeManager.getFileOrFolder(Keys.web.headerLogo, "${baseFolder}/logo.png");
  49. if (file.exists()) {
  50. return Math.max(lastModified, file.lastModified());
  51. } else {
  52. return lastModified;
  53. }
  54. }
  55. @Override
  56. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  57. throws ServletException, IOException {
  58. InputStream is = null;
  59. try {
  60. String contentType = null;
  61. File file = runtimeManager.getFileOrFolder(Keys.web.headerLogo, "${baseFolder}/logo.png");
  62. if (file.exists()) {
  63. // custom logo
  64. ServletContext context = request.getSession().getServletContext();
  65. contentType = context.getMimeType(file.getName());
  66. response.setContentLength((int) file.length());
  67. response.setDateHeader("Last-Modified", Math.max(lastModified, file.lastModified()));
  68. is = new FileInputStream(file);
  69. } else {
  70. // default logo
  71. response.setDateHeader("Last-Modified", lastModified);
  72. is = getClass().getResourceAsStream("/logo.png");
  73. }
  74. if (contentType == null) {
  75. contentType = "image/png";
  76. }
  77. response.setContentType(contentType);
  78. response.setHeader("Cache-Control", "public, max-age=3600, must-revalidate");
  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 {
  90. if (is != null) {
  91. is.close();
  92. }
  93. }
  94. }
  95. }