2 * Copyright (c) 2010-2020 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
14 package org.openhab.binding.ipcamera.internal;
16 import java.io.UnsupportedEncodingException;
17 import java.net.InetAddress;
18 import java.net.NetworkInterface;
19 import java.net.SocketException;
20 import java.net.URLEncoder;
21 import java.util.Enumeration;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
26 * The {@link Helper} class has static functions that help the IpCamera binding not need as many external libs.
29 * @author Matthew Skinner - Initial contribution
35 * The {@link searchString} Used to grab values out of JSON or other quote encapsulated structures without needing
36 * an external lib. String may be terminated by ," or }.
38 * @author Matthew Skinner - Initial contribution
40 public static String searchString(String rawString, String searchedString) {
43 index = rawString.indexOf(searchedString);
44 if (index != -1) // -1 means "not found"
46 result = rawString.substring(index + searchedString.length(), rawString.length());
47 index = result.indexOf(',');
49 index = result.indexOf('"');
51 index = result.indexOf('}');
55 return result.substring(0, index);
58 return result.substring(0, index);
61 result = result.substring(0, index);
62 index = result.indexOf('"');
66 return result.substring(0, index);
73 public static String fetchXML(String message, String sectionHeading, String key) {
75 int sectionHeaderBeginning = 0;
76 if (!sectionHeading.isEmpty()) {// looking for a sectionHeading
77 sectionHeaderBeginning = message.indexOf(sectionHeading);
79 if (sectionHeaderBeginning == -1) {
82 int startIndex = message.indexOf(key, sectionHeaderBeginning + sectionHeading.length());
83 if (startIndex == -1) {
86 int endIndex = message.indexOf("<", startIndex + key.length());
87 if (endIndex > startIndex) {
88 result = message.substring(startIndex + key.length(), endIndex);
90 // remove any quotes and anything after the quote.
91 sectionHeaderBeginning = result.indexOf("\"");
92 if (sectionHeaderBeginning > 0) {
93 result = result.substring(0, sectionHeaderBeginning);
95 // remove any ">" and anything after it.
96 sectionHeaderBeginning = result.indexOf(">");
97 if (sectionHeaderBeginning > 0) {
98 result = result.substring(0, sectionHeaderBeginning);
104 * The {@link encodeSpecialChars} Is used to replace spaces with %20 in Strings meant for URL queries.
106 * @author Matthew Skinner - Initial contribution
108 public static String encodeSpecialChars(String text) {
109 String processed = text;
111 processed = URLEncoder.encode(text, "UTF-8").replace("+", "%20");
112 } catch (UnsupportedEncodingException e) {
117 public static String getLocalIpAddress() {
118 String ipAddress = "";
120 for (Enumeration<NetworkInterface> enumNetworks = NetworkInterface.getNetworkInterfaces(); enumNetworks
121 .hasMoreElements();) {
122 NetworkInterface networkInterface = enumNetworks.nextElement();
123 for (Enumeration<InetAddress> enumIpAddr = networkInterface.getInetAddresses(); enumIpAddr
124 .hasMoreElements();) {
125 InetAddress inetAddress = enumIpAddr.nextElement();
126 if (!inetAddress.isLoopbackAddress() && inetAddress.getHostAddress().toString().length() < 18
127 && inetAddress.isSiteLocalAddress()) {
128 ipAddress = inetAddress.getHostAddress().toString();
132 } catch (SocketException ex) {