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

com.sap.cloud.sdk.cloudplatform.metering.OAuthService Maven / Gradle / Ivy

Go to download

Implementation of the Cloud platform abstraction for API metering functionality on the SAP Cloud Platform (Neo).

There is a newer version: 2.28.0
Show newest version
/*
 * Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved.
 */

package com.sap.cloud.sdk.cloudplatform.metering;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.slf4j.Logger;

import com.netflix.hystrix.exception.HystrixRuntimeException;
import com.sap.cloud.sdk.cloudplatform.logging.CloudLoggerFactory;
import com.sap.cloud.sdk.cloudplatform.security.Role;
import com.sap.cloud.sdk.cloudplatform.tenant.ScpNeoTenant;
import com.sap.cloud.sdk.cloudplatform.tenant.TenantAccessor;

class OAuthService
{
    private static final Logger logger = CloudLoggerFactory.getLogger(OAuthService.class);

    // Caution: although not public, this method is used directly by Financial Statement Insights!
    static OAuthToken getAccessToken()
    {
        try {
            return new OAuthTokenCommand().execute();
        }
        catch( final HystrixRuntimeException e ) {
            logger.warn("Failed to retrieve OAuth token.", e);
            return null;
        }
    }

    static List getUsersForRole( @Nonnull final Role role, @Nullable final OAuthToken token )
    {
        if( token == null ) {
            logger.warn("Failed to retrieve users for role \"" + role + "\": no OAuth token available.");
            return new ArrayList<>();
        }

        try {
            final ScpNeoTenant currentTenant = (ScpNeoTenant) TenantAccessor.getCurrentTenant();

            return new UsersForRoleCommand(
                currentTenant.getTenantContext().getTenant().getAccount().getId(),
                role,
                token).execute();
        }
        catch( final HystrixRuntimeException e ) {
            logger.warn("Failed to retrieve users for role \"" + role + "\".", e);
            return new ArrayList<>();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy