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

RobotsTxtServlet.java 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright 2012 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.IOException;
  19. import com.google.inject.Inject;
  20. import com.google.inject.Singleton;
  21. import javax.servlet.ServletException;
  22. import javax.servlet.http.HttpServlet;
  23. import javax.servlet.http.HttpServletRequest;
  24. import javax.servlet.http.HttpServletResponse;
  25. import com.gitblit.Keys;
  26. import com.gitblit.manager.IRuntimeManager;
  27. import com.gitblit.utils.FileUtils;
  28. /**
  29. * Handles requests for robots.txt
  30. *
  31. * @author James Moger
  32. *
  33. */
  34. @Singleton
  35. public class RobotsTxtServlet extends HttpServlet {
  36. private static final long serialVersionUID = 1L;
  37. private IRuntimeManager runtimeManager;
  38. @Inject
  39. public RobotsTxtServlet(IRuntimeManager runtimeManager) {
  40. this.runtimeManager = runtimeManager;
  41. }
  42. @Override
  43. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  44. throws ServletException, java.io.IOException {
  45. processRequest(request, response);
  46. }
  47. @Override
  48. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  49. throws ServletException, IOException {
  50. processRequest(request, response);
  51. }
  52. protected void processRequest(javax.servlet.http.HttpServletRequest request,
  53. javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,
  54. java.io.IOException {
  55. File file = runtimeManager.getFileOrFolder(Keys.web.robots.txt, null);
  56. String content = "";
  57. if (file.exists()) {
  58. content = FileUtils.readContent(file, "\n");
  59. }
  60. response.getWriter().append(content);
  61. }
  62. }