Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

LogoServlet.java 3.1KB

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