2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.upnpcontrol.internal.util;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.stream.Collectors;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
25 * @author Mark Herwege - Initial contribution
28 public final class UpnpProtocolMatcher {
30 private static final Logger LOGGER = LoggerFactory.getLogger(UpnpProtocolMatcher.class);
32 private UpnpProtocolMatcher() {
36 * Test if an UPnP protocol matches the object class. This method is used to filter resources for the primary
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
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);
50 String protocolType = protocolDetails[2].toLowerCase();
51 int index = protocolType.indexOf("/");
53 LOGGER.debug("Protocol string {} not valid", protocol);
56 protocolType = protocolType.substring(0, index);
58 String[] objectClassDetails = objectClass.split("\\.");
59 if (objectClassDetails.length < 3) {
60 LOGGER.debug("Object class {} not valid", objectClass);
63 String objectType = objectClassDetails[2].toLowerCase();
65 LOGGER.debug("Matching protocol type '{}' with object type '{}'", protocolType, objectType);
66 return objectType.startsWith(protocolType);
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.
74 * @param protocol format: <protocol>:<network>:<contentFormat>:<additionalInfo>
76 * @return true if protocol in protocolSet
78 public static boolean testProtocol(String protocol, List<String> protocolSet) {
79 int index = protocol.lastIndexOf(":");
81 LOGGER.debug("Protocol {} not valid", protocol);
84 String p = protocol.toLowerCase().substring(0, index);
85 List<String> pSet = new ArrayList<>();
86 protocolSet.forEach(f -> {
87 int i = f.lastIndexOf(":");
89 LOGGER.debug("Protocol {} from set not valid", f);
91 pSet.add(f.toLowerCase().substring(0, i));
94 LOGGER.trace("Testing {} in {}", p, pSet);
95 return pSet.contains(p);
99 * Test if any of the UPnP protocols in protocolList can be found in a set of protocols.
101 * @param protocolList
103 * @return true if one of the protocols in protocolSet
105 public static boolean testProtocolList(List<String> protocolList, List<String> protocolSet) {
106 return protocolList.stream().anyMatch(p -> testProtocol(p, protocolSet));
110 * Return all UPnP protocols from protocolList that are part of a set of protocols.
112 * @param protocolList
114 * @return sublist of protocolList
116 public static List<String> getProtocols(List<String> protocolList, List<String> protocolSet) {
117 return protocolList.stream().filter(p -> testProtocol(p, protocolSet)).collect(Collectors.toList());