]> git.basschouten.com Git - openhab-addons.git/blob
02221d0b2c11d50331ec319d2236dd47d4f1bbb6
[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.kostalinverter.internal.thirdgeneration;
14
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.jetty.client.HttpClient;
21 import org.eclipse.jetty.client.api.ContentResponse;
22 import org.eclipse.jetty.client.api.Request;
23 import org.eclipse.jetty.client.util.StringContentProvider;
24 import org.eclipse.jetty.http.HttpHeader;
25 import org.eclipse.jetty.http.HttpMethod;
26 import org.eclipse.jetty.http.HttpVersion;
27
28 import com.google.gson.FieldNamingPolicy;
29 import com.google.gson.Gson;
30 import com.google.gson.GsonBuilder;
31 import com.google.gson.JsonArray;
32 import com.google.gson.JsonElement;
33 import com.google.gson.JsonObject;
34
35 /**
36  * The {@link ThirdGenerationHttpHelper} is handling http communication with the device
37  * handlers.
38  *
39  * @author RenĂ© Stakemeier - Initial contribution
40  */
41 final class ThirdGenerationHttpHelper {
42
43     private ThirdGenerationHttpHelper() {
44     }
45
46     // base URL of the web api
47     private static final String WEB_API = "/api/v1";
48     // GSON handler
49     private static final Gson GSON = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
50             .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
51
52     /**
53      * Helper function to execute a HTTP post request
54      *
55      * @param httpClient httpClient to use for communication
56      * @param url IP or hostname or the device
57      * @param resource web API resource to post to
58      * @param parameters the JSON content to post
59      * @return the HTTP response for the created post request
60      * @throws ExecutionException Error during the execution of the http request
61      * @throws TimeoutException Connection timed out
62      * @throws InterruptedException Connection interrupted
63      */
64     static ContentResponse executeHttpPost(HttpClient httpClient, String url, String resource, JsonObject parameters)
65             throws InterruptedException, TimeoutException, ExecutionException {
66         return executeHttpPost(httpClient, url, resource, parameters, null);
67     }
68
69     /**
70      * Helper function to execute a HTTP post request
71      *
72      * @param httpClient httpClient to use for communication
73      * @param url IP or hostname or the device
74      * @param resource web API resource to post to
75      * @param sessionId optional session ID
76      * @param parameters the JSON content to post
77      * @return the HTTP response for the created post request
78      * @throws ExecutionException Error during the execution of the http request
79      * @throws TimeoutException Connection timed out
80      * @throws InterruptedException Connection interrupted
81      */
82     static ContentResponse executeHttpPost(HttpClient httpClient, String url, String resource, JsonElement parameters,
83             @Nullable String sessionId) throws InterruptedException, TimeoutException, ExecutionException {
84         Request response = httpClient.newRequest(String.format("%s/%s%s", url, WEB_API, resource), 80).scheme("http")
85                 .agent("Jetty HTTP client").version(HttpVersion.HTTP_1_1).method(HttpMethod.POST)
86                 .header(HttpHeader.ACCEPT, "application/json").header(HttpHeader.CONTENT_TYPE, "application/json")
87                 .timeout(5, TimeUnit.SECONDS);
88         response.content(new StringContentProvider(parameters.toString()));
89         if (sessionId != null) {
90             response.header(HttpHeader.AUTHORIZATION, String.format("Session %s", sessionId));
91         }
92         return response.send();
93     }
94
95     /**
96      * Helper function to execute a HTTP get request
97      *
98      * @param httpClient httpClient to use for communication
99      * @param url IP or hostname or the device
100      * @param resource web API resource to get
101      * @return the HTTP response for the created get request
102      * @throws ExecutionException Error during the execution of the http request
103      * @throws TimeoutException Connection timed out
104      * @throws InterruptedException Connection interrupted
105      */
106     static ContentResponse executeHttpGet(HttpClient httpClient, String url, String resource)
107             throws InterruptedException, TimeoutException, ExecutionException {
108         return executeHttpGet(httpClient, url, resource, null);
109     }
110
111     /**
112      * Helper function to execute a HTTP get request
113      *
114      * @param httpClient httpClient to use for communication
115      * @param url IP or hostname or the device
116      * @param resource web API resource to get
117      * @param sessionId optional session ID
118      * @return the HTTP response for the created get request
119      * @throws ExecutionException Error during the execution of the http request
120      * @throws TimeoutException Connection timed out
121      * @throws InterruptedException Connection interrupted
122      * @throws Exception thrown if there are communication problems
123      */
124     static ContentResponse executeHttpGet(HttpClient httpClient, String url, String resource,
125             @Nullable String sessionId) throws InterruptedException, TimeoutException, ExecutionException {
126         Request response = httpClient.newRequest(String.format("%s/%s%s", url, WEB_API, resource), 80).scheme("http")
127                 .agent("Jetty HTTP client").version(HttpVersion.HTTP_1_1).method(HttpMethod.GET)
128                 .header(HttpHeader.ACCEPT, "application/json").header(HttpHeader.CONTENT_TYPE, "application/json")
129                 .timeout(5, TimeUnit.SECONDS);
130         if (sessionId != null) {
131             response.header(HttpHeader.AUTHORIZATION, String.format("Session %s", sessionId));
132         }
133         return response.send();
134     }
135
136     /**
137      * Helper to extract the JsonArray from a HTTP response.
138      * Use only, if you expect a JsonArray and no other types (e.g. JSON array)!
139      *
140      * @param reponse the HTTP response
141      * @return the JSON object
142      */
143     static JsonArray getJsonArrayFromResponse(ContentResponse reponse) {
144         return GSON.fromJson(reponse.getContentAsString(), JsonArray.class);
145     }
146
147     /**
148      * Helper to extract the JSON object from a HTTP response.
149      * Use only, if you expect a JSON object and no other types (e.g. JSON array)!
150      *
151      * @param reponse the HTTP response
152      * @return the JSON object
153      */
154     static JsonObject getJsonObjectFromResponse(ContentResponse reponse) {
155         return GSON.fromJson(reponse.getContentAsString(), JsonObject.class);
156     }
157 }