Search in sources :

Example 1 with ValidationException

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);
    }
}
Also used : UploadModelResult(org.eclipse.vorto.repository.api.upload.UploadModelResult) ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) IModelValidator(org.eclipse.vorto.repository.core.impl.validation.IModelValidator) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) HashSet(java.util.HashSet)

Example 2 with ValidationException

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);
    }
}
Also used : ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) IModelValidator(org.eclipse.vorto.repository.core.impl.validation.IModelValidator)

Example 3 with ValidationException

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));
}
Also used : ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) ModelEMFResource(org.eclipse.vorto.repository.core.impl.ModelEMFResource) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) XtextResource(org.eclipse.xtext.resource.XtextResource) ModelEMFResource(org.eclipse.vorto.repository.core.impl.ModelEMFResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextLinkingDiagnostic(org.eclipse.xtext.linking.impl.XtextLinkingDiagnostic) IOException(java.io.IOException)

Aggregations

ModelInfo (org.eclipse.vorto.repository.api.ModelInfo)3 ValidationException (org.eclipse.vorto.repository.core.impl.validation.ValidationException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArrayList (java.util.ArrayList)2 IModelValidator (org.eclipse.vorto.repository.core.impl.validation.IModelValidator)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 UploadModelResult (org.eclipse.vorto.repository.api.upload.UploadModelResult)1 FatalModelRepositoryException (org.eclipse.vorto.repository.core.FatalModelRepositoryException)1 ModelEMFResource (org.eclipse.vorto.repository.core.impl.ModelEMFResource)1 XtextLinkingDiagnostic (org.eclipse.xtext.linking.impl.XtextLinkingDiagnostic)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)1