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  import org.fourthline.cling.model.types.HostPort;
20  
21  /**
22   * @author Christian Bauer
23   */
24  public class HostHeader extends UpnpHeader<HostPort> {
25  
26      int port = Constants.UPNP_MULTICAST_PORT;
27      String group = Constants.IPV4_UPNP_MULTICAST_GROUP;
28  
29      public HostHeader() {
30          setValue(new HostPort(group, port));
31      }
32  
33      public HostHeader(int port) {
34          setValue(new HostPort(group, port));
35      }
36  
37      public HostHeader(String host, int port) {
38          setValue(new HostPort(host, port));
39      }
40  
41      public void setString(String s) throws InvalidHeaderException {
42          // UDA 1.1/1.0 section 1.2.2
43          if (s.contains(":")) {
44              // We have a port in the header, so we have to use that instead of the UDA default
45              try {
46                  this.port = Integer.valueOf(s.substring(s.indexOf(":")+1));
47                  this.group = s.substring(0, s.indexOf(":"));
48                  setValue(new HostPort(group, port));
49              } catch (NumberFormatException ex) {
50                  throw new InvalidHeaderException("Invalid HOST header value, can't parse port: " + s + " - " + ex.getMessage());
51              }
52          } else {
53              this.group = s;
54              setValue(new HostPort(group, port));
55          }
56      }
57  
58      public String getString() {
59          return getValue().toString();
60      }
61  }