1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.http.server.resolver;
12
13 import javax.servlet.http.HttpServletRequest;
14
15 import org.eclipse.jgit.lib.Config;
16 import org.eclipse.jgit.lib.PersonIdent;
17 import org.eclipse.jgit.lib.Repository;
18 import org.eclipse.jgit.transport.ReceivePack;
19 import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
20 import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
21 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 public class DefaultReceivePackFactory implements
38 ReceivePackFactory<HttpServletRequest> {
39 private static class ServiceConfig {
40 final boolean set;
41
42 final boolean enabled;
43
44 ServiceConfig(Config cfg) {
45 set = cfg.getString("http", null, "receivepack") != null;
46 enabled = cfg.getBoolean("http", "receivepack", false);
47 }
48 }
49
50
51 @Override
52 public ReceivePack create(HttpServletRequest req, Repository db)
53 throws ServiceNotEnabledException, ServiceNotAuthorizedException {
54 final ServiceConfig cfg = db.getConfig().get(ServiceConfig::new);
55 String user = req.getRemoteUser();
56
57 if (cfg.set) {
58 if (cfg.enabled) {
59 if (user == null || "".equals(user))
60 user = "anonymous";
61 return createFor(req, db, user);
62 }
63 throw new ServiceNotEnabledException();
64 }
65
66 if (user != null && !"".equals(user))
67 return createFor(req, db, user);
68 throw new ServiceNotAuthorizedException();
69 }
70
71 private static ReceivePack createFor(final HttpServletRequest req,
72 final Repository db, final String user) {
73 final ReceivePack rp = new ReceivePack(db);
74 rp.setRefLogIdent(toPersonIdent(req, user));
75 return rp;
76 }
77
78 private static PersonIdent toPersonIdent(HttpServletRequest req, String user) {
79 return new PersonIdent(user, user + "@" + req.getRemoteHost());
80 }
81 }