Search in sources :

Example 1 with GenericApplicationException

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

the class BackupRestoreService method restoreRepository.

@Override
public Collection<Namespace> restoreRepository(byte[] backupFile, Predicate<Namespace> namespaceFilter) {
    Preconditions.checkNotNull(backupFile, "Backup file must not be null");
    try {
        Collection<Namespace> namespacesRestored = Lists.newArrayList();
        Map<String, byte[]> backups = getBackups(backupFile);
        backups.forEach((namespaceName, backup) -> {
            LOGGER.info(String.format("Restoring backup for [%s]", namespaceName));
            Namespace namespace = namespaceRepository.findByName(namespaceName);
            if (null != namespace && namespaceFilter.test(namespace)) {
                try {
                    String workspaceId = namespace.getWorkspaceId();
                    IRepositoryManager repoMgr = modelRepositoryFactory.getRepositoryManager(workspaceId, authSupplier.get());
                    if (!repoMgr.exists(workspaceId)) {
                        repoMgr.createWorkspace(workspaceId);
                    } else {
                        repoMgr.removeWorkspace(workspaceId);
                        repoMgr.createWorkspace(workspaceId);
                    }
                    repoMgr.restore(backup);
                    this.modelRepositoryFactory.getPolicyManager(workspaceId, SecurityContextHolder.getContext().getAuthentication()).restorePolicyEntries();
                    namespacesRestored.add(namespace);
                } catch (Exception e) {
                    LOGGER.error(String.format("Error while restoring [%s]", namespaceName), e);
                }
            } else {
                LOGGER.info(String.format("Skipping restoration of [%s] either because the namespace could not be found, or was filtered out.", namespaceName));
            }
        });
        if (!namespacesRestored.isEmpty()) {
            indexingService.reindexAllModels();
        }
        return namespacesRestored;
    } catch (IOException e) {
        throw new GenericApplicationException("Problem while reading zip file during restore", e);
    }
}
Also used : IRepositoryManager(org.eclipse.vorto.repository.core.IRepositoryManager) IOException(java.io.IOException) GenericApplicationException(org.eclipse.vorto.repository.web.GenericApplicationException) Namespace(org.eclipse.vorto.repository.domain.Namespace) GenericApplicationException(org.eclipse.vorto.repository.web.GenericApplicationException) IOException(java.io.IOException)

Example 2 with GenericApplicationException

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

the class BackupRestoreService method createZippedInputStream.

private byte[] createZippedInputStream(Map<String, byte[]> backups) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);
    try {
        for (Map.Entry<String, byte[]> entry : backups.entrySet()) {
            ZipEntry zipEntry = new ZipEntry(entry.getKey() + ".xml");
            zos.putNextEntry(zipEntry);
            zos.write(entry.getValue());
            zos.closeEntry();
        }
        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) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericApplicationException(org.eclipse.vorto.repository.web.GenericApplicationException) HashMap(java.util.HashMap) Map(java.util.Map) GenericApplicationException(org.eclipse.vorto.repository.web.GenericApplicationException) IOException(java.io.IOException)

Example 3 with GenericApplicationException

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

the class ModelRepositoryController method sendAsZipFile.

private void sendAsZipFile(final HttpServletResponse response, final String fileName, Map<ModelInfo, FileContent> models) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);
    try {
        for (Map.Entry<ModelInfo, FileContent> model : models.entrySet()) {
            ModelInfo modelResource = model.getKey();
            ZipEntry zipEntry = new ZipEntry(modelResource.getId().getPrettyFormat() + modelResource.getType().getExtension());
            zos.putNextEntry(zipEntry);
            zos.write(model.getValue().getContent());
            zos.closeEntry();
        }
        zos.close();
        baos.close();
    } catch (Exception ex) {
        throw new GenericApplicationException("error in creating zip file.", ex);
    }
    response.setHeader(CONTENT_DISPOSITION, ATTACHMENT_FILENAME + fileName);
    response.setContentType(APPLICATION_OCTET_STREAM);
    try {
        IOUtils.copy(new ByteArrayInputStream(baos.toByteArray()), response.getOutputStream());
        response.flushBuffer();
    } catch (IOException e) {
        throw new GenericApplicationException("Error copying file.", e);
    }
}
Also used : FileContent(org.eclipse.vorto.repository.core.FileContent) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) GenericApplicationException(org.eclipse.vorto.repository.web.GenericApplicationException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ModelAlreadyExistsException(org.eclipse.vorto.repository.core.ModelAlreadyExistsException) NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) ModelNotReleasedException(org.eclipse.vorto.repository.model.ModelNotReleasedException) GenericApplicationException(org.eclipse.vorto.repository.web.GenericApplicationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DoesNotExistException(org.eclipse.vorto.repository.services.exceptions.DoesNotExistException) OperationForbiddenException(org.eclipse.vorto.repository.services.exceptions.OperationForbiddenException) WorkflowException(org.eclipse.vorto.repository.workflow.WorkflowException) ModelNamespaceNotOfficialException(org.eclipse.vorto.repository.model.ModelNamespaceNotOfficialException) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException)

Example 4 with GenericApplicationException

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

the class BackupController method backupRepository.

private void backupRepository(final HttpServletResponse response, Predicate<Namespace> namespaceFilter, Optional<String> ext) {
    response.setHeader(CONTENT_DISPOSITION, ATTACHMENT_FILENAME + String.format("backup-%s%s.zip", SIMPLEDATEFORMAT.format(new Date()), ext.orElse("")));
    response.setContentType(APPLICATION_OCTET_STREAM);
    try {
        IOUtils.copy(new ByteArrayInputStream(backupRestoreService.createBackup(namespaceFilter)), response.getOutputStream());
        response.flushBuffer();
    } catch (IOException e) {
        throw new GenericApplicationException("Error copying file.", e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) GenericApplicationException(org.eclipse.vorto.repository.web.GenericApplicationException) Date(java.util.Date)

Example 5 with GenericApplicationException

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

the class ModelRepositoryController method getModelImage.

@ApiOperation(value = "Returns the image of a vorto model")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Wrong input"), @ApiResponse(code = 404, message = "Model not found") })
@GetMapping("/{modelId:.+}/images")
public void getModelImage(@ApiParam(value = "The modelId of vorto model, e.g. com.mycompany.Car:1.0.0", required = true) @PathVariable final String modelId, @ApiParam(value = "Response", required = true) final HttpServletResponse response) {
    Objects.requireNonNull(modelId, "modelId must not be null");
    final ModelId modelID = ModelId.fromPrettyFormat(modelId);
    IModelRepository modelRepo = getModelRepository(modelID);
    // first searches by "display image" tag
    List<Attachment> imageAttachments = modelRepo.getAttachmentsByTag(modelID, Attachment.TAG_DISPLAY_IMAGE);
    // if none present, searches just by "image" tag (for backwards compatibility)
    if (imageAttachments.isEmpty()) {
        imageAttachments = modelRepo.getAttachmentsByTag(modelID, Attachment.TAG_IMAGE);
    }
    // still nope
    if (imageAttachments.isEmpty()) {
        response.setStatus(404);
        return;
    }
    // fetches the first element: either it's the only one (if the display image tag is present)
    // or arbitrarily the first image found (for backwards compatibility)
    Optional<FileContent> imageContent = modelRepo.getAttachmentContent(modelID, imageAttachments.get(0).getFilename());
    if (!imageContent.isPresent()) {
        response.setStatus(404);
        return;
    }
    try {
        response.setHeader(CONTENT_DISPOSITION, ATTACHMENT_FILENAME + modelID.getName() + ".png");
        response.setContentType(APPLICATION_OCTET_STREAM);
        IOUtils.copy(new ByteArrayInputStream(imageContent.get().getContent()), response.getOutputStream());
        response.flushBuffer();
    } catch (IOException e) {
        throw new GenericApplicationException("Error copying file.", e);
    }
}
Also used : IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) FileContent(org.eclipse.vorto.repository.core.FileContent) ByteArrayInputStream(java.io.ByteArrayInputStream) Attachment(org.eclipse.vorto.repository.core.Attachment) IOException(java.io.IOException) GenericApplicationException(org.eclipse.vorto.repository.web.GenericApplicationException) ModelId(org.eclipse.vorto.model.ModelId) GetMapping(org.springframework.web.bind.annotation.GetMapping) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

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