Skip to content

Commit

Permalink
SHIBUI-2270
Browse files Browse the repository at this point in the history
Adding export file as a single property file option
  • Loading branch information
chasegawa committed Aug 30, 2022
1 parent f0211d0 commit 61d0580
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Map<String,String>> convertPropertiesToMaps(List<ShibPropertySetting> properties) {
HashMap<String, Map<String,String>> result = new HashMap<>();
for (ShibPropertySetting setting:properties){
Expand All @@ -90,6 +100,25 @@ private Map<String, Map<String,String>> convertPropertiesToMaps(List<ShibPropert
return result;
}

private byte[] prepDownloadAsZipWithSingleFile(Map<String, Map<String,String>> propertiesFiles) throws IOException {
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(byteOutputStream);
zipOutputStream.putNextEntry(new ZipEntry("shibboleth.properties"));

for (String filename : propertiesFiles.keySet()) {
Map<String, String> 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<String, Map<String,String>> propertiesFiles) throws IOException {
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(byteOutputStream);
Expand Down

0 comments on commit 61d0580

Please sign in to comment.