Search in sources :

Example 81 with UserStoreException

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);
}
Also used : JSONObject(org.json.simple.JSONObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) JSONArray(org.json.simple.JSONArray) ArrayList(java.util.ArrayList) UserStoreException(org.wso2.carbon.user.api.UserStoreException) CommentList(org.wso2.carbon.apimgt.api.model.CommentList) ArrayList(java.util.ArrayList) List(java.util.List) JSONObject(org.json.simple.JSONObject)

Example 82 with UserStoreException

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;
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserStoreException(org.wso2.carbon.user.api.UserStoreException)

Example 83 with UserStoreException

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;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) File(java.io.File)

Example 84 with UserStoreException

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;
}
Also used : APIMgtInternalException(org.wso2.carbon.apimgt.api.APIMgtInternalException) ArrayList(java.util.ArrayList) UserStoreException(org.wso2.carbon.user.api.UserStoreException) APIMgtBadRequestException(org.wso2.carbon.apimgt.api.APIMgtBadRequestException)

Example 85 with UserStoreException

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;
}
Also used : UserStoreException(org.wso2.carbon.user.api.UserStoreException) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager)

Aggregations

UserStoreException (org.wso2.carbon.user.api.UserStoreException)127 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)65 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)47 Test (org.junit.Test)37 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)37 RealmService (org.wso2.carbon.user.core.service.RealmService)36 ArrayList (java.util.ArrayList)33 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)33 API (org.wso2.carbon.apimgt.api.model.API)31 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)27 HashMap (java.util.HashMap)25 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)23 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)23 Resource (org.wso2.carbon.registry.core.Resource)23 Endpoint (org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint)21 JSONObject (org.json.simple.JSONObject)20 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)20 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)20 HashSet (java.util.HashSet)19 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)18