|
27 | 27 | import java.util.Optional; |
28 | 28 | import java.util.Set; |
29 | 29 | import java.util.WeakHashMap; |
| 30 | +import java.util.concurrent.CompletableFuture; |
30 | 31 | import java.util.concurrent.ConcurrentHashMap; |
31 | 32 | import java.util.concurrent.ConcurrentLinkedDeque; |
32 | 33 | import java.util.concurrent.ScheduledExecutorService; |
33 | 34 | import java.util.concurrent.ScheduledFuture; |
34 | 35 | import java.util.concurrent.TimeUnit; |
35 | 36 | import java.util.concurrent.atomic.AtomicBoolean; |
36 | 37 | import java.util.concurrent.atomic.AtomicReference; |
| 38 | +import java.util.function.Function; |
37 | 39 | import java.util.function.Supplier; |
38 | 40 |
|
39 | 41 | import org.eclipse.jdt.annotation.Nullable; |
@@ -510,14 +512,16 @@ private void handleRequestEvent(JSONObject data) { |
510 | 512 |
|
511 | 513 | Iterator<String> queryIterator = requestQueryJson.keys(); |
512 | 514 | // Add query parameters to URI builder, if any |
513 | | - newPath += "?"; |
514 | | - while (queryIterator.hasNext()) { |
515 | | - String queryName = queryIterator.next(); |
516 | | - newPath += queryName; |
517 | | - newPath += "="; |
518 | | - newPath += URLEncoder.encode(requestQueryJson.getString(queryName), "UTF-8"); |
519 | | - if (queryIterator.hasNext()) { |
520 | | - newPath += "&"; |
| 515 | + if (queryIterator.hasNext()) { |
| 516 | + newPath += "?"; |
| 517 | + while (queryIterator.hasNext()) { |
| 518 | + String queryName = queryIterator.next(); |
| 519 | + newPath += queryName; |
| 520 | + newPath += "="; |
| 521 | + newPath += URLEncoder.encode(requestQueryJson.getString(queryName), "UTF-8"); |
| 522 | + if (queryIterator.hasNext()) { |
| 523 | + newPath += "&"; |
| 524 | + } |
521 | 525 | } |
522 | 526 | } |
523 | 527 | // Finally get the future request URI |
@@ -870,6 +874,62 @@ public void sendItemUpdate(String itemName, String itemState) { |
870 | 874 | } |
871 | 875 | } |
872 | 876 |
|
| 877 | + /** |
| 878 | + * Register a webhook with the openHAB Cloud for the given local path. |
| 879 | + * |
| 880 | + * @param localPath the local path to forward webhook requests to |
| 881 | + * @param future the future to complete with the webhook URL or an error |
| 882 | + */ |
| 883 | + public void registerWebhook(String localPath, CompletableFuture<String> future) { |
| 884 | + emitWebhookEvent("webhook:register", localPath, future, response -> response.getString("webhookUrl"), |
| 885 | + "Webhook registration timed out"); |
| 886 | + } |
| 887 | + |
| 888 | + /** |
| 889 | + * Remove a webhook from the openHAB Cloud for the given local path. |
| 890 | + * |
| 891 | + * @param localPath the local path whose webhook should be removed |
| 892 | + * @param future the future to complete when the webhook is removed or on error |
| 893 | + */ |
| 894 | + public void removeWebhook(String localPath, CompletableFuture<Void> future) { |
| 895 | + emitWebhookEvent("webhook:remove", localPath, future, response -> null, "Webhook removal timed out"); |
| 896 | + } |
| 897 | + |
| 898 | + private <T> void emitWebhookEvent(String eventName, String localPath, CompletableFuture<T> future, |
| 899 | + Function<JSONObject, T> successHandler, String timeoutMessage) { |
| 900 | + if (!isConnected()) { |
| 901 | + future.completeExceptionally(new IOException("Not connected to openHAB Cloud")); |
| 902 | + return; |
| 903 | + } |
| 904 | + try { |
| 905 | + JSONObject data = new JSONObject(); |
| 906 | + data.put("localPath", localPath); |
| 907 | + socket.emit(eventName, data, (io.socket.client.Ack) args -> { |
| 908 | + try { |
| 909 | + if (args == null || args.length == 0 || !(args[0] instanceof JSONObject)) { |
| 910 | + future.completeExceptionally(new IOException("Missing or invalid response from openHAB Cloud")); |
| 911 | + return; |
| 912 | + } |
| 913 | + JSONObject response = (JSONObject) args[0]; |
| 914 | + if (response.optBoolean("success")) { |
| 915 | + future.complete(successHandler.apply(response)); |
| 916 | + } else { |
| 917 | + future.completeExceptionally(new IOException(response.optString("error", "Unknown error"))); |
| 918 | + } |
| 919 | + } catch (JSONException | ClassCastException e) { |
| 920 | + future.completeExceptionally(new IOException("Invalid response from cloud", e)); |
| 921 | + } |
| 922 | + }); |
| 923 | + scheduler.schedule(() -> { |
| 924 | + if (!future.isDone()) { |
| 925 | + future.completeExceptionally(new IOException(timeoutMessage)); |
| 926 | + } |
| 927 | + }, 30, TimeUnit.SECONDS); |
| 928 | + } catch (JSONException e) { |
| 929 | + future.completeExceptionally(new IOException("Failed to build webhook request", e)); |
| 930 | + } |
| 931 | + } |
| 932 | + |
873 | 933 | /** |
874 | 934 | * Returns true if openHAB Cloud connection is active |
875 | 935 | */ |
|
0 commit comments