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 8.3KB

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