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.UpnpResponse;
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.ServiceUSNHeader;
25  import org.fourthline.cling.model.message.header.UDNHeader;
26  import org.fourthline.cling.model.message.header.USNRootDeviceHeader;
27  import org.fourthline.cling.model.message.header.UpnpHeader;
28  import org.fourthline.cling.model.types.NamedDeviceType;
29  import org.fourthline.cling.model.types.NamedServiceType;
30  import org.fourthline.cling.model.types.UDN;
31  
32  import java.net.URL;
33  
34  /**
35   * @author Christian Bauer
36   */
37  public class IncomingSearchResponse extends IncomingDatagramMessage<UpnpResponse> {
38  
39      public IncomingSearchResponse(IncomingDatagramMessage<UpnpResponse> source) {
40          super(source);
41      }
42  
43      public boolean isSearchResponseMessage() {
44          UpnpHeader st = getHeaders().getFirstHeader(UpnpHeader.Type.ST);
45          UpnpHeader usn = getHeaders().getFirstHeader(UpnpHeader.Type.USN);
46          UpnpHeader ext = getHeaders().getFirstHeader(UpnpHeader.Type.EXT); // Has no value!
47          return st != null && st.getValue() != null && usn != null && usn.getValue() != null && ext != null;
48      }
49  
50      public UDN getRootDeviceUDN() {
51          // This processes the headers as specified in UDA 1.0, tables in section 1.1.12
52  
53          UpnpHeader<UDN> udnHeader = getHeaders().getFirstHeader(UpnpHeader.Type.USN, USNRootDeviceHeader.class);
54          if (udnHeader != null) return udnHeader.getValue();
55  
56          udnHeader = getHeaders().getFirstHeader(UpnpHeader.Type.USN, UDNHeader.class);
57          if (udnHeader != null) return udnHeader.getValue();
58  
59          UpnpHeader<NamedDeviceType> deviceTypeHeader = getHeaders().getFirstHeader(UpnpHeader.Type.USN, DeviceUSNHeader.class);
60          if (deviceTypeHeader != null) return deviceTypeHeader.getValue().getUdn();
61  
62          UpnpHeader<NamedServiceType> serviceTypeHeader = getHeaders().getFirstHeader(UpnpHeader.Type.USN, ServiceUSNHeader.class);
63          if (serviceTypeHeader != null) return serviceTypeHeader.getValue().getUdn();
64  
65          return null;
66      }
67  
68      public URL getLocationURL() {
69          LocationHeader header = getHeaders().getFirstHeader(UpnpHeader.Type.LOCATION, LocationHeader.class);
70          if (header != null) {
71              return header.getValue();
72          }
73          return null;
74      }
75  
76      public Integer getMaxAge() {
77          MaxAgeHeader header = getHeaders().getFirstHeader(UpnpHeader.Type.MAX_AGE, MaxAgeHeader.class);
78          if (header != null) {
79              return header.getValue();
80          }
81          return null;
82      }
83  
84      public byte[] getInterfaceMacHeader() {
85          InterfaceMacHeader header = getHeaders().getFirstHeader(UpnpHeader.Type.EXT_IFACE_MAC, InterfaceMacHeader.class);
86          if (header != null) {
87              return header.getValue();
88          }
89          return null;
90      }
91  
92  }