Search in sources :

Example 1 with DisableClassLoadingConstructor

use of org.craftercms.commons.config.DisableClassLoadingConstructor 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)

Example 2 with DisableClassLoadingConstructor

use of org.craftercms.commons.config.DisableClassLoadingConstructor in project studio by craftercms.

the class AbstractPluginDescriptorUpgradeOperation method execute.

@Override
public void execute(final String site) throws UpgradeException {
    Path descriptorFile = getRepositoryPath(site).getParent().resolve(descriptorPath);
    if (Files.notExists(descriptorFile)) {
        logger.info("Plugin descriptor file not found for site {0}", site);
        return;
    }
    try (Reader reader = Files.newBufferedReader(descriptorFile)) {
        PluginDescriptor descriptor = descriptorReader.read(reader);
        if (descriptor.getDescriptorVersion().equals(descriptorVersion)) {
            logger.info("Plugin descriptor already update for site " + site);
            return;
        }
        logger.info("Updating plugin descriptor for site " + site);
        doPluginDescriptorUpdates(descriptor);
        descriptor.setDescriptorVersion(descriptorVersion);
        DumperOptions options = new DumperOptions();
        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
        options.setPrettyFlow(true);
        Yaml yaml = new Yaml(new DisableClassLoadingConstructor(), new Representer() {

            @Override
            protected NodeTuple representJavaBeanProperty(final Object javaBean, final Property property, final Object propertyValue, final Tag customTag) {
                if (propertyValue != null) {
                    return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
                }
                return null;
            }
        }, options);
        String content = yaml.dumpAsMap(descriptor);
        writeToRepo(site, descriptorPath, new ByteArrayInputStream(content.getBytes()));
        commitAllChanges(site);
    } catch (Exception e) {
        throw new UpgradeException("Plugin descriptor can't be read for site " + site);
    }
}
Also used : Path(java.nio.file.Path) PluginDescriptorReader(org.craftercms.commons.plugin.PluginDescriptorReader) Reader(java.io.Reader) Yaml(org.yaml.snakeyaml.Yaml) UpgradeException(org.craftercms.studio.api.v2.exception.UpgradeException) UpgradeException(org.craftercms.studio.api.v2.exception.UpgradeException) PluginDescriptor(org.craftercms.commons.plugin.model.PluginDescriptor) Representer(org.yaml.snakeyaml.representer.Representer) ByteArrayInputStream(java.io.ByteArrayInputStream) DumperOptions(org.yaml.snakeyaml.DumperOptions) Tag(org.yaml.snakeyaml.nodes.Tag) DisableClassLoadingConstructor(org.craftercms.commons.config.DisableClassLoadingConstructor) Property(org.yaml.snakeyaml.introspector.Property) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 DisableClassLoadingConstructor (org.craftercms.commons.config.DisableClassLoadingConstructor)2 Yaml (org.yaml.snakeyaml.Yaml)2 IOException (java.io.IOException)1 Reader (java.io.Reader)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 PluginDescriptorReader (org.craftercms.commons.plugin.PluginDescriptorReader)1 PluginDescriptor (org.craftercms.commons.plugin.model.PluginDescriptor)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 UpgradeException (org.craftercms.studio.api.v2.exception.UpgradeException)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 DumperOptions (org.yaml.snakeyaml.DumperOptions)1 Property (org.yaml.snakeyaml.introspector.Property)1