1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.transport;
12
13 import static java.nio.charset.StandardCharsets.UTF_8;
14 import static org.eclipse.jgit.util.RawParseUtils.lastIndexOfTrim;
15
16 import java.text.SimpleDateFormat;
17 import java.util.Date;
18 import java.util.Locale;
19 import java.util.TimeZone;
20
21 import org.eclipse.jgit.lib.PersonIdent;
22 import org.eclipse.jgit.util.MutableInteger;
23 import org.eclipse.jgit.util.RawParseUtils;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class PushCertificateIdent {
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public static PushCertificateIdent parse(String str) {
64 MutableInteger p = new MutableInteger();
65 byte[] raw = str.getBytes(UTF_8);
66 int tzBegin = raw.length - 1;
67 tzBegin = lastIndexOfTrim(raw, ' ', tzBegin);
68 if (tzBegin < 0 || raw[tzBegin] != ' ') {
69 return new PushCertificateIdent(str, str, 0, 0);
70 }
71 int whenBegin = tzBegin++;
72 int tz = RawParseUtils.parseTimeZoneOffset(raw, tzBegin, p);
73 boolean hasTz = p.value != tzBegin;
74
75 whenBegin = lastIndexOfTrim(raw, ' ', whenBegin);
76 if (whenBegin < 0 || raw[whenBegin] != ' ') {
77 return new PushCertificateIdent(str, str, 0, 0);
78 }
79 int idEnd = whenBegin++;
80 long when = RawParseUtils.parseLongBase10(raw, whenBegin, p);
81 boolean hasWhen = p.value != whenBegin;
82
83 if (hasTz && hasWhen) {
84 idEnd = whenBegin - 1;
85 } else {
86
87
88 tz = 0;
89 when = 0;
90 if (hasTz && !hasWhen) {
91
92
93 idEnd = tzBegin - 1;
94 } else {
95
96 idEnd = raw.length;
97 }
98 }
99 String id = new String(raw, 0, idEnd, UTF_8);
100
101 return new PushCertificateIdent(str, id, when * 1000L, tz);
102 }
103
104 private final String raw;
105 private final String userId;
106 private final long when;
107 private final int tzOffset;
108
109
110
111
112
113
114
115
116
117
118
119 public PushCertificateIdent(String userId, long when, int tzOffset) {
120 this.userId = userId;
121 this.when = when;
122 this.tzOffset = tzOffset;
123 StringBuilder sb = new StringBuilder(userId).append(' ').append(when / 1000)
124 .append(' ');
125 PersonIdent.appendTimezone(sb, tzOffset);
126 raw = sb.toString();
127 }
128
129 private PushCertificateIdent(String raw, String userId, long when,
130 int tzOffset) {
131 this.raw = raw;
132 this.userId = userId;
133 this.when = when;
134 this.tzOffset = tzOffset;
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148 public String getRaw() {
149 return raw;
150 }
151
152
153
154
155
156
157 public String getUserId() {
158 return userId;
159 }
160
161
162
163
164
165
166
167
168 public String getName() {
169 int nameEnd = userId.indexOf('<');
170 if (nameEnd < 0 || userId.indexOf('>', nameEnd) < 0) {
171 nameEnd = userId.length();
172 }
173 nameEnd--;
174 while (nameEnd >= 0 && userId.charAt(nameEnd) == ' ') {
175 nameEnd--;
176 }
177 int nameBegin = 0;
178 while (nameBegin < nameEnd && userId.charAt(nameBegin) == ' ') {
179 nameBegin++;
180 }
181 return userId.substring(nameBegin, nameEnd + 1);
182 }
183
184
185
186
187
188
189
190 public String getEmailAddress() {
191 int emailBegin = userId.indexOf('<');
192 if (emailBegin < 0) {
193 return null;
194 }
195 int emailEnd = userId.indexOf('>', emailBegin);
196 if (emailEnd < 0) {
197 return null;
198 }
199 return userId.substring(emailBegin + 1, emailEnd);
200 }
201
202
203
204
205
206
207 public Date getWhen() {
208 return new Date(when);
209 }
210
211
212
213
214
215
216
217 public TimeZone getTimeZone() {
218 return PersonIdent.getTimeZone(tzOffset);
219 }
220
221
222
223
224
225
226
227 public int getTimeZoneOffset() {
228 return tzOffset;
229 }
230
231
232 @Override
233 public boolean equals(Object o) {
234 return (o instanceof PushCertificateIdent)
235 && raw.equals(((PushCertificateIdent) o).raw);
236 }
237
238
239 @Override
240 public int hashCode() {
241 return raw.hashCode();
242 }
243
244
245 @SuppressWarnings("nls")
246 @Override
247 public String toString() {
248 SimpleDateFormat fmt;
249 fmt = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy Z", Locale.US);
250 fmt.setTimeZone(getTimeZone());
251 return getClass().getSimpleName()
252 + "[raw=\"" + raw + "\","
253 + " userId=\"" + userId + "\","
254 + " " + fmt.format(Long.valueOf(when)) + "]";
255 }
256 }