Search in sources :

Example 1 with TemplateManagementServerException

use of org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException in project carbon-identity-framework by wso2.

the class TemplateManagerDAOImpl method deleteTemplate.

/**
 * Delete {@link Template} for a given template name and a tenant Id.
 *
 * @param templateName name of the {@link Template} to be deleted.the tenant
 * @param tenantId     tenant Id of the tenant which the {@link Template} resides.
 * @return TemplateInfo of the deleted {@link Template}.
 * @throws TemplateManagementException If error occurs while deleting the {@link Template}
 */
public TemplateInfo deleteTemplate(String templateName, Integer tenantId) throws TemplateManagementException {
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        jdbcTemplate.executeUpdate(DELETE_TEMPLATE, preparedStatement -> {
            preparedStatement.setString(1, templateName);
            preparedStatement.setInt(2, tenantId);
        });
    } catch (DataAccessException e) {
        throw new TemplateManagementServerException(String.format(ERROR_CODE_DELETE_TEMPLATE.getMessage(), tenantId.toString(), templateName), ERROR_CODE_DELETE_TEMPLATE.getCode(), e);
    }
    return new TemplateInfo(tenantId, templateName);
}
Also used : TemplateInfo(org.wso2.carbon.identity.template.mgt.model.TemplateInfo) TemplateManagementServerException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 2 with TemplateManagementServerException

use of org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException in project carbon-identity-framework by wso2.

the class TemplateManagerDAOImpl method getAllTemplates.

/**
 * List {@link TemplateInfo} items for a given search criteria.
 *
 * @param tenantId Tenant Id to be searched.
 * @param limit    Maximum number of results expected.
 * @param offset   Result offset.
 * @return List of {@link TemplateInfo} entries.
 * @throws TemplateManagementException If error occurs while searching the Templates.
 */
public List<TemplateInfo> getAllTemplates(Integer tenantId, Integer limit, Integer offset) throws TemplateManagementException {
    List<TemplateInfo> templates;
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        String query;
        if (isH2DB() || isMySQLDB() || isPostgreSQLDB()) {
            query = LIST_PAGINATED_TEMPLATES_MYSQL;
        } else if (isDB2DB()) {
            query = LIST_PAGINATED_TEMPLATES_DB2;
            int initialOffset = offset;
            offset = offset + limit;
            limit = initialOffset + 1;
        } else if (isMSSqlDB()) {
            int initialOffset = offset;
            offset = limit + offset;
            limit = initialOffset + 1;
            query = LIST_PAGINATED_TEMPLATES_MSSQL;
        } else {
            // oracle
            query = LIST_PAGINATED_TEMPLATES_ORACLE;
            limit = offset + limit;
        }
        int finalLimit = limit;
        int finalOffset = offset;
        templates = jdbcTemplate.executeQuery(query, (resultSet, rowNumber) -> new TemplateInfo(resultSet.getString(1), resultSet.getString(2)), preparedStatement -> {
            preparedStatement.setInt(1, tenantId);
            preparedStatement.setInt(2, finalLimit);
            preparedStatement.setInt(3, finalOffset);
        });
    } catch (DataAccessException e) {
        throw new TemplateManagementServerException(String.format(ERROR_CODE_PAGINATED_LIST_TEMPLATES.getMessage(), tenantId, limit, offset), ERROR_CODE_PAGINATED_LIST_TEMPLATES.getCode(), e);
    }
    return templates;
}
Also used : LIST_PAGINATED_TEMPLATES_MYSQL(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.LIST_PAGINATED_TEMPLATES_MYSQL) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) ERROR_CODE_SELECT_TEMPLATE_BY_NAME(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_SELECT_TEMPLATE_BY_NAME) LIST_PAGINATED_TEMPLATES_MSSQL(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.LIST_PAGINATED_TEMPLATES_MSSQL) TemplateManagementSQLException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementSQLException) DELETE_TEMPLATE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.DELETE_TEMPLATE) TemplateInfo(org.wso2.carbon.identity.template.mgt.model.TemplateInfo) ERROR_CODE_UPDATE_TEMPLATE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_UPDATE_TEMPLATE) ERROR_CODE_INSERT_TEMPLATE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_INSERT_TEMPLATE) JdbcUtils(org.wso2.carbon.identity.core.util.JdbcUtils) LIST_PAGINATED_TEMPLATES_DB2(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.LIST_PAGINATED_TEMPLATES_DB2) ERROR_CODE_PAGINATED_LIST_TEMPLATES(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_PAGINATED_LIST_TEMPLATES) ERROR_CODE_DELETE_TEMPLATE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_DELETE_TEMPLATE) TemplateManagerDAO(org.wso2.carbon.identity.template.mgt.dao.TemplateManagerDAO) IOException(java.io.IOException) TemplateManagementServerException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException) JdbcUtils.isMSSqlDB(org.wso2.carbon.identity.core.util.JdbcUtils.isMSSqlDB) GET_TEMPLATE_BY_NAME(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.GET_TEMPLATE_BY_NAME) JdbcUtils.isMySQLDB(org.wso2.carbon.identity.core.util.JdbcUtils.isMySQLDB) LIST_PAGINATED_TEMPLATES_ORACLE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.LIST_PAGINATED_TEMPLATES_ORACLE) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) JdbcUtils.isH2DB(org.wso2.carbon.identity.core.util.JdbcUtils.isH2DB) TemplateManagementException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementException) JdbcUtils.isPostgreSQLDB(org.wso2.carbon.identity.core.util.JdbcUtils.isPostgreSQLDB) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) TemplateMgtConstants(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants) TemplateMgtUtils(org.wso2.carbon.identity.template.mgt.util.TemplateMgtUtils) JdbcUtils.isDB2DB(org.wso2.carbon.identity.core.util.JdbcUtils.isDB2DB) Template(org.wso2.carbon.identity.template.mgt.model.Template) InputStream(java.io.InputStream) TemplateInfo(org.wso2.carbon.identity.template.mgt.model.TemplateInfo) TemplateManagementServerException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 3 with TemplateManagementServerException

use of org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException in project identity-api-server by wso2.

the class ServerIdpManagementService method handleTemplateMgtException.

/**
 * Handle template management exceptions and return related API errors.
 *
 * @param e {@link TemplateManagementException}.
 * @param errorEnum Error message with error code and description.
 * @param data Additional information.
 * @return API error.
 */
private APIError handleTemplateMgtException(TemplateManagementException e, Constants.ErrorMessage errorEnum, String data) {
    ErrorResponse errorResponse;
    Response.Status status;
    if (e instanceof TemplateManagementClientException) {
        if (e.getErrorCode() != null) {
            String errorCode = e.getErrorCode();
            errorResponse = getErrorBuilder(errorCode, e.getMessage(), data).build(log, e.getMessage());
            errorCode = errorCode.contains(TEMPLATE_MGT_ERROR_CODE_DELIMITER) ? errorCode : Constants.IDP_MANAGEMENT_PREFIX + errorCode;
            errorResponse.setCode(errorCode);
        } else {
            errorResponse = getErrorBuilder(errorEnum, data).build(log, e.getMessage());
        }
        errorResponse.setDescription(e.getMessage());
        status = Response.Status.BAD_REQUEST;
    } else if (e instanceof TemplateManagementServerException) {
        if (e.getErrorCode() != null) {
            String errorCode = e.getErrorCode();
            errorResponse = getErrorBuilder(errorCode, e.getMessage(), data).build(log, e, includeData(e.getMessage(), data));
            errorCode = errorCode.contains(TEMPLATE_MGT_ERROR_CODE_DELIMITER) ? errorCode : Constants.IDP_MANAGEMENT_PREFIX + errorCode;
            errorResponse.setCode(errorCode);
        } else {
            errorResponse = getErrorBuilder(errorEnum, data).build(log, e, includeData(e.getMessage(), data));
        }
        errorResponse.setDescription(e.getMessage());
        status = Response.Status.INTERNAL_SERVER_ERROR;
    } else {
        if (e.getErrorCode() != null) {
            errorResponse = getErrorBuilder(e.getErrorCode(), e.getMessage(), data).build(log, e, includeData(e.getMessage(), data));
        } else {
            errorResponse = getErrorBuilder(errorEnum, data).build(log, e, includeData(e.getMessage(), data));
        }
        status = Response.Status.INTERNAL_SERVER_ERROR;
    }
    return new APIError(status, errorResponse);
}
Also used : IdentityProviderTemplateListResponse(org.wso2.carbon.identity.api.server.idp.v1.model.IdentityProviderTemplateListResponse) ErrorResponse(org.wso2.carbon.identity.api.server.common.error.ErrorResponse) IdentityProviderListResponse(org.wso2.carbon.identity.api.server.idp.v1.model.IdentityProviderListResponse) FederatedAuthenticatorListResponse(org.wso2.carbon.identity.api.server.idp.v1.model.FederatedAuthenticatorListResponse) Response(javax.ws.rs.core.Response) IdentityProviderResponse(org.wso2.carbon.identity.api.server.idp.v1.model.IdentityProviderResponse) OutboundConnectorListResponse(org.wso2.carbon.identity.api.server.idp.v1.model.OutboundConnectorListResponse) ProvisioningResponse(org.wso2.carbon.identity.api.server.idp.v1.model.ProvisioningResponse) TemplateManagementServerException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException) APIError(org.wso2.carbon.identity.api.server.common.error.APIError) TemplateManagementClientException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementClientException) ErrorResponse(org.wso2.carbon.identity.api.server.common.error.ErrorResponse)

Example 4 with TemplateManagementServerException

use of org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException in project carbon-identity-framework by wso2.

the class TemplateManagerDAOImpl method getTemplateByName.

/**
 * Retrieve {@link Template} by template name and tenant Id.
 *
 * @param templateName name of the {@link Template} to retrieve.
 * @param tenantId     tenant Id of the tenant which the {@link Template} resides.
 * @return {@link Template} for the given name and tenant Id.
 * @throws TemplateManagementException If error occurs while retrieving {@link Template}.
 */
public Template getTemplateByName(String templateName, Integer tenantId) throws TemplateManagementException {
    Template template;
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        template = jdbcTemplate.fetchSingleRecord(GET_TEMPLATE_BY_NAME, ((resultSet, rowNumber) -> {
            Template templateResult;
            try {
                templateResult = new Template(resultSet.getString(1), resultSet.getString(2), IOUtils.toString(resultSet.getBinaryStream(3)));
            } catch (IOException e) {
                // SQLException is thrown since the QueryFilter throws an SQLException
                throw new TemplateManagementSQLException(String.format(ERROR_CODE_SELECT_TEMPLATE_BY_NAME.getMessage(), tenantId, templateName), ERROR_CODE_SELECT_TEMPLATE_BY_NAME.getCode(), e);
            }
            return templateResult;
        }), preparedStatement -> {
            preparedStatement.setString(1, templateName);
            preparedStatement.setInt(2, tenantId);
        });
    } catch (DataAccessException e) {
        throw new TemplateManagementServerException(String.format(ERROR_CODE_SELECT_TEMPLATE_BY_NAME.getMessage(), tenantId, templateName), ERROR_CODE_SELECT_TEMPLATE_BY_NAME.getCode(), e);
    }
    return template;
}
Also used : LIST_PAGINATED_TEMPLATES_MYSQL(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.LIST_PAGINATED_TEMPLATES_MYSQL) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) ERROR_CODE_SELECT_TEMPLATE_BY_NAME(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_SELECT_TEMPLATE_BY_NAME) LIST_PAGINATED_TEMPLATES_MSSQL(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.LIST_PAGINATED_TEMPLATES_MSSQL) TemplateManagementSQLException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementSQLException) DELETE_TEMPLATE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.DELETE_TEMPLATE) TemplateInfo(org.wso2.carbon.identity.template.mgt.model.TemplateInfo) ERROR_CODE_UPDATE_TEMPLATE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_UPDATE_TEMPLATE) ERROR_CODE_INSERT_TEMPLATE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_INSERT_TEMPLATE) JdbcUtils(org.wso2.carbon.identity.core.util.JdbcUtils) LIST_PAGINATED_TEMPLATES_DB2(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.LIST_PAGINATED_TEMPLATES_DB2) ERROR_CODE_PAGINATED_LIST_TEMPLATES(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_PAGINATED_LIST_TEMPLATES) ERROR_CODE_DELETE_TEMPLATE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_DELETE_TEMPLATE) TemplateManagerDAO(org.wso2.carbon.identity.template.mgt.dao.TemplateManagerDAO) IOException(java.io.IOException) TemplateManagementServerException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException) JdbcUtils.isMSSqlDB(org.wso2.carbon.identity.core.util.JdbcUtils.isMSSqlDB) GET_TEMPLATE_BY_NAME(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.GET_TEMPLATE_BY_NAME) JdbcUtils.isMySQLDB(org.wso2.carbon.identity.core.util.JdbcUtils.isMySQLDB) LIST_PAGINATED_TEMPLATES_ORACLE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.LIST_PAGINATED_TEMPLATES_ORACLE) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) JdbcUtils.isH2DB(org.wso2.carbon.identity.core.util.JdbcUtils.isH2DB) TemplateManagementException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementException) JdbcUtils.isPostgreSQLDB(org.wso2.carbon.identity.core.util.JdbcUtils.isPostgreSQLDB) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) TemplateMgtConstants(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants) TemplateMgtUtils(org.wso2.carbon.identity.template.mgt.util.TemplateMgtUtils) JdbcUtils.isDB2DB(org.wso2.carbon.identity.core.util.JdbcUtils.isDB2DB) Template(org.wso2.carbon.identity.template.mgt.model.Template) InputStream(java.io.InputStream) TemplateManagementServerException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException) IOException(java.io.IOException) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) Template(org.wso2.carbon.identity.template.mgt.model.Template) TemplateManagementSQLException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementSQLException)

Aggregations

TemplateManagementServerException (org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException)4 JdbcTemplate (org.wso2.carbon.database.utils.jdbc.JdbcTemplate)3 DataAccessException (org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)3 TemplateInfo (org.wso2.carbon.identity.template.mgt.model.TemplateInfo)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 List (java.util.List)2 IOUtils (org.apache.commons.io.IOUtils)2 JdbcUtils (org.wso2.carbon.identity.core.util.JdbcUtils)2 JdbcUtils.isDB2DB (org.wso2.carbon.identity.core.util.JdbcUtils.isDB2DB)2 JdbcUtils.isH2DB (org.wso2.carbon.identity.core.util.JdbcUtils.isH2DB)2 JdbcUtils.isMSSqlDB (org.wso2.carbon.identity.core.util.JdbcUtils.isMSSqlDB)2 JdbcUtils.isMySQLDB (org.wso2.carbon.identity.core.util.JdbcUtils.isMySQLDB)2 JdbcUtils.isPostgreSQLDB (org.wso2.carbon.identity.core.util.JdbcUtils.isPostgreSQLDB)2 TemplateMgtConstants (org.wso2.carbon.identity.template.mgt.TemplateMgtConstants)2 ERROR_CODE_DELETE_TEMPLATE (org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_DELETE_TEMPLATE)2 ERROR_CODE_INSERT_TEMPLATE (org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_INSERT_TEMPLATE)2 ERROR_CODE_PAGINATED_LIST_TEMPLATES (org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_PAGINATED_LIST_TEMPLATES)2 ERROR_CODE_SELECT_TEMPLATE_BY_NAME (org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_SELECT_TEMPLATE_BY_NAME)2 ERROR_CODE_UPDATE_TEMPLATE (org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_UPDATE_TEMPLATE)2