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.discovery;
17  
18  import org.fourthline.cling.model.message.IncomingDatagramMessage;
19  import org.fourthline.cling.model.message.UpnpRequest;
20  import org.fourthline.cling.model.message.header.DeviceUSNHeader;
21  import org.fourthline.cling.model.message.header.InterfaceMacHeader;
22  import org.fourthline.cling.model.message.header.LocationHeader;
23  import org.fourthline.cling.model.message.header.MaxAgeHeader;
24  import org.fourthline.cling.model.message.header.NTSHeader;
25  import org.fourthline.cling.model.message.header.ServiceUSNHeader;
26  import org.fourthline.cling.model.message.header.UDNHeader;
27  import org.fourthline.cling.model.message.header.USNRootDeviceHeader;
28  import org.fourthline.cling.model.message.header.UpnpHeader;
29  import org.fourthline.cling.model.types.NamedDeviceType;
30  import org.fourthline.cling.model.types.NamedServiceType;
31  import org.fourthline.cling.model.types.NotificationSubtype;
32  import org.fourthline.cling.model.types.UDN;
33  
34  import java.net.URL;
35  
36  /**
37   * @author Christian Bauer
38   */
39  public class IncomingNotificationRequest extends IncomingDatagramMessage<UpnpRequest> {
40  
41      public IncomingNotificationRequest(IncomingDatagramMessage<UpnpRequest> source) {
42          super(source);
43      }
44  
45      public boolean isAliveMessage() {
46          NTSHeader nts = getHeaders().getFirstHeader(UpnpHeader.Type.NTS, NTSHeader.class);
47          return nts != null && nts.getValue().equals(NotificationSubtype.ALIVE);
48      }
49  
50      public boolean isByeByeMessage() {
51          NTSHeader nts = getHeaders().getFirstHeader(UpnpHeader.Type.NTS, NTSHeader.class);
52          return nts != null && nts.getValue().equals(NotificationSubtype.BYEBYE);
53      }
54  
55      public URL getLocationURL() {
56          LocationHeader header = getHeaders().getFirstHeader(UpnpHeader.Type.LOCATION, LocationHeader.class);
57          if (header != null) {
58              return header.getValue();
59          }
60          return null;
61      }
62  
63      /**
64       * @return The UDN value after parsing various USN header values, or <code>null</code>.
65       */
66      public UDN getUDN() {
67          // This processes the headers as specified in UDA 1.0, tables in section 1.1.12
68  
69          UpnpHeader<UDN> udnHeader = getHeaders().getFirstHeader(UpnpHeader.Type.USN, USNRootDeviceHeader.class);
70          if (udnHeader != null) return udnHeader.getValue();
71  
72          udnHeader = getHeaders().getFirstHeader(UpnpHeader.Type.USN, UDNHeader.class);
73          if (udnHeader != null) return udnHeader.getValue();
74  
75          UpnpHeader<NamedDeviceType> deviceTypeHeader = getHeaders().getFirstHeader(UpnpHeader.Type.USN, DeviceUSNHeader.class);
76          if (deviceTypeHeader != null) return deviceTypeHeader.getValue().getUdn();
77  
78          UpnpHeader<NamedServiceType> serviceTypeHeader = getHeaders().getFirstHeader(UpnpHeader.Type.USN, ServiceUSNHeader.class);
79          if (serviceTypeHeader != null) return serviceTypeHeader.getValue().getUdn();
80  
81          return null;
82      }
83  
84      public Integer getMaxAge() {
85          MaxAgeHeader header = getHeaders().getFirstHeader(UpnpHeader.Type.MAX_AGE, MaxAgeHeader.class);
86          if (header != null) {
87              return header.getValue();
88          }
89          return null;
90      }
91  
92      public byte[] getInterfaceMacHeader() {
93          InterfaceMacHeader header = getHeaders().getFirstHeader(UpnpHeader.Type.EXT_IFACE_MAC, InterfaceMacHeader.class);
94          if (header != null) {
95              return header.getValue();
96          }
97          return null;
98      }
99  
100 }