2 * Copyright (c) 2010-2022 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.konnected.internal;
15 import java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.nio.charset.StandardCharsets;
18 import java.util.Properties;
19 import java.util.concurrent.TimeUnit;
21 import org.openhab.core.io.net.http.HttpUtil;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * HTTP Get and Put reqeust class.
28 * @author Zachary Christiansen - Initial contribution
30 public class KonnectedHTTPUtils {
31 private final Logger logger = LoggerFactory.getLogger(KonnectedHTTPUtils.class);
32 private int requestTimeout;
33 private String logTest = "";
35 public KonnectedHTTPUtils(int requestTimeout) {
36 this.requestTimeout = (int) TimeUnit.SECONDS.toMillis(requestTimeout);
39 public void setRequestTimeout(int requestTimeout) {
40 this.requestTimeout = (int) TimeUnit.SECONDS.toMillis(requestTimeout);
44 * Sends a {@link doPut} request with a timeout of 30 seconds
46 * @param urlAddress the address to send the request
47 * @param payload the json payload to include with the request
49 private String doPut(String urlAddress, String payload) throws IOException {
50 logger.debug("The String url we want to put is : {}", urlAddress);
51 logger.debug("The payload we want to put is: {}", payload);
52 ByteArrayInputStream input = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
53 String retVal = HttpUtil.executeUrl("PUT", urlAddress, getHttpHeaders(), input, "application/json",
55 logger.trace("return value: {}", retVal);
59 protected Properties getHttpHeaders() {
60 Properties httpHeaders = new Properties();
61 httpHeaders.put("Content-Type", "application/json");
66 * Sends a {@link doGet} request with a timeout of 30 seconds
68 * @param urlAddress the address to send the request
71 private synchronized String doGet(String urlAddress) throws IOException {
72 logger.debug("The String url we want to get is : {}", urlAddress);
73 String retVal = HttpUtil.executeUrl("GET", urlAddress, requestTimeout);
74 logger.trace("return value: {}", retVal);
79 * Sends a {@link doGet} request with a timeout of 30 seconds
81 * @param urlAddress the address to send the request
82 * @param payload the json payload you want to send as part of the request
85 private synchronized String doGet(String urlAddress, String payload) throws IOException {
86 logger.debug("The String url we want to get is : {}", urlAddress);
87 ByteArrayInputStream input = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
88 String retVal = HttpUtil.executeUrl("GET", urlAddress, getHttpHeaders(), input, "application/json",
90 logger.trace("return value: {}", retVal);
95 * Sends a {@link doGet} request with a timeout of 30 seconds
97 * @param urlAddress the address to send the request
98 * @param payload the json payload you want to send as part of the request, may be null.
99 * @param retry the number of retries before throwing the IOexpcetion back to the handler
101 public synchronized String doGet(String urlAddress, String payload, int retryCount)
102 throws KonnectedHttpRetryExceeded {
103 String response = null;
108 if (payload == null) {
109 response = doGet(urlAddress, payload);
111 response = doGet(urlAddress);
114 } catch (IOException e) {
116 if (x > retryCount) {
117 throw new KonnectedHttpRetryExceeded("The number of retry attempts was exceeded", e.getCause());
125 * Sends a {@link doPut} request with a timeout of 30 seconds
127 * @param urlAddress the address to send the request
128 * @param payload the json payload you want to send as part of the request
129 * @param retry the number of retries before throwing the IOexpcetion back to the handler
131 public synchronized String doPut(String urlAddress, String payload, int retryCount)
132 throws KonnectedHttpRetryExceeded {
133 String response = null;
138 response = doPut(urlAddress, payload);
140 } catch (IOException e) {
142 if (x > retryCount) {
143 throw new KonnectedHttpRetryExceeded("The number of retry attempts was exceeded", e.getCause());