2 * Copyright (c) 2010-2024 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.fronius.internal;
15 import java.io.IOException;
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;
24 * A version of HttpUtil implementation that retries on failure
26 * @author Jimmy Tanagra - Initial contribution
30 public class FroniusHttpUtil {
31 private static final Logger logger = LoggerFactory.getLogger(FroniusHttpUtil.class);
34 * Issue a HTTP GET request and retry on failure
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
41 public static synchronized String executeUrl(String url, int timeout) throws FroniusCommunicationException {
45 Throwable lastException = null;
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();
59 if (attemptCount > 1) {
60 logger.debug("Attempt #{} successful {}", attemptCount, url);
65 if (attemptCount >= 3) {
66 logger.debug("Failed connecting to {} after {} attempts.", url, attemptCount, lastException);
67 throw new FroniusCommunicationException("Unable to connect", lastException);
70 logger.debug("HTTP error on attempt #{} {}", attemptCount, url);
71 Thread.sleep(500 * attemptCount);
74 } catch (InterruptedException e) {
75 Thread.currentThread().interrupt();
76 throw new FroniusCommunicationException("Interrupted", e);