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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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(final Filter[] filters, final 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(final ServletContext context, final 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. public String getInitParameter(String name) {
  116. return null;
  117. }
  118. public Enumeration<String> getInitParameterNames() {
  119. return new Enumeration<String>() {
  120. public boolean hasMoreElements() {
  121. return false;
  122. }
  123. public String nextElement() {
  124. throw new NoSuchElementException();
  125. }
  126. };
  127. }
  128. public ServletContext getServletContext() {
  129. return context;
  130. }
  131. public String getServletName() {
  132. return ref.getClass().getName();
  133. }
  134. });
  135. inited.add(ref);
  136. }
  137. }
  138. /**
  139. * Destroy all contained filters and servlets.
  140. *
  141. * @param destroyed
  142. * <i>(input/output)</i> the set of filters and servlets which
  143. * have already been destroyed within the container context. If
  144. * those same instances appear in this pipeline they are not
  145. * destroyed a second time. Filters and servlets that are first
  146. * destroyed by this pipeline will be added to this set.
  147. */
  148. void destroy(final Set<Object> destroyed) {
  149. for (Filter ref : filters)
  150. destroyFilter(ref, destroyed);
  151. destroyServlet(servlet, destroyed);
  152. }
  153. private static void destroyFilter(Filter ref, Set<Object> destroyed) {
  154. if (!destroyed.contains(ref)) {
  155. ref.destroy();
  156. destroyed.add(ref);
  157. }
  158. }
  159. private static void destroyServlet(HttpServlet ref, Set<Object> destroyed) {
  160. if (!destroyed.contains(ref)) {
  161. ref.destroy();
  162. destroyed.add(ref);
  163. }
  164. }
  165. /**
  166. * Determine if this pipeline handles the request's URL.
  167. * <p>
  168. * This method should match on the request's {@code getPathInfo()} method,
  169. * as {@link MetaServlet} passes the request along as-is to each pipeline's
  170. * match method.
  171. *
  172. * @param req
  173. * current HTTP request being considered by {@link MetaServlet}.
  174. * @return {@code true} if this pipeline is configured to handle the
  175. * request; {@code false} otherwise.
  176. */
  177. abstract boolean match(HttpServletRequest req);
  178. /**
  179. * Execute the filters and the servlet on the request.
  180. * <p>
  181. * Invoked by {@link MetaServlet} once {@link #match(HttpServletRequest)}
  182. * has determined this pipeline is the correct pipeline to handle the
  183. * current request.
  184. *
  185. * @param req
  186. * current HTTP request.
  187. * @param rsp
  188. * current HTTP response.
  189. * @throws ServletException
  190. * request cannot be completed.
  191. * @throws IOException
  192. * IO error prevents the request from being completed.
  193. */
  194. void service(HttpServletRequest req, HttpServletResponse rsp)
  195. throws ServletException, IOException {
  196. if (0 < filters.length)
  197. new Chain(filters, servlet).doFilter(req, rsp);
  198. else
  199. servlet.service(req, rsp);
  200. }
  201. private static class Chain implements FilterChain {
  202. private final Filter[] filters;
  203. private final HttpServlet servlet;
  204. private int filterIdx;
  205. Chain(final Filter[] filters, final HttpServlet servlet) {
  206. this.filters = filters;
  207. this.servlet = servlet;
  208. }
  209. public void doFilter(ServletRequest req, ServletResponse rsp)
  210. throws IOException, ServletException {
  211. if (filterIdx < filters.length)
  212. filters[filterIdx++].doFilter(req, rsp, this);
  213. else
  214. servlet.service(req, rsp);
  215. }
  216. }
  217. }