]> git.basschouten.com Git - openhab-addons.git/blob
1540d832f1aeca4619e5765e3981c6c2ddd44e73
[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.mielecloud.internal.webservice.retry;
14
15 import java.util.concurrent.ExecutionException;
16 import java.util.function.Consumer;
17 import java.util.function.Supplier;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.mielecloud.internal.auth.OAuthException;
22 import org.openhab.binding.mielecloud.internal.auth.OAuthTokenRefresher;
23 import org.openhab.binding.mielecloud.internal.webservice.ConnectionError;
24 import org.openhab.binding.mielecloud.internal.webservice.exception.AuthorizationFailedException;
25 import org.openhab.binding.mielecloud.internal.webservice.exception.MieleWebserviceException;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * The {@link AuthorizationFailedRetryStrategy} retries an operation after refreshing the access token in case of an
31  * authorization failure.
32  *
33  * @author Björn Lange - Initial contribution
34  */
35 @NonNullByDefault
36 public class AuthorizationFailedRetryStrategy implements RetryStrategy {
37     /**
38      * Message of exception thrown by the Jetty client in case of unmatching header fields and body content. E.g.
39      * application/json header with HTML body content. Mostly thrown when an invalid 401 response is received.
40      */
41     public static final String JETTY_401_HEADER_BODY_MISMATCH_EXCEPTION_MESSAGE = "org.eclipse.jetty.client.HttpResponseException: HTTP protocol violation: Authentication challenge without WWW-Authenticate header";
42
43     private final Logger logger = LoggerFactory.getLogger(this.getClass());
44
45     private final OAuthTokenRefresher tokenRefresher;
46     private final String serviceHandle;
47
48     public AuthorizationFailedRetryStrategy(OAuthTokenRefresher tokenRefresher, String serviceHandle) {
49         this.tokenRefresher = tokenRefresher;
50         this.serviceHandle = serviceHandle;
51     }
52
53     private void refreshToken() {
54         try {
55             logger.debug("Refreshing Miele OAuth access token.");
56             tokenRefresher.refreshToken(serviceHandle);
57             logger.debug("Miele OAuth access token has successfully been refreshed.");
58         } catch (OAuthException e) {
59             throw new MieleWebserviceException("Failed to refresh access token.", e,
60                     ConnectionError.AUTHORIZATION_FAILED);
61         }
62     }
63
64     @Override
65     public <@Nullable T> T performRetryableOperation(Supplier<T> operation, Consumer<Exception> onException) {
66         try {
67             return operation.get();
68         } catch (AuthorizationFailedException e) {
69             onException.accept(e);
70             refreshToken();
71         } catch (MieleWebserviceException e) {
72             // Workaround for HTML response from cloud in case of a 401 HTTP error.
73             var cause = e.getCause();
74             if (!(cause instanceof ExecutionException)) {
75                 throw e;
76             }
77
78             if (!JETTY_401_HEADER_BODY_MISMATCH_EXCEPTION_MESSAGE.equals(cause.getMessage())) {
79                 throw e;
80             }
81
82             onException.accept(e);
83             refreshToken();
84         }
85
86         try {
87             return operation.get();
88         } catch (AuthorizationFailedException e) {
89             throw new MieleWebserviceException("Request failed after access token renewal.", e,
90                     ConnectionError.AUTHORIZATION_FAILED);
91         }
92     }
93 }