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