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