James Moger
2012-06-15 27506b7e75927e5dd09761f5eed058580b822771
Configurable robots.txt (issue-99)
1 files added
2 files modified
90 ■■■■■ changed files
distrib/gitblit.properties 7 ●●●●● patch | view | raw | blame | history
src/WEB-INF/web.xml 15 ●●●●● patch | view | raw | blame | history
src/com/gitblit/RobotsTxtServlet.java 68 ●●●●● patch | view | raw | blame | history
distrib/gitblit.properties
@@ -295,6 +295,13 @@
# SINCE 0.7.0 
web.enableRpcAdministration = false
# Full path to a configurable robots.txt file.  With this file you can control
# what parts of your Gitblit server respectable robots are allowed to traverse.
# http://googlewebmastercentral.blogspot.com/2008/06/improving-on-robots-exclusion-protocol.html
#
# SINCE 1.0.0
web.robots.txt =
# If true, the web ui layout will respond and adapt to the browser's dimensions.
# if false, the web ui will use a 940px fixed-width layout.
# http://twitter.github.com/bootstrap/scaffolding.html#responsive
src/WEB-INF/web.xml
@@ -99,6 +99,19 @@
    </servlet-mapping>    
    
    <!-- Robots.txt Servlet
         <url-pattern> MUST match:
            * Wicket Filter ignorePaths parameter -->
    <servlet>
        <servlet-name>RobotsTxtServlet</servlet-name>
        <servlet-class>com.gitblit.RobotsTxtServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>RobotsTxtServlet</servlet-name>
        <url-pattern>/robots.txt</url-pattern>
    </servlet-mapping>
    <!-- Git Access Restriction Filter
         <url-pattern> MUST match: 
            * GitServlet
@@ -202,7 +215,7 @@
                 * PagesFilter <url-pattern>
                 * PagesServlet <url-pattern>
                 * com.gitblit.Constants.PAGES_PATH -->
            <param-value>git/,feed/,zip/,federation/,rpc/,pages/</param-value>
            <param-value>git/,feed/,zip/,federation/,rpc/,pages/,robots.txt</param-value>
        </init-param>
    </filter>
    <filter-mapping>
src/com/gitblit/RobotsTxtServlet.java
New file
@@ -0,0 +1,68 @@
/*
 * Copyright 2012 gitblit.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitblit;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.gitblit.utils.FileUtils;
import com.gitblit.utils.StringUtils;
/**
 * Handles requests for robots.txt
 *
 * @author James Moger
 *
 */
public class RobotsTxtServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public RobotsTxtServlet() {
        super();
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException {
        processRequest(request, response);
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    protected void processRequest(javax.servlet.http.HttpServletRequest request,
            javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,
            java.io.IOException {
        String robotstxt = GitBlit.getString(Keys.web.robots.txt, null);
        String content = "";
        if (!StringUtils.isEmpty(robotstxt)) {
            File robotsfile = new File(robotstxt);
            if (robotsfile.exists()) {
                content = FileUtils.readContent(robotsfile, "\n");
            }
        }
        response.getWriter().append(content);
    }
}