1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.internal.transport.parser;
11
12 import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_AGENT;
13
14 import java.util.Collections;
15 import java.util.HashSet;
16 import java.util.Set;
17
18 import org.eclipse.jgit.annotations.Nullable;
19 import org.eclipse.jgit.errors.PackProtocolException;
20 import org.eclipse.jgit.internal.JGitText;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class FirstWant {
39 private final String line;
40
41 private final Set<String> capabilities;
42
43 @Nullable
44 private final String agent;
45
46 private static final String AGENT_PREFIX = OPTION_AGENT + '=';
47
48
49
50
51
52
53
54
55
56
57 public static FirstWant fromLine(String line) throws PackProtocolException {
58 String wantLine;
59 Set<String> capabilities;
60 String agent = null;
61
62 if (line.length() > 45) {
63 String opt = line.substring(45);
64 if (!opt.startsWith(" ")) {
65 throw new PackProtocolException(JGitText.get().wantNoSpaceWithCapabilities);
66 }
67 opt = opt.substring(1);
68
69 HashSet<String> opts = new HashSet<>();
70 for (String clientCapability : opt.split(" ")) {
71 if (clientCapability.startsWith(AGENT_PREFIX)) {
72 agent = clientCapability.substring(AGENT_PREFIX.length());
73 } else {
74 opts.add(clientCapability);
75 }
76 }
77 wantLine = line.substring(0, 45);
78 capabilities = Collections.unmodifiableSet(opts);
79 } else {
80 wantLine = line;
81 capabilities = Collections.emptySet();
82 }
83
84 return new FirstWant(wantLine, capabilities, agent);
85 }
86
87 private FirstWant(String line, Set<String> capabilities,
88 @Nullable String agent) {
89 this.line = line;
90 this.capabilities = capabilities;
91 this.agent = agent;
92 }
93
94
95 public String getLine() {
96 return line;
97 }
98
99
100
101
102
103 public Set<String> getCapabilities() {
104 return capabilities;
105 }
106
107
108 @Nullable
109 public String getAgent() {
110 return agent;
111 }
112 }