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.types;
17  
18  import org.fourthline.cling.model.Constants;
19  
20  import java.util.logging.Logger;
21  import java.util.regex.Pattern;
22  import java.util.regex.Matcher;
23  
24  /**
25   * Service identifier with a fixed <code>upnp-org</code> namespace.
26   * <p>
27   * Also accepts the namespace sometimes used by broken devices, <code>schemas-upnp-org</code>.
28   * </p>
29   *
30   * @author Christian Bauer
31   */
32  public class UDAServiceId extends ServiceId {
33  	
34  	private static Logger log = Logger.getLogger(UDAServiceId.class.getName());
35  
36      public static final String DEFAULT_NAMESPACE = "upnp-org";
37      public static final String BROKEN_DEFAULT_NAMESPACE = "schemas-upnp-org"; // TODO: UPNP VIOLATION: Intel UPnP tools!
38  
39      public static final Pattern PATTERN =
40              Pattern.compile("urn:" + DEFAULT_NAMESPACE + ":serviceId:(" + Constants.REGEX_ID+ ")");
41  
42       // Note: 'service' vs. 'serviceId'
43      public static final Pattern BROKEN_PATTERN =
44              Pattern.compile("urn:" + BROKEN_DEFAULT_NAMESPACE + ":service:(" + Constants.REGEX_ID+ ")");
45  
46      public UDAServiceId(String id) {
47          super(DEFAULT_NAMESPACE, id);
48      }
49  
50      public static UDAServiceId valueOf(String s) throws InvalidValueException {
51          Matcher matcher = UDAServiceId.PATTERN.matcher(s);
52          if (matcher.matches() && matcher.groupCount() >= 1) {
53              return new UDAServiceId(matcher.group(1));
54          }
55  
56          matcher = UDAServiceId.BROKEN_PATTERN.matcher(s);
57          if (matcher.matches() && matcher.groupCount() >= 1) {
58              return new UDAServiceId(matcher.group(1));
59          }
60  
61          // TODO: UPNP VIOLATION: Handle garbage sent by Eyecon Android app
62          matcher = Pattern.compile("urn:upnp-orgerviceId:urnchemas-upnp-orgervice:(" + Constants.REGEX_ID + ")").matcher(s);
63          if (matcher.matches()) {
64              log.warning("UPnP specification violation, recovering from Eyecon garbage: " + s);
65              return new UDAServiceId(matcher.group(1));
66          }
67  
68          // Some devices just set the last token of the Service ID, e.g. 'ContentDirectory'
69          if("ContentDirectory".equals(s) ||
70             "ConnectionManager".equals(s) ||
71             "RenderingControl".equals(s) ||
72             "AVTransport".equals(s)) {
73              log.warning("UPnP specification violation, fixing broken Service ID: " + s);
74              return new UDAServiceId(s);
75          }
76  
77          throw new InvalidValueException("Can't parse UDA service ID string (upnp-org/id): " + s);
78      }
79  
80  }