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.Constants;
19  
20  import java.util.regex.Matcher;
21  import java.util.regex.Pattern;
22  import java.util.Locale;
23  
24  /**
25   * @author Christian Bauer
26   */
27  public class MaxAgeHeader extends UpnpHeader<Integer> {
28  
29      // UDA 1.1 expands on the rules in UDA 1.0 and clearly says that anything but max-age has to be ignored
30      public static final Pattern MAX_AGE_REGEX = Pattern.compile(".*max-age\\s*=\\s*([0-9]+).*");
31  
32      public MaxAgeHeader(Integer maxAge) {
33          setValue(maxAge);
34      }
35  
36      public MaxAgeHeader() {
37          setValue(Constants.MIN_ADVERTISEMENT_AGE_SECONDS);
38      }
39  
40      public void setString(String s) throws InvalidHeaderException {
41  
42          Matcher matcher = MAX_AGE_REGEX.matcher(s.toLowerCase(Locale.ROOT));
43          if (!matcher.matches()){
44              throw new InvalidHeaderException("Invalid cache-control value, can't parse max-age seconds: " + s);
45          }
46  
47          Integer maxAge = Integer.parseInt(matcher.group(1));
48          setValue(maxAge);
49      }
50  
51      public String getString() {
52          return "max-age="+getValue().toString();
53      }
54  }