SuffixPipeline.java

  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. package org.eclipse.jgit.http.server.glue;

  11. import java.io.IOException;

  12. import javax.servlet.Filter;
  13. import javax.servlet.ServletException;
  14. import javax.servlet.http.HttpServlet;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;

  17. /**
  18.  * Selects requests by matching the suffix of the URI.
  19.  * <p>
  20.  * The suffix string is literally matched against the path info of the servlet
  21.  * request, as this class assumes it is invoked by {@link MetaServlet}. Suffix
  22.  * strings may include path components. Examples include {@code /info/refs}, or
  23.  * just simple extension matches like {@code .txt}.
  24.  * <p>
  25.  * When dispatching to the rest of the pipeline the HttpServletRequest is
  26.  * modified so that {@code getPathInfo()} does not contain the suffix that
  27.  * caused this pipeline to be selected.
  28.  */
  29. class SuffixPipeline extends UrlPipeline {
  30.     static class Binder extends ServletBinderImpl {
  31.         private final String suffix;

  32.         Binder(String suffix) {
  33.             this.suffix = suffix;
  34.         }

  35.         @Override
  36.         UrlPipeline create() {
  37.             return new SuffixPipeline(suffix, getFilters(), getServlet());
  38.         }
  39.     }

  40.     private final String suffix;

  41.     private final int suffixLen;

  42.     SuffixPipeline(final String suffix, final Filter[] filters,
  43.             final HttpServlet servlet) {
  44.         super(filters, servlet);
  45.         this.suffix = suffix;
  46.         this.suffixLen = suffix.length();
  47.     }

  48.     @Override
  49.     boolean match(HttpServletRequest req) {
  50.         final String pathInfo = req.getPathInfo();
  51.         return pathInfo != null && pathInfo.endsWith(suffix);
  52.     }

  53.     @Override
  54.     void service(HttpServletRequest req, HttpServletResponse rsp)
  55.             throws ServletException, IOException {
  56.         String curInfo = req.getPathInfo();
  57.         String newPath = req.getServletPath() + curInfo;
  58.         String newInfo = curInfo.substring(0, curInfo.length() - suffixLen);
  59.         super.service(new WrappedRequest(req, newPath, newInfo), rsp);
  60.     }

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     public String toString() {
  64.         return "Pipeline[ *" + suffix + " ]";
  65.     }
  66. }