]> git.basschouten.com Git - openhab-addons.git/blob
0c98f72ac6b138489994c3df7e9cf6f4a133f093
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.mercedesme.internal.server;
14
15 import java.net.InetAddress;
16 import java.net.NetworkInterface;
17 import java.net.SocketException;
18 import java.util.ArrayList;
19 import java.util.Enumeration;
20 import java.util.List;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.mercedesme.internal.Constants;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * The {@link Utils} class defines an HTTP Server for authentication callbacks
29  *
30  * @author Bernd Weymann - Initial contribution
31  */
32 @NonNullByDefault
33 public class Utils {
34     private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class);
35     private static final List<Integer> PORTS = new ArrayList<Integer>();
36     private static int port = 8090;
37
38     /**
39      * Get free port without other Thread interference
40      *
41      * @return
42      */
43     public static synchronized int getFreePort() {
44         while (PORTS.contains(port)) {
45             port++;
46         }
47         PORTS.add(port);
48         return port;
49     }
50
51     public static synchronized void addPort(int portNr) {
52         if (PORTS.contains(portNr)) {
53             LOGGER.warn("Port {} already occupied", portNr);
54         }
55         PORTS.add(portNr);
56     }
57
58     public static synchronized void removePort(int portNr) {
59         PORTS.remove(Integer.valueOf(portNr));
60     }
61
62     public static String getCallbackIP() throws SocketException {
63         // https://stackoverflow.com/questions/1062041/ip-address-not-obtained-in-java
64         for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces
65                 .hasMoreElements();) {
66             NetworkInterface iface = ifaces.nextElement();
67             try {
68                 if (!iface.isLoopback()) {
69                     if (iface.isUp()) {
70                         for (Enumeration<InetAddress> addresses = iface.getInetAddresses(); addresses
71                                 .hasMoreElements();) {
72                             InetAddress address = addresses.nextElement();
73                             return address.getHostAddress();
74                         }
75                     }
76                 }
77             } catch (SocketException se) {
78                 // Calling one network interface failed - continue searching
79                 LOGGER.trace("Network {} failed {}", iface.getName(), se.getMessage());
80             }
81         }
82         throw new SocketException("IP address not detected");
83     }
84
85     public static String getCallbackAddress(String callbackIP, int callbackPort) {
86         return "http://" + callbackIP + Constants.COLON + callbackPort + Constants.CALLBACK_ENDPOINT;
87     }
88 }