Search in sources :

Example 1 with InvalidConfigurationException

use of org.craftercms.studio.api.v2.exception.configuration.InvalidConfigurationException in project studio by craftercms.

the class ConfigurationServiceImpl method validate.

@SuppressWarnings("unchecked")
protected InputStream validate(InputStream content, String filename) throws ServiceLayerException {
    // Check the filename to see if it needs to be validated
    String extension = getExtension(filename);
    if (isEmpty(extension)) {
        // without extension there is no way to know
        return content;
    }
    try {
        // Copy the contents of the stream
        byte[] bytes;
        bytes = IOUtils.toByteArray(content);
        // Perform the validation
        switch(extension.toLowerCase()) {
            case "xml":
                try {
                    DocumentHelper.parseText(new String(bytes));
                } catch (Exception e) {
                    throw new InvalidConfigurationException("Invalid XML file", e);
                }
                break;
            case "yaml":
            case "yml":
                try {
                    Yaml yaml = new Yaml(new DisableClassLoadingConstructor());
                    Map<String, Object> map = (Map<String, Object>) yaml.load(new ByteArrayInputStream(bytes));
                } catch (Exception e) {
                    throw new InvalidConfigurationException("Invalid YAML file", e);
                }
        }
        // Return a new stream
        return new ByteArrayInputStream(bytes);
    } catch (IOException e) {
        throw new ServiceLayerException("Error validating configuration", e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) IOException(java.io.IOException) DisableClassLoadingConstructor(org.craftercms.commons.config.DisableClassLoadingConstructor) Map(java.util.Map) HashMap(java.util.HashMap) SAXException(org.xml.sax.SAXException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) DocumentException(org.dom4j.DocumentException) ConfigurationException(org.craftercms.studio.api.v2.exception.configuration.ConfigurationException) IOException(java.io.IOException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) InvalidConfigurationException(org.craftercms.studio.api.v2.exception.configuration.InvalidConfigurationException) Yaml(org.yaml.snakeyaml.Yaml) InvalidConfigurationException(org.craftercms.studio.api.v2.exception.configuration.InvalidConfigurationException)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DisableClassLoadingConstructor (org.craftercms.commons.config.DisableClassLoadingConstructor)1 ContentNotFoundException (org.craftercms.studio.api.v1.exception.ContentNotFoundException)1 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)1 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)1 ConfigurationException (org.craftercms.studio.api.v2.exception.configuration.ConfigurationException)1 InvalidConfigurationException (org.craftercms.studio.api.v2.exception.configuration.InvalidConfigurationException)1 DocumentException (org.dom4j.DocumentException)1 SAXException (org.xml.sax.SAXException)1 Yaml (org.yaml.snakeyaml.Yaml)1