View Javadoc
1   /*
2    * Copyright (C) 2009-2010, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.http.server;
12  
13  import java.util.Enumeration;
14  
15  import javax.servlet.Filter;
16  import javax.servlet.FilterConfig;
17  import javax.servlet.ServletConfig;
18  import javax.servlet.ServletContext;
19  import javax.servlet.ServletException;
20  import javax.servlet.http.HttpServletRequest;
21  
22  import org.eclipse.jgit.http.server.glue.MetaServlet;
23  import org.eclipse.jgit.http.server.resolver.AsIsFileService;
24  import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
25  import org.eclipse.jgit.transport.resolver.RepositoryResolver;
26  import org.eclipse.jgit.transport.resolver.UploadPackFactory;
27  
28  /**
29   * Handles Git repository access over HTTP.
30   * <p>
31   * Applications embedding this servlet should map a directory path within the
32   * application to this servlet, for example:
33   *
34   * <pre>
35   *   &lt;servlet&gt;
36   *     &lt;servlet-name&gt;GitServlet&lt;/servlet-name&gt;
37   *     &lt;servlet-class&gt;org.eclipse.jgit.http.server.GitServlet&lt;/servlet-class&gt;
38   *     &lt;init-param&gt;
39   *       &lt;param-name&gt;base-path&lt;/param-name&gt;
40   *       &lt;param-value&gt;/var/srv/git&lt;/param-value&gt;
41   *     &lt;/init-param&gt;
42   *     &lt;init-param&gt;
43   *       &lt;param-name&gt;export-all&lt;/param-name&gt;
44   *       &lt;param-value&gt;0&lt;/param-value&gt;
45   *     &lt;/init-param&gt;
46   * &lt;/servlet&gt;
47   *   &lt;servlet-mapping&gt;
48   *     &lt;servlet-name&gt;GitServlet&lt;/servlet-name&gt;
49   *     &lt;url-pattern&gt;/git/*&lt;/url-pattern&gt;
50   *   &lt;/servlet-mapping&gt;
51   * </pre>
52   *
53   * <p>
54   * Applications may wish to add additional repository action URLs to this
55   * servlet by taking advantage of its extension from
56   * {@link org.eclipse.jgit.http.server.glue.MetaServlet}. Callers may register
57   * their own URL suffix translations through {@link #serve(String)}, or their
58   * regex translations through {@link #serveRegex(String)}. Each translation
59   * should contain a complete filter pipeline which ends with the HttpServlet
60   * that should handle the requested action.
61   */
62  public class GitServlet extends MetaServlet {
63  	private static final long serialVersionUID = 1L;
64  
65  	private final GitFilter gitFilter;
66  
67  	/**
68  	 * New servlet that will load its base directory from {@code web.xml}.
69  	 * <p>
70  	 * The required parameter {@code base-path} must be configured to point to
71  	 * the local filesystem directory where all served Git repositories reside.
72  	 */
73  	public GitServlet() {
74  		super(new GitFilter());
75  		gitFilter = (GitFilter) getDelegateFilter();
76  	}
77  
78  	/**
79  	 * New servlet configured with a specific resolver.
80  	 *
81  	 * @param resolver
82  	 *            the resolver to use when matching URL to Git repository. If
83  	 *            null the {@code base-path} parameter will be looked for in the
84  	 *            parameter table during init, which usually comes from the
85  	 *            {@code web.xml} file of the web application.
86  	 */
87  	public void setRepositoryResolver(RepositoryResolver<HttpServletRequest> resolver) {
88  		gitFilter.setRepositoryResolver(resolver);
89  	}
90  
91  	/**
92  	 * Set AsIsFileService
93  	 *
94  	 * @param f
95  	 *            the filter to validate direct access to repository files
96  	 *            through a dumb client. If {@code null} then dumb client
97  	 *            support is completely disabled.
98  	 */
99  	public void setAsIsFileService(AsIsFileService f) {
100 		gitFilter.setAsIsFileService(f);
101 	}
102 
103 	/**
104 	 * Set upload-pack factory
105 	 *
106 	 * @param f
107 	 *            the factory to construct and configure an
108 	 *            {@link org.eclipse.jgit.transport.UploadPack} session when a
109 	 *            fetch or clone is requested by a client.
110 	 */
111 	public void setUploadPackFactory(UploadPackFactory<HttpServletRequest> f) {
112 		gitFilter.setUploadPackFactory(f);
113 	}
114 
115 	/**
116 	 * Add upload-pack filter
117 	 *
118 	 * @param filter
119 	 *            filter to apply before any of the UploadPack operations. The
120 	 *            UploadPack instance is available in the request attribute
121 	 *            {@link org.eclipse.jgit.http.server.ServletUtils#ATTRIBUTE_HANDLER}.
122 	 */
123 	public void addUploadPackFilter(Filter filter) {
124 		gitFilter.addUploadPackFilter(filter);
125 	}
126 
127 	/**
128 	 * Set receive-pack factory
129 	 *
130 	 * @param f
131 	 *            the factory to construct and configure a
132 	 *            {@link org.eclipse.jgit.transport.ReceivePack} session when a
133 	 *            push is requested by a client.
134 	 */
135 	public void setReceivePackFactory(ReceivePackFactory<HttpServletRequest> f) {
136 		gitFilter.setReceivePackFactory(f);
137 	}
138 
139 	/**
140 	 * Add receive-pack filter
141 	 *
142 	 * @param filter
143 	 *            filter to apply before any of the ReceivePack operations. The
144 	 *            ReceivePack instance is available in the request attribute
145 	 *            {@link org.eclipse.jgit.http.server.ServletUtils#ATTRIBUTE_HANDLER}.
146 	 */
147 	public void addReceivePackFilter(Filter filter) {
148 		gitFilter.addReceivePackFilter(filter);
149 	}
150 
151 	/** {@inheritDoc} */
152 	@Override
153 	public void init(ServletConfig config) throws ServletException {
154 		gitFilter.init(new FilterConfig() {
155 			@Override
156 			public String getFilterName() {
157 				return gitFilter.getClass().getName();
158 			}
159 
160 			@Override
161 			public String getInitParameter(String name) {
162 				return config.getInitParameter(name);
163 			}
164 
165 			@Override
166 			public Enumeration<String> getInitParameterNames() {
167 				return config.getInitParameterNames();
168 			}
169 
170 			@Override
171 			public ServletContext getServletContext() {
172 				return config.getServletContext();
173 			}
174 		});
175 	}
176 }