]> git.basschouten.com Git - openhab-addons.git/blob
711832947f332d1220fd257ba55593f69dd98e45
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.upnpcontrol.internal.util;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.stream.Collectors;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  *
25  * @author Mark Herwege - Initial contribution
26  */
27 @NonNullByDefault
28 public final class UpnpProtocolMatcher {
29
30     private static final Logger LOGGER = LoggerFactory.getLogger(UpnpProtocolMatcher.class);
31
32     private UpnpProtocolMatcher() {
33     }
34
35     /**
36      * Test if an UPnP protocol matches the object class. This method is used to filter resources for the primary
37      * resource.
38      *
39      * @param protocol format: <protocol>:<network>:<contentFormat>:<additionalInfo>
40      *            e.g. http-get:*:audio/mpeg:*
41      * @param objectClass e.g. object.item.audioItem.musicTrack
42      * @return true if protocol matches objectClass
43      */
44     public static boolean testProtocol(String protocol, String objectClass) {
45         String[] protocolDetails = protocol.split(":");
46         if (protocolDetails.length < 3) {
47             LOGGER.debug("Protocol string {} not valid", protocol);
48             return false;
49         }
50         String protocolType = protocolDetails[2].toLowerCase();
51         int index = protocolType.indexOf("/");
52         if (index <= 0) {
53             LOGGER.debug("Protocol string {} not valid", protocol);
54             return false;
55         }
56         protocolType = protocolType.substring(0, index);
57
58         String[] objectClassDetails = objectClass.split("\\.");
59         if (objectClassDetails.length < 3) {
60             LOGGER.debug("Object class {} not valid", objectClass);
61             return false;
62         }
63         String objectType = objectClassDetails[2].toLowerCase();
64
65         LOGGER.debug("Matching protocol type '{}' with object type '{}'", protocolType, objectType);
66         return objectType.startsWith(protocolType);
67     }
68
69     /**
70      * Test if a UPnP protocol is in a set of protocols.
71      * Ignore vendor specific additionalInfo part in UPnP protocol string.
72      * Do all comparisons in lower case.
73      *
74      * @param protocol format: <protocol>:<network>:<contentFormat>:<additionalInfo>
75      * @param protocolSet
76      * @return true if protocol in protocolSet
77      */
78     public static boolean testProtocol(String protocol, List<String> protocolSet) {
79         int index = protocol.lastIndexOf(":");
80         if (index <= 0) {
81             LOGGER.debug("Protocol {} not valid", protocol);
82             return false;
83         }
84         String p = protocol.toLowerCase().substring(0, index);
85         List<String> pSet = new ArrayList<>();
86         protocolSet.forEach(f -> {
87             int i = f.lastIndexOf(":");
88             if (i <= 0) {
89                 LOGGER.debug("Protocol {} from set not valid", f);
90             } else {
91                 pSet.add(f.toLowerCase().substring(0, i));
92             }
93         });
94         LOGGER.trace("Testing {} in {}", p, pSet);
95         return pSet.contains(p);
96     }
97
98     /**
99      * Test if any of the UPnP protocols in protocolList can be found in a set of protocols.
100      *
101      * @param protocolList
102      * @param protocolSet
103      * @return true if one of the protocols in protocolSet
104      */
105     public static boolean testProtocolList(List<String> protocolList, List<String> protocolSet) {
106         return protocolList.stream().anyMatch(p -> testProtocol(p, protocolSet));
107     }
108
109     /**
110      * Return all UPnP protocols from protocolList that are part of a set of protocols.
111      *
112      * @param protocolList
113      * @param protocolSet
114      * @return sublist of protocolList
115      */
116     public static List<String> getProtocols(List<String> protocolList, List<String> protocolSet) {
117         return protocolList.stream().filter(p -> testProtocol(p, protocolSet)).collect(Collectors.toList());
118     }
119 }