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.io.IOException;
14  
15  import javax.servlet.Filter;
16  import javax.servlet.ServletException;
17  import javax.servlet.http.HttpServlet;
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  
21  /**
22   * Selects requests by matching the suffix of the URI.
23   * <p>
24   * The suffix string is literally matched against the path info of the servlet
25   * request, as this class assumes it is invoked by {@link MetaServlet}. Suffix
26   * strings may include path components. Examples include {@code /info/refs}, or
27   * just simple extension matches like {@code .txt}.
28   * <p>
29   * When dispatching to the rest of the pipeline the HttpServletRequest is
30   * modified so that {@code getPathInfo()} does not contain the suffix that
31   * caused this pipeline to be selected.
32   */
33  class SuffixPipeline extends UrlPipeline {
34  	static class Binder extends ServletBinderImpl {
35  		private final String suffix;
36  
37  		Binder(String suffix) {
38  			this.suffix = suffix;
39  		}
40  
41  		@Override
42  		UrlPipeline create() {
43  			return new SuffixPipeline(suffix, getFilters(), getServlet());
44  		}
45  	}
46  
47  	private final String suffix;
48  
49  	private final int suffixLen;
50  
51  	SuffixPipeline(final String suffix, final Filter[] filters,
52  			final HttpServlet servlet) {
53  		super(filters, servlet);
54  		this.suffix = suffix;
55  		this.suffixLen = suffix.length();
56  	}
57  
58  	@Override
59  	boolean match(HttpServletRequest req) {
60  		final String pathInfo = req.getPathInfo();
61  		return pathInfo != null && pathInfo.endsWith(suffix);
62  	}
63  
64  	@Override
65  	void service(HttpServletRequest req, HttpServletResponse rsp)
66  			throws ServletException, IOException {
67  		String curInfo = req.getPathInfo();
68  		String newPath = req.getServletPath() + curInfo;
69  		String newInfo = curInfo.substring(0, curInfo.length() - suffixLen);
70  		super.service(new WrappedRequest(req, newPath, newInfo), rsp);
71  	}
72  
73  	/** {@inheritDoc} */
74  	@Override
75  	public String toString() {
76  		return "Pipeline[ *" + suffix + " ]";
77  	}
78  }