You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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