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);
}
}
Aggregations