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(final String p) {
91 pattern = Pattern.compile(p);
92 }
93
94 Binder(final Pattern p) {
95 pattern = p;
96 }
97
98 UrlPipeline create() {
99 return new RegexPipeline(pattern, getFilters(), getServlet());
100 }
101 }
102
103 private final Pattern pattern;
104
105 RegexPipeline(final Pattern pattern, final Filter[] filters,
106 final HttpServlet servlet) {
107 super(filters, servlet);
108 this.pattern = pattern;
109 }
110
111 boolean match(final HttpServletRequest req) {
112 final String pathInfo = req.getPathInfo();
113 return pathInfo != null && pattern.matcher(pathInfo).matches();
114 }
115
116 @Override
117 void service(HttpServletRequest req, HttpServletResponse rsp)
118 throws ServletException, IOException {
119 final String reqInfo = req.getPathInfo();
120 if (reqInfo == null) {
121 rsp.sendError(SC_NOT_FOUND);
122 return;
123 }
124
125 final Matcher cur = pattern.matcher(reqInfo);
126 if (!cur.matches()) {
127 rsp.sendError(SC_NOT_FOUND);
128 return;
129 }
130
131 final String reqPath = req.getServletPath();
132 final Object old = req.getAttribute(REGEX_GROUPS);
133 try {
134 if (1 <= cur.groupCount()) {
135 // If there are groups extract every capture group and
136 // build a request for them so RegexGroupFilter can pick
137 // a different capture group later. Continue using the
138 // first capture group as the path info.
139 WrappedRequest groups[] = new WrappedRequest[cur.groupCount()];
140 for (int groupId = 1; groupId <= cur.groupCount(); groupId++) {
141 final int s = cur.start(groupId);
142 final String path, info;
143
144 path = reqPath + reqInfo.substring(0, s);
145 info = cur.group(groupId);
146 groups[groupId - 1] = new WrappedRequest(req, path, info);
147 }
148 req.setAttribute(REGEX_GROUPS, groups);
149 super.service(groups[0], rsp);
150
151 } else {
152 // No capture groups were present, service the whole request.
153 final String path = reqPath + reqInfo;
154 final String info = null;
155 super.service(new WrappedRequest(req, path, info), rsp);
156 }
157 } finally {
158 if (old != null)
159 req.setAttribute(REGEX_GROUPS, old);
160 else
161 req.removeAttribute(REGEX_GROUPS);
162 }
163 }
164
165 @Override
166 public String toString() {
167 return "Pipeline[regex: " + pattern + " ]";
168 }
169 }