View Javadoc
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.resolver.ReceivePackFactory;
58  import org.eclipse.jgit.transport.resolver.RepositoryResolver;
59  import org.eclipse.jgit.transport.resolver.UploadPackFactory;
60  
61  /**
62   * Handles Git repository access over HTTP.
63   * <p>
64   * Applications embedding this servlet should map a directory path within the
65   * application to this servlet, for example:
66   *
67   * <pre>
68   *   &lt;servlet&gt;
69   *     &lt;servlet-name&gt;GitServlet&lt;/servlet-name&gt;
70   *     &lt;servlet-class&gt;org.eclipse.jgit.http.server.GitServlet&lt;/servlet-class&gt;
71   *     &lt;init-param&gt;
72   *       &lt;param-name&gt;base-path&lt;/param-name&gt;
73   *       &lt;param-value&gt;/var/srv/git&lt;/param-value&gt;
74   *     &lt;/init-param&gt;
75   *     &lt;init-param&gt;
76   *       &lt;param-name&gt;export-all&lt;/param-name&gt;
77   *       &lt;param-value&gt;0&lt;/param-value&gt;
78   *     &lt;/init-param&gt;
79   * &lt;/servlet&gt;
80   *   &lt;servlet-mapping&gt;
81   *     &lt;servlet-name&gt;GitServlet&lt;/servlet-name&gt;
82   *     &lt;url-pattern&gt;/git/*&lt;/url-pattern&gt;
83   *   &lt;/servlet-mapping&gt;
84   * </pre>
85   *
86   * <p>
87   * Applications may wish to add additional repository action URLs to this
88   * servlet by taking advantage of its extension from
89   * {@link org.eclipse.jgit.http.server.glue.MetaServlet}. Callers may register
90   * their own URL suffix translations through {@link #serve(String)}, or their
91   * regex translations through {@link #serveRegex(String)}. Each translation
92   * should contain a complete filter pipeline which ends with the HttpServlet
93   * that should handle the requested action.
94   */
95  public class GitServlet extends MetaServlet {
96  	private static final long serialVersionUID = 1L;
97  
98  	private final GitFilter gitFilter;
99  
100 	/**
101 	 * New servlet that will load its base directory from {@code web.xml}.
102 	 * <p>
103 	 * The required parameter {@code base-path} must be configured to point to
104 	 * the local filesystem directory where all served Git repositories reside.
105 	 */
106 	public GitServlet() {
107 		super(new GitFilter());
108 		gitFilter = (GitFilter) getDelegateFilter();
109 	}
110 
111 	/**
112 	 * New servlet configured with a specific resolver.
113 	 *
114 	 * @param resolver
115 	 *            the resolver to use when matching URL to Git repository. If
116 	 *            null the {@code base-path} parameter will be looked for in the
117 	 *            parameter table during init, which usually comes from the
118 	 *            {@code web.xml} file of the web application.
119 	 */
120 	public void setRepositoryResolver(RepositoryResolver<HttpServletRequest> resolver) {
121 		gitFilter.setRepositoryResolver(resolver);
122 	}
123 
124 	/**
125 	 * Set AsIsFileService
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 	 * Set upload-pack factory
138 	 *
139 	 * @param f
140 	 *            the factory to construct and configure an
141 	 *            {@link org.eclipse.jgit.transport.UploadPack} session when a
142 	 *            fetch or clone is requested by a client.
143 	 */
144 	public void setUploadPackFactory(UploadPackFactory<HttpServletRequest> f) {
145 		gitFilter.setUploadPackFactory(f);
146 	}
147 
148 	/**
149 	 * Add upload-pack filter
150 	 *
151 	 * @param filter
152 	 *            filter to apply before any of the UploadPack operations. The
153 	 *            UploadPack instance is available in the request attribute
154 	 *            {@link org.eclipse.jgit.http.server.ServletUtils#ATTRIBUTE_HANDLER}.
155 	 */
156 	public void addUploadPackFilter(Filter filter) {
157 		gitFilter.addUploadPackFilter(filter);
158 	}
159 
160 	/**
161 	 * Set receive-pack factory
162 	 *
163 	 * @param f
164 	 *            the factory to construct and configure a
165 	 *            {@link org.eclipse.jgit.transport.ReceivePack} session when a
166 	 *            push is requested by a client.
167 	 */
168 	public void setReceivePackFactory(ReceivePackFactory<HttpServletRequest> f) {
169 		gitFilter.setReceivePackFactory(f);
170 	}
171 
172 	/**
173 	 * Add receive-pack filter
174 	 *
175 	 * @param filter
176 	 *            filter to apply before any of the ReceivePack operations. The
177 	 *            ReceivePack instance is available in the request attribute
178 	 *            {@link org.eclipse.jgit.http.server.ServletUtils#ATTRIBUTE_HANDLER}.
179 	 */
180 	public void addReceivePackFilter(Filter filter) {
181 		gitFilter.addReceivePackFilter(filter);
182 	}
183 
184 	/** {@inheritDoc} */
185 	@Override
186 	public void init(ServletConfig config) throws ServletException {
187 		gitFilter.init(new FilterConfig() {
188 			@Override
189 			public String getFilterName() {
190 				return gitFilter.getClass().getName();
191 			}
192 
193 			@Override
194 			public String getInitParameter(String name) {
195 				return config.getInitParameter(name);
196 			}
197 
198 			@Override
199 			public Enumeration<String> getInitParameterNames() {
200 				return config.getInitParameterNames();
201 			}
202 
203 			@Override
204 			public ServletContext getServletContext() {
205 				return config.getServletContext();
206 			}
207 		});
208 	}
209 }