]> git.basschouten.com Git - openhab-addons.git/blob
5ca7c68e6afbe0ae85276f107c1daa4ead9535e1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.deconz.internal.netutils;
14
15 import java.io.ByteArrayInputStream;
16 import java.net.URI;
17 import java.util.concurrent.CompletableFuture;
18 import java.util.concurrent.TimeUnit;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.eclipse.jetty.client.HttpClient;
23 import org.eclipse.jetty.client.HttpResponse;
24 import org.eclipse.jetty.client.api.Request;
25 import org.eclipse.jetty.client.util.BufferingResponseListener;
26 import org.eclipse.jetty.client.util.InputStreamContentProvider;
27 import org.eclipse.jetty.http.HttpMethod;
28
29 /**
30  * An asynchronous API for HTTP interactions.
31  *
32  * @author David Graeff - Initial contribution
33  */
34 @NonNullByDefault
35 public class AsyncHttpClient {
36
37     private final HttpClient client;
38
39     public AsyncHttpClient(HttpClient client) {
40         this.client = client;
41     }
42
43     /**
44      * Perform a POST request
45      *
46      * @param address The address
47      * @param jsonString The message body
48      * @param timeout A timeout
49      * @return The result
50      */
51     public CompletableFuture<Result> post(String address, String jsonString, int timeout) {
52         return doNetwork(HttpMethod.POST, address, jsonString, timeout);
53     }
54
55     /**
56      * Perform a PUT request
57      *
58      * @param address The address
59      * @param jsonString The message body
60      * @param timeout A timeout
61      * @return The result
62      */
63     public CompletableFuture<Result> put(String address, @Nullable String jsonString, int timeout) {
64         return doNetwork(HttpMethod.PUT, address, jsonString, timeout);
65     }
66
67     /**
68      * Perform a GET request
69      *
70      * @param address The address
71      * @param timeout A timeout
72      * @return The result
73      */
74     public CompletableFuture<Result> get(String address, int timeout) {
75         return doNetwork(HttpMethod.GET, address, null, timeout);
76     }
77
78     /**
79      * Perform a DELETE request
80      *
81      * @param address The address
82      * @param timeout A timeout
83      * @return The result
84      */
85     public CompletableFuture<Result> delete(String address, int timeout) {
86         return doNetwork(HttpMethod.DELETE, address, null, timeout);
87     }
88
89     private CompletableFuture<Result> doNetwork(HttpMethod method, String address, @Nullable String body, int timeout) {
90         final CompletableFuture<Result> f = new CompletableFuture<>();
91         Request request = client.newRequest(URI.create(address));
92         if (body != null) {
93             try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
94                     final InputStreamContentProvider inputStreamContentProvider = new InputStreamContentProvider(
95                             byteArrayInputStream)) {
96                 request.content(inputStreamContentProvider, "application/json");
97             } catch (Exception e) {
98                 f.completeExceptionally(e);
99                 return f;
100             }
101         }
102
103         request.method(method).timeout(timeout, TimeUnit.MILLISECONDS).send(new BufferingResponseListener() {
104             @NonNullByDefault({})
105             @Override
106             public void onComplete(org.eclipse.jetty.client.api.Result result) {
107                 final HttpResponse response = (HttpResponse) result.getResponse();
108                 if (result.getFailure() != null) {
109                     f.completeExceptionally(result.getFailure());
110                     return;
111                 }
112                 f.complete(new Result(getContentAsString(), response.getStatus()));
113             }
114         });
115         return f;
116     }
117
118     public static class Result {
119         private final String body;
120         private final int responseCode;
121
122         public Result(String body, int responseCode) {
123             this.body = body;
124             this.responseCode = responseCode;
125         }
126
127         public String getBody() {
128             return body;
129         }
130
131         public int getResponseCode() {
132             return responseCode;
133         }
134     }
135 }