/**
 * Copyright (c) 2010-2019 by the respective copyright holders.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.openhab.io.hueemulation.internal;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;

import javax.servlet.http.HttpServletResponse;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
 * Network utility methods
 *
 * @author David Graeff - Initial contribution
 */
@NonNullByDefault
public class Utils {
    /**
     * Sets Hue API Headers
     */
    static void setHeaders(HttpServletResponse response) {
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    }

    /**
     * Try to get the ethernet interface MAC for the network interface that belongs to the given IP address.
     * Returns a default MAC on any failure.
     *
     * @param address IP address
     * @return A MAC of the form "00:00:88:00:bb:ee"
     */
    static String getMAC(InetAddress address) {
        NetworkInterface networkInterface;
        final byte[] mac;
        try {
            networkInterface = NetworkInterface.getByInetAddress(address);
            mac = networkInterface.getHardwareAddress();
            if (mac == null) {
                return "00:00:88:00:bb:ee";
            }
        } catch (SocketException e) {
            return "00:00:88:00:bb:ee";
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
        }
        return sb.toString();
    }

}

@@ -1705,16 +1705,118 @@
 dress);%0A
+            if (networkInterface == null) %7B%0A                return %2200:00:88:00:bb:ee%22;%0A            %7D%0A
         

Fix NullPointerException in Utils.getMAC (#4570)