You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RegexPipeline.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (C) 2009-2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.http.server.glue;
  44. import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
  45. import static org.eclipse.jgit.http.server.glue.MetaFilter.REGEX_GROUPS;
  46. import java.io.IOException;
  47. import java.util.regex.Matcher;
  48. import java.util.regex.Pattern;
  49. import javax.servlet.Filter;
  50. import javax.servlet.ServletException;
  51. import javax.servlet.http.HttpServlet;
  52. import javax.servlet.http.HttpServletRequest;
  53. import javax.servlet.http.HttpServletResponse;
  54. import org.eclipse.jgit.http.server.ServletUtils;
  55. /**
  56. * Selects requests by matching the URI against a regular expression.
  57. * <p>
  58. * The pattern is bound and matched against the path info of the servlet
  59. * request, as this class assumes it is invoked by {@link MetaServlet}.
  60. * <p>
  61. * If there are capture groups in the regular expression, the matched ranges of
  62. * the capture groups are stored as an array of modified HttpServetRequests,
  63. * into the request attribute {@link MetaFilter#REGEX_GROUPS}. Using a capture
  64. * group that may not capture, e.g. {@code "(/foo)?"}, will cause an error at
  65. * request handling time.
  66. * <p>
  67. * Each servlet request has been altered to have its {@code getServletPath()}
  68. * method return the original path info up to the beginning of the corresponding
  69. * capture group, and its {@code getPathInfo()} method return the matched text.
  70. * A {@link RegexGroupFilter} can be applied in the pipeline to switch the
  71. * current HttpServletRequest to reference a different capture group before
  72. * running additional filters, or the final servlet.
  73. * <p>
  74. * Note that for {@code getPathInfo()} to start with a leading "/" as described
  75. * in the servlet documentation, capture groups must actually capture the
  76. * leading "/".
  77. * <p>
  78. * This class dispatches the remainder of the pipeline using the first capture
  79. * group as the current request, making {@code RegexGroupFilter} required only
  80. * to access capture groups beyond the first.
  81. */
  82. class RegexPipeline extends UrlPipeline {
  83. static class Binder extends ServletBinderImpl {
  84. private final Pattern pattern;
  85. Binder(final String p) {
  86. pattern = Pattern.compile(p);
  87. }
  88. Binder(final Pattern p) {
  89. pattern = p;
  90. }
  91. UrlPipeline create() {
  92. return new RegexPipeline(pattern, getFilters(), getServlet());
  93. }
  94. }
  95. private final Pattern pattern;
  96. RegexPipeline(final Pattern pattern, final Filter[] filters,
  97. final HttpServlet servlet) {
  98. super(filters, servlet);
  99. this.pattern = pattern;
  100. }
  101. boolean match(final HttpServletRequest req) {
  102. final String pathInfo = ServletUtils.getEncodedPathInfo(req);
  103. return pathInfo != null && pattern.matcher(pathInfo).matches();
  104. }
  105. @Override
  106. void service(HttpServletRequest req, HttpServletResponse rsp)
  107. throws ServletException, IOException {
  108. final String reqInfo = ServletUtils.getEncodedPathInfo(req);
  109. if (reqInfo == null) {
  110. rsp.sendError(SC_NOT_FOUND);
  111. return;
  112. }
  113. final Matcher cur = pattern.matcher(reqInfo);
  114. if (!cur.matches()) {
  115. rsp.sendError(SC_NOT_FOUND);
  116. return;
  117. }
  118. final String reqPath = req.getServletPath();
  119. final Object old = req.getAttribute(REGEX_GROUPS);
  120. try {
  121. if (1 <= cur.groupCount()) {
  122. // If there are groups extract every capture group and
  123. // build a request for them so RegexGroupFilter can pick
  124. // a different capture group later. Continue using the
  125. // first capture group as the path info.
  126. WrappedRequest groups[] = new WrappedRequest[cur.groupCount()];
  127. for (int groupId = 1; groupId <= cur.groupCount(); groupId++) {
  128. final int s = cur.start(groupId);
  129. final String path, info;
  130. path = reqPath + reqInfo.substring(0, s);
  131. info = cur.group(groupId);
  132. groups[groupId - 1] = new WrappedRequest(req, path, info);
  133. }
  134. req.setAttribute(REGEX_GROUPS, groups);
  135. super.service(groups[0], rsp);
  136. } else {
  137. // No capture groups were present, service the whole request.
  138. final String path = reqPath + reqInfo;
  139. final String info = null;
  140. super.service(new WrappedRequest(req, path, info), rsp);
  141. }
  142. } finally {
  143. if (old != null)
  144. req.setAttribute(REGEX_GROUPS, old);
  145. else
  146. req.removeAttribute(REGEX_GROUPS);
  147. }
  148. }
  149. @Override
  150. public String toString() {
  151. return "Pipeline[regex: " + pattern + " ]";
  152. }
  153. }