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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. /**
  55. * Selects requests by matching the URI against a regular expression.
  56. * <p>
  57. * The pattern is bound and matched against the path info of the servlet
  58. * request, as this class assumes it is invoked by {@link MetaServlet}.
  59. * <p>
  60. * If there are capture groups in the regular expression, the matched ranges of
  61. * the capture groups are stored as an array of modified HttpServetRequests,
  62. * into the request attribute {@link MetaFilter#REGEX_GROUPS}. Using a capture
  63. * group that may not capture, e.g. {@code "(/foo)?"}, will cause an error at
  64. * request handling time.
  65. * <p>
  66. * Each servlet request has been altered to have its {@code getServletPath()}
  67. * method return the original path info up to the beginning of the corresponding
  68. * capture group, and its {@code getPathInfo()} method return the matched text.
  69. * A {@link RegexGroupFilter} can be applied in the pipeline to switch the
  70. * current HttpServletRequest to reference a different capture group before
  71. * running additional filters, or the final servlet.
  72. * <p>
  73. * Note that for {@code getPathInfo()} to start with a leading "/" as described
  74. * in the servlet documentation, capture groups must actually capture the
  75. * leading "/".
  76. * <p>
  77. * This class dispatches the remainder of the pipeline using the first capture
  78. * group as the current request, making {@code RegexGroupFilter} required only
  79. * to access capture groups beyond the first.
  80. */
  81. class RegexPipeline extends UrlPipeline {
  82. static class Binder extends ServletBinderImpl {
  83. private final Pattern pattern;
  84. Binder(final String p) {
  85. pattern = Pattern.compile(p);
  86. }
  87. Binder(final Pattern p) {
  88. pattern = p;
  89. }
  90. UrlPipeline create() {
  91. return new RegexPipeline(pattern, getFilters(), getServlet());
  92. }
  93. }
  94. private final Pattern pattern;
  95. RegexPipeline(final Pattern pattern, final Filter[] filters,
  96. final HttpServlet servlet) {
  97. super(filters, servlet);
  98. this.pattern = pattern;
  99. }
  100. boolean match(final HttpServletRequest req) {
  101. final String pathInfo = req.getPathInfo();
  102. return pathInfo != null && pattern.matcher(pathInfo).matches();
  103. }
  104. @Override
  105. void service(HttpServletRequest req, HttpServletResponse rsp)
  106. throws ServletException, IOException {
  107. final String reqInfo = req.getPathInfo();
  108. if (reqInfo == null) {
  109. rsp.sendError(SC_NOT_FOUND);
  110. return;
  111. }
  112. final Matcher cur = pattern.matcher(reqInfo);
  113. if (!cur.matches()) {
  114. rsp.sendError(SC_NOT_FOUND);
  115. return;
  116. }
  117. final String reqPath = req.getServletPath();
  118. final Object old = req.getAttribute(REGEX_GROUPS);
  119. try {
  120. if (1 <= cur.groupCount()) {
  121. // If there are groups extract every capture group and
  122. // build a request for them so RegexGroupFilter can pick
  123. // a different capture group later. Continue using the
  124. // first capture group as the path info.
  125. WrappedRequest groups[] = new WrappedRequest[cur.groupCount()];
  126. for (int groupId = 1; groupId <= cur.groupCount(); groupId++) {
  127. final int s = cur.start(groupId);
  128. final String path, info;
  129. path = reqPath + reqInfo.substring(0, s);
  130. info = cur.group(groupId);
  131. groups[groupId - 1] = new WrappedRequest(req, path, info);
  132. }
  133. req.setAttribute(REGEX_GROUPS, groups);
  134. super.service(groups[0], rsp);
  135. } else {
  136. // No capture groups were present, service the whole request.
  137. final String path = reqPath + reqInfo;
  138. final String info = null;
  139. super.service(new WrappedRequest(req, path, info), rsp);
  140. }
  141. } finally {
  142. if (old != null)
  143. req.setAttribute(REGEX_GROUPS, old);
  144. else
  145. req.removeAttribute(REGEX_GROUPS);
  146. }
  147. }
  148. @Override
  149. public String toString() {
  150. return "Pipeline[regex: " + pattern + " ]";
  151. }
  152. }