use of org.eclipse.vorto.repository.core.impl.validation.ValidationException in project vorto by eclipse.
the class BulkUploadHelper method uploadMultiple.
public List<UploadModelResult> uploadMultiple(byte[] content, String zipFileName, String callerId) {
if (!isValid(zipFileName)) {
throw new FatalModelRepositoryException("Filename/type is invalid", null);
}
List<UploadModelResult> invalidResult = new ArrayList<UploadModelResult>();
List<UploadModelResult> validResult = new ArrayList<UploadModelResult>();
Set<ModelInfo> parsedModels = new HashSet<>();
ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(content));
ZipEntry entry = null;
try {
while ((entry = zis.getNextEntry()) != null) {
if (!entry.isDirectory()) {
final String fileName = entry.getName();
if (ModelParserFactory.hasParserFor(fileName)) {
try {
parsedModels.add(ModelParserFactory.getParser(fileName).parse(new ByteArrayInputStream(copyStream(zis, entry))));
} catch (ValidationException grammarProblem) {
invalidResult.add(UploadModelResultFactory.create(grammarProblem));
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
try {
List<IModelValidator> bulkUploadValidators = constructBulkUploadValidators(parsedModels);
for (ModelInfo modelResource : parsedModels) {
try {
for (IModelValidator validator : bulkUploadValidators) {
validator.validate(modelResource, InvocationContext.create(callerId));
}
UploadModelResult result = UploadModelResult.valid(modelResource);
if (!invalidResult.contains(result)) {
validResult.add(result);
}
} catch (ValidationException validationException) {
invalidResult.add(UploadModelResultFactory.create(validationException));
}
}
if (invalidResult.isEmpty()) {
DependencyManager dm = new DependencyManager(parsedModels);
return safelyUpload(dm.getSorted());
} else {
List<UploadModelResult> completeResult = new ArrayList<>();
completeResult.addAll(invalidResult);
completeResult.addAll(validResult);
return completeResult;
}
} catch (Exception e) {
throw new FatalModelRepositoryException("Invalid zip file", e);
}
}
use of org.eclipse.vorto.repository.core.impl.validation.ValidationException in project vorto by eclipse.
the class JcrModelRepository method upload.
@Override
public UploadModelResult upload(byte[] content, String fileName, String callerId) {
try {
ModelInfo resource = ModelParserFactory.getParser(fileName).parse(new ByteArrayInputStream(content));
List<ValidationException> validationExceptions = new ArrayList<ValidationException>();
for (IModelValidator validator : validators) {
try {
validator.validate(resource, InvocationContext.create(callerId));
} catch (ValidationException validationException) {
validationExceptions.add(validationException);
}
}
if (validationExceptions.size() <= 0) {
return UploadModelResult.valid(createUploadHandle(content, resource.getType()), resource);
} else {
return UploadModelResultFactory.create(validationExceptions.toArray(new ValidationException[validationExceptions.size()]));
}
} catch (ValidationException e) {
return UploadModelResultFactory.invalid(e);
}
}
use of org.eclipse.vorto.repository.core.impl.validation.ValidationException in project vorto by eclipse.
the class AbstractModelParser method parse.
@Override
public ModelInfo parse(InputStream is) {
XtextResourceSet resourceSet = getInjector().getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
Resource resource = resourceSet.createResource(URI.createURI("dummy:/" + fileName));
try {
resource.load(is, resourceSet.getLoadOptions());
} catch (IOException e) {
throw new ValidationException(e.getMessage(), null);
}
List<org.eclipse.emf.ecore.resource.Resource.Diagnostic> grammarErrors = getGrammarErrors(resource.getErrors());
if (!grammarErrors.isEmpty()) {
ModelInfo invalidModelResource = new ModelInfo(parseModelIdFromFileName(), ModelType.fromFileName(fileName));
throw new ValidationException(grammarErrors.get(0).getMessage(), invalidModelResource);
}
return new ModelEMFResource((Model) resource.getContents().get(0));
}
Aggregations