1
2
3
4
5 package net.sourceforge.jsdp;
6
7 import java.net.InetAddress;
8 import java.net.UnknownHostException;
9 import java.util.Date;
10 import java.util.regex.Matcher;
11 import java.util.regex.Pattern;
12
13 import net.sourceforge.jsdp.util.Address;
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 public class Origin implements Field {
39
40
41 private static final long serialVersionUID = 8892230839201408790L;
42
43
44 private static final Pattern fieldPattern = Pattern.compile("([^ ]+) (\\d+) (\\d+) IN (IP4|IP6) (([^ ]+))");
45
46
47 private static final Pattern userPattern = Pattern.compile("[\\w'-\\./:?#\\$&\\*;=@\\[\\]\\^_`\\{\\}\\|\\+\\~]+");
48
49
50 private String user;
51
52
53 private long sessionID;
54
55
56 private long sessionVersion;
57
58
59 private Address address;
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 public Origin() throws SDPException {
75
76 super();
77
78 setUser(System.getProperty("user.name"));
79
80 this.sessionID = Time.getNTP(new Date());
81 this.sessionVersion = Time.getNTP(new Date());
82
83 try {
84 this.address = new Address(InetAddress.getLocalHost().getHostName());
85 }
86 catch (UnknownHostException noLocalHost) {
87 throw new SDPException("Cannot find the localhost name");
88 }
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102 public Origin(final long sessionVersion, final String address) throws SDPException {
103
104 super();
105
106 setUser(System.getProperty("user.name"));
107 this.sessionID = Time.getNTP(new Date());
108 setSessionVersion(sessionVersion);
109
110 this.address = new Address(address);
111 }
112
113
114
115
116
117
118
119
120 protected Origin(final Origin origin) {
121
122 super();
123
124 this.user = new String(origin.user);
125 this.sessionID = origin.sessionID;
126 this.sessionVersion = origin.sessionVersion;
127 this.address = (Address) origin.address.clone();
128 }
129
130
131
132
133
134
135
136
137
138
139 public Origin(final String address) throws SDPException {
140
141 super();
142
143 setUser(System.getProperty("user.name"));
144 this.sessionID = Time.getNTP(new Date());
145 this.sessionVersion = Time.getNTP(new Date());
146
147 this.address = new Address(address);
148 }
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163 public Origin(final String user, final long sessionID, final long sessionVersion, final String address) throws SDPException {
164
165 super();
166
167 setUser(user);
168 setSessionID(sessionID);
169 setSessionVersion(sessionVersion);
170
171 this.address = new Address(address);
172 }
173
174
175
176
177
178
179
180
181
182
183
184
185 public Origin(final String user, final long sessionVersion, final String address) throws SDPException {
186
187 super();
188
189 setUser(user);
190 this.sessionID = Time.getNTP(new Date());
191 setSessionVersion(sessionVersion);
192
193 this.address = new Address(address);
194 }
195
196
197
198
199
200
201
202
203
204
205 public static Origin parse(final String field) throws SDPParseException {
206
207 if (!field.startsWith("o=")) {
208 throw new SDPParseException("The string \"" + field + "\" isn't an origin field");
209 }
210
211 Origin o = null;
212
213
214 Matcher matcher = fieldPattern.matcher(field.substring(2));
215
216
217 if (matcher.matches()) {
218 try {
219
220 String type = matcher.group(4);
221 o = new Origin(matcher.group(1), Long.parseLong(matcher.group(2)), Long.parseLong(matcher.group(3)), matcher.group(5));
222
223 if (!o.getAddressType().equals(type)) {
224 throw new SDPParseException("The address " + o.getAddress() + " isn't an " + type + " address");
225 }
226 }
227 catch (SDPException parseException) {
228 throw new SDPParseException("The string \"" + field + "\" isn't a valid origin field", parseException);
229 }
230 }
231 else {
232 throw new SDPParseException("The string \"" + field + "\" isn't a valid origin field");
233 }
234
235 return o;
236 }
237
238
239
240
241
242
243 public Object clone() {
244
245 return new Origin(this);
246 }
247
248
249
250
251
252
253 public String getAddress() {
254
255 return address.getAddress();
256 }
257
258
259
260
261
262
263 public String getAddressType() {
264
265 return address.getAddressType();
266 }
267
268
269
270
271
272
273
274 public String getNetType() {
275
276 return Address.IN;
277 }
278
279
280
281
282
283
284 public long getSessionID() {
285
286 return sessionID;
287 }
288
289
290
291
292
293
294 public long getSessionVersion() {
295
296 return sessionVersion;
297 }
298
299
300
301
302
303
304 public char getType() {
305
306 return Field.ORIGIN_FIELD;
307 }
308
309
310
311
312
313
314 public String getUser() {
315
316 return user;
317 }
318
319
320
321
322
323
324
325
326 public void setAddress(final String address) throws SDPException {
327
328 this.address.setAddress(address);
329 }
330
331
332
333
334
335
336
337
338 public void setSessionID(final long sessionID) throws SDPException {
339
340 if (sessionID < 0) {
341 throw new SDPException("Session id cannot be negative");
342 }
343
344 this.sessionID = sessionID;
345 }
346
347
348
349
350
351
352
353
354 public void setSessionVersion(final long sessionVersion) throws SDPException {
355
356 if (sessionVersion < 0) {
357 throw new SDPException("Session version cannot be negative");
358 }
359
360 this.sessionVersion = sessionVersion;
361 }
362
363
364
365
366
367
368
369
370
371 public void setUser(final String user) throws SDPException {
372
373 if (!userPattern.matcher(user).matches()) {
374 throw new SDPException("Invalid user name: " + user);
375 }
376
377 this.user = user;
378 }
379
380
381
382
383
384
385
386 public String toString() {
387
388 StringBuffer result = new StringBuffer(getType() + "=");
389
390 result.append(this.user + " ");
391 result.append(this.sessionID + " ");
392 result.append(this.sessionVersion);
393 result.append(" IN ");
394 result.append(address.toString());
395
396 return result.toString();
397 }
398 }