Search in sources :

Example 1 with TemplateManagementException

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

the class CacheBackedConfigStoreBasedTemplateHandler method clearTemplateCache.

/**
 * Clearing cache entries related to the template.
 *
 * @param templateId Template id.
 */
private void clearTemplateCache(String templateId) throws TemplateManagementException {
    Template template = this.getTemplateById(templateId);
    if (template != null) {
        if (log.isDebugEnabled()) {
            log.debug("Removing entry for Template with id " + templateId + " from cache.");
        }
        ConfigStoreBasedTemplateCacheKey cacheKey = new ConfigStoreBasedTemplateCacheKey(templateId);
        configStoreBasedTemplateCache.clearCacheEntry(cacheKey, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Entry for Template with id " + templateId + " not found in cache or DB");
        }
    }
}
Also used : ConfigStoreBasedTemplateCacheKey(org.wso2.carbon.identity.template.mgt.cache.ConfigStoreBasedTemplateCacheKey) Template(org.wso2.carbon.identity.template.mgt.model.Template)

Example 2 with TemplateManagementException

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

the class ConfigStoreBasedTemplateHandler method listTemplates.

@Override
public List<Template> listTemplates(String templateType, Integer limit, Integer offset, Condition searchCondition) throws TemplateManagementException {
    ConfigurationManager configManager = TemplateManagerDataHolder.getInstance().getConfigurationManager();
    try {
        Resources resourcesList;
        if (searchCondition == null) {
            resourcesList = configManager.getResourcesByType(templateType);
        } else {
            resourcesList = configManager.getTenantResources(searchCondition);
        }
        return resourcesList.getResources().stream().map(resource -> {
            resource.setResourceType(templateType);
            return new ResourceToTemplate().apply(resource);
        }).collect(Collectors.toList());
    } catch (ConfigurationManagementException e) {
        if (ConfigurationConstants.ErrorMessages.ERROR_CODE_RESOURCE_TYPE_DOES_NOT_EXISTS.getCode().equals(e.getErrorCode())) {
            if (log.isDebugEnabled()) {
                log.debug("Template type : '" + templateType + "' has not been created in the database.", e);
            }
            return Collections.emptyList();
        } else if (ConfigurationConstants.ErrorMessages.ERROR_CODE_RESOURCES_DOES_NOT_EXISTS.getCode().equals(e.getErrorCode())) {
            if (log.isDebugEnabled()) {
                String message = "Templates do not exist for template type: " + templateType;
                if (searchCondition != null) {
                    message = message + ", and search  criteria:" + searchCondition.toString();
                }
                log.debug(message, e);
            }
            return Collections.emptyList();
        }
        throw handleServerException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_LIST_TEMPLATES, e, templateType, getTenantDomainFromCarbonContext());
    }
}
Also used : ResourceToTemplate(org.wso2.carbon.identity.template.mgt.function.ResourceToTemplate) WSTrustConnectorValidator.validateWSTrustTemplateAvailability(org.wso2.carbon.identity.template.mgt.handler.impl.WSTrustConnectorValidator.validateWSTrustTemplateAvailability) ConfigurationManagementException(org.wso2.carbon.identity.configuration.mgt.core.exception.ConfigurationManagementException) ConfigurationManager(org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager) Resource(org.wso2.carbon.identity.configuration.mgt.core.model.Resource) TemplateHandler(org.wso2.carbon.identity.template.mgt.handler.TemplateHandler) EnumUtils(org.apache.commons.lang3.EnumUtils) TemplateMgtUtils.getTenantDomainFromCarbonContext(org.wso2.carbon.identity.template.mgt.util.TemplateMgtUtils.getTenantDomainFromCarbonContext) Condition(org.wso2.carbon.identity.configuration.mgt.core.search.Condition) TemplateMgtUtils.handleClientException(org.wso2.carbon.identity.template.mgt.util.TemplateMgtUtils.handleClientException) TemplateToResource(org.wso2.carbon.identity.template.mgt.function.TemplateToResource) TemplateToResourceAdd(org.wso2.carbon.identity.template.mgt.function.TemplateToResourceAdd) TemplateMgtUtils.handleServerException(org.wso2.carbon.identity.template.mgt.util.TemplateMgtUtils.handleServerException) ResourceTypeAdd(org.wso2.carbon.identity.configuration.mgt.core.model.ResourceTypeAdd) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ConfigurationConstants(org.wso2.carbon.identity.configuration.mgt.core.constant.ConfigurationConstants) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) TemplateManagementException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementException) TemplateMgtConstants(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants) TemplateManagerDataHolder(org.wso2.carbon.identity.template.mgt.internal.TemplateManagerDataHolder) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) Resources(org.wso2.carbon.identity.configuration.mgt.core.model.Resources) Collections(java.util.Collections) Template(org.wso2.carbon.identity.template.mgt.model.Template) InputStream(java.io.InputStream) ResourceToTemplate(org.wso2.carbon.identity.template.mgt.function.ResourceToTemplate) Resources(org.wso2.carbon.identity.configuration.mgt.core.model.Resources) ConfigurationManagementException(org.wso2.carbon.identity.configuration.mgt.core.exception.ConfigurationManagementException) ConfigurationManager(org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager)

Example 3 with TemplateManagementException

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

the class ConfigStoreBasedTemplateHandler method addTemplateToConfigStore.

private String addTemplateToConfigStore(Template template) throws TemplateManagementException {
    if (!isValidTemplateType(template.getTemplateType().toString())) {
        throw handleClientException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_INVALID_TEMPLATE_TYPE, template.getTemplateType().toString());
    }
    ConfigurationManager configManager = TemplateManagerDataHolder.getInstance().getConfigurationManager();
    try {
        Resource resource = configManager.addResource(template.getTemplateType().toString(), new TemplateToResourceAdd().apply(template));
        configManager.addFile(template.getTemplateType().toString(), template.getTemplateName(), template.getTemplateName() + "_template_object", IOUtils.toInputStream(template.getTemplateScript()));
        return resource.getResourceId();
    } catch (ConfigurationManagementException e) {
        if (ConfigurationConstants.ErrorMessages.ERROR_CODE_RESOURCE_ALREADY_EXISTS.getCode().equals(e.getErrorCode())) {
            throw handleClientException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_ALREADY_EXIST, e, template.getTemplateName());
        } else if (ConfigurationConstants.ErrorMessages.ERROR_CODE_RESOURCE_TYPE_DOES_NOT_EXISTS.getCode().equals(e.getErrorCode())) {
            // in the database, create the resource-type and retry the template creation.
            try {
                createResourceType(template.getTemplateType().toString());
                return addTemplateToConfigStore(template);
            } catch (ConfigurationManagementException e1) {
                throw handleServerException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_INSERT_TEMPLATE, e, template.getTemplateName());
            }
        } else {
            throw handleServerException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_INSERT_TEMPLATE, e, template.getTemplateName());
        }
    }
}
Also used : Resource(org.wso2.carbon.identity.configuration.mgt.core.model.Resource) TemplateToResource(org.wso2.carbon.identity.template.mgt.function.TemplateToResource) TemplateToResourceAdd(org.wso2.carbon.identity.template.mgt.function.TemplateToResourceAdd) ConfigurationManagementException(org.wso2.carbon.identity.configuration.mgt.core.exception.ConfigurationManagementException) ConfigurationManager(org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager)

Example 4 with TemplateManagementException

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

the class TemplateManagerImpl method listTemplates.

/**
 * This method is used to get the names and descriptions of all or filtered existing templates.
 *
 * @param limit  Number of search results.
 * @param offset Start index of the search.
 * @return Filtered list of TemplateInfo elements.
 * @throws TemplateManagementException Template Management Exception.
 */
@Override
public List<TemplateInfo> listTemplates(Integer limit, Integer offset) throws TemplateManagementException {
    validatePaginationParameters(limit, offset);
    if (limit == 0) {
        limit = DEFAULT_SEARCH_LIMIT;
        if (log.isDebugEnabled()) {
            log.debug("Limit is not defined in the request, default to: " + limit);
        }
    }
    TemplateManagerDAO templateManagerDAO = new TemplateManagerDAOImpl();
    return templateManagerDAO.getAllTemplates(getTenantIdFromCarbonContext(), limit, offset);
}
Also used : TemplateManagerDAOImpl(org.wso2.carbon.identity.template.mgt.dao.impl.TemplateManagerDAOImpl) TemplateManagerDAO(org.wso2.carbon.identity.template.mgt.dao.TemplateManagerDAO)

Example 5 with TemplateManagementException

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

the class TemplateManagerDAOImpl method addTemplate.

/**
 * Add a {@link Template}.
 *
 * @param template {@link Template} to insert.
 * @return Inserted {@link TemplateInfo}.
 * @throws TemplateManagementException If error occurs while adding the {@link Template}.
 */
public Template addTemplate(Template template) throws TemplateManagementException {
    Template templateResult;
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        jdbcTemplate.executeUpdate(TemplateMgtConstants.SqlQueries.INSERT_TEMPLATE, preparedStatement -> {
            preparedStatement.setInt(1, template.getTenantId());
            preparedStatement.setString(2, template.getTemplateName());
            preparedStatement.setString(3, template.getDescription());
            InputStream inputStream = IOUtils.toInputStream(template.getTemplateScript());
            try {
                preparedStatement.setBinaryStream(4, inputStream, inputStream.available());
            } catch (IOException e) {
                // SQLException is thrown since the QueryFilter throws an SQLException
                throw TemplateMgtUtils.handleSQLException(ERROR_CODE_INSERT_TEMPLATE, template.getTemplateName(), e);
            }
        });
    } catch (DataAccessException e) {
        throw TemplateMgtUtils.handleServerException(ERROR_CODE_INSERT_TEMPLATE, template.getTemplateName(), e);
    }
    templateResult = new Template(template.getTenantId(), template.getTemplateName(), template.getDescription(), template.getTemplateScript());
    return templateResult;
}
Also used : InputStream(java.io.InputStream) 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)

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