Search in sources :

Example 1 with ValidationReport

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);
    }
}
Also used : ValidationReport(org.eclipse.vorto.repository.importer.ValidationReport) BulkUploadException(org.eclipse.vorto.repository.web.core.exceptions.BulkUploadException) IOException(java.io.IOException) BulkUploadException(org.eclipse.vorto.repository.web.core.exceptions.BulkUploadException)

Example 2 with ValidationReport

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()));
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ValidationReport(org.eclipse.vorto.repository.importer.ValidationReport) FileContentResource(org.eclipse.vorto.repository.plugin.FileContentResource) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ModelId(org.eclipse.vorto.model.ModelId)

Example 3 with ValidationReport

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);
    }
}
Also used : IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) IUserContext(org.eclipse.vorto.repository.core.IUserContext) ResponseEntity(org.springframework.http.ResponseEntity) ModelResource(org.eclipse.vorto.repository.core.ModelResource) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) ByteArrayInputStream(java.io.ByteArrayInputStream) ValidationReport(org.eclipse.vorto.repository.importer.ValidationReport) Authentication(org.springframework.security.core.Authentication) ModelId(org.eclipse.vorto.model.ModelId) ApiOperation(io.swagger.annotations.ApiOperation) PutMapping(org.springframework.web.bind.annotation.PutMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

ValidationReport (org.eclipse.vorto.repository.importer.ValidationReport)3 ModelId (org.eclipse.vorto.model.ModelId)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 IModelRepository (org.eclipse.vorto.repository.core.IModelRepository)1 IUserContext (org.eclipse.vorto.repository.core.IUserContext)1 ModelInfo (org.eclipse.vorto.repository.core.ModelInfo)1 ModelResource (org.eclipse.vorto.repository.core.ModelResource)1 ValidationException (org.eclipse.vorto.repository.core.impl.validation.ValidationException)1 FileContentResource (org.eclipse.vorto.repository.plugin.FileContentResource)1 BulkUploadException (org.eclipse.vorto.repository.web.core.exceptions.BulkUploadException)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 Authentication (org.springframework.security.core.Authentication)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1 MultiValueMap (org.springframework.util.MultiValueMap)1 PutMapping (org.springframework.web.bind.annotation.PutMapping)1