1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.http.server;
12
13 import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
14 import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED;
15 import static org.eclipse.jgit.http.server.GitSmartHttpTools.infoRefsResultType;
16 import static org.eclipse.jgit.http.server.GitSmartHttpTools.sendError;
17 import static org.eclipse.jgit.http.server.ServletUtils.ATTRIBUTE_HANDLER;
18 import static org.eclipse.jgit.http.server.ServletUtils.getRepository;
19
20 import java.io.IOException;
21 import java.util.List;
22
23 import javax.servlet.Filter;
24 import javax.servlet.FilterChain;
25 import javax.servlet.FilterConfig;
26 import javax.servlet.ServletException;
27 import javax.servlet.ServletRequest;
28 import javax.servlet.ServletResponse;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.eclipse.jgit.lib.Repository;
33 import org.eclipse.jgit.transport.PacketLineOut;
34 import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
35 import org.eclipse.jgit.transport.ServiceMayNotContinueException;
36 import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
37 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
38
39
40 abstract class SmartServiceInfoRefs implements Filter {
41 private final String svc;
42
43 private final Filter[] filters;
44
45 SmartServiceInfoRefs(String service, List<Filter> filters) {
46 this.svc = service;
47 this.filters = filters.toArray(new Filter[0]);
48 }
49
50
51 @Override
52 public void init(FilterConfig config) throws ServletException {
53
54 }
55
56
57 @Override
58 public void destroy() {
59
60 }
61
62
63 @Override
64 public void doFilter(ServletRequest request, ServletResponse response,
65 FilterChain chain) throws IOException, ServletException {
66 final HttpServletRequest req = (HttpServletRequest) request;
67 final HttpServletResponse res = (HttpServletResponse) response;
68
69 if (svc.equals(req.getParameter("service"))) {
70 final Repository db = getRepository(req);
71 try {
72 begin(req, db);
73 } catch (ServiceNotAuthorizedException e) {
74 res.sendError(SC_UNAUTHORIZED, e.getMessage());
75 return;
76 } catch (ServiceNotEnabledException e) {
77 sendError(req, res, SC_FORBIDDEN, e.getMessage());
78 return;
79 }
80
81 try {
82 if (filters.length == 0)
83 service(req, response);
84 else
85 new Chain().doFilter(request, response);
86 } finally {
87 req.removeAttribute(ATTRIBUTE_HANDLER);
88 }
89 } else {
90 chain.doFilter(request, response);
91 }
92 }
93
94 private void service(ServletRequest request, ServletResponse response)
95 throws IOException {
96 final HttpServletRequest req = (HttpServletRequest) request;
97 final HttpServletResponse res = (HttpServletResponse) response;
98 final SmartOutputStreamtOutputStream.html#SmartOutputStream">SmartOutputStream buf = new SmartOutputStream(req, res, true);
99 try {
100 res.setContentType(infoRefsResultType(svc));
101
102 final PacketLineOut out = new PacketLineOut(buf);
103 respond(req, out, svc);
104 buf.close();
105 } catch (ServiceNotAuthorizedException e) {
106 res.sendError(SC_UNAUTHORIZED, e.getMessage());
107 } catch (ServiceNotEnabledException e) {
108 sendError(req, res, SC_FORBIDDEN, e.getMessage());
109 } catch (ServiceMayNotContinueException e) {
110 if (e.isOutput())
111 buf.close();
112 else
113 sendError(req, res, e.getStatusCode(), e.getMessage());
114 }
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128 protected abstract void begin(HttpServletRequest req, Repository db)
129 throws IOException, ServiceNotEnabledException,
130 ServiceNotAuthorizedException;
131
132
133
134
135
136
137
138
139
140
141
142 protected abstract void advertise(HttpServletRequest req,
143 PacketLineOutRefAdvertiser pck) throws IOException,
144 ServiceNotEnabledException, ServiceNotAuthorizedException;
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 protected void respond(HttpServletRequest req,
169 PacketLineOut pckOut, String serviceName)
170 throws IOException, ServiceNotEnabledException,
171 ServiceNotAuthorizedException {
172 pckOut.writeString("# service=" + svc + '\n');
173 pckOut.end();
174 advertise(req, new PacketLineOutRefAdvertiser(pckOut));
175 }
176
177 private class Chain implements FilterChain {
178 private int filterIdx;
179
180 @Override
181 public void doFilter(ServletRequest req, ServletResponse rsp)
182 throws IOException, ServletException {
183 if (filterIdx < filters.length)
184 filters[filterIdx++].doFilter(req, rsp, this);
185 else
186 service(req, rsp);
187 }
188 }
189 }