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.

MetaServlet.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.glue;
  11. import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
  12. import java.io.IOException;
  13. import javax.servlet.ServletConfig;
  14. import javax.servlet.ServletContext;
  15. import javax.servlet.ServletException;
  16. import javax.servlet.ServletRequest;
  17. import javax.servlet.ServletResponse;
  18. import javax.servlet.http.HttpServlet;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. /**
  22. * Generic container servlet to manage routing to different pipelines.
  23. * <p>
  24. * Callers can create and configure a new processing pipeline by using one of
  25. * the {@link #serve(String)} or {@link #serveRegex(String)} methods to allocate
  26. * a binder for a particular URL pattern.
  27. * <p>
  28. * Registered filters and servlets are initialized lazily, usually during the
  29. * first request. Once initialized the bindings in this servlet cannot be
  30. * modified without destroying the servlet and thereby destroying all registered
  31. * filters and servlets.
  32. */
  33. public class MetaServlet extends HttpServlet {
  34. private static final long serialVersionUID = 1L;
  35. private final MetaFilter filter;
  36. /**
  37. * Empty servlet with no bindings.
  38. */
  39. public MetaServlet() {
  40. this(new MetaFilter());
  41. }
  42. /**
  43. * Initialize a servlet wrapping a filter.
  44. *
  45. * @param delegateFilter
  46. * the filter being wrapped by the servlet.
  47. */
  48. protected MetaServlet(MetaFilter delegateFilter) {
  49. filter = delegateFilter;
  50. }
  51. /**
  52. * Get delegate filter
  53. *
  54. * @return filter this servlet delegates all routing logic to.
  55. */
  56. protected MetaFilter getDelegateFilter() {
  57. return filter;
  58. }
  59. /**
  60. * Construct a binding for a specific path.
  61. *
  62. * @param path
  63. * pattern to match.
  64. * @return binder for the passed path.
  65. */
  66. public ServletBinder serve(String path) {
  67. return filter.serve(path);
  68. }
  69. /**
  70. * Construct a binding for a regular expression.
  71. *
  72. * @param expression
  73. * the regular expression to pattern match the URL against.
  74. * @return binder for the passed expression.
  75. */
  76. public ServletBinder serveRegex(String expression) {
  77. return filter.serveRegex(expression);
  78. }
  79. /** {@inheritDoc} */
  80. @Override
  81. public void init(ServletConfig config) throws ServletException {
  82. String name = filter.getClass().getName();
  83. ServletContext ctx = config.getServletContext();
  84. filter.init(new NoParameterFilterConfig(name, ctx));
  85. }
  86. /** {@inheritDoc} */
  87. @Override
  88. public void destroy() {
  89. filter.destroy();
  90. }
  91. /** {@inheritDoc} */
  92. @Override
  93. protected void service(HttpServletRequest req, HttpServletResponse res)
  94. throws ServletException, IOException {
  95. filter.doFilter(req, res,
  96. (ServletRequest request, ServletResponse response) -> {
  97. ((HttpServletResponse) response).sendError(SC_NOT_FOUND);
  98. });
  99. }
  100. /**
  101. * Configure a newly created binder.
  102. *
  103. * @param b
  104. * the newly created binder.
  105. * @return binder for the caller, potentially after adding one or more
  106. * filters into the pipeline.
  107. */
  108. protected ServletBinder register(ServletBinder b) {
  109. return filter.register(b);
  110. }
  111. }