1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.http.server.glue;
12
13 import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
14 import static org.eclipse.jgit.http.server.glue.MetaFilter.REGEX_GROUPS;
15
16 import java.io.IOException;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 import javax.servlet.Filter;
21 import javax.servlet.ServletException;
22 import javax.servlet.http.HttpServlet;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 class RegexPipeline extends UrlPipeline {
54 static class Binder extends ServletBinderImpl {
55 private final Pattern pattern;
56
57 Binder(String p) {
58 pattern = Pattern.compile(p);
59 }
60
61 Binder(Pattern p) {
62 pattern = p;
63 }
64
65 @Override
66 UrlPipeline create() {
67 return new RegexPipeline(pattern, getFilters(), getServlet());
68 }
69 }
70
71 private final Pattern pattern;
72
73 RegexPipeline(final Pattern pattern, final Filter[] filters,
74 final HttpServlet servlet) {
75 super(filters, servlet);
76 this.pattern = pattern;
77 }
78
79 @Override
80 boolean match(HttpServletRequest req) {
81 final String pathInfo = req.getPathInfo();
82 return pathInfo != null && pattern.matcher(pathInfo).matches();
83 }
84
85 @Override
86 void service(HttpServletRequest req, HttpServletResponse rsp)
87 throws ServletException, IOException {
88 final String reqInfo = req.getPathInfo();
89 if (reqInfo == null) {
90 rsp.sendError(SC_NOT_FOUND);
91 return;
92 }
93
94 final Matcher cur = pattern.matcher(reqInfo);
95 if (!cur.matches()) {
96 rsp.sendError(SC_NOT_FOUND);
97 return;
98 }
99
100 final String reqPath = req.getServletPath();
101 final Object old = req.getAttribute(REGEX_GROUPS);
102 try {
103 if (1 <= cur.groupCount()) {
104
105
106
107
108 WrappedRequest[] groups = new WrappedRequest[cur.groupCount()];
109 for (int groupId = 1; groupId <= cur.groupCount(); groupId++) {
110 final int s = cur.start(groupId);
111 final String path, info;
112
113 path = reqPath + reqInfo.substring(0, s);
114 info = cur.group(groupId);
115 groups[groupId - 1] = new WrappedRequest(req, path, info);
116 }
117 req.setAttribute(REGEX_GROUPS, groups);
118 super.service(groups[0], rsp);
119
120 } else {
121
122 final String path = reqPath + reqInfo;
123 final String info = null;
124 super.service(new WrappedRequest(req, path, info), rsp);
125 }
126 } finally {
127 if (old != null)
128 req.setAttribute(REGEX_GROUPS, old);
129 else
130 req.removeAttribute(REGEX_GROUPS);
131 }
132 }
133
134
135 @Override
136 public String toString() {
137 return "Pipeline[regex: " + pattern + " ]";
138 }
139 }