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