use of org.eclipse.vorto.repository.importer.ValidationReport in project vorto by eclipse.
the class BulkUploadHelper method uploadMultiple.
public List<ValidationReport> uploadMultiple(byte[] content, String zipFileName, IUserContext user) {
if (!isValid(zipFileName)) {
throw new FatalModelRepositoryException("Filename/type is invalid", null);
}
ZipParseResult parseResult = parseZipFile(content);
try {
/*
* Create mapping function that will convert from a ModelInfo to an UploadModelResult using
* validators
*/
Function<ModelInfo, ValidationReport> convertToValidationReport = createConvertToUploadModelResultFn(constructBulkUploadValidators(parseResult.validModels), InvocationContext.create(user));
/*
* Convert parsed models to ValidationReport
*/
Set<ValidationReport> validatedModelResults = parseResult.validModels.stream().map(convertToValidationReport).collect(Collectors.toSet());
/*
* Add everything to a Set to eliminate redundancy
*/
Set<ValidationReport> validationReports = new HashSet<>();
validationReports.addAll(parseResult.invalidModels);
validationReports.addAll(validatedModelResults);
return new ArrayList<>(validationReports);
} catch (Exception e) {
throw new BulkUploadException("Invalid zip file", e);
}
}
use of org.eclipse.vorto.repository.importer.ValidationReport in project vorto by eclipse.
the class RemoteImporter method validate.
@Override
protected List<ValidationReport> validate(FileUpload fileUpload, Context context) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", new FileContentResource(fileUpload));
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
ResponseEntity<org.eclipse.vorto.plugin.importer.ValidationReport> validationResult = restTemplate.postForEntity(this.endpointUrl + "/api/2/plugins/importers/{pluginkey}/file_validation", requestEntity, org.eclipse.vorto.plugin.importer.ValidationReport.class, this.info.getKey());
org.eclipse.vorto.plugin.importer.ValidationReport result = validationResult.getBody();
if (result.isValid()) {
ModelInfo modelInfo = new ModelInfo(new ModelId(fileUpload.getName(), context.getTargetNamespace().get(), "1.0.0"), org.eclipse.vorto.model.ModelType.Functionblock);
ValidationReport report = modelValidationHelper.validateModelCreation(modelInfo, context.getUser());
return Arrays.asList(report);
} else {
return Arrays.asList(ValidationReport.invalid(result.getMessage()));
}
}
use of org.eclipse.vorto.repository.importer.ValidationReport in project vorto by eclipse.
the class ModelRepositoryController method saveModel.
@ApiOperation("Saves a model to the repository.")
@PreAuthorize("hasAuthority('sysadmin') or " + "hasPermission(T(org.eclipse.vorto.model.ModelId).fromPrettyFormat(#modelId)," + "T(org.eclipse.vorto.repository.core.PolicyEntry.Permission).MODIFY)")
@PutMapping(value = "/{modelId:.+}", produces = "application/json")
public ResponseEntity<ValidationReport> saveModel(@ApiParam(value = "modelId", required = true) @PathVariable String modelId, @RequestBody ModelContent content) {
try {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
IModelRepository modelRepository = getModelRepository(ModelId.fromPrettyFormat(modelId));
ModelId modelID = ModelId.fromPrettyFormat(modelId);
if (modelRepository.getById(modelID) == null) {
return new ResponseEntity<>(ValidationReport.invalid(null, "Model was not found"), HttpStatus.NOT_FOUND);
}
IUserContext userContext = UserContext.user(authentication, getWorkspaceId(modelId));
ModelResource modelInfo = (ModelResource) modelParserFactory.getParser("model" + ModelType.valueOf(content.getType()).getExtension()).parse(new ByteArrayInputStream(content.getContentDsl().getBytes()));
if (!modelID.equals(modelInfo.getId())) {
return new ResponseEntity<>(ValidationReport.invalid(modelInfo, "You may not change the model ID (name, namespace, version). For this please create a new model."), HttpStatus.BAD_REQUEST);
}
ValidationReport validationReport = modelValidationHelper.validateModelUpdate(modelInfo, userContext);
if (validationReport.isValid()) {
modelRepository.save(modelInfo.getId(), content.getContentDsl().getBytes(), modelInfo.getId().getName() + modelInfo.getType().getExtension(), userContext);
}
return new ResponseEntity<>(validationReport, HttpStatus.OK);
} catch (ValidationException validationException) {
LOGGER.warn(validationException);
return new ResponseEntity<>(ValidationReport.invalid(null, validationException), HttpStatus.BAD_REQUEST);
}
}
Aggregations