use of org.wso2.carbon.user.api.UserStoreException in project carbon-apimgt by wso2.
the class APIConsumerImpl method checkAppAttributes.
/**
* This method is used to validate keys of custom attributes, configured by user
*
* @param application
* @param userId user name of logged in user
* @throws APIManagementException
*/
public void checkAppAttributes(Application application, String userId) throws APIManagementException {
JSONArray applicationAttributesFromConfig = getAppAttributesFromConfig(userId);
Map<String, String> applicationAttributes = application.getApplicationAttributes();
List attributeKeys = new ArrayList<String>();
int applicationId = application.getId();
int tenantId = 0;
Map<String, String> newApplicationAttributes = new HashMap<>();
String tenantDomain = MultitenantUtils.getTenantDomain(userId);
try {
tenantId = getTenantId(tenantDomain);
} catch (UserStoreException e) {
handleException("Error in getting tenantId of " + tenantDomain, e);
}
for (Object object : applicationAttributesFromConfig) {
JSONObject attribute = (JSONObject) object;
attributeKeys.add(attribute.get(APIConstants.ApplicationAttributes.ATTRIBUTE));
}
for (Object key : applicationAttributes.keySet()) {
if (!attributeKeys.contains(key)) {
apiMgtDAO.deleteApplicationAttributes((String) key, applicationId);
if (log.isDebugEnabled()) {
log.debug("Removing " + key + "from application - " + application.getName());
}
}
}
for (Object key : attributeKeys) {
if (!applicationAttributes.keySet().contains(key)) {
newApplicationAttributes.put((String) key, "");
}
}
apiMgtDAO.addApplicationAttributes(newApplicationAttributes, applicationId, tenantId);
}
use of org.wso2.carbon.user.api.UserStoreException in project carbon-apimgt by wso2.
the class APIAdminImpl method getKeyManagerConfigurationsByOrganization.
@Override
public List<KeyManagerConfigurationDTO> getKeyManagerConfigurationsByOrganization(String organization) throws APIManagementException {
// For Choreo scenario (Choreo organization uses the same super tenant Resident Key Manager
// Hence no need to register the default key manager per organization)
String tenantDomain = organization;
try {
if (APIUtil.isInternalOrganization(organization)) {
KeyMgtRegistrationService.registerDefaultKeyManager(organization);
} else {
tenantDomain = APIUtil.getInternalOrganizationDomain(organization);
}
} catch (UserStoreException e) {
throw new APIManagementException("Error while retrieving tenant id for organization " + organization, e);
}
List<KeyManagerConfigurationDTO> keyManagerConfigurationsByTenant = apiMgtDAO.getKeyManagerConfigurationsByOrganization(tenantDomain);
Iterator<KeyManagerConfigurationDTO> iterator = keyManagerConfigurationsByTenant.iterator();
KeyManagerConfigurationDTO defaultKeyManagerConfiguration = null;
while (iterator.hasNext()) {
KeyManagerConfigurationDTO keyManagerConfigurationDTO = iterator.next();
if (APIConstants.KeyManager.DEFAULT_KEY_MANAGER.equals(keyManagerConfigurationDTO.getName())) {
defaultKeyManagerConfiguration = keyManagerConfigurationDTO;
iterator.remove();
break;
}
}
if (defaultKeyManagerConfiguration != null) {
APIUtil.getAndSetDefaultKeyManagerConfiguration(defaultKeyManagerConfiguration);
keyManagerConfigurationsByTenant.add(defaultKeyManagerConfiguration);
}
// and append those to the previous list
if (!StringUtils.equals(organization, tenantDomain)) {
List<KeyManagerConfigurationDTO> keyManagerConfigurationsByOrganization = apiMgtDAO.getKeyManagerConfigurationsByOrganization(organization);
keyManagerConfigurationsByTenant.addAll(keyManagerConfigurationsByOrganization);
}
setAliasForTokenExchangeKeyManagers(keyManagerConfigurationsByTenant, tenantDomain);
for (KeyManagerConfigurationDTO keyManagerConfigurationDTO : keyManagerConfigurationsByTenant) {
decryptKeyManagerConfigurationValues(keyManagerConfigurationDTO);
getKeyManagerEndpoints(keyManagerConfigurationDTO);
}
setIdentityProviderRelatedInformation(keyManagerConfigurationsByTenant, organization);
return keyManagerConfigurationsByTenant;
}
use of org.wso2.carbon.user.api.UserStoreException in project carbon-apimgt by wso2.
the class WSO2APIPublisher method exportAPIArchive.
/**
* Exports API as an advertised API using APIImportExportManager.
*
* @param api API artifact to import
* @return API archive
* @throws APIManagementException If an error occurs while exporting API.
*/
private File exportAPIArchive(API api) throws APIManagementException {
File apiArchive;
String tenantDomain = null;
int tenantId;
try {
// Get tenant domain and ID to generate the original DevPortal URL (redirect URL) for the original Store
tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(api.getId().getProviderName()));
tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
// Export API as an archive file and set it as a multipart entity in the request
ImportExportAPI importExportAPI = APIImportExportUtil.getImportExportAPI();
apiArchive = importExportAPI.exportAPI(api.getUuid(), api.getId().getName(), api.getId().getVersion(), String.valueOf(api.getRevisionId()), api.getId().getProviderName(), Boolean.TRUE, ExportFormat.JSON, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, getExternalStoreRedirectURLForAPI(tenantId, api.getUuid()), api.getOrganization());
if (log.isDebugEnabled()) {
log.debug("API successfully exported to file: " + apiArchive.getName());
}
} catch (APIImportExportException e) {
String errorMessage = "Error while exporting API: " + api.getId().getApiName() + " version: " + api.getId().getVersion();
throw new APIManagementException(errorMessage, e);
} catch (UserStoreException e) {
String errorMessage = "Error while getting tenantId for tenant domain: " + tenantDomain + " when exporting API:" + api.getId().getApiName() + " version: " + api.getId().getVersion();
throw new APIManagementException(errorMessage, e);
}
return apiArchive;
}
use of org.wso2.carbon.user.api.UserStoreException in project carbon-apimgt by wso2.
the class OnPremResolver method resolve.
@Override
public String resolve(Map<String, Object> properties) throws APIManagementException {
ArrayList requestedTenantDomain = (ArrayList) ((TreeMap) (properties.get(APIConstants.PROPERTY_HEADERS_KEY))).get(HEADER_X_WSO2_TENANT);
String tenantDomain = null;
if (requestedTenantDomain != null) {
String header = requestedTenantDomain.get(0).toString();
if (StringUtils.isEmpty(header)) {
tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
} else {
tenantDomain = header;
}
try {
if (!APIUtil.isTenantAvailable(tenantDomain)) {
String errorMessage = "Provided tenant domain '" + tenantDomain + "' is invalid";
throw new APIMgtBadRequestException(errorMessage);
}
} catch (UserStoreException e) {
String errorMessage = "Error while checking availability of tenant " + tenantDomain;
throw new APIMgtInternalException(errorMessage);
}
}
if (StringUtils.isEmpty(tenantDomain)) {
tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
}
// Set "carbon.super" if tenantDomain is still not resolved.
if (StringUtils.isEmpty(tenantDomain)) {
tenantDomain = APIConstants.SUPER_TENANT_DOMAIN;
}
return tenantDomain;
}
use of org.wso2.carbon.user.api.UserStoreException 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;
}
Aggregations