Java Code Examples for org.wso2.carbon.identity.base.IdentityRuntimeException#error()

The following examples show how to use org.wso2.carbon.identity.base.IdentityRuntimeException#error() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RegistryResourceMgtServiceImpl.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
@Override
public Resource getIdentityResource(String path,
                                    String tenantDomain) throws IdentityRuntimeException {
    startTenantFlow(tenantDomain);
    try {
        Registry registry = getRegistryForTenant(tenantDomain);
        Resource resource = null;

        if (registry.resourceExists(path)) {
            resource = registry.get(path);
        }
        return resource;
    } catch (RegistryException e) {
        String errorMsg = String.format(ERROR_GET_RESOURCE, path, tenantDomain);
        throw IdentityRuntimeException.error(errorMsg, e);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
 
Example 2
Source File: RegistryResourceMgtServiceImpl.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
@Override
public void putIdentityResource(Resource identityResource,
                                String path,
                                String tenantDomain) throws IdentityRuntimeException {
    startTenantFlow(tenantDomain);
    try {
        Registry registry = getRegistryForTenant(tenantDomain);
        registry.put(path, identityResource);
        if (log.isDebugEnabled()) {
            log.debug(String.format(MSG_RESOURCE_PERSIST, path, tenantDomain));
        }
    } catch (RegistryException e) {
        String errorMsg = String.format(ERROR_PERSIST_RESOURCE, tenantDomain, path);
        throw IdentityRuntimeException.error(errorMsg, e);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
 
Example 3
Source File: AuthenticationContext.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Add authentication params to the message context parameters Map.
 *
 * @param authenticatorParams Map of authenticator and params.
 */
public void addAuthenticatorParams(Map<String, Map<String, String>> authenticatorParams) {

    if (MapUtils.isEmpty(authenticatorParams)) {
        return;
    }
    Object runtimeParamsObj = getParameter(FrameworkConstants.RUNTIME_PARAMS);
    if (runtimeParamsObj == null) {
        addParameter(FrameworkConstants.RUNTIME_PARAMS, authenticatorParams);
        return;
    }
    if (runtimeParamsObj instanceof Map) {
        Map<String, Map<String, String>> runtimeParams = (Map<String, Map<String, String>>) runtimeParamsObj;
        for (Map.Entry<String, Map<String, String>> params : authenticatorParams.entrySet()) {
            if (runtimeParams.get(params.getKey()) != null) {
                runtimeParams.get(params.getKey()).putAll(params.getValue());
            } else {
                runtimeParams.put(params.getKey(), params.getValue());
            }
        }
    } else {
        throw IdentityRuntimeException.error("There is already a object set with RUNTIME_PARAMS key in the " +
                "message context.");
    }
}
 
Example 4
Source File: IdentityTenantUtil.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
public static int getTenantId(String tenantDomain) throws IdentityRuntimeException {

        int tenantId = MultitenantConstants.INVALID_TENANT_ID;
        try {
            tenantId = realmService.getTenantManager().getTenantId(tenantDomain);
        } catch (UserStoreException e) {
            // Ideally user.core should be throwing an unchecked exception, in which case no need to wrap at this
            // level once more without adding any valuable contextual information. Because we don't have exception
            // enrichment properly implemented, we are appending the error message from the UserStoreException to the
            // new message
            throw IdentityRuntimeException.error("Error occurred while retrieving tenantId for tenantDomain: " +
                    tenantDomain + e.getMessage(), e);
        }
        if(tenantId == MultitenantConstants.INVALID_TENANT_ID){
            throw IdentityRuntimeException.error("Invalid tenant domain " + tenantDomain);
        } else {
            return tenantId;
        }

    }
 
Example 5
Source File: IdentityTenantUtil.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
public static String getTenantDomain(int tenantId) throws IdentityRuntimeException {

        String tenantDomain = null;
        try {
            tenantDomain = realmService.getTenantManager().getDomain(tenantId);
        } catch (UserStoreException e) {
            // Ideally user.core should be throwing an unchecked exception, in which case no need to wrap at this
            // level once more without adding any valuable contextual information. Because we don't have exception
            // enrichment properly implemented, we are appending the error message from the UserStoreException to the
            // new message
            throw IdentityRuntimeException.error("Error occurred while retrieving tenantDomain for tenantId: " +
                    tenantId + e.getMessage(), e);
        }
        if(tenantDomain == null){
            throw IdentityRuntimeException.error("Invalid tenant domain " + tenantDomain);
        } else {
            return tenantDomain;
        }

    }
 
Example 6
Source File: IdentityTenantUtil.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
public static int getTenantId(String tenantDomain) throws IdentityRuntimeException {

        int tenantId = MultitenantConstants.INVALID_TENANT_ID;
        try {
            if (realmService != null) {
                tenantId = realmService.getTenantManager().getTenantId(tenantDomain);
            }
        } catch (UserStoreException e) {
            // Ideally user.core should be throwing an unchecked exception, in which case no need to wrap at this
            // level once more without adding any valuable contextual information. Because we don't have exception
            // enrichment properly implemented, we are appending the error message from the UserStoreException to the
            // new message
            throw IdentityRuntimeException.error("Error occurred while retrieving tenantId for tenantDomain: " +
                    tenantDomain + e.getMessage(), e);
        }
        if(tenantId == MultitenantConstants.INVALID_TENANT_ID){
            throw IdentityRuntimeException.error("Invalid tenant domain " + tenantDomain);
        } else {
            return tenantId;
        }

    }
 
Example 7
Source File: IdentityTenantUtil.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
public static String getTenantDomain(int tenantId) throws IdentityRuntimeException {

        String tenantDomain = null;
        try {
            tenantDomain = realmService.getTenantManager().getDomain(tenantId);
        } catch (UserStoreException e) {
            // Ideally user.core should be throwing an unchecked exception, in which case no need to wrap at this
            // level once more without adding any valuable contextual information. Because we don't have exception
            // enrichment properly implemented, we are appending the error message from the UserStoreException to the
            // new message
            throw IdentityRuntimeException.error("Error occurred while retrieving tenantDomain for tenantId: " +
                    tenantId + e.getMessage(), e);
        }
        if (tenantDomain == null) {
            throw IdentityRuntimeException.error("Can not find the tenant domain for the tenant id " + tenantId);
        } else {
            return tenantDomain;
        }

    }
 
Example 8
Source File: IdentityTenantUtil.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Get the tenant id of the given user.
 *
 * @param username Username
 * @return Tenant Id of domain user belongs to.
 * @throws IdentityRuntimeException Error when getting the tenant Id from tenant domain
 */
public static int getTenantIdOfUser(String username) throws IdentityRuntimeException {

    int tenantId = MultitenantConstants.INVALID_TENANT_ID;
    String domainName = MultitenantUtils.getTenantDomain(username);
    if (domainName != null) {
        try {
            TenantManager tenantManager = IdentityTenantUtil.getRealmService().getTenantManager();
            tenantId = tenantManager.getTenantId(domainName);
        } catch (UserStoreException e) {
            String errorMsg = "Error when getting the tenant id from the tenant domain : " + domainName;
            throw IdentityRuntimeException.error(errorMsg, e);
        }
    }
    if(tenantId == MultitenantConstants.INVALID_TENANT_ID){
        throw IdentityRuntimeException.error("Invalid tenant domain of user " + username);
    } else {
        return tenantId;
    }
}
 
Example 9
Source File: IdentityUtil.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Get the host name of the server.
 * @return Hostname
 */
public static String getHostName() {

    String hostName = ServerConfiguration.getInstance().getFirstProperty(IdentityCoreConstants.HOST_NAME);
    if (hostName == null) {
        try {
            hostName = NetworkUtils.getLocalHostname();
        } catch (SocketException e) {
            throw IdentityRuntimeException.error("Error while trying to read hostname.", e);
        }
    }
    return hostName;
}
 
Example 10
Source File: SAMLSSOConfigServiceClient.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
public String[] getSigningAlgorithmUris() throws IdentityRuntimeException {
    String[] signingAlgorithmUris;
    try {
        signingAlgorithmUris = stub.getSigningAlgorithmUris();
    } catch (RemoteException e) {
        throw IdentityRuntimeException.error(e.getMessage(), e);
    }
    return signingAlgorithmUris;
}
 
Example 11
Source File: JDBCPersistenceManager.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an database connection for Identity data source.
 *
 * @return Database connection
 * @throws IdentityException Exception occurred when getting the data source.
 */
public Connection getDBConnection() throws IdentityRuntimeException {
    try {
        Connection dbConnection = dataSource.getConnection();
        dbConnection.setAutoCommit(false);
        dbConnection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        return dbConnection;
    } catch (SQLException e) {
        String errMsg = "Error when getting a database connection object from the Identity data source.";
        throw IdentityRuntimeException.error(errMsg, e);
    }
}
 
Example 12
Source File: SAMLSSOConfigServiceClient.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
public String[] getDigestAlgorithmURIs() throws IdentityRuntimeException {
    String[] digestAlgorithms;
    try {
        digestAlgorithms = stub.getDigestAlgorithmURIs();
    } catch (RemoteException e) {
        throw IdentityRuntimeException.error(e.getMessage(), e);
    }
    return digestAlgorithms;
}
 
Example 13
Source File: MessageContext.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
public void addParameters(Map<T1,T2> parameters){
    for (Map.Entry<T1,T2> parameter : parameters.entrySet()) {
        if(this.parameters.containsKey(parameter.getKey())) {
            throw IdentityRuntimeException.error("Parameters map trying to override existing key " + parameter.getKey());
        }
        parameters.put(parameter.getKey(), parameter.getValue());
    }
}
 
Example 14
Source File: MessageContext.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
public void addParameter(T1 key, T2 value){
    if(this.parameters.containsKey(key)) {
        throw IdentityRuntimeException.error("Parameters map trying to override existing key " +
                key);
    }
    parameters.put(key, value);
}
 
Example 15
Source File: IdentityDatabaseUtil.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Get a database connection instance for the User DB
 *
 * @return Database Connection
 * @throws IdentityRuntimeException Error when getting a database connection to Identity database
 */
public static Connection getUserDBConnection() throws IdentityRuntimeException {
    Connection connection;
    try {
        connection = UmPersistenceManager.getInstance().getDataSource().getConnection();
    } catch (SQLException e) {
        throw IdentityRuntimeException.error("Database error. Could not get a connection", e);
    }
    return connection;
}
 
Example 16
Source File: IdentityConfigParser.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
private void buildEventListenerData() {
    OMElement eventListeners = this.getConfigElement(IdentityConstants.EVENT_LISTENERS);
    if (eventListeners != null) {
        Iterator<OMElement> eventListener = eventListeners.getChildrenWithName(
                new QName(IdentityCoreConstants.IDENTITY_DEFAULT_NAMESPACE, IdentityConstants.EVENT_LISTENER));

        if (eventListener != null) {
            while (eventListener.hasNext()) {
                OMElement eventListenerElement = eventListener.next();
                String eventListenerType = eventListenerElement.getAttributeValue(new QName(
                        IdentityConstants.EVENT_LISTENER_TYPE));
                String eventListenerName = eventListenerElement.getAttributeValue(new QName(
                        IdentityConstants.EVENT_LISTENER_NAME));
                int order = Integer.parseInt(eventListenerElement.getAttributeValue(new QName(
                        IdentityConstants.EVENT_LISTENER_ORDER)));
                String enable = eventListenerElement.getAttributeValue(new QName(
                        IdentityConstants.EVENT_LISTENER_ENABLE));

                if (StringUtils.isBlank(eventListenerType) || StringUtils.isBlank(eventListenerName)) {
                    throw IdentityRuntimeException.error("eventListenerType or eventListenerName is not defined " +
                            "correctly");
                }
                IdentityEventListenerConfigKey configKey = new IdentityEventListenerConfigKey(eventListenerType, eventListenerName);
                IdentityEventListenerConfig identityEventListenerConfig = new IdentityEventListenerConfig(enable, order, configKey);
                eventListenerConfiguration.put(configKey, identityEventListenerConfig);

            }
        }

    }
}
 
Example 17
Source File: IdentityUtil.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * This method is used to return a URL with a proxy context path, a web context root and the tenant domain (If
 * required) when provided with a URL context.
 *
 * @param endpoint            Endpoint.
 * @param addProxyContextPath Add proxy context path to the URL.
 * @param addWebContextRoot   Add web context path to the URL.
 * @return Complete URL for the given URL context.
 * @throws IdentityRuntimeException If error occurred while constructing the URL
 */
public static String getServerURL(String endpoint, boolean addProxyContextPath, boolean addWebContextRoot)
        throws IdentityRuntimeException {

    String hostName = ServerConfiguration.getInstance().getFirstProperty(IdentityCoreConstants.HOST_NAME);

    try {
        if (hostName == null) {
            hostName = NetworkUtils.getLocalHostname();
        }
    } catch (SocketException e) {
        throw IdentityRuntimeException.error("Error while trying to read hostname.", e);
    }

    String mgtTransport = CarbonUtils.getManagementTransport();
    AxisConfiguration axisConfiguration = IdentityCoreServiceComponent.getConfigurationContextService().
            getServerConfigContext().getAxisConfiguration();
    int mgtTransportPort = CarbonUtils.getTransportProxyPort(axisConfiguration, mgtTransport);
    if (mgtTransportPort <= 0) {
        mgtTransportPort = CarbonUtils.getTransportPort(axisConfiguration, mgtTransport);
    }
    if (hostName.endsWith("/")) {
        hostName = hostName.substring(0, hostName.length() - 1);
    }
    StringBuilder serverUrl = new StringBuilder(mgtTransport).append("://").append(hostName.toLowerCase());
    // If it's well known HTTPS port, skip adding port
    if (mgtTransportPort != IdentityCoreConstants.DEFAULT_HTTPS_PORT) {
        serverUrl.append(":").append(mgtTransportPort);
    }

    appendContextToUri(endpoint, addProxyContextPath, addWebContextRoot, serverUrl);
    return serverUrl.toString();
}
 
Example 18
Source File: RegistryResourceMgtServiceImpl.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isResourceExists(String path,
                                String tenantDomain) throws IdentityRuntimeException {
    startTenantFlow(tenantDomain);
    try {
        Registry registry = getRegistryForTenant(tenantDomain);
        return registry.resourceExists(path);
    } catch (RegistryException e) {
        String errorMsg = "Error when checking for resource existence at %s in %s tenant domain.";
        throw IdentityRuntimeException.error(String.format(errorMsg, path, tenantDomain), e);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
 
Example 19
Source File: IdentityConfigParser.java    From carbon-identity-framework with Apache License 2.0 4 votes vote down vote up
private void buildEventListenerData() {
    OMElement eventListeners = this.getConfigElement(IdentityConstants.EVENT_LISTENERS);
    if (eventListeners != null) {
        Iterator<OMElement> eventListener = eventListeners.getChildrenWithName(
                new QName(IdentityCoreConstants.IDENTITY_DEFAULT_NAMESPACE, IdentityConstants.EVENT_LISTENER));

        if (eventListener != null) {
            while (eventListener.hasNext()) {
                OMElement eventListenerElement = eventListener.next();
                String eventListenerType = eventListenerElement.getAttributeValue(new QName(
                        IdentityConstants.EVENT_LISTENER_TYPE));
                String eventListenerName = eventListenerElement.getAttributeValue(new QName(
                        IdentityConstants.EVENT_LISTENER_NAME));
                int order = Integer.parseInt(eventListenerElement.getAttributeValue(new QName(
                        IdentityConstants.EVENT_LISTENER_ORDER)));
                String enable = eventListenerElement.getAttributeValue(new QName(
                        IdentityConstants.EVENT_LISTENER_ENABLE));
                Iterator<OMElement> propertyElements = eventListenerElement.getChildrenWithName(new QName
                        (IdentityConstants.EVENT_LISTENER_PROPERTY));
                Properties properties = new Properties();
                while (propertyElements.hasNext()){
                    OMElement propertyElem = propertyElements.next();
                    String propertyName = propertyElem.getAttributeValue(new QName(
                            IdentityConstants.EVENT_LISTENER_PROPERTY_NAME));
                    String propertyValue = propertyElem.getText();
                    properties.setProperty(propertyName, propertyValue);
                }

                if (StringUtils.isBlank(eventListenerType) || StringUtils.isBlank(eventListenerName)) {
                    throw IdentityRuntimeException.error("eventListenerType or eventListenerName is not defined " +
                            "correctly");
                }
                IdentityEventListenerConfigKey configKey = new IdentityEventListenerConfigKey(eventListenerType, eventListenerName);
                IdentityEventListenerConfig identityEventListenerConfig = new IdentityEventListenerConfig(enable,
                        order, configKey, properties);
                eventListenerConfiguration.put(configKey, identityEventListenerConfig);

            }
        }

    }
}
 
Example 20
Source File: IdentityConfigParser.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
private void buildCacheConfig() {
    OMElement cacheConfig = this.getConfigElement(IdentityConstants.CACHE_CONFIG);
    if (cacheConfig != null) {
        Iterator<OMElement> cacheManagers = cacheConfig.getChildrenWithName(
                new QName(IdentityCoreConstants.IDENTITY_DEFAULT_NAMESPACE, IdentityConstants.CACHE_MANAGER));

        if (cacheManagers != null) {
            while (cacheManagers.hasNext()) {
                OMElement cacheManager = cacheManagers.next();

                String cacheManagerName = cacheManager.getAttributeValue(new QName(
                        IdentityConstants.CACHE_MANAGER_NAME));

                if (StringUtils.isBlank(cacheManagerName)) {
                    throw IdentityRuntimeException.error("CacheManager name not defined correctly");
                }

                Iterator<OMElement> caches = cacheManager.getChildrenWithName(
                        new QName(IdentityCoreConstants.IDENTITY_DEFAULT_NAMESPACE, IdentityConstants.CACHE));

                if (caches != null) {
                    while (caches.hasNext()) {
                        OMElement cache = caches.next();

                        String cacheName = cache.getAttributeValue(new QName(IdentityConstants.CACHE_NAME));

                        if (StringUtils.isBlank(cacheName)) {
                            throw IdentityRuntimeException.error("Cache name not defined correctly");
                        }

                        IdentityCacheConfigKey identityCacheConfigKey = new IdentityCacheConfigKey(cacheManagerName,
                                cacheName);
                        IdentityCacheConfig identityCacheConfig = new IdentityCacheConfig(identityCacheConfigKey);

                        String enable = cache.getAttributeValue(new QName(IdentityConstants.CACHE_ENABLE));
                        if (StringUtils.isNotBlank(enable)) {
                            identityCacheConfig.setEnabled(Boolean.parseBoolean(enable));
                        }

                        String timeout = cache.getAttributeValue(new QName(IdentityConstants.CACHE_TIMEOUT));
                        if (StringUtils.isNotBlank(timeout)) {
                            identityCacheConfig.setTimeout(Integer.parseInt(timeout));
                        }

                        String capacity = cache.getAttributeValue(new QName(IdentityConstants.CACHE_CAPACITY));
                        if (StringUtils.isNotBlank(capacity)) {
                            identityCacheConfig.setCapacity(Integer.parseInt(capacity));
                        }

                        // Add the config to container
                        identityCacheConfigurationHolder.put(identityCacheConfigKey, identityCacheConfig);
                    }
                }
            }
        }

    }
}