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

com.bimface.sdk.service.AccessTokenService Maven / Gradle / Ivy

The newest version!
package com.bimface.sdk.service;

import com.bimface.exception.BimfaceException;
import com.bimface.sdk.bean.response.AccessTokenBean;
import com.bimface.sdk.client.ApiClient;
import com.bimface.sdk.config.Config;
import com.bimface.sdk.config.Endpoint;
import com.bimface.sdk.config.authorization.AccessTokenStorage;
import com.bimface.sdk.config.authorization.Credential;
import com.bimface.sdk.constants.BimfaceConstants;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 获取AccessToken
 * 
 * @author bimface, 2016-06-01.
 */
@Slf4j
public class AccessTokenService {
    private Credential         credential;
    private volatile AccessTokenStorage accessTokenStorage;
    private ApiClient apiClient;
    private Config config;

    private Logger logger = LoggerFactory.getLogger(AccessTokenService.class);

    public AccessTokenService(Endpoint endpoint, Credential credential,
                              AccessTokenStorage accessTokenStorage) {
        this(endpoint, credential, null, accessTokenStorage);
    }

    public AccessTokenService(Endpoint endpoint, Credential credential, Config config,
                              AccessTokenStorage accessTokenStorage) {
        this.credential = credential;
        this.accessTokenStorage = accessTokenStorage;
        this.apiClient = ApiClient.getApiClient(endpoint.getApiHost(), config);
        this.config = config;
        this.accessTokenStorage.setTokenTimeout(null == config ? BimfaceConstants.DEFAULT_TOKEN_TIMEOUT : config.getTokenTimeout());
    }

    public AccessTokenBean getAccessTokenBean() throws BimfaceException {
        AccessTokenBean accessTokenBean = accessTokenStorage.get();
        if (accessTokenBean == null) {
            synchronized (this) {
                if (accessTokenBean == null) {
                    accessTokenBean = grantAccessTokenBean();
                    accessTokenStorage.put(accessTokenBean);
                }
            }
        }
        return accessTokenBean;
    }

    public synchronized void updateAccessTokenBean() throws BimfaceException {
        AccessTokenBean accessTokenBean = grantAccessTokenBean();
        accessTokenStorage.put(accessTokenBean);
    }

    public String getAccessToken() throws BimfaceException {
        AccessTokenBean accessTokenBean = getAccessTokenBean();
        return accessTokenBean.getToken();
    }

    private AccessTokenBean grantAccessTokenBean() throws BimfaceException {
        logger.warn("....call api server to get token...");
        return apiClient.applyOAuthToken(credential);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy