MetadataBeforeSave implements StorageItemBeforeSave { @Override public void beforeSave(StorageItem storageItem, StorageItemUploadPart part) { if (part != null) { Map<String, Object> metadata = new LinkedHashMap<>(); metadata.put("originalFilename", part.getName()); Map<String, List<String>> httpHeaders = new LinkedHashMap<>(); httpHeaders.put("Cache-Control", Collections.singletonList("public, max-age=31536000")); httpHeaders.put("Content-Length", Collections.singletonList(String.valueOf(part.getSize()))); httpHeaders.put("Content-Type", Collections.singletonList(part.getContentType())); metadata.put("http.headers", httpHeaders); storageItem.getMetadata().putAll(metadata); } }  @Override void beforeSave(StorageItem storageItem, StorageItemUploadPart part);  }
@Test public void verifyHttpHeaders() { long fileSize = 100; String fileContentType = "image/jpeg"; when(part.getSize()).thenReturn(fileSize); when(part.getContentType()).thenReturn(fileContentType); new MetadataBeforeSave().beforeSave(item, part); Map<String, Object> httpHeaders = (Map<String, Object>) item.getMetadata().get("http.headers"); assertEquals(((List<String>) httpHeaders.get("Cache-Control")).get(0), "public, max-age=31536000"); assertEquals(((List<String>) httpHeaders.get("Content-Length")).get(0), String.valueOf(fileSize)); assertEquals(((List<String>) httpHeaders.get("Content-Type")).get(0), fileContentType); }
@Test public void verifyOriginalFileName() { String originalFilename = "test"; when(part.getName()).thenReturn(originalFilename); new MetadataBeforeSave().beforeSave(item, part); assertEquals(item.getMetadata().get("originalFilename"), originalFilename); }