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 java.util.ArrayList;
14  import java.util.List;
15  
16  import javax.servlet.Filter;
17  import javax.servlet.http.HttpServlet;
18  import javax.servlet.http.HttpServletResponse;
19  
20  import org.eclipse.jgit.http.server.HttpServerText;
21  
22  abstract class ServletBinderImpl implements ServletBinder {
23  	private final List<Filter> filters;
24  
25  	private HttpServlet httpServlet;
26  
27  	ServletBinderImpl() {
28  		this.filters = new ArrayList<>();
29  	}
30  
31  	/** {@inheritDoc} */
32  	@Override
33  	public ServletBinder through(Filter filter) {
34  		if (filter == null)
35  			throw new NullPointerException(HttpServerText.get().filterMustNotBeNull);
36  		filters.add(filter);
37  		return this;
38  	}
39  
40  	/** {@inheritDoc} */
41  	@Override
42  	public void with(HttpServlet servlet) {
43  		if (servlet == null)
44  			throw new NullPointerException(HttpServerText.get().servletMustNotBeNull);
45  		if (httpServlet != null)
46  			throw new IllegalStateException(HttpServerText.get().servletWasAlreadyBound);
47  		httpServlet = servlet;
48  	}
49  
50  	/**
51  	 * Get the servlet
52  	 *
53  	 * @return the configured servlet, or singleton returning 404 if none.
54  	 */
55  	protected HttpServlet getServlet() {
56  		if (httpServlet != null) {
57  			return httpServlet;
58  		}
59  		return new ErrorServlet(HttpServletResponse.SC_NOT_FOUND);
60  	}
61  
62  	/**
63  	 * Get filters
64  	 *
65  	 * @return the configured filters; zero-length array if none.
66  	 */
67  	protected Filter[] getFilters() {
68  		return filters.toArray(new Filter[0]);
69  	}
70  
71  	/** @return the pipeline that matches and executes this chain. */
72  	abstract UrlPipeline create();
73  }