您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RepositoryFilter.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
  45. import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
  46. import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
  47. import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED;
  48. import static org.eclipse.jgit.http.server.GitSmartHttpTools.sendError;
  49. import static org.eclipse.jgit.http.server.ServletUtils.ATTRIBUTE_REPOSITORY;
  50. import java.io.IOException;
  51. import java.text.MessageFormat;
  52. import javax.servlet.Filter;
  53. import javax.servlet.FilterChain;
  54. import javax.servlet.FilterConfig;
  55. import javax.servlet.ServletContext;
  56. import javax.servlet.ServletException;
  57. import javax.servlet.ServletRequest;
  58. import javax.servlet.ServletResponse;
  59. import javax.servlet.http.HttpServletRequest;
  60. import javax.servlet.http.HttpServletResponse;
  61. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  62. import org.eclipse.jgit.lib.Repository;
  63. import org.eclipse.jgit.transport.ServiceMayNotContinueException;
  64. import org.eclipse.jgit.transport.resolver.RepositoryResolver;
  65. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  66. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  67. /**
  68. * Open a repository named by the path info through
  69. * {@link org.eclipse.jgit.transport.resolver.RepositoryResolver}.
  70. * <p>
  71. * This filter assumes it is invoked by
  72. * {@link org.eclipse.jgit.http.server.GitServlet} and is likely to not work as
  73. * expected if called from any other class. This filter assumes the path info of
  74. * the current request is a repository name which can be used by the configured
  75. * {@link org.eclipse.jgit.transport.resolver.RepositoryResolver} to open a
  76. * {@link org.eclipse.jgit.lib.Repository} and attach it to the current request.
  77. * <p>
  78. * This filter sets request attribute
  79. * {@link org.eclipse.jgit.http.server.ServletUtils#ATTRIBUTE_REPOSITORY} when
  80. * it discovers the repository, and automatically closes and removes the
  81. * attribute when the request is complete.
  82. */
  83. public class RepositoryFilter implements Filter {
  84. private final RepositoryResolver<HttpServletRequest> resolver;
  85. private ServletContext context;
  86. /**
  87. * Create a new filter.
  88. *
  89. * @param resolver
  90. * the resolver which will be used to translate the URL name
  91. * component to the actual
  92. * {@link org.eclipse.jgit.lib.Repository} instance for the
  93. * current web request.
  94. */
  95. public RepositoryFilter(RepositoryResolver<HttpServletRequest> resolver) {
  96. this.resolver = resolver;
  97. }
  98. /** {@inheritDoc} */
  99. @Override
  100. public void init(FilterConfig config) throws ServletException {
  101. context = config.getServletContext();
  102. }
  103. /** {@inheritDoc} */
  104. @Override
  105. public void destroy() {
  106. context = null;
  107. }
  108. /** {@inheritDoc} */
  109. @Override
  110. public void doFilter(final ServletRequest request,
  111. final ServletResponse response, final FilterChain chain)
  112. throws IOException, ServletException {
  113. HttpServletRequest req = (HttpServletRequest) request;
  114. HttpServletResponse res = (HttpServletResponse) response;
  115. if (request.getAttribute(ATTRIBUTE_REPOSITORY) != null) {
  116. context.log(MessageFormat.format(HttpServerText.get().internalServerErrorRequestAttributeWasAlreadySet
  117. , ATTRIBUTE_REPOSITORY
  118. , getClass().getName()));
  119. sendError(req, res, SC_INTERNAL_SERVER_ERROR);
  120. return;
  121. }
  122. String name = req.getPathInfo();
  123. while (name != null && 0 < name.length() && name.charAt(0) == '/')
  124. name = name.substring(1);
  125. if (name == null || name.length() == 0) {
  126. sendError(req, res, SC_NOT_FOUND);
  127. return;
  128. }
  129. try (Repository db = resolver.open(req, name)) {
  130. request.setAttribute(ATTRIBUTE_REPOSITORY, db);
  131. chain.doFilter(request, response);
  132. } catch (RepositoryNotFoundException e) {
  133. sendError(req, res, SC_NOT_FOUND);
  134. return;
  135. } catch (ServiceNotEnabledException e) {
  136. sendError(req, res, SC_FORBIDDEN, e.getMessage());
  137. return;
  138. } catch (ServiceNotAuthorizedException e) {
  139. res.sendError(SC_UNAUTHORIZED, e.getMessage());
  140. return;
  141. } catch (ServiceMayNotContinueException e) {
  142. sendError(req, res, e.getStatusCode(), e.getMessage());
  143. return;
  144. } finally {
  145. request.removeAttribute(ATTRIBUTE_REPOSITORY);
  146. }
  147. }
  148. }