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;
}
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;
}
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();
}
}
}
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;
}
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;
}
Aggregations