James Moger
2015-11-22 ed552ba47c02779c270ffd62841d6d1048dade70
commit | author | age
27506b 1 /*
JM 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  */
7bf6e1 16 package com.gitblit.servlet;
27506b 17
JM 18 import java.io.File;
19 import java.io.IOException;
20
cdb2fe 21 import com.google.inject.Inject;
JM 22 import com.google.inject.Singleton;
27506b 23 import javax.servlet.ServletException;
1b34b0 24 import javax.servlet.http.HttpServlet;
27506b 25 import javax.servlet.http.HttpServletRequest;
JM 26 import javax.servlet.http.HttpServletResponse;
27
7bf6e1 28 import com.gitblit.Keys;
db4f6b 29 import com.gitblit.manager.IRuntimeManager;
27506b 30 import com.gitblit.utils.FileUtils;
JM 31
32 /**
33  * Handles requests for robots.txt
699e71 34  *
27506b 35  * @author James Moger
699e71 36  *
27506b 37  */
1b34b0 38 @Singleton
JM 39 public class RobotsTxtServlet extends HttpServlet {
27506b 40
JM 41     private static final long serialVersionUID = 1L;
42
65d5bb 43     private IRuntimeManager runtimeManager;
cacf8b 44
1b34b0 45     @Inject
JM 46     public RobotsTxtServlet(IRuntimeManager runtimeManager) {
47         this.runtimeManager = runtimeManager;
27506b 48     }
699e71 49
27506b 50     @Override
JM 51     protected void doPost(HttpServletRequest request, HttpServletResponse response)
52             throws ServletException, java.io.IOException {
53         processRequest(request, response);
54     }
55
56     @Override
57     protected void doGet(HttpServletRequest request, HttpServletResponse response)
58             throws ServletException, IOException {
59         processRequest(request, response);
60     }
61
62     protected void processRequest(javax.servlet.http.HttpServletRequest request,
63             javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,
64             java.io.IOException {
db4f6b 65         File file = runtimeManager.getFileOrFolder(Keys.web.robots.txt, null);
27506b 66         String content = "";
93d506 67         if (file.exists()) {
JM 68             content = FileUtils.readContent(file, "\n");
27506b 69         }
JM 70         response.getWriter().append(content);
71     }
72 }