Search in sources :

Example 1 with IdmExportImportService

use of eu.bcvsolutions.idm.core.api.service.IdmExportImportService in project CzechIdMng by bcvsolutions.

the class IdmExportImportController method download.

@ResponseBody
@RequestMapping(value = "/{backendId}/download", method = RequestMethod.GET)
@PreAuthorize("hasAuthority('" + CoreGroupPermission.EXPORTIMPORT_READ + "')")
@ApiOperation(value = "Download export", nickname = "downloadExport", tags = { IdmExportImportController.TAG }, authorizations = { @Authorization(value = SwaggerConfig.AUTHENTICATION_BASIC, scopes = { @AuthorizationScope(scope = CoreGroupPermission.EXPORTIMPORT_READ, description = "") }), @Authorization(value = SwaggerConfig.AUTHENTICATION_CIDMST, scopes = { @AuthorizationScope(scope = CoreGroupPermission.EXPORTIMPORT_READ, description = "") }) })
public ResponseEntity<InputStreamResource> download(@ApiParam(value = "Batch's uuid identifier.", required = true) @PathVariable @NotNull String backendId) {
    // 
    IdmExportImportDto batch = getDto(backendId);
    if (batch == null) {
        throw new ResultCodeException(CoreResultCode.NOT_FOUND, ImmutableMap.of("entity", backendId));
    }
    try {
        // Batch read rights check was performed above (getDto).
        InputStream is = ((IdmExportImportService) getService()).download(batch);
        // 
        // Generate name of ZIP from batch name.
        String zipName = batch.getExecutorName();
        if (Strings.isNotEmpty(batch.getName())) {
            String spinaledName = SpinalCase.format(batch.getName());
            if (spinaledName.length() > 30) {
                spinaledName = spinaledName.substring(0, 29);
            }
            zipName = MessageFormat.format("{0}.{1}", spinaledName, ExportManager.EXTENSION_ZIP);
        }
        return ResponseEntity.ok().contentLength(is.available()).contentType(MediaType.APPLICATION_OCTET_STREAM).header(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=\"%s\"", zipName)).body(new InputStreamResource(is));
    } catch (Exception ex) {
        throw new ResultCodeException(CoreResultCode.INTERNAL_SERVER_ERROR, ex);
    }
}
Also used : IdmExportImportDto(eu.bcvsolutions.idm.core.api.dto.IdmExportImportDto) InputStream(java.io.InputStream) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IdmExportImportService(eu.bcvsolutions.idm.core.api.service.IdmExportImportService) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IOException(java.io.IOException) InputStreamResource(org.springframework.core.io.InputStreamResource) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

IdmExportImportDto (eu.bcvsolutions.idm.core.api.dto.IdmExportImportDto)1 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)1 IdmExportImportService (eu.bcvsolutions.idm.core.api.service.IdmExportImportService)1 ApiOperation (io.swagger.annotations.ApiOperation)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamResource (org.springframework.core.io.InputStreamResource)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1