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.mielecloud.internal.webservice.retry;
15 import java.util.concurrent.ExecutionException;
16 import java.util.function.Consumer;
17 import java.util.function.Supplier;
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;
30 * The {@link AuthorizationFailedRetryStrategy} retries an operation after refreshing the access token in case of an
31 * authorization failure.
33 * @author Björn Lange - Initial contribution
36 public class AuthorizationFailedRetryStrategy implements RetryStrategy {
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.
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";
43 private final Logger logger = LoggerFactory.getLogger(this.getClass());
45 private final OAuthTokenRefresher tokenRefresher;
46 private final String serviceHandle;
48 public AuthorizationFailedRetryStrategy(OAuthTokenRefresher tokenRefresher, String serviceHandle) {
49 this.tokenRefresher = tokenRefresher;
50 this.serviceHandle = serviceHandle;
53 private void refreshToken() {
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);
65 public <@Nullable T> T performRetryableOperation(Supplier<T> operation, Consumer<Exception> onException) {
67 return operation.get();
68 } catch (AuthorizationFailedException e) {
69 onException.accept(e);
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)) {
78 if (!JETTY_401_HEADER_BODY_MISMATCH_EXCEPTION_MESSAGE.equals(cause.getMessage())) {
82 onException.accept(e);
87 return operation.get();
88 } catch (AuthorizationFailedException e) {
89 throw new MieleWebserviceException("Request failed after access token renewal.", e,
90 ConnectionError.AUTHORIZATION_FAILED);