Search in sources :

Example 1 with APIUtil.handleException

use of org.wso2.carbon.apimgt.impl.utils.APIUtil.handleException in project carbon-apimgt by wso2.

the class APIAdminImpl method getMonetizationImplClass.

/**
 * These methods load the monetization implementation class
 *
 * @return monetization implementation class
 * @throws APIManagementException if failed to load monetization implementation class
 */
public Monetization getMonetizationImplClass() throws APIManagementException {
    APIManagerConfiguration configuration = org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
    Monetization monetizationImpl = null;
    if (configuration == null) {
        log.error("API Manager configuration is not initialized.");
    } else {
        String monetizationImplClass = configuration.getFirstProperty(APIConstants.Monetization.MONETIZATION_IMPL);
        if (monetizationImplClass == null) {
            monetizationImpl = new DefaultMonetizationImpl();
        } else {
            try {
                monetizationImpl = (Monetization) APIUtil.getClassInstance(monetizationImplClass);
            } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
                APIUtil.handleException("Failed to load monetization implementation class.", e);
            }
        }
    }
    return monetizationImpl;
}
Also used : Monetization(org.wso2.carbon.apimgt.api.model.Monetization) DefaultMonetizationImpl(org.wso2.carbon.apimgt.impl.monetization.DefaultMonetizationImpl)

Example 2 with APIUtil.handleException

use of org.wso2.carbon.apimgt.impl.utils.APIUtil.handleException in project carbon-apimgt by wso2.

the class APIKeyMgtRemoteUserStoreMgtService method getUserRoles.

/**
 * Get the role list of a user. Works for any tenant domain.
 * @param username username with tenant domain
 * @return list of roles
 * @throws APIManagementException
 */
public String[] getUserRoles(String username) throws APIManagementException {
    String[] userRoles = null;
    String tenantDomain = MultitenantUtils.getTenantDomain(username);
    PrivilegedCarbonContext.startTenantFlow();
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
    UserStoreManager userStoreManager;
    try {
        userStoreManager = CarbonContext.getThreadLocalCarbonContext().getUserRealm().getUserStoreManager();
        userRoles = userStoreManager.getRoleListOfUser(MultitenantUtils.getTenantAwareUsername(username));
    } catch (UserStoreException e) {
        APIUtil.handleException("Error occurred retrieving roles of user " + username, e);
    } finally {
        PrivilegedCarbonContext.getThreadLocalCarbonContext().endTenantFlow();
    }
    return userRoles;
}
Also used : UserStoreException(org.wso2.carbon.user.api.UserStoreException) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager)

Example 3 with APIUtil.handleException

use of org.wso2.carbon.apimgt.impl.utils.APIUtil.handleException in project carbon-apimgt by wso2.

the class RegistryCacheInvalidationService method invalidateCache.

/**
 * This method invalidates registry cache for given resource in given tenant domain
 * @param path
 * @param tenantDomain
 * @throws APIManagementException
 */
public void invalidateCache(String path, String tenantDomain) throws APIManagementException {
    Registry registry;
    boolean isTenantFlowStarted = false;
    try {
        int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
        isTenantFlowStarted = true;
        registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
        Cache<RegistryCacheKey, GhostResource> cache = RegistryUtils.getResourceCache(RegistryConstants.REGISTRY_CACHE_BACKED_ID);
        RegistryCacheKey cacheKey = null;
        // Is registry mounted
        if (registry.getRegistryContext().getRemoteInstances().size() > 0) {
            for (Mount mount : registry.getRegistryContext().getMounts()) {
                for (RemoteConfiguration configuration : registry.getRegistryContext().getRemoteInstances()) {
                    if (path.startsWith(mount.getPath())) {
                        DataBaseConfiguration dataBaseConfiguration = registry.getRegistryContext().getDBConfig(configuration.getDbConfig());
                        String connectionId = (dataBaseConfiguration.getUserName() != null ? dataBaseConfiguration.getUserName().split("@")[0] : dataBaseConfiguration.getUserName()) + "@" + dataBaseConfiguration.getDbUrl();
                        cacheKey = RegistryUtils.buildRegistryCacheKey(connectionId, tenantId, path);
                        if (cacheKey != null && cache.containsKey(cacheKey)) {
                            cache.remove(cacheKey);
                        }
                    }
                }
            }
        } else {
            DataBaseConfiguration dataBaseConfiguration = registry.getRegistryContext().getDefaultDataBaseConfiguration();
            String connectionId = (dataBaseConfiguration.getUserName() != null ? dataBaseConfiguration.getUserName().split("@")[0] : dataBaseConfiguration.getUserName()) + "@" + dataBaseConfiguration.getDbUrl();
            cacheKey = RegistryUtils.buildRegistryCacheKey(connectionId, tenantId, path);
            if (cacheKey != null && cache.containsKey(cacheKey)) {
                cache.remove(cacheKey);
            }
        }
    } catch (RegistryException e) {
        APIUtil.handleException("Error in accessing governance registry while invalidating cache for " + path + "in tenant " + tenantDomain, e);
    } catch (UserStoreException e) {
        APIUtil.handleException("Error in retrieving Tenant Information while invalidating cache for " + path + "in tenant " + tenantDomain, e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
}
Also used : RemoteConfiguration(org.wso2.carbon.registry.core.config.RemoteConfiguration) RegistryCacheKey(org.wso2.carbon.registry.core.caching.RegistryCacheKey) Mount(org.wso2.carbon.registry.core.config.Mount) UserStoreException(org.wso2.carbon.user.api.UserStoreException) DataBaseConfiguration(org.wso2.carbon.registry.core.config.DataBaseConfiguration) Registry(org.wso2.carbon.registry.core.Registry) GhostResource(org.wso2.carbon.registry.api.GhostResource) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 4 with APIUtil.handleException

use of org.wso2.carbon.apimgt.impl.utils.APIUtil.handleException in project carbon-apimgt by wso2.

the class APIKeyMgtRemoteUserStoreMgtService method authenticate.

/**
 * validates a username,password combination. Works for any tenant domain.
 * @param username username of the user(including tenant domain)
 * @param password password of the user
 * @return true if username,password is correct
 * @throws APIManagementException
 */
public boolean authenticate(String username, String password) throws APIManagementException {
    String tenantDomain = MultitenantUtils.getTenantDomain(username);
    PrivilegedCarbonContext.startTenantFlow();
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
    UserStoreManager userStoreManager;
    boolean isAuthenticated = false;
    try {
        userStoreManager = CarbonContext.getThreadLocalCarbonContext().getUserRealm().getUserStoreManager();
        String tenantAwareUserName = MultitenantUtils.getTenantAwareUsername(username);
        isAuthenticated = userStoreManager.authenticate(tenantAwareUserName, password);
    } catch (UserStoreException e) {
        APIUtil.handleException("Error occurred while validating credentials of user " + username, e);
    } finally {
        PrivilegedCarbonContext.getThreadLocalCarbonContext().endTenantFlow();
    }
    return isAuthenticated;
}
Also used : UserStoreException(org.wso2.carbon.user.api.UserStoreException) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager)

Example 5 with APIUtil.handleException

use of org.wso2.carbon.apimgt.impl.utils.APIUtil.handleException in project carbon-apimgt by wso2.

the class APIKeyMgtRemoteUserStoreMgtService method getUserAuthenticationInfo.

public BasicAuthValidationInfoDTO getUserAuthenticationInfo(String username, String password) throws APIManagementException {
    String tenantDomain = MultitenantUtils.getTenantDomain(username);
    PrivilegedCarbonContext.startTenantFlow();
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
    UserStoreManager userStoreManager;
    BasicAuthValidationInfoDTO basicAuthValidationInfoDTO = new BasicAuthValidationInfoDTO();
    boolean isAuthenticated;
    String[] userRoles;
    String domainQualifiedUsername;
    try {
        userStoreManager = CarbonContext.getThreadLocalCarbonContext().getUserRealm().getUserStoreManager();
        isAuthenticated = userStoreManager.authenticate(MultitenantUtils.getTenantAwareUsername(username), password);
        if (isAuthenticated) {
            basicAuthValidationInfoDTO.setAuthenticated(true);
            domainQualifiedUsername = UserCoreUtil.addDomainToName(username, UserCoreUtil.getDomainFromThreadLocal());
            basicAuthValidationInfoDTO.setDomainQualifiedUsername(domainQualifiedUsername);
        } else {
            // return default validation DTO with authentication false
            return basicAuthValidationInfoDTO;
        }
        // Get role list of user.
        // Should give the domain qualified username when getting the role list of user.
        userRoles = userStoreManager.getRoleListOfUser(MultitenantUtils.getTenantAwareUsername(domainQualifiedUsername));
        basicAuthValidationInfoDTO.setUserRoleList(userRoles);
    } catch (UserStoreException e) {
        APIUtil.handleException("Error occurred while retrieving user authentication info of user " + username, e);
    } finally {
        PrivilegedCarbonContext.getThreadLocalCarbonContext().endTenantFlow();
    }
    return basicAuthValidationInfoDTO;
}
Also used : BasicAuthValidationInfoDTO(org.wso2.carbon.apimgt.impl.dto.BasicAuthValidationInfoDTO) UserStoreException(org.wso2.carbon.user.api.UserStoreException) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager)

Aggregations

UserStoreException (org.wso2.carbon.user.api.UserStoreException)4 Monetization (org.wso2.carbon.apimgt.api.model.Monetization)3 DefaultMonetizationImpl (org.wso2.carbon.apimgt.impl.monetization.DefaultMonetizationImpl)3 UserStoreManager (org.wso2.carbon.user.api.UserStoreManager)3 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 APIAdmin (org.wso2.carbon.apimgt.api.APIAdmin)1 KeyManagerConfigurationDTO (org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO)1 APICategory (org.wso2.carbon.apimgt.api.model.APICategory)1 AccessTokenInfo (org.wso2.carbon.apimgt.api.model.AccessTokenInfo)1 AccessTokenRequest (org.wso2.carbon.apimgt.api.model.AccessTokenRequest)1 Application (org.wso2.carbon.apimgt.api.model.Application)1 KeyManager (org.wso2.carbon.apimgt.api.model.KeyManager)1 OAuthApplicationInfo (org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo)1 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)1