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