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.binding.kostalinverter.internal.thirdgeneration;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
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;
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;
36 * The {@link ThirdGenerationHttpHelper} is handling http communication with the device
39 * @author René Stakemeier - Initial contribution
41 final class ThirdGenerationHttpHelper {
43 private ThirdGenerationHttpHelper() {
46 // base URL of the web api
47 private static final String WEB_API = "/api/v1";
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();
53 * Helper function to execute a HTTP post request
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
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);
70 * Helper function to execute a HTTP post request
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
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));
92 return response.send();
96 * Helper function to execute a HTTP get request
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
106 static ContentResponse executeHttpGet(HttpClient httpClient, String url, String resource)
107 throws InterruptedException, TimeoutException, ExecutionException {
108 return executeHttpGet(httpClient, url, resource, null);
112 * Helper function to execute a HTTP get request
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
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));
133 return response.send();
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)!
140 * @param reponse the HTTP response
141 * @return the JSON object
143 static JsonArray getJsonArrayFromResponse(ContentResponse reponse) {
144 return GSON.fromJson(reponse.getContentAsString(), JsonArray.class);
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)!
151 * @param reponse the HTTP response
152 * @return the JSON object
154 static JsonObject getJsonObjectFromResponse(ContentResponse reponse) {
155 return GSON.fromJson(reponse.getContentAsString(), JsonObject.class);