com.ionoscloud.s3.credentials.ChainedProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ionos-cloud-sdk-s3 Show documentation
Show all versions of ionos-cloud-sdk-s3 Show documentation
IONOS Java SDK for Amazon S3 Compatible Cloud Storage
The newest version!
package com.ionoscloud.s3.credentials;
import java.security.ProviderException;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Nonnull;
/** Chained credential provider work with list of credential providers. */
public class ChainedProvider implements Provider {
private final List providers;
private Provider currentProvider;
private Credentials credentials;
public ChainedProvider(@Nonnull Provider... providers) {
this.providers = Arrays.asList(providers);
}
@Override
public synchronized Credentials fetch() {
if (credentials != null && !credentials.isExpired()) {
return credentials;
}
if (currentProvider != null) {
try {
credentials = currentProvider.fetch();
return credentials;
} catch (ProviderException e) {
// Ignore and fallback to iteration.
}
}
for (Provider provider : providers) {
try {
credentials = provider.fetch();
currentProvider = provider;
return credentials;
} catch (ProviderException e) {
// Ignore and continue to next iteration.
}
}
throw new ProviderException("All providers fail to fetch credentials");
}
}