Search in sources :

Example 1 with IModelImporter

use of org.eclipse.vorto.repository.importer.IModelImporter in project vorto by eclipse.

the class ImportController method uploadModel.

@RequestMapping(method = RequestMethod.POST)
@PreAuthorize("hasAuthority('model_creator')")
public ResponseEntity<UploadModelResult> uploadModel(@ApiParam(value = "The vorto model file to upload", required = true) @RequestParam("file") MultipartFile file, @RequestParam("key") String key, @RequestParam(required = true, value = "targetNamespace") String targetNamespace) {
    if (file.getSize() > maxModelSize) {
        throw new UploadTooLargeException("model", maxModelSize);
    }
    LOGGER.info(String.format("uploadModel: [%s]", file.getOriginalFilename()));
    try {
        IModelImporter importer = importerService.getImporterByKey(key).get();
        UploadModelResult result = importer.upload(FileUpload.create(file.getOriginalFilename(), file.getBytes()), Context.create(getUserContext(targetNamespace), Optional.of(targetNamespace)));
        if (!result.isValid()) {
            result.setMessage(String.format(UPLOAD_FAIL, file.getOriginalFilename()));
        } else {
            if (result.hasWarnings()) {
                result.setMessage(String.format(UPLOAD_WARNING, file.getOriginalFilename()));
            } else {
                result.setMessage(String.format(UPLOAD_VALID, file.getOriginalFilename()));
            }
        }
        return new ResponseEntity<>(result, HttpStatus.OK);
    } catch (IOException e) {
        return new ResponseEntity<>(new UploadModelResult(null, e.getMessage(), Collections.emptyList()), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
Also used : UploadModelResult(org.eclipse.vorto.repository.importer.UploadModelResult) ResponseEntity(org.springframework.http.ResponseEntity) UploadTooLargeException(org.eclipse.vorto.repository.web.core.exceptions.UploadTooLargeException) IModelImporter(org.eclipse.vorto.repository.importer.IModelImporter) IOException(java.io.IOException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with IModelImporter

use of org.eclipse.vorto.repository.importer.IModelImporter in project vorto by eclipse.

the class ImportController method doImport.

@RequestMapping(value = "/{handleId:.+}", method = RequestMethod.PUT)
@PreAuthorize("hasAuthority('model_creator')")
public ResponseEntity<List<ModelInfo>> doImport(@ApiParam(value = "The file name of uploaded model", required = true) @PathVariable final String handleId, @RequestParam("key") String key, @RequestParam(required = true, value = "targetNamespace") String targetNamespace) {
    LOGGER.info(String.format("Importing Model with handleID %s", handleId));
    try {
        IModelImporter importer = importerService.getImporterByKey(key).get();
        List<ModelInfo> importedModels = importer.doImport(handleId, Context.create(getUserContext(targetNamespace), Optional.of(targetNamespace)));
        for (ModelInfo modelInfo : importedModels) {
            workflowService.start(modelInfo.getId(), getUserContext(targetNamespace));
        }
        return new ResponseEntity<>(importedModels, HttpStatus.OK);
    } catch (Exception e) {
        LOGGER.error(String.format("Error Importing model. %s", handleId), e);
        throw new IllegalArgumentException(String.format("Could not import with handle ID %s", handleId), e);
    }
}
Also used : ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ResponseEntity(org.springframework.http.ResponseEntity) IModelImporter(org.eclipse.vorto.repository.importer.IModelImporter) UploadTooLargeException(org.eclipse.vorto.repository.web.core.exceptions.UploadTooLargeException) IOException(java.io.IOException) WorkspaceNotFoundException(org.eclipse.vorto.repository.core.WorkspaceNotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

IOException (java.io.IOException)2 IModelImporter (org.eclipse.vorto.repository.importer.IModelImporter)2 UploadTooLargeException (org.eclipse.vorto.repository.web.core.exceptions.UploadTooLargeException)2 ResponseEntity (org.springframework.http.ResponseEntity)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ModelInfo (org.eclipse.vorto.repository.core.ModelInfo)1 WorkspaceNotFoundException (org.eclipse.vorto.repository.core.WorkspaceNotFoundException)1 UploadModelResult (org.eclipse.vorto.repository.importer.UploadModelResult)1