2 * Copyright (c) 2010-2024 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.samsungtv.internal;
15 import java.io.IOException;
16 import java.io.StringReader;
17 import java.util.Base64;
18 import java.util.Objects;
19 import java.util.Optional;
21 import javax.xml.parsers.DocumentBuilderFactory;
22 import javax.xml.parsers.ParserConfigurationException;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.jupnp.model.meta.RemoteDevice;
27 import org.openhab.core.types.Command;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.w3c.dom.Document;
31 import org.xml.sax.InputSource;
32 import org.xml.sax.SAXException;
35 * The {@link Utils} is a collection of static utilities
37 * @author Nick Waterton - Initial contribution
41 private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class);
42 public static DocumentBuilderFactory factory = getDocumentBuilder();
44 private static DocumentBuilderFactory getDocumentBuilder() {
45 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
47 // see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
48 factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
49 factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
50 factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
51 factory.setXIncludeAware(false);
52 factory.setExpandEntityReferences(false);
53 } catch (ParserConfigurationException e) {
54 LOGGER.debug("XMLParser Configuration Error: {}", e.getMessage());
60 * Build {@link Document} from {@link String} which contains XML content.
63 * {@link String} which contains XML content.
64 * @return {@link Optional Document} or empty if convert has failed.
66 public static Optional<Document> loadXMLFromString(String xml, String host) {
68 return Optional.ofNullable(factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml))));
69 } catch (ParserConfigurationException | SAXException | IOException e) {
70 LOGGER.debug("{}: Error loading XML: {}", host, e.getMessage());
72 return Optional.empty();
75 public static boolean isSoundChannel(String name) {
76 return (name.contains("Volume") || name.contains("Mute"));
79 public static String b64encode(String str) {
80 return Base64.getUrlEncoder().encodeToString(str.getBytes());
83 public static String truncCmd(Command command) {
84 String cmd = command.toString();
85 return (cmd.length() <= 80) ? cmd : cmd.substring(0, 80) + "...";
88 public static String getModelName(@Nullable RemoteDevice device) {
89 return Objects.requireNonNull(Optional.ofNullable(device).map(a -> a.getDetails()).map(a -> a.getModelDetails())
90 .map(a -> a.getModelName()).orElse(""));
93 public static String getManufacturer(@Nullable RemoteDevice device) {
94 return Objects.requireNonNull(Optional.ofNullable(device).map(a -> a.getDetails())
95 .map(a -> a.getManufacturerDetails()).map(a -> a.getManufacturer()).orElse(""));
98 public static String getFriendlyName(@Nullable RemoteDevice device) {
99 return Objects.requireNonNull(
100 Optional.ofNullable(device).map(a -> a.getDetails()).map(a -> a.getFriendlyName()).orElse(""));
103 public static String getUdn(@Nullable RemoteDevice device) {
104 return Objects.requireNonNull(Optional.ofNullable(device).map(a -> a.getIdentity()).map(a -> a.getUdn())
105 .map(a -> a.getIdentifierString()).orElse(""));
108 public static String getHost(@Nullable RemoteDevice device) {
109 return Objects.requireNonNull(Optional.ofNullable(device).map(a -> a.getIdentity())
110 .map(a -> a.getDescriptorURL()).map(a -> a.getHost()).orElse(""));
113 public static String getType(@Nullable RemoteDevice device) {
115 .requireNonNull(Optional.ofNullable(device).map(a -> a.getType()).map(a -> a.getType()).orElse(""));