15e33a1deSShawn O. Pearce /* 2*5c5f7c6bSMatthias Sohn * Copyright (C) 2009-2010, Google Inc. and others 35e33a1deSShawn O. Pearce * 4*5c5f7c6bSMatthias Sohn * This program and the accompanying materials are made available under the 5*5c5f7c6bSMatthias Sohn * terms of the Eclipse Distribution License v. 1.0 which is available at 6*5c5f7c6bSMatthias Sohn * https://www.eclipse.org/org/documents/edl-v10.php. 75e33a1deSShawn O. Pearce * 8*5c5f7c6bSMatthias Sohn * SPDX-License-Identifier: BSD-3-Clause 95e33a1deSShawn O. Pearce */ 105e33a1deSShawn O. Pearce 115e33a1deSShawn O. Pearce package org.eclipse.jgit.http.server; 125e33a1deSShawn O. Pearce 13c3554ac5SShawn O. Pearce import java.util.Enumeration; 145e33a1deSShawn O. Pearce 15af3562f7SShawn O. Pearce import javax.servlet.Filter; 16c3554ac5SShawn O. Pearce import javax.servlet.FilterConfig; 175e33a1deSShawn O. Pearce import javax.servlet.ServletConfig; 18c3554ac5SShawn O. Pearce import javax.servlet.ServletContext; 195e33a1deSShawn O. Pearce import javax.servlet.ServletException; 201b7a5a29SShawn O. Pearce import javax.servlet.http.HttpServletRequest; 215e33a1deSShawn O. Pearce 225e33a1deSShawn O. Pearce import org.eclipse.jgit.http.server.glue.MetaServlet; 23af3562f7SShawn O. Pearce import org.eclipse.jgit.http.server.resolver.AsIsFileService; 241b7a5a29SShawn O. Pearce import org.eclipse.jgit.transport.resolver.ReceivePackFactory; 251b7a5a29SShawn O. Pearce import org.eclipse.jgit.transport.resolver.RepositoryResolver; 261b7a5a29SShawn O. Pearce import org.eclipse.jgit.transport.resolver.UploadPackFactory; 275e33a1deSShawn O. Pearce 285e33a1deSShawn O. Pearce /** 295e33a1deSShawn O. Pearce * Handles Git repository access over HTTP. 305e33a1deSShawn O. Pearce * <p> 315e33a1deSShawn O. Pearce * Applications embedding this servlet should map a directory path within the 325e33a1deSShawn O. Pearce * application to this servlet, for example: 335e33a1deSShawn O. Pearce * 345e33a1deSShawn O. Pearce * <pre> 355e33a1deSShawn O. Pearce * <servlet> 365e33a1deSShawn O. Pearce * <servlet-name>GitServlet</servlet-name> 375e33a1deSShawn O. Pearce * <servlet-class>org.eclipse.jgit.http.server.GitServlet</servlet-class> 385e33a1deSShawn O. Pearce * <init-param> 395e33a1deSShawn O. Pearce * <param-name>base-path</param-name> 405e33a1deSShawn O. Pearce * <param-value>/var/srv/git</param-value> 415e33a1deSShawn O. Pearce * </init-param> 425e33a1deSShawn O. Pearce * <init-param> 435e33a1deSShawn O. Pearce * <param-name>export-all</param-name> 445e33a1deSShawn O. Pearce * <param-value>0</param-value> 455e33a1deSShawn O. Pearce * </init-param> 465e33a1deSShawn O. Pearce * </servlet> 475e33a1deSShawn O. Pearce * <servlet-mapping> 485e33a1deSShawn O. Pearce * <servlet-name>GitServlet</servlet-name> 495e33a1deSShawn O. Pearce * <url-pattern>/git/*</url-pattern> 505e33a1deSShawn O. Pearce * </servlet-mapping> 515e33a1deSShawn O. Pearce * </pre> 525e33a1deSShawn O. Pearce * 535e33a1deSShawn O. Pearce * <p> 545e33a1deSShawn O. Pearce * Applications may wish to add additional repository action URLs to this 553b00041cSMatthias Sohn * servlet by taking advantage of its extension from 563b00041cSMatthias Sohn * {@link org.eclipse.jgit.http.server.glue.MetaServlet}. Callers may register 573b00041cSMatthias Sohn * their own URL suffix translations through {@link #serve(String)}, or their 583b00041cSMatthias Sohn * regex translations through {@link #serveRegex(String)}. Each translation 593b00041cSMatthias Sohn * should contain a complete filter pipeline which ends with the HttpServlet 603b00041cSMatthias Sohn * that should handle the requested action. 615e33a1deSShawn O. Pearce */ 625e33a1deSShawn O. Pearce public class GitServlet extends MetaServlet { 635e33a1deSShawn O. Pearce private static final long serialVersionUID = 1L; 645e33a1deSShawn O. Pearce 65c3554ac5SShawn O. Pearce private final GitFilter gitFilter; 66af3562f7SShawn O. Pearce 675e33a1deSShawn O. Pearce /** 685e33a1deSShawn O. Pearce * New servlet that will load its base directory from {@code web.xml}. 695e33a1deSShawn O. Pearce * <p> 705e33a1deSShawn O. Pearce * The required parameter {@code base-path} must be configured to point to 715e33a1deSShawn O. Pearce * the local filesystem directory where all served Git repositories reside. 725e33a1deSShawn O. Pearce */ GitServlet()735e33a1deSShawn O. Pearce public GitServlet() { 74c3554ac5SShawn O. Pearce super(new GitFilter()); 75c3554ac5SShawn O. Pearce gitFilter = (GitFilter) getDelegateFilter(); 765e33a1deSShawn O. Pearce } 775e33a1deSShawn O. Pearce 785e33a1deSShawn O. Pearce /** 795e33a1deSShawn O. Pearce * New servlet configured with a specific resolver. 805e33a1deSShawn O. Pearce * 815e33a1deSShawn O. Pearce * @param resolver 825e33a1deSShawn O. Pearce * the resolver to use when matching URL to Git repository. If 835e33a1deSShawn O. Pearce * null the {@code base-path} parameter will be looked for in the 845e33a1deSShawn O. Pearce * parameter table during init, which usually comes from the 855e33a1deSShawn O. Pearce * {@code web.xml} file of the web application. 865e33a1deSShawn O. Pearce */ setRepositoryResolver(RepositoryResolver<HttpServletRequest> resolver)871b7a5a29SShawn O. Pearce public void setRepositoryResolver(RepositoryResolver<HttpServletRequest> resolver) { 88c3554ac5SShawn O. Pearce gitFilter.setRepositoryResolver(resolver); 895e33a1deSShawn O. Pearce } 905e33a1deSShawn O. Pearce 915e33a1deSShawn O. Pearce /** 923b00041cSMatthias Sohn * Set AsIsFileService 933b00041cSMatthias Sohn * 945e33a1deSShawn O. Pearce * @param f 955e33a1deSShawn O. Pearce * the filter to validate direct access to repository files 965e33a1deSShawn O. Pearce * through a dumb client. If {@code null} then dumb client 975e33a1deSShawn O. Pearce * support is completely disabled. 985e33a1deSShawn O. Pearce */ setAsIsFileService(AsIsFileService f)995e33a1deSShawn O. Pearce public void setAsIsFileService(AsIsFileService f) { 100c3554ac5SShawn O. Pearce gitFilter.setAsIsFileService(f); 1015e33a1deSShawn O. Pearce } 1025e33a1deSShawn O. Pearce 10381fea92eSShawn O. Pearce /** 1043b00041cSMatthias Sohn * Set upload-pack factory 1053b00041cSMatthias Sohn * 10681fea92eSShawn O. Pearce * @param f 1073b00041cSMatthias Sohn * the factory to construct and configure an 1083b00041cSMatthias Sohn * {@link org.eclipse.jgit.transport.UploadPack} session when a 1093b00041cSMatthias Sohn * fetch or clone is requested by a client. 1102e521446SShawn O. Pearce */ setUploadPackFactory(UploadPackFactory<HttpServletRequest> f)1111b7a5a29SShawn O. Pearce public void setUploadPackFactory(UploadPackFactory<HttpServletRequest> f) { 112c3554ac5SShawn O. Pearce gitFilter.setUploadPackFactory(f); 1132e521446SShawn O. Pearce } 1142e521446SShawn O. Pearce 1152e521446SShawn O. Pearce /** 1163b00041cSMatthias Sohn * Add upload-pack filter 1173b00041cSMatthias Sohn * 118af3562f7SShawn O. Pearce * @param filter 119af3562f7SShawn O. Pearce * filter to apply before any of the UploadPack operations. The 120af3562f7SShawn O. Pearce * UploadPack instance is available in the request attribute 1213b00041cSMatthias Sohn * {@link org.eclipse.jgit.http.server.ServletUtils#ATTRIBUTE_HANDLER}. 122af3562f7SShawn O. Pearce */ addUploadPackFilter(Filter filter)123af3562f7SShawn O. Pearce public void addUploadPackFilter(Filter filter) { 124c3554ac5SShawn O. Pearce gitFilter.addUploadPackFilter(filter); 125af3562f7SShawn O. Pearce } 126af3562f7SShawn O. Pearce 127af3562f7SShawn O. Pearce /** 1283b00041cSMatthias Sohn * Set receive-pack factory 1293b00041cSMatthias Sohn * 1302e521446SShawn O. Pearce * @param f 1313b00041cSMatthias Sohn * the factory to construct and configure a 1323b00041cSMatthias Sohn * {@link org.eclipse.jgit.transport.ReceivePack} session when a 1333b00041cSMatthias Sohn * push is requested by a client. 13481fea92eSShawn O. Pearce */ setReceivePackFactory(ReceivePackFactory<HttpServletRequest> f)1351b7a5a29SShawn O. Pearce public void setReceivePackFactory(ReceivePackFactory<HttpServletRequest> f) { 136c3554ac5SShawn O. Pearce gitFilter.setReceivePackFactory(f); 13781fea92eSShawn O. Pearce } 13881fea92eSShawn O. Pearce 139af3562f7SShawn O. Pearce /** 1403b00041cSMatthias Sohn * Add receive-pack filter 1413b00041cSMatthias Sohn * 142af3562f7SShawn O. Pearce * @param filter 143af3562f7SShawn O. Pearce * filter to apply before any of the ReceivePack operations. The 144af3562f7SShawn O. Pearce * ReceivePack instance is available in the request attribute 1453b00041cSMatthias Sohn * {@link org.eclipse.jgit.http.server.ServletUtils#ATTRIBUTE_HANDLER}. 146af3562f7SShawn O. Pearce */ addReceivePackFilter(Filter filter)147af3562f7SShawn O. Pearce public void addReceivePackFilter(Filter filter) { 148c3554ac5SShawn O. Pearce gitFilter.addReceivePackFilter(filter); 1495e33a1deSShawn O. Pearce } 1505e33a1deSShawn O. Pearce 1513b00041cSMatthias Sohn /** {@inheritDoc} */ 1525e33a1deSShawn O. Pearce @Override init(ServletConfig config)1536d370d83SHan-Wen Nienhuys public void init(ServletConfig config) throws ServletException { 154c3554ac5SShawn O. Pearce gitFilter.init(new FilterConfig() { 1557ac182f4SDavid Pursehouse @Override 156c3554ac5SShawn O. Pearce public String getFilterName() { 157c3554ac5SShawn O. Pearce return gitFilter.getClass().getName(); 1585e33a1deSShawn O. Pearce } 1595e33a1deSShawn O. Pearce 1607ac182f4SDavid Pursehouse @Override 161c3554ac5SShawn O. Pearce public String getInitParameter(String name) { 162c3554ac5SShawn O. Pearce return config.getInitParameter(name); 1632e521446SShawn O. Pearce } 1642e521446SShawn O. Pearce 1657ac182f4SDavid Pursehouse @Override 16604bc9b3dSRobin Rosenberg public Enumeration<String> getInitParameterNames() { 167c3554ac5SShawn O. Pearce return config.getInitParameterNames(); 16881fea92eSShawn O. Pearce } 16981fea92eSShawn O. Pearce 1707ac182f4SDavid Pursehouse @Override 171c3554ac5SShawn O. Pearce public ServletContext getServletContext() { 172c3554ac5SShawn O. Pearce return config.getServletContext(); 1732e521446SShawn O. Pearce } 174c3554ac5SShawn O. Pearce }); 1755e33a1deSShawn O. Pearce } 1765e33a1deSShawn O. Pearce } 177