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