2 * Copyright (c) 2010-2023 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
13 package org.openhab.io.hueemulation.internal;
15 import java.net.InetAddress;
16 import java.net.NetworkInterface;
17 import java.net.SocketException;
18 import java.nio.charset.StandardCharsets;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
23 import javax.ws.rs.core.Response;
24 import javax.ws.rs.core.Response.ResponseBuilder;
25 import javax.ws.rs.core.UriInfo;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.io.hueemulation.internal.dto.response.HueResponse;
30 import org.openhab.io.hueemulation.internal.dto.response.HueResponse.HueErrorMessage;
31 import org.openhab.io.hueemulation.internal.dto.response.HueResponseSuccessSimple;
32 import org.openhab.io.hueemulation.internal.dto.response.HueSuccessGeneric;
34 import com.google.gson.Gson;
35 import com.google.gson.reflect.TypeToken;
38 * Network utility methods
40 * @author David Graeff - Initial contribution
43 public class NetworkUtils {
45 * Try to get the ethernet interface MAC for the network interface that belongs to the given IP address.
46 * Returns a default MAC on any failure.
48 * @param address IP address
49 * @return A MAC of the form "00:00:88:00:bb:ee"
51 static String getMAC(InetAddress address) {
52 NetworkInterface networkInterface;
55 networkInterface = NetworkInterface.getByInetAddress(address);
56 if (networkInterface == null) {
57 return "00:00:88:00:bb:ee";
59 mac = networkInterface.getHardwareAddress();
61 return "00:00:88:00:bb:ee";
63 } catch (SocketException e) {
64 return "00:00:88:00:bb:ee";
67 StringBuilder sb = new StringBuilder();
68 for (int i = 0; i < mac.length; i++) {
69 sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
75 * Adds cors headers to the given response and returns it.
77 public static ResponseBuilder ResponseWithCors(ResponseBuilder response) {
78 return response.encoding(StandardCharsets.UTF_8.name()) //
79 .header("Access-Control-Allow-Origin", "*")
80 .header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization")
81 .header("Access-Control-Allow-Credentials", "true")
82 .header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD")
83 .header("Access-Control-Max-Age", "1209600");
87 * Creates a json response with the correct Hue error code
89 * @param gson A gson instance
90 * @param uri The original uri of the request
91 * @param type Any of HueResponse.*
92 * @param message A message
95 public static Response singleError(Gson gson, UriInfo uri, int type, @Nullable String message) {
96 HueResponse e = new HueResponse(
97 new HueErrorMessage(type, uri.getPath().replace("/api", ""), message != null ? message : ""));
98 String str = gson.toJson(Collections.singleton(e), new TypeToken<List<?>>() {
102 case HueResponse.UNAUTHORIZED:
105 case HueResponse.METHOD_NOT_ALLOWED:
108 case HueResponse.NOT_AVAILABLE:
111 case HueResponse.ARGUMENTS_INVALID:
112 case HueResponse.LINK_BUTTON_NOT_PRESSED:
116 return Response.status(httpCode).entity(str).build();
119 public static Response singleSuccess(Gson gson, String message, String uriPart) {
120 List<HueResponse> responses = new ArrayList<>();
121 responses.add(new HueResponse(new HueSuccessGeneric(message, uriPart)));
122 return Response.ok(gson.toJson(responses, new TypeToken<List<?>>() {
123 }.getType())).build();
126 public static Response singleSuccess(Gson gson, String message) {
127 List<HueResponseSuccessSimple> responses = new ArrayList<>();
128 responses.add(new HueResponseSuccessSimple(message));
129 return Response.ok(gson.toJson(responses, new TypeToken<List<?>>() {
130 }.getType())).build();
133 public static Response successList(Gson gson, List<HueSuccessGeneric> successList) {
134 List<HueResponse> responses = new ArrayList<>();
135 for (HueSuccessGeneric s : successList) {
137 responses.add(new HueResponse(s));
140 return Response.ok(gson.toJson(responses, new TypeToken<List<?>>() {
141 }.getType())).build();