]> git.basschouten.com Git - openhab-addons.git/blob
595db4425d9b8728779f41774a16fcb32a805e36
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.konnected.internal;
14
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;
20
21 import org.openhab.core.io.net.http.HttpUtil;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * HTTP Get and Put reqeust class.
27  *
28  * @author Zachary Christiansen - Initial contribution
29  */
30 public class KonnectedHTTPUtils {
31     private final Logger logger = LoggerFactory.getLogger(KonnectedHTTPUtils.class);
32     private int requestTimeout;
33     private String logTest = "";
34
35     public KonnectedHTTPUtils(int requestTimeout) {
36         this.requestTimeout = (int) TimeUnit.SECONDS.toMillis(requestTimeout);
37     }
38
39     public void setRequestTimeout(int requestTimeout) {
40         this.requestTimeout = (int) TimeUnit.SECONDS.toMillis(requestTimeout);
41     }
42
43     /**
44      * Sends a {@link doPut} request with a timeout of 30 seconds
45      *
46      * @param urlAddress the address to send the request
47      * @param payload the json payload to include with the request
48      */
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",
54                 requestTimeout);
55         logger.trace("return value: {}", retVal);
56         return retVal;
57     }
58
59     protected Properties getHttpHeaders() {
60         Properties httpHeaders = new Properties();
61         httpHeaders.put("Content-Type", "application/json");
62         return httpHeaders;
63     }
64
65     /**
66      * Sends a {@link doGet} request with a timeout of 30 seconds
67      *
68      * @param urlAddress the address to send the request
69      */
70
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);
75         return retVal;
76     }
77
78     /**
79      * Sends a {@link doGet} request with a timeout of 30 seconds
80      *
81      * @param urlAddress the address to send the request
82      * @param payload the json payload you want to send as part of the request
83      */
84
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",
89                 requestTimeout);
90         logger.trace("return value: {}", retVal);
91         return retVal;
92     }
93
94     /**
95      * Sends a {@link doGet} request with a timeout of 30 seconds
96      *
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
100      */
101     public synchronized String doGet(String urlAddress, String payload, int retryCount)
102             throws KonnectedHttpRetryExceeded {
103         String response = null;
104         int x = 0;
105         Boolean loop = true;
106         while (loop) {
107             try {
108                 if (payload == null) {
109                     response = doGet(urlAddress, payload);
110                 } else {
111                     response = doGet(urlAddress);
112                 }
113                 loop = false;
114             } catch (IOException e) {
115                 x++;
116                 if (x > retryCount) {
117                     throw new KonnectedHttpRetryExceeded("The number of retry attempts was exceeded", e.getCause());
118                 }
119             }
120         }
121         return response;
122     }
123
124     /**
125      * Sends a {@link doPut} request with a timeout of 30 seconds
126      *
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
130      */
131     public synchronized String doPut(String urlAddress, String payload, int retryCount)
132             throws KonnectedHttpRetryExceeded {
133         String response = null;
134         int x = 0;
135         Boolean loop = true;
136         while (loop) {
137             try {
138                 response = doPut(urlAddress, payload);
139                 loop = false;
140             } catch (IOException e) {
141                 x++;
142                 if (x > retryCount) {
143                     throw new KonnectedHttpRetryExceeded("The number of retry attempts was exceeded", e.getCause());
144                 }
145             }
146         }
147         return response;
148     }
149 }