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.

UrlPipeline.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.glue;
  44. import java.io.IOException;
  45. import java.util.Enumeration;
  46. import java.util.NoSuchElementException;
  47. import java.util.Set;
  48. import javax.servlet.Filter;
  49. import javax.servlet.FilterChain;
  50. import javax.servlet.ServletConfig;
  51. import javax.servlet.ServletContext;
  52. import javax.servlet.ServletException;
  53. import javax.servlet.ServletRequest;
  54. import javax.servlet.ServletResponse;
  55. import javax.servlet.http.HttpServlet;
  56. import javax.servlet.http.HttpServletRequest;
  57. import javax.servlet.http.HttpServletResponse;
  58. /**
  59. * Encapsulates the entire serving stack for a single URL.
  60. * <p>
  61. * Subclasses provide the implementation of {@link #match(HttpServletRequest)},
  62. * which is called by {@link MetaServlet} in registration order to determine the
  63. * pipeline that will be used to handle a request.
  64. * <p>
  65. * The very bottom of each pipeline is a single {@link HttpServlet} that will
  66. * handle producing the response for this pipeline's URL. {@link Filter}s may
  67. * also be registered and applied around the servlet's processing, to manage
  68. * request attributes, set standard response headers, or completely override the
  69. * response generation.
  70. */
  71. abstract class UrlPipeline {
  72. /** Filters to apply around {@link #servlet}; may be empty but never null. */
  73. private final Filter[] filters;
  74. /** Instance that must generate the response; never null. */
  75. private final HttpServlet servlet;
  76. UrlPipeline(Filter[] filters, HttpServlet servlet) {
  77. this.filters = filters;
  78. this.servlet = servlet;
  79. }
  80. /**
  81. * Initialize all contained filters and servlets.
  82. *
  83. * @param context
  84. * the servlet container context our {@link MetaServlet} is
  85. * running within.
  86. * @param inited
  87. * <i>(input/output)</i> the set of filters and servlets which
  88. * have already been initialized within the container context. If
  89. * those same instances appear in this pipeline they are not
  90. * initialized a second time. Filters and servlets that are first
  91. * initialized by this pipeline will be added to this set.
  92. * @throws ServletException
  93. * a filter or servlet is unable to initialize.
  94. */
  95. void init(ServletContext context, Set<Object> inited)
  96. throws ServletException {
  97. for (Filter ref : filters)
  98. initFilter(ref, context, inited);
  99. initServlet(servlet, context, inited);
  100. }
  101. private static void initFilter(final Filter ref,
  102. final ServletContext context, final Set<Object> inited)
  103. throws ServletException {
  104. if (!inited.contains(ref)) {
  105. ref.init(new NoParameterFilterConfig(ref.getClass().getName(),
  106. context));
  107. inited.add(ref);
  108. }
  109. }
  110. private static void initServlet(final HttpServlet ref,
  111. final ServletContext context, final Set<Object> inited)
  112. throws ServletException {
  113. if (!inited.contains(ref)) {
  114. ref.init(new ServletConfig() {
  115. @Override
  116. public String getInitParameter(String name) {
  117. return null;
  118. }
  119. @Override
  120. public Enumeration<String> getInitParameterNames() {
  121. return new Enumeration<String>() {
  122. @Override
  123. public boolean hasMoreElements() {
  124. return false;
  125. }
  126. @Override
  127. public String nextElement() {
  128. throw new NoSuchElementException();
  129. }
  130. };
  131. }
  132. @Override
  133. public ServletContext getServletContext() {
  134. return context;
  135. }
  136. @Override
  137. public String getServletName() {
  138. return ref.getClass().getName();
  139. }
  140. });
  141. inited.add(ref);
  142. }
  143. }
  144. /**
  145. * Destroy all contained filters and servlets.
  146. *
  147. * @param destroyed
  148. * <i>(input/output)</i> the set of filters and servlets which
  149. * have already been destroyed within the container context. If
  150. * those same instances appear in this pipeline they are not
  151. * destroyed a second time. Filters and servlets that are first
  152. * destroyed by this pipeline will be added to this set.
  153. */
  154. void destroy(Set<Object> destroyed) {
  155. for (Filter ref : filters)
  156. destroyFilter(ref, destroyed);
  157. destroyServlet(servlet, destroyed);
  158. }
  159. private static void destroyFilter(Filter ref, Set<Object> destroyed) {
  160. if (!destroyed.contains(ref)) {
  161. ref.destroy();
  162. destroyed.add(ref);
  163. }
  164. }
  165. private static void destroyServlet(HttpServlet ref, Set<Object> destroyed) {
  166. if (!destroyed.contains(ref)) {
  167. ref.destroy();
  168. destroyed.add(ref);
  169. }
  170. }
  171. /**
  172. * Determine if this pipeline handles the request's URL.
  173. * <p>
  174. * This method should match on the request's {@code getPathInfo()} method,
  175. * as {@link MetaServlet} passes the request along as-is to each pipeline's
  176. * match method.
  177. *
  178. * @param req
  179. * current HTTP request being considered by {@link MetaServlet}.
  180. * @return {@code true} if this pipeline is configured to handle the
  181. * request; {@code false} otherwise.
  182. */
  183. abstract boolean match(HttpServletRequest req);
  184. /**
  185. * Execute the filters and the servlet on the request.
  186. * <p>
  187. * Invoked by {@link MetaServlet} once {@link #match(HttpServletRequest)}
  188. * has determined this pipeline is the correct pipeline to handle the
  189. * current request.
  190. *
  191. * @param req
  192. * current HTTP request.
  193. * @param rsp
  194. * current HTTP response.
  195. * @throws ServletException
  196. * request cannot be completed.
  197. * @throws IOException
  198. * IO error prevents the request from being completed.
  199. */
  200. void service(HttpServletRequest req, HttpServletResponse rsp)
  201. throws ServletException, IOException {
  202. if (0 < filters.length)
  203. new Chain(filters, servlet).doFilter(req, rsp);
  204. else
  205. servlet.service(req, rsp);
  206. }
  207. private static class Chain implements FilterChain {
  208. private final Filter[] filters;
  209. private final HttpServlet servlet;
  210. private int filterIdx;
  211. Chain(Filter[] filters, HttpServlet servlet) {
  212. this.filters = filters;
  213. this.servlet = servlet;
  214. }
  215. @Override
  216. public void doFilter(ServletRequest req, ServletResponse rsp)
  217. throws IOException, ServletException {
  218. if (filterIdx < filters.length)
  219. filters[filterIdx++].doFilter(req, rsp, this);
  220. else
  221. servlet.service(req, rsp);
  222. }
  223. }
  224. }