]> git.basschouten.com Git - openhab-addons.git/blob
9ae3a76659f7a0e73de8c49d0610008587a91574
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.fronius.internal;
14
15 import java.io.IOException;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.io.net.http.HttpUtil;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * 
24  * A version of HttpUtil implementation that retries on failure
25  *
26  * @author Jimmy Tanagra - Initial contribution
27  * 
28  */
29 @NonNullByDefault
30 public class FroniusHttpUtil {
31     private static final Logger logger = LoggerFactory.getLogger(FroniusHttpUtil.class);
32
33     /**
34      * Issue a HTTP GET request and retry on failure
35      *
36      * @param url the url to execute
37      * @param timeout the socket timeout in milliseconds to wait for data
38      * @return the response body
39      * @throws FroniusCommunicationException when the request execution failed or interrupted
40      */
41     public static synchronized String executeUrl(String url, int timeout) throws FroniusCommunicationException {
42         int attemptCount = 1;
43         try {
44             while (true) {
45                 Throwable lastException = null;
46                 String result = null;
47                 try {
48                     result = HttpUtil.executeUrl("GET", url, timeout);
49                 } catch (IOException e) {
50                     // HttpUtil::executeUrl wraps InterruptedException into IOException.
51                     // Unwrap and rethrow it so that we don't retry on InterruptedException
52                     if (e.getCause() instanceof InterruptedException) {
53                         throw (InterruptedException) e.getCause();
54                     }
55                     lastException = e;
56                 }
57
58                 if (result != null) {
59                     if (attemptCount > 1) {
60                         logger.debug("Attempt #{} successful {}", attemptCount, url);
61                     }
62                     return result;
63                 }
64
65                 if (attemptCount >= 3) {
66                     logger.debug("Failed connecting to {} after {} attempts.", url, attemptCount, lastException);
67                     throw new FroniusCommunicationException("Unable to connect", lastException);
68                 }
69
70                 logger.debug("HTTP error on attempt #{} {}", attemptCount, url);
71                 Thread.sleep(500 * attemptCount);
72                 attemptCount++;
73             }
74         } catch (InterruptedException e) {
75             Thread.currentThread().interrupt();
76             throw new FroniusCommunicationException("Interrupted", e);
77         }
78     }
79 }