Search in sources :

Example 16 with TemplateManagementException

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

the class TemplateManagerImpl method addTemplateUsingTemplateMgtDAO.

@Override
public Template addTemplateUsingTemplateMgtDAO(Template template) throws TemplateManagementException {
    validateInputParameters(template);
    if (isTemplateExists(template.getTemplateName())) {
        if (log.isDebugEnabled()) {
            log.debug("A template already exists with the name: " + template.getTemplateName());
        }
        throw handleClientException(ERROR_CODE_TEMPLATE_ALREADY_EXIST, template.getTemplateName());
    }
    TemplateManagerDAO templateManagerDAO = new TemplateManagerDAOImpl();
    return templateManagerDAO.addTemplate(template);
}
Also used : TemplateManagerDAOImpl(org.wso2.carbon.identity.template.mgt.dao.impl.TemplateManagerDAOImpl) TemplateManagerDAO(org.wso2.carbon.identity.template.mgt.dao.TemplateManagerDAO)

Example 17 with TemplateManagementException

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

the class TemplateManagerImpl method updateTemplate.

/**
 * This method is used to update an existing Template.
 *
 * @param templateName Name of the updated template.
 * @param template     Template element.
 * @return Return the updated Template element.
 * @throws TemplateManagementException Template Management Exception.
 */
@Override
public Template updateTemplate(String templateName, Template template) throws TemplateManagementException {
    validateInputParameters(template);
    TemplateManagerDAO templateManagerDAO = new TemplateManagerDAOImpl();
    if (!isTemplateExists(templateName)) {
        if (log.isDebugEnabled()) {
            log.debug("No template found for the name: " + templateName);
        }
        throw handleClientException(ERROR_CODE_TEMPLATE_NAME_INVALID, templateName);
    }
    if (!StringUtils.equals(templateName, template.getTemplateName()) && isTemplateExists(template.getTemplateName())) {
        // with the name of the updated template
        if (log.isDebugEnabled()) {
            log.debug("A template already exists with the name: " + template.getTemplateName());
        }
        throw handleClientException(ERROR_CODE_TEMPLATE_ALREADY_EXIST, template.getTemplateName());
    }
    return templateManagerDAO.updateTemplate(templateName, template);
}
Also used : TemplateManagerDAOImpl(org.wso2.carbon.identity.template.mgt.dao.impl.TemplateManagerDAOImpl) TemplateManagerDAO(org.wso2.carbon.identity.template.mgt.dao.TemplateManagerDAO)

Example 18 with TemplateManagementException

use of org.wso2.carbon.identity.template.mgt.exception.TemplateManagementException 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)

Example 19 with TemplateManagementException

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

the class TemplateManagementServiceClient method addTemplate.

public Template addTemplate(TemplateRequestDTO templateRequestDTO) throws TemplateManagementException {
    handleLoggedInUserAuthorization(TemplateMgtConstants.PERMISSION_TEMPLATE_MGT_ADD);
    Template template = new Template(templateRequestDTO.getTenantId(), templateRequestDTO.getTemplateName(), templateRequestDTO.getDescription(), templateRequestDTO.getTemplateScript());
    return getTemplateManager().addTemplateUsingTemplateMgtDAO(template);
}
Also used : Template(org.wso2.carbon.identity.template.mgt.model.Template)

Example 20 with TemplateManagementException

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

the class TemplateManagementServiceClient method handleLoggedInUserAuthorization.

private void handleLoggedInUserAuthorization(String permission) throws TemplateManagementException {
    try {
        int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
        if (StringUtils.isBlank(loggedInUser)) {
            throw new TemplateManagementException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_NO_AUTH_USER_FOUND.getMessage(), TemplateMgtConstants.ErrorMessages.ERROR_CODE_NO_AUTH_USER_FOUND.getCode());
        }
        AuthorizationManager authorizationManager = TemplateManagementUIServiceDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId).getAuthorizationManager();
        if (!authorizationManager.isUserAuthorized(loggedInUser, permission, CarbonConstants.UI_PERMISSION_ACTION)) {
            throw new TemplateManagementException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_USER_NOT_AUTHORIZED.getMessage(), TemplateMgtConstants.ErrorMessages.ERROR_CODE_USER_NOT_AUTHORIZED.getCode());
        }
    } catch (UserStoreException e) {
        throw new TemplateManagementException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_UNEXPECTED.getMessage(), TemplateMgtConstants.ErrorMessages.ERROR_CODE_UNEXPECTED.getCode());
    }
}
Also used : TemplateManagementException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) AuthorizationManager(org.wso2.carbon.user.api.AuthorizationManager)

Aggregations

Template (org.wso2.carbon.identity.template.mgt.model.Template)16 TemplateManagementException (org.wso2.carbon.identity.template.mgt.exception.TemplateManagementException)10 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 ConfigurationManager (org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager)5 ConfigurationManagementException (org.wso2.carbon.identity.configuration.mgt.core.exception.ConfigurationManagementException)5 TemplateManagerDAO (org.wso2.carbon.identity.template.mgt.dao.TemplateManagerDAO)5 JdbcTemplate (org.wso2.carbon.database.utils.jdbc.JdbcTemplate)4 DataAccessException (org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)4 TemplateToResource (org.wso2.carbon.identity.template.mgt.function.TemplateToResource)4 List (java.util.List)3 IOUtils (org.apache.commons.io.IOUtils)3 IdentityProviderTemplate (org.wso2.carbon.identity.api.server.idp.v1.model.IdentityProviderTemplate)3 Resource (org.wso2.carbon.identity.configuration.mgt.core.model.Resource)3 TemplateManager (org.wso2.carbon.identity.template.mgt.TemplateManager)3 TemplateMgtConstants (org.wso2.carbon.identity.template.mgt.TemplateMgtConstants)3 TemplateManagementServerException (org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 JdbcUtils (org.wso2.carbon.identity.core.util.JdbcUtils)2 JdbcUtils.isDB2DB (org.wso2.carbon.identity.core.util.JdbcUtils.isDB2DB)2