Search in sources :

Example 6 with GenericApplicationException

use of org.eclipse.vorto.repository.web.GenericApplicationException in project vorto by eclipse.

the class ModelRepositoryController method uploadModelImage.

@PostMapping("/{modelId:.+}/images")
@PreAuthorize("hasAuthority('sysadmin') or " + "hasPermission(T(org.eclipse.vorto.model.ModelId).fromPrettyFormat(#modelId)," + "T(org.eclipse.vorto.repository.core.PolicyEntry.Permission).MODIFY)")
public ResponseEntity<AttachResult> uploadModelImage(@ApiParam(value = "The image to upload", required = true) @RequestParam("file") MultipartFile file, @ApiParam(value = "The model ID of vorto model, e.g. com.mycompany.Car:1.0.0", required = true) @PathVariable final String modelId) {
    LOGGER.info("uploadImage: [" + file.getOriginalFilename() + ", " + file.getSize() + "]");
    ModelId actualModelID = ModelId.fromPrettyFormat(modelId);
    if (!attachmentValidator.validateAttachmentSize(file.getSize())) {
        return new ResponseEntity<>(AttachResult.fail(actualModelID, file.getOriginalFilename(), String.format("The attachment is too large. Maximum size allowed is %dMB", attachmentValidator.getMaxFileSizeSetting())), HttpStatus.PAYLOAD_TOO_LARGE);
    }
    try {
        IUserContext user = UserContext.user(SecurityContextHolder.getContext().getAuthentication(), getWorkspaceId(modelId));
        getModelRepository(actualModelID).attachFile(actualModelID, new FileContent(file.getOriginalFilename(), file.getBytes()), user, Attachment.TAG_IMAGE, Attachment.TAG_DISPLAY_IMAGE);
    } catch (IOException e) {
        throw new GenericApplicationException("error in attaching file to model '" + modelId + "'", e);
    }
    return new ResponseEntity<>(AttachResult.success(actualModelID, file.getOriginalFilename()), HttpStatus.CREATED);
}
Also used : FileContent(org.eclipse.vorto.repository.core.FileContent) IUserContext(org.eclipse.vorto.repository.core.IUserContext) ResponseEntity(org.springframework.http.ResponseEntity) IOException(java.io.IOException) GenericApplicationException(org.eclipse.vorto.repository.web.GenericApplicationException) ModelId(org.eclipse.vorto.model.ModelId) PostMapping(org.springframework.web.bind.annotation.PostMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 7 with GenericApplicationException

use of org.eclipse.vorto.repository.web.GenericApplicationException in project vorto by eclipse.

the class ModelController method createZipWithAllDependencies.

private byte[] createZipWithAllDependencies(ModelId modelId) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);
    try {
        addModelToZip(zos, modelId);
        zos.close();
        baos.close();
        return baos.toByteArray();
    } catch (Exception ex) {
        throw new GenericApplicationException("Error while generating zip file.", ex);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericApplicationException(org.eclipse.vorto.repository.web.GenericApplicationException) GenericApplicationException(org.eclipse.vorto.repository.web.GenericApplicationException) ModelNotFoundException(org.eclipse.vorto.repository.core.ModelNotFoundException) IOException(java.io.IOException)

Example 8 with GenericApplicationException

use of org.eclipse.vorto.repository.web.GenericApplicationException in project vorto by eclipse.

the class ModelController method downloadModelById.

@PreAuthorize("isAuthenticated() or hasAuthority('model_viewer')")
@GetMapping("/{modelId:.+}/file")
public void downloadModelById(@ApiParam(value = "The modelId of vorto model, e.g. com.mycompany:Car:1.0.0", required = true) @PathVariable final String modelId, @ApiParam(value = "Set true if dependencies shall be included", required = false) @RequestParam(value = "includeDependencies", required = false) final boolean includeDependencies, final HttpServletResponse response) {
    Objects.requireNonNull(modelId, "modelId must not be null");
    final ModelId modelID = ModelId.fromPrettyFormat(modelId);
    LOGGER.info("Download of Model file : [" + modelID.toString() + "]");
    if (includeDependencies) {
        byte[] zipContent = createZipWithAllDependencies(modelID);
        response.setHeader(CONTENT_DISPOSITION, ATTACHMENT_FILENAME + modelID.getNamespace() + "_" + modelID.getName() + "_" + modelID.getVersion() + ".zip");
        response.setContentType(APPLICATION_OCTET_STREAM);
        try {
            IOUtils.copy(new ByteArrayInputStream(zipContent), response.getOutputStream());
            response.flushBuffer();
        } catch (IOException e) {
            throw new GenericApplicationException("Error copying file.", e);
        }
    } else {
        createSingleModelContent(modelID, response);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) GenericApplicationException(org.eclipse.vorto.repository.web.GenericApplicationException) ModelId(org.eclipse.vorto.model.ModelId) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

IOException (java.io.IOException)8 GenericApplicationException (org.eclipse.vorto.repository.web.GenericApplicationException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ZipOutputStream (java.util.zip.ZipOutputStream)3 ModelId (org.eclipse.vorto.model.ModelId)3 FileContent (org.eclipse.vorto.repository.core.FileContent)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ZipEntry (java.util.zip.ZipEntry)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 Date (java.util.Date)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 Attachment (org.eclipse.vorto.repository.core.Attachment)1 FatalModelRepositoryException (org.eclipse.vorto.repository.core.FatalModelRepositoryException)1 IModelRepository (org.eclipse.vorto.repository.core.IModelRepository)1