]> git.basschouten.com Git - openhab-addons.git/blob
0d14e52104e14405c800333769c5ec30478bbff7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.avmfritz.internal.hardware.callbacks;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jetty.http.HttpMethod;
17 import org.openhab.binding.avmfritz.internal.hardware.FritzAhaWebInterface;
18
19 /**
20  * Callback implementation for reauthorization and retry
21  *
22  * @author Robert Bausdorf, Christian Brauers - Initial contribution
23  */
24 @NonNullByDefault
25 public class FritzAhaReauthCallback implements FritzAhaCallback {
26
27     public static final String WEBSERVICE_PATH = "webservices/homeautoswitch.lua";
28     /**
29      * Path to HTTP interface
30      */
31     private final String path;
32     /**
33      * Arguments to use
34      */
35     private final String args;
36     /**
37      * Web interface to use
38      */
39     private final FritzAhaWebInterface webIface;
40     /**
41      * Method used
42      */
43     private final HttpMethod httpMethod;
44     /**
45      * Number of remaining retries
46      */
47     private int retries;
48     /**
49      * Callback to execute on next retry
50      */
51     private FritzAhaCallback retryCallback;
52     /**
53      * Whether the request returned a valid response
54      */
55     private boolean validRequest;
56
57     /**
58      * Returns whether the request returned a valid response
59      *
60      * @return Validity of response
61      */
62     public boolean isValidRequest() {
63         return validRequest;
64     }
65
66     /**
67      * Returns whether there will be another retry on an invalid response
68      *
69      * @return true if there will be no more retries, false otherwise
70      */
71     public boolean isFinalAttempt() {
72         return retries <= 0;
73     }
74
75     /**
76      * Sets different Callback to use on retry (initial value: same callback after decremented retry counter)
77      *
78      * @param retryCallback Callback to retry with
79      */
80     public void setRetryCallback(FritzAhaCallback retryCallback) {
81         this.retryCallback = retryCallback;
82     }
83
84     @Override
85     public String getPath() {
86         return path;
87     }
88
89     @Override
90     public String getArgs() {
91         return args;
92     }
93
94     public FritzAhaWebInterface getWebIface() {
95         return webIface;
96     }
97
98     @Override
99     public void execute(int status, String response) {
100         if (status != 200 || "".equals(response) || ".".equals(response)) {
101             validRequest = false;
102             if (retries >= 1) {
103                 webIface.authenticate();
104                 retries--;
105                 switch (httpMethod) {
106                     case GET:
107                         webIface.asyncGet(path, args, retryCallback);
108                         break;
109                     case POST:
110                         webIface.asyncPost(path, args, retryCallback);
111                         break;
112                     default:
113                         break;
114                 }
115             }
116         } else {
117             validRequest = true;
118         }
119     }
120
121     /**
122      * Constructor for retryable authentication
123      *
124      * @param path
125      *            Path to HTTP interface
126      * @param args
127      *            Arguments to use
128      * @param webIface
129      *            Web interface to use
130      * @param httpMethod
131      *            Method used
132      * @param retries
133      *            Number of retries
134      */
135     public FritzAhaReauthCallback(String path, String args, FritzAhaWebInterface webIface, HttpMethod httpMethod,
136             int retries) {
137         this.path = path;
138         this.args = args;
139         this.webIface = webIface;
140         this.httpMethod = httpMethod;
141         this.retries = retries;
142         retryCallback = this;
143     }
144 }