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.avmfritz.internal.hardware.callbacks;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jetty.http.HttpMethod;
17 import org.openhab.binding.avmfritz.internal.hardware.FritzAhaWebInterface;
20 * Callback implementation for reauthorization and retry
22 * @author Robert Bausdorf, Christian Brauers - Initial contribution
25 public class FritzAhaReauthCallback implements FritzAhaCallback {
27 public static final String WEBSERVICE_PATH = "webservices/homeautoswitch.lua";
29 * Path to HTTP interface
31 private final String path;
35 private final String args;
37 * Web interface to use
39 private final FritzAhaWebInterface webIface;
43 private final HttpMethod httpMethod;
45 * Number of remaining retries
49 * Callback to execute on next retry
51 private FritzAhaCallback retryCallback;
53 * Whether the request returned a valid response
55 private boolean validRequest;
58 * Returns whether the request returned a valid response
60 * @return Validity of response
62 public boolean isValidRequest() {
67 * Returns whether there will be another retry on an invalid response
69 * @return true if there will be no more retries, false otherwise
71 public boolean isFinalAttempt() {
76 * Sets different Callback to use on retry (initial value: same callback after decremented retry counter)
78 * @param retryCallback Callback to retry with
80 public void setRetryCallback(FritzAhaCallback retryCallback) {
81 this.retryCallback = retryCallback;
85 public String getPath() {
90 public String getArgs() {
94 public FritzAhaWebInterface getWebIface() {
99 public void execute(int status, String response) {
100 if (status != 200 || "".equals(response) || ".".equals(response)) {
101 validRequest = false;
103 webIface.authenticate();
105 switch (httpMethod) {
107 webIface.asyncGet(path, args, retryCallback);
110 webIface.asyncPost(path, args, retryCallback);
122 * Constructor for retryable authentication
125 * Path to HTTP interface
129 * Web interface to use
135 public FritzAhaReauthCallback(String path, String args, FritzAhaWebInterface webIface, HttpMethod httpMethod,
139 this.webIface = webIface;
140 this.httpMethod = httpMethod;
141 this.retries = retries;
142 retryCallback = this;