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);
}
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);
}
}
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);
}
}
Aggregations