All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.tweeenveertig.openstack.command.core.AbstractSecureCommand Maven / Gradle / Ivy

There is a newer version: 0.7.0
Show newest version
package nl.tweeenveertig.openstack.command.core;

import nl.tweeenveertig.openstack.model.Access;
import nl.tweeenveertig.openstack.model.Account;
import nl.tweeenveertig.openstack.exception.UnauthorizedException;
import nl.tweeenveertig.openstack.headers.Token;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpRequestBase;

public abstract class AbstractSecureCommand extends AbstractCommand {

    private Account account;

    public AbstractSecureCommand(Account account, HttpClient httpClient, String url, String token) {
        super(httpClient, url);
        this.account = account;
        setToken(token);
    }

    public AbstractSecureCommand(Account account, HttpClient httpClient, Access access) {
        this(account, httpClient, access.getInternalURL(), access.getToken());
    }

    @Override
    public N call() {
        try {
            return super.call();
        } catch (UnauthorizedException err) {
            if (account.isAllowReauthenticate()) {
                Access access = account.authenticate();
                setToken(access.getToken());
                return super.call();
            }
            throw err;
        }
    }

    private void setToken(String token) {
        setHeader(new Token(token));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy