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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.MetaServlet.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 MetaServlet#REGEX_GROUPS}.
  63. * <p>
  64. * Each servlet request has been altered to have its {@code getPathInfo()}
  65. * method return the matched text of the corresponding capture group. A
  66. * {@link RegexGroupFilter} can be applied in the pipeline to switch the current
  67. * HttpServletRequest to reference a different capture group before running
  68. * additional filters, or the final servlet.
  69. * <p>
  70. * This class dispatches the remainder of the pipeline using the first capture
  71. * group as the current request, making {@code RegexGroupFilter} required only
  72. * to access capture groups beyond the first.
  73. */
  74. class RegexPipeline extends UrlPipeline {
  75. static class Binder extends ServletBinderImpl {
  76. private final Pattern pattern;
  77. Binder(final String p) {
  78. pattern = Pattern.compile(p);
  79. }
  80. UrlPipeline create() {
  81. return new RegexPipeline(pattern, getFilters(), getServlet());
  82. }
  83. }
  84. private final Pattern pattern;
  85. RegexPipeline(final Pattern pattern, final Filter[] filters,
  86. final HttpServlet servlet) {
  87. super(filters, servlet);
  88. this.pattern = pattern;
  89. }
  90. boolean match(final HttpServletRequest req) {
  91. final String pathInfo = req.getPathInfo();
  92. return pathInfo != null && pattern.matcher(pathInfo).matches();
  93. }
  94. @Override
  95. void service(HttpServletRequest req, HttpServletResponse rsp)
  96. throws ServletException, IOException {
  97. final String reqInfo = req.getPathInfo();
  98. if (reqInfo == null) {
  99. rsp.sendError(SC_NOT_FOUND);
  100. return;
  101. }
  102. final Matcher cur = pattern.matcher(reqInfo);
  103. if (!cur.matches()) {
  104. rsp.sendError(SC_NOT_FOUND);
  105. return;
  106. }
  107. final String reqPath = req.getServletPath();
  108. final Object old = req.getAttribute(REGEX_GROUPS);
  109. try {
  110. if (1 <= cur.groupCount()) {
  111. // If there are groups extract every capture group and
  112. // build a request for them so RegexGroupFilter can pick
  113. // a different capture group later. Continue using the
  114. // first capture group as the path info.
  115. WrappedRequest groups[] = new WrappedRequest[cur.groupCount()];
  116. for (int groupId = 1; groupId <= cur.groupCount(); groupId++) {
  117. final int s = cur.start(groupId);
  118. final String path, info;
  119. path = reqPath + reqInfo.substring(0, s);
  120. info = cur.group(groupId);
  121. groups[groupId - 1] = new WrappedRequest(req, path, info);
  122. }
  123. req.setAttribute(REGEX_GROUPS, groups);
  124. super.service(groups[0], rsp);
  125. } else {
  126. // No capture groups were present, service the whole request.
  127. final String path = reqPath + reqInfo;
  128. final String info = null;
  129. super.service(new WrappedRequest(req, path, info), rsp);
  130. }
  131. } finally {
  132. if (old != null)
  133. req.setAttribute(REGEX_GROUPS, old);
  134. else
  135. req.removeAttribute(REGEX_GROUPS);
  136. }
  137. }
  138. @Override
  139. public String toString() {
  140. return "Pipeline[regex: " + pattern + " ]";
  141. }
  142. }