View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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   * @author Christian Bauer
25   */
26  public class TimeoutHeader extends UpnpHeader<Integer> {
27  
28      // It's probably OK to assume that "infinite" means 4000 years?
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       * Defaults to {@link org.fourthline.cling.model.UserConstants#DEFAULT_SUBSCRIPTION_DURATION_SECONDS}.
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  }