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.

GitServlet.java 5.6KB

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