]> git.basschouten.com Git - openhab-addons.git/blob
5b5029725565b5189a675f4a7e4981232fc1b3f7
[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.Objects;
19 import java.util.Optional;
20
21 import javax.xml.parsers.DocumentBuilderFactory;
22 import javax.xml.parsers.ParserConfigurationException;
23
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;
33
34 /**
35  * The {@link Utils} is a collection of static utilities
36  *
37  * @author Nick Waterton - Initial contribution
38  */
39 @NonNullByDefault
40 public class Utils {
41     private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class);
42     public static DocumentBuilderFactory factory = getDocumentBuilder();
43
44     private static DocumentBuilderFactory getDocumentBuilder() {
45         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
46         try {
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());
55         }
56         return factory;
57     }
58
59     /**
60      * Build {@link Document} from {@link String} which contains XML content.
61      *
62      * @param xml
63      *            {@link String} which contains XML content.
64      * @return {@link Optional Document} or empty if convert has failed.
65      */
66     public static Optional<Document> loadXMLFromString(String xml, String host) {
67         try {
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());
71         }
72         return Optional.empty();
73     }
74
75     public static boolean isSoundChannel(String name) {
76         return (name.contains("Volume") || name.contains("Mute"));
77     }
78
79     public static String b64encode(String str) {
80         return Base64.getUrlEncoder().encodeToString(str.getBytes());
81     }
82
83     public static String truncCmd(Command command) {
84         String cmd = command.toString();
85         return (cmd.length() <= 80) ? cmd : cmd.substring(0, 80) + "...";
86     }
87
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(""));
91     }
92
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(""));
96     }
97
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(""));
101     }
102
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(""));
106     }
107
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(""));
111     }
112
113     public static String getType(@Nullable RemoteDevice device) {
114         return Objects
115                 .requireNonNull(Optional.ofNullable(device).map(a -> a.getType()).map(a -> a.getType()).orElse(""));
116     }
117 }