Search in sources :

Example 1 with FunctionLibraryManagementException

use of org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException in project carbon-identity-framework by wso2.

the class JsGraphBuilder method loadLocalLibrary.

/**
 * Loads the required function library from the database.
 *
 * @param functionLibraryName functionLibraryName
 * @return functionLibraryScript
 * @throws FunctionLibraryManagementException
 */
public String loadLocalLibrary(String functionLibraryName) throws FunctionLibraryManagementException {
    FunctionLibraryManagementService functionLibMgtService = FrameworkServiceComponent.getFunctionLibraryManagementService();
    FunctionLibrary functionLibrary;
    String libraryScript = null;
    functionLibrary = functionLibMgtService.getFunctionLibrary(functionLibraryName, CarbonContext.getThreadLocalCarbonContext().getTenantDomain());
    if (functionLibrary != null) {
        libraryScript = functionLibrary.getFunctionLibraryScript();
    } else {
        log.error("No function library available with " + functionLibraryName + "name.");
    }
    return libraryScript;
}
Also used : FunctionLibrary(org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary) FunctionLibraryManagementService(org.wso2.carbon.identity.functions.library.mgt.FunctionLibraryManagementService)

Example 2 with FunctionLibraryManagementException

use of org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException in project carbon-identity-framework by wso2.

the class FunctionLibraryManagementAdminService method getFunctionLibrary.

/**
 * Get a function library using function library name.
 *
 * @param functionLibraryName Name of the function library
 * @return Function library
 * @throws FunctionLibraryManagementException
 */
public FunctionLibrary getFunctionLibrary(String functionLibraryName) throws FunctionLibraryManagementException {
    try {
        functionLibMgtService = FunctionLibraryManagementServiceImpl.getInstance();
        FunctionLibrary functionLibrary;
        functionLibrary = functionLibMgtService.getFunctionLibrary(functionLibraryName, getTenantDomain());
        return functionLibrary;
    } catch (FunctionLibraryManagementException e) {
        log.error("Error while retrieving script library " + functionLibraryName + " for tenant domain " + getTenantDomain() + ".", e);
        throw e;
    }
}
Also used : FunctionLibraryManagementException(org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException) FunctionLibrary(org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary)

Example 3 with FunctionLibraryManagementException

use of org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException in project carbon-identity-framework by wso2.

the class FunctionLibraryManagementServiceImpl method createFunctionLibrary.

@Override
public void createFunctionLibrary(FunctionLibrary functionLibrary, String tenantDomain) throws FunctionLibraryManagementException {
    validateInputs(functionLibrary);
    FunctionLibraryDAO functionLibraryDAO = new FunctionLibraryDAOImpl();
    if (functionLibraryDAO.isFunctionLibraryExists(functionLibrary.getFunctionLibraryName(), tenantDomain)) {
        throw FunctionLibraryExceptionManagementUtil.handleClientException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_ALREADY_EXIST_SCRIPT_LIBRARY, functionLibrary.getFunctionLibraryName());
    }
    String functionLibraryName = functionLibrary.getFunctionLibraryName();
    if (!isRegexValidated(functionLibraryName)) {
        throw FunctionLibraryExceptionManagementUtil.handleClientException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_INVALID_SCRIPT_LIBRARY_NAME, FunctionLibraryMgtUtil.FUNCTION_LIBRARY_NAME_VALIDATING_REGEX);
    }
    functionLibraryDAO.createFunctionLibrary(functionLibrary, tenantDomain);
}
Also used : FunctionLibraryDAOImpl(org.wso2.carbon.identity.functions.library.mgt.dao.impl.FunctionLibraryDAOImpl) FunctionLibraryDAO(org.wso2.carbon.identity.functions.library.mgt.dao.FunctionLibraryDAO)

Example 4 with FunctionLibraryManagementException

use of org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException in project carbon-identity-framework by wso2.

the class FunctionLibraryDAOImpl method getFunctionLibrary.

/**
 * Retrieve a function library from the given name.
 *
 * @param functionLibraryName Function library name
 * @param tenantDomain        Tenant domain
 * @return Function library
 * @throws FunctionLibraryManagementException
 */
public FunctionLibrary getFunctionLibrary(String functionLibraryName, String tenantDomain) throws FunctionLibraryManagementException {
    // get logged-in users tenant identifier.
    int tenantID = MultitenantConstants.INVALID_TENANT_ID;
    if (tenantDomain != null) {
        tenantID = IdentityTenantUtil.getTenantId(tenantDomain);
    }
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
        try (PreparedStatement getFunctionLibStmt = connection.prepareStatement(FunctionLibMgtDBQueries.LOAD_FUNCTIONLIB_FROM_TENANTID_AND_NAME)) {
            getFunctionLibStmt.setInt(1, tenantID);
            getFunctionLibStmt.setString(2, functionLibraryName);
            try (ResultSet resultSet = getFunctionLibStmt.executeQuery()) {
                if (resultSet.next()) {
                    FunctionLibrary functionlib = new FunctionLibrary();
                    functionlib.setFunctionLibraryName(resultSet.getString("NAME"));
                    functionlib.setDescription(resultSet.getString("DESCRIPTION"));
                    functionlib.setFunctionLibraryScript(IOUtils.toString(resultSet.getBinaryStream("DATA")));
                    return functionlib;
                } else {
                    return null;
                }
            }
        } catch (IOException e) {
            throw FunctionLibraryExceptionManagementUtil.handleServerException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_RETRIEVE_SCRIPT_LIBRARY, functionLibraryName, e);
        }
    } catch (SQLException e) {
        throw FunctionLibraryExceptionManagementUtil.handleServerException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_RETRIEVE_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) ResultSet(java.sql.ResultSet) FunctionLibrary(org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException)

Example 5 with FunctionLibraryManagementException

use of org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException in project carbon-identity-framework by wso2.

the class FunctionLibraryDAOImpl method updateFunctionLibrary.

/**
 * Update an existing function library.
 *
 * @param oldFunctionLibName Previous name of the function library
 * @param functionLibrary    Function library
 * @param tenantDomain       Tenant domain
 * @throws FunctionLibraryManagementException
 */
public void updateFunctionLibrary(String oldFunctionLibName, FunctionLibrary functionLibrary, String tenantDomain) throws FunctionLibraryManagementException {
    // get logged-in users tenant identifier.
    int tenantID = MultitenantConstants.INVALID_TENANT_ID;
    if (tenantDomain != null) {
        tenantID = IdentityTenantUtil.getTenantId(tenantDomain);
    }
    if (tenantID != MultitenantConstants.INVALID_TENANT_ID) {
        try (Connection connection = IdentityDatabaseUtil.getDBConnection()) {
            try (PreparedStatement updateFunctionLibStmt = connection.prepareStatement(FunctionLibMgtDBQueries.UPDATE_FUNCTIONLIB_INFO)) {
                updateFunctionLibStmt.setString(1, functionLibrary.getFunctionLibraryName());
                updateFunctionLibStmt.setString(2, functionLibrary.getDescription());
                setBlobValue(functionLibrary.getFunctionLibraryScript(), updateFunctionLibStmt, 3);
                updateFunctionLibStmt.setInt(4, tenantID);
                updateFunctionLibStmt.setString(5, oldFunctionLibName);
                updateFunctionLibStmt.executeUpdate();
                IdentityDatabaseUtil.commitTransaction(connection);
            } catch (SQLException e1) {
                IdentityDatabaseUtil.rollbackTransaction(connection);
                throw FunctionLibraryExceptionManagementUtil.handleServerException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_UPDATE_SCRIPT_LIBRARY, oldFunctionLibName, e1);
            }
        } catch (SQLException e) {
            throw FunctionLibraryExceptionManagementUtil.handleServerException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_UPDATE_SCRIPT_LIBRARY, oldFunctionLibName, e);
        } catch (IOException e) {
            throw FunctionLibraryExceptionManagementUtil.handleServerException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_PROCESSING_CONTENT_STREAM_SCRIPT_LIBRARY, oldFunctionLibName, e);
        } catch (IdentityRuntimeException e) {
            throw FunctionLibraryExceptionManagementUtil.handleServerException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_DATABASE_CONNECTION, e);
        }
    } else {
        throw FunctionLibraryExceptionManagementUtil.handleServerException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_INVALID_TENANT);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException)

Aggregations

FunctionLibrary (org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary)14 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 Test (org.testng.annotations.Test)12 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)12 FunctionLibraryDAO (org.wso2.carbon.identity.functions.library.mgt.dao.FunctionLibraryDAO)11 Connection (java.sql.Connection)10 FunctionLibraryManagementException (org.wso2.carbon.identity.functions.library.mgt.exception.FunctionLibraryManagementException)9 FunctionLibraryDAOImpl (org.wso2.carbon.identity.functions.library.mgt.dao.impl.FunctionLibraryDAOImpl)7 SQLException (java.sql.SQLException)6 IOException (java.io.IOException)4 PreparedStatement (java.sql.PreparedStatement)4 IdentityRuntimeException (org.wso2.carbon.identity.base.IdentityRuntimeException)4 File (java.io.File)2 ResultSet (java.sql.ResultSet)2 ScriptLibraryResponse (org.wso2.carbon.identity.api.server.script.library.v1.model.ScriptLibraryResponse)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Response (javax.ws.rs.core.Response)1 APIError (org.wso2.carbon.identity.api.server.common.error.APIError)1 ErrorResponse (org.wso2.carbon.identity.api.server.common.error.ErrorResponse)1