1 /* 2 * Copyright (C) 2009-2010, Google Inc. 3 * and other copyright owners as documented in the project's IP log. 4 * 5 * This program and the accompanying materials are made available 6 * under the terms of the Eclipse Distribution License v1.0 which 7 * accompanies this distribution, is reproduced below, and is 8 * available at http://www.eclipse.org/org/documents/edl-v10.php 9 * 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or 13 * without modification, are permitted provided that the following 14 * conditions are met: 15 * 16 * - Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * - Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials provided 22 * with the distribution. 23 * 24 * - Neither the name of the Eclipse Foundation, Inc. nor the 25 * names of its contributors may be used to endorse or promote 26 * products derived from this software without specific prior 27 * written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 42 */ 43 44 package org.eclipse.jgit.http.server; 45 46 import java.util.Enumeration; 47 48 import javax.servlet.Filter; 49 import javax.servlet.FilterConfig; 50 import javax.servlet.ServletConfig; 51 import javax.servlet.ServletContext; 52 import javax.servlet.ServletException; 53 import javax.servlet.http.HttpServletRequest; 54 55 import org.eclipse.jgit.http.server.glue.MetaServlet; 56 import org.eclipse.jgit.http.server.resolver.AsIsFileService; 57 import org.eclipse.jgit.transport.ReceivePack; 58 import org.eclipse.jgit.transport.UploadPack; 59 import org.eclipse.jgit.transport.resolver.ReceivePackFactory; 60 import org.eclipse.jgit.transport.resolver.RepositoryResolver; 61 import org.eclipse.jgit.transport.resolver.UploadPackFactory; 62 63 /** 64 * Handles Git repository access over HTTP. 65 * <p> 66 * Applications embedding this servlet should map a directory path within the 67 * application to this servlet, for example: 68 * 69 * <pre> 70 * <servlet> 71 * <servlet-name>GitServlet</servlet-name> 72 * <servlet-class>org.eclipse.jgit.http.server.GitServlet</servlet-class> 73 * <init-param> 74 * <param-name>base-path</param-name> 75 * <param-value>/var/srv/git</param-value> 76 * </init-param> 77 * <init-param> 78 * <param-name>export-all</param-name> 79 * <param-value>0</param-value> 80 * </init-param> 81 * </servlet> 82 * <servlet-mapping> 83 * <servlet-name>GitServlet</servlet-name> 84 * <url-pattern>/git/*</url-pattern> 85 * </servlet-mapping> 86 * </pre> 87 * 88 * <p> 89 * Applications may wish to add additional repository action URLs to this 90 * servlet by taking advantage of its extension from {@link MetaServlet}. 91 * Callers may register their own URL suffix translations through 92 * {@link #serve(String)}, or their regex translations through 93 * {@link #serveRegex(String)}. Each translation should contain a complete 94 * filter pipeline which ends with the HttpServlet that should handle the 95 * requested action. 96 */ 97 public class GitServlet extends MetaServlet { 98 private static final long serialVersionUID = 1L; 99 100 private final GitFilter gitFilter; 101 102 /** 103 * New servlet that will load its base directory from {@code web.xml}. 104 * <p> 105 * The required parameter {@code base-path} must be configured to point to 106 * the local filesystem directory where all served Git repositories reside. 107 */ 108 public GitServlet() { 109 super(new GitFilter()); 110 gitFilter = (GitFilter) getDelegateFilter(); 111 } 112 113 /** 114 * New servlet configured with a specific resolver. 115 * 116 * @param resolver 117 * the resolver to use when matching URL to Git repository. If 118 * null the {@code base-path} parameter will be looked for in the 119 * parameter table during init, which usually comes from the 120 * {@code web.xml} file of the web application. 121 */ 122 public void setRepositoryResolver(RepositoryResolver<HttpServletRequest> resolver) { 123 gitFilter.setRepositoryResolver(resolver); 124 } 125 126 /** 127 * @param f 128 * the filter to validate direct access to repository files 129 * through a dumb client. If {@code null} then dumb client 130 * support is completely disabled. 131 */ 132 public void setAsIsFileService(AsIsFileService f) { 133 gitFilter.setAsIsFileService(f); 134 } 135 136 /** 137 * @param f 138 * the factory to construct and configure an {@link UploadPack} 139 * session when a fetch or clone is requested by a client. 140 */ 141 public void setUploadPackFactory(UploadPackFactory<HttpServletRequest> f) { 142 gitFilter.setUploadPackFactory(f); 143 } 144 145 /** 146 * @param filter 147 * filter to apply before any of the UploadPack operations. The 148 * UploadPack instance is available in the request attribute 149 * {@link ServletUtils#ATTRIBUTE_HANDLER}. 150 */ 151 public void addUploadPackFilter(Filter filter) { 152 gitFilter.addUploadPackFilter(filter); 153 } 154 155 /** 156 * @param f 157 * the factory to construct and configure a {@link ReceivePack} 158 * session when a push is requested by a client. 159 */ 160 public void setReceivePackFactory(ReceivePackFactory<HttpServletRequest> f) { 161 gitFilter.setReceivePackFactory(f); 162 } 163 164 /** 165 * @param filter 166 * filter to apply before any of the ReceivePack operations. The 167 * ReceivePack instance is available in the request attribute 168 * {@link ServletUtils#ATTRIBUTE_HANDLER}. 169 */ 170 public void addReceivePackFilter(Filter filter) { 171 gitFilter.addReceivePackFilter(filter); 172 } 173 174 @Override 175 public void init(final ServletConfig config) throws ServletException { 176 gitFilter.init(new FilterConfig() { 177 public String getFilterName() { 178 return gitFilter.getClass().getName(); 179 } 180 181 public String getInitParameter(String name) { 182 return config.getInitParameter(name); 183 } 184 185 public Enumeration<String> getInitParameterNames() { 186 return config.getInitParameterNames(); 187 } 188 189 public ServletContext getServletContext() { 190 return config.getServletContext(); 191 } 192 }); 193 } 194 }