Search in sources :

Example 16 with IdentityRuntimeException

use of org.wso2.carbon.identity.base.IdentityRuntimeException in project carbon-identity-framework by wso2.

the class FunctionLibraryDAOImpl method deleteFunctionLibrary.

/**
 * Delete an existing function library.
 *
 * @param functionLibraryName Function library name
 * @param tenantDomain        Tenant domain
 * @throws FunctionLibraryManagementException
 */
public void deleteFunctionLibrary(String functionLibraryName, String tenantDomain) throws FunctionLibraryManagementException {
    int tenantID = MultitenantConstants.INVALID_TENANT_ID;
    if (tenantDomain != null) {
        tenantID = IdentityTenantUtil.getTenantId(tenantDomain);
    }
    try (Connection connection = IdentityDatabaseUtil.getDBConnection()) {
        try (PreparedStatement deleteFunctionLibStmt = connection.prepareStatement(FunctionLibMgtDBQueries.REMOVE_FUNCTIONLIB)) {
            deleteFunctionLibStmt.setInt(1, tenantID);
            deleteFunctionLibStmt.setString(2, functionLibraryName);
            deleteFunctionLibStmt.executeUpdate();
            IdentityDatabaseUtil.commitTransaction(connection);
            if (log.isDebugEnabled()) {
                log.debug("Removed the script library " + functionLibraryName + " successfully.");
            }
        } catch (SQLException e1) {
            IdentityDatabaseUtil.rollbackTransaction(connection);
            throw FunctionLibraryExceptionManagementUtil.handleServerException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_PROCESSING_CONTENT_STREAM_SCRIPT_LIBRARY, functionLibraryName, e1);
        }
    } catch (SQLException e) {
        throw FunctionLibraryExceptionManagementUtil.handleServerException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_PROCESSING_CONTENT_STREAM_SCRIPT_LIBRARY, functionLibraryName, e);
    } catch (IdentityRuntimeException e) {
        throw FunctionLibraryExceptionManagementUtil.handleServerException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_DATABASE_CONNECTION, e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException)

Example 17 with IdentityRuntimeException

use of org.wso2.carbon.identity.base.IdentityRuntimeException in project carbon-identity-framework by wso2.

the class DatabaseCertificateRetriever method getCertificate.

/**
 * @param certificateId Database identifier of the certificate.
 * @param tenant        Tenant where the certificate belongs to. But in this implementation the passed tenant is
 *                      not considered since the database id is already there.
 * @return The certificate for the given database identifier.
 * @throws CertificateRetrievingException
 */
@Override
public X509Certificate getCertificate(String certificateId, Tenant tenant) throws CertificateRetrievingException {
    Connection connection;
    try {
        connection = IdentityDatabaseUtil.getDBConnection(false);
    } catch (IdentityRuntimeException e) {
        throw new CertificateRetrievingException("Couldn't get a database connection.", e);
    }
    PreparedStatement statementToGetApplicationCertificate = null;
    ResultSet queryResults = null;
    try {
        statementToGetApplicationCertificate = connection.prepareStatement(QUERY_TO_GET_APPLICATION_CERTIFICATE);
        statementToGetApplicationCertificate.setInt(1, Integer.parseInt(certificateId));
        queryResults = statementToGetApplicationCertificate.executeQuery();
        String certificateContent = null;
        while (queryResults.next()) {
            certificateContent = getBlobValue(queryResults.getBinaryStream(1));
        }
        if (StringUtils.isNotBlank(certificateContent)) {
            return (X509Certificate) IdentityUtil.convertPEMEncodedContentToCertificate(certificateContent);
        }
    } catch (SQLException e) {
        String errorMessage = String.format("An error occurred while retrieving the certificate content from " + "the database for the ID '%s'", certificateId);
        throw new CertificateRetrievingException(errorMessage, e);
    } catch (CertificateException e) {
        String errorMessage = String.format("An error occurred while build a certificate using the certificate " + "content from the database for the ID '%s'", certificateId);
        throw new CertificateRetrievingException(errorMessage, e);
    } catch (IOException e) {
        String errorMessage = String.format("An error occurred while reading the certificate blob from the " + "database for the ID '%s'", certificateId);
        throw new CertificateRetrievingException(errorMessage, e);
    } finally {
        IdentityDatabaseUtil.closeAllConnections(connection, queryResults, statementToGetApplicationCertificate);
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) CertificateException(java.security.cert.CertificateException) IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate)

Example 18 with IdentityRuntimeException

use of org.wso2.carbon.identity.base.IdentityRuntimeException in project carbon-identity-framework by wso2.

the class RegistryResourceMgtServiceImpl method putIdentityResource.

@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();
    }
}
Also used : Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 19 with IdentityRuntimeException

use of org.wso2.carbon.identity.base.IdentityRuntimeException in project carbon-identity-framework by wso2.

the class RegistryResourceMgtServiceImpl method deleteIdentityResource.

@Override
public void deleteIdentityResource(String path, String tenantDomain) throws IdentityRuntimeException {
    startTenantFlow(tenantDomain);
    try {
        Registry registry = getRegistryForTenant(tenantDomain);
        if (registry.resourceExists(path)) {
            registry.delete(path);
        } else {
            String errorMsg = String.format(ERROR_NO_RESOURCE_FOUND, path, tenantDomain);
            log.error(errorMsg);
            throw IdentityRuntimeException.error(errorMsg);
        }
    } catch (RegistryException e) {
        String errorMsg = String.format(ERROR_DELETE_RESOURCE, tenantDomain, path);
        throw IdentityRuntimeException.error(errorMsg, e);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
Also used : Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 20 with IdentityRuntimeException

use of org.wso2.carbon.identity.base.IdentityRuntimeException in project carbon-identity-framework by wso2.

the class IdentityTenantUtil method getTenantIdOfUser.

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

Aggregations

IdentityRuntimeException (org.wso2.carbon.identity.base.IdentityRuntimeException)17 Connection (java.sql.Connection)14 SQLException (java.sql.SQLException)14 PreparedStatement (java.sql.PreparedStatement)13 IOException (java.io.IOException)8 ResultSet (java.sql.ResultSet)5 SessionSerializerException (org.wso2.carbon.identity.application.authentication.framework.exception.SessionSerializerException)4 Registry (org.wso2.carbon.registry.core.Registry)4 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)4 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)3 ArrayList (java.util.ArrayList)2 IdentityEventException (org.wso2.carbon.identity.event.IdentityEventException)2 IdentityEventMessageContext (org.wso2.carbon.identity.event.bean.IdentityEventMessageContext)2 Event (org.wso2.carbon.identity.event.event.Event)2 FunctionLibrary (org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary)2 UserStoreException (org.wso2.carbon.user.api.UserStoreException)2 CertificateException (java.security.cert.CertificateException)1 X509Certificate (java.security.cert.X509Certificate)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1