From 61d05800a03a44b7a6fa39a635faa7caeaa1fe6a Mon Sep 17 00:00:00 2001 From: chasegawa Date: Tue, 30 Aug 2022 09:52:46 -0700 Subject: [PATCH] SHIBUI-2270 Adding export file as a single property file option --- .../controller/ShibPropertiesController.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/ShibPropertiesController.java b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/ShibPropertiesController.java index f90392108..b613c2b4f 100644 --- a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/ShibPropertiesController.java +++ b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/ShibPropertiesController.java @@ -76,6 +76,16 @@ public ResponseEntity getPropertySetAsZip(@PathVariable Integer resourceId) t return ResponseEntity.ok().header("Content-Disposition", sb.toString()).body(prepDownloadAsZip(convertPropertiesToMaps(set.getProperties()))); } + @GetMapping(value="/property/set/{resourceId}/onefile", produces="application/zip") + @Transactional(readOnly = true) + @Operation(description = "Return the property set with the given resourceId as a zip file of a single properties files", + summary = "Return the property set with the given resourceId as a zip file of a single properties files", method = "GET") + public ResponseEntity getPropertySetOneFileAsZip(@PathVariable Integer resourceId) throws EntityNotFoundException, IOException { + ShibPropertySet set = service.getSet(resourceId); + StringBuilder sb = new StringBuilder("attachment; filename=\"").append(set.getName()).append(".zip\""); + return ResponseEntity.ok().header("Content-Disposition", sb.toString()).body(prepDownloadAsZipWithSingleFile(convertPropertiesToMaps(set.getProperties()))); + } + private Map> convertPropertiesToMaps(List properties) { HashMap> result = new HashMap<>(); for (ShibPropertySetting setting:properties){ @@ -90,6 +100,25 @@ private Map> convertPropertiesToMaps(List> propertiesFiles) throws IOException { + ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); + ZipOutputStream zipOutputStream = new ZipOutputStream(byteOutputStream); + zipOutputStream.putNextEntry(new ZipEntry("shibboleth.properties")); + + for (String filename : propertiesFiles.keySet()) { + Map properties = propertiesFiles.get(filename); + StringBuilder props = new StringBuilder(); + for (String key : properties.keySet()) { + props.append(key).append("=").append(properties.get(key)).append("\n"); + } + ByteArrayInputStream inputStream = new ByteArrayInputStream(props.toString().getBytes()); + IOUtils.copy(inputStream, zipOutputStream); + } + zipOutputStream.closeEntry(); + zipOutputStream.close(); + return byteOutputStream.toByteArray(); + } + private byte[] prepDownloadAsZip(Map> propertiesFiles) throws IOException { ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); ZipOutputStream zipOutputStream = new ZipOutputStream(byteOutputStream);