James Moger
2012-09-10 fabe060d3a435f116128851f828e35c2af5fde67
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  */
16 package com.gitblit;
17
18 import java.io.File;
19 import java.io.IOException;
20
21 import javax.servlet.ServletException;
22 import javax.servlet.http.HttpServlet;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
26 import com.gitblit.utils.FileUtils;
27 import com.gitblit.utils.StringUtils;
28
29 /**
30  * Handles requests for robots.txt
31  * 
32  * @author James Moger
33  * 
34  */
35 public class RobotsTxtServlet extends HttpServlet {
36
37     private static final long serialVersionUID = 1L;
38
39     public RobotsTxtServlet() {
40         super();
41     }
42     
43     @Override
44     protected void doPost(HttpServletRequest request, HttpServletResponse response)
45             throws ServletException, java.io.IOException {
46         processRequest(request, response);
47     }
48
49     @Override
50     protected void doGet(HttpServletRequest request, HttpServletResponse response)
51             throws ServletException, IOException {
52         processRequest(request, response);
53     }
54
55     protected void processRequest(javax.servlet.http.HttpServletRequest request,
56             javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,
57             java.io.IOException {
58         String robotstxt = GitBlit.getString(Keys.web.robots.txt, null);        
59         String content = "";
60         if (!StringUtils.isEmpty(robotstxt)) {
61             File robotsfile = new File(robotstxt);
62             if (robotsfile.exists()) {
63                 content = FileUtils.readContent(robotsfile, "\n");
64             }
65         }
66         response.getWriter().append(content);
67     }
68 }