1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.fourthline.cling.model.message.header;
17
18 import org.fourthline.cling.model.UserConstants;
19
20 import java.util.regex.Pattern;
21 import java.util.regex.Matcher;
22
23
24
25
26 public class TimeoutHeader extends UpnpHeader<Integer> {
27
28
29 public static final Integer INFINITE_VALUE = Integer.MAX_VALUE;
30
31 public static final Pattern PATTERN = Pattern.compile("Second-(?:([0-9]+)|infinite)");
32
33
34
35
36 public TimeoutHeader() {
37 setValue(UserConstants.DEFAULT_SUBSCRIPTION_DURATION_SECONDS);
38 }
39
40 public TimeoutHeader(int timeoutSeconds) {
41 setValue(timeoutSeconds);
42 }
43
44 public TimeoutHeader(Integer timeoutSeconds) {
45 setValue(timeoutSeconds);
46 }
47
48 public void setString(String s) throws InvalidHeaderException {
49
50 Matcher matcher = PATTERN.matcher(s);
51 if (!matcher.matches()) {
52 throw new InvalidHeaderException("Can't parse timeout seconds integer from: " + s);
53 }
54
55 if (matcher.group(1) != null) {
56 setValue(Integer.parseInt(matcher.group(1)));
57 } else {
58 setValue(INFINITE_VALUE);
59 }
60
61 }
62
63 public String getString() {
64 return "Second-" + (getValue().equals(INFINITE_VALUE) ? "infinite" : getValue());
65 }
66 }