/*
* Copyright (C) 2009-2010, Google Inc. and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.http.server.glue;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jgit.http.server.HttpServerText;
/**
* Generic container filter to manage routing to different pipelines.
*
* Callers can create and configure a new processing pipeline by using one of
* the {@link #serve(String)} or {@link #serveRegex(String)} methods to allocate
* a binder for a particular URL pattern.
*
* Registered filters and servlets are initialized lazily, usually during the
* first request. Once initialized the bindings in this servlet cannot be
* modified without destroying the servlet and thereby destroying all registered
* filters and servlets.
*/
public class MetaFilter implements Filter {
static final String REGEX_GROUPS = "org.eclipse.jgit.http.server.glue.MetaServlet.serveRegex";
private ServletContext servletContext;
private final List bindings;
private volatile UrlPipeline[] pipelines;
/**
* Empty filter with no bindings.
*/
public MetaFilter() {
this.bindings = new ArrayList<>();
}
/**
* Construct a binding for a specific path.
*
* @param path
* pattern to match.
* @return binder for the passed path.
*/
public ServletBinder serve(String path) {
if (path.startsWith("*"))
return register(new SuffixPipeline.Binder(path.substring(1)));
throw new IllegalArgumentException(MessageFormat.format(HttpServerText
.get().pathNotSupported, path));
}
/**
* Construct a binding for a regular expression.
*
* @param expression
* the regular expression to pattern match the URL against.
* @return binder for the passed expression.
*/
public ServletBinder serveRegex(String expression) {
return register(new RegexPipeline.Binder(expression));
}
/**
* Construct a binding for a regular expression.
*
* @param pattern
* the regular expression to pattern match the URL against.
* @return binder for the passed expression.
*/
public ServletBinder serveRegex(Pattern pattern) {
return register(new RegexPipeline.Binder(pattern));
}
/** {@inheritDoc} */
@Override
public void init(FilterConfig filterConfig) throws ServletException {
servletContext = filterConfig.getServletContext();
}
/** {@inheritDoc} */
@Override
public void destroy() {
if (pipelines != null) {
Set