Skip to content
Snippets Groups Projects
Commit e52b243a authored by OZGCloud's avatar OZGCloud
Browse files

OZG-6223 OZG-7381 Set content-length header

parent d501a5bb
No related branches found
No related tags found
No related merge requests found
...@@ -23,7 +23,9 @@ ...@@ -23,7 +23,9 @@
*/ */
package de.ozgcloud.alfa.attachment; package de.ozgcloud.alfa.attachment;
import java.io.OutputStream; import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.springframework.hateoas.CollectionModel; import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel; import org.springframework.hateoas.EntityModel;
...@@ -62,18 +64,28 @@ public class AttachmentByVorgangController { ...@@ -62,18 +64,28 @@ public class AttachmentByVorgangController {
@GetMapping(produces = APPLICATION_ZIP_VALUE) @GetMapping(produces = APPLICATION_ZIP_VALUE)
public ResponseEntity<StreamingResponseBody> download(@PathVariable String vorgangId) { public ResponseEntity<StreamingResponseBody> download(@PathVariable String vorgangId) {
var zipFile = createZipFile(vorgangId);
return ResponseEntity.ok() return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=%s", buildZipName(vorgangId))) .header(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=%s", buildZipName(vorgangId)))
.contentType(MediaType.valueOf(APPLICATION_ZIP_VALUE)) .contentType(MediaType.valueOf(APPLICATION_ZIP_VALUE))
.body(out -> createZipFile(out, vorgangId)); .contentLength(getFileSize(zipFile))
.body(out -> zipDownloadService.writeZipFileContent(zipFile, out));
} }
String buildZipName(String vorgangId) { String buildZipName(String vorgangId) {
return vorgangController.getVorgang(vorgangId).getNummer() + "_Anhaenge.zip"; return vorgangController.getVorgang(vorgangId).getNummer() + "_Anhaenge.zip";
} }
void createZipFile(OutputStream out, String vorgangId) { File createZipFile(String vorgangId) {
var ozgFiles = fileService.getAttachments(vorgangId); var ozgFiles = fileService.getAttachments(vorgangId);
zipDownloadService.write(ozgFiles.toList(), out); return zipDownloadService.createZipFile(ozgFiles.toList());
}
long getFileSize(File file) {
try {
return Files.size(file.toPath());
} catch (IOException e) {
throw new RuntimeException(e);
}
} }
} }
...@@ -50,7 +50,7 @@ public class ZipDownloadService { ...@@ -50,7 +50,7 @@ public class ZipDownloadService {
writeZipFileContent(createZipFile(ozgFiles), out); writeZipFileContent(createZipFile(ozgFiles), out);
} }
File createZipFile(List<OzgFile> ozgFiles) { public File createZipFile(List<OzgFile> ozgFiles) {
var file = TempFileUtils.createTmpFile().toFile(); var file = TempFileUtils.createTmpFile().toFile();
try (var zipOutputStream = new ZipOutputStream(new FileOutputStream(file))) { try (var zipOutputStream = new ZipOutputStream(new FileOutputStream(file))) {
ozgFiles.forEach(ozgFile -> addOzgFileToZip(ozgFile, zipOutputStream)); ozgFiles.forEach(ozgFile -> addOzgFileToZip(ozgFile, zipOutputStream));
...@@ -70,7 +70,7 @@ public class ZipDownloadService { ...@@ -70,7 +70,7 @@ public class ZipDownloadService {
} }
} }
void writeZipFileContent(File zipFile, OutputStream outputStream) { public void writeZipFileContent(File zipFile, OutputStream outputStream) {
try (var fileInputStream = new FileInputStream(zipFile)) { try (var fileInputStream = new FileInputStream(zipFile)) {
fileInputStream.transferTo(outputStream); fileInputStream.transferTo(outputStream);
} catch (Exception e) { } catch (Exception e) {
......
...@@ -29,8 +29,10 @@ import static org.mockito.Mockito.*; ...@@ -29,8 +29,10 @@ import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import java.io.File;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.List; import java.util.List;
import java.util.Random;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Stream; import java.util.stream.Stream;
...@@ -117,11 +119,15 @@ class AttachmentByVorgangControllerTest { ...@@ -117,11 +119,15 @@ class AttachmentByVorgangControllerTest {
@DisplayName("Download") @DisplayName("Download")
@Nested @Nested
class TestDownload { class TestDownload {
private final String filename = LoremIpsum.getInstance().getWords(1); private final String zipFileName = LoremIpsum.getInstance().getWords(1);
private final long zipFileSize = Math.abs(new Random().nextInt());
private final File zipFile = new File(UUID.randomUUID().toString() + ".zip");
@BeforeEach @BeforeEach
void setUpMock() { void setUpMock() {
doReturn(filename).when(controller).buildZipName(VorgangHeaderTestFactory.ID); doReturn(zipFileName).when(controller).buildZipName(VorgangHeaderTestFactory.ID);
doReturn(zipFileSize).when(controller).getFileSize(zipFile);
doReturn(zipFile).when(controller).createZipFile(VorgangHeaderTestFactory.ID);
} }
@SneakyThrows @SneakyThrows
...@@ -145,7 +151,7 @@ class AttachmentByVorgangControllerTest { ...@@ -145,7 +151,7 @@ class AttachmentByVorgangControllerTest {
void shouldHaveContentDispositonHeader() { void shouldHaveContentDispositonHeader() {
var response = doRequest(); var response = doRequest();
response.andExpect(header().string(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename)); response.andExpect(header().string(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + zipFileName));
} }
@SneakyThrows @SneakyThrows
...@@ -156,12 +162,28 @@ class AttachmentByVorgangControllerTest { ...@@ -156,12 +162,28 @@ class AttachmentByVorgangControllerTest {
response.andExpect(header().string(HttpHeaders.CONTENT_TYPE, AttachmentByVorgangController.APPLICATION_ZIP_VALUE)); response.andExpect(header().string(HttpHeaders.CONTENT_TYPE, AttachmentByVorgangController.APPLICATION_ZIP_VALUE));
} }
@SneakyThrows
@Test
void shouldHaveContentLength() {
var response = doRequest();
response.andExpect(header().longValue(HttpHeaders.CONTENT_LENGTH, zipFileSize));
}
@SneakyThrows
@Test
void shouldCreateZipFile() {
doRequest();
verify(controller).createZipFile(VorgangHeaderTestFactory.ID);
}
@SneakyThrows @SneakyThrows
@Test @Test
void shouldWriteZipFile() { void shouldWriteZipFile() {
doRequest(); doRequest();
verify(controller).createZipFile(any(), eq(VorgangHeaderTestFactory.ID)); verify(zipDownloadService).writeZipFileContent(eq(zipFile), any(OutputStream.class));
} }
@SneakyThrows @SneakyThrows
...@@ -206,8 +228,6 @@ class AttachmentByVorgangControllerTest { ...@@ -206,8 +228,6 @@ class AttachmentByVorgangControllerTest {
@Nested @Nested
class TestCreateZipFile { class TestCreateZipFile {
@Mock
private OutputStream outputStream;
@Test @Test
void shouldGetAttachments() { void shouldGetAttachments() {
...@@ -223,11 +243,11 @@ class AttachmentByVorgangControllerTest { ...@@ -223,11 +243,11 @@ class AttachmentByVorgangControllerTest {
callController(); callController();
verify(zipDownloadService).write(List.of(ozgFile), outputStream); verify(zipDownloadService).createZipFile(List.of(ozgFile));
} }
private void callController() { private void callController() {
controller.createZipFile(outputStream, VorgangHeaderTestFactory.ID); controller.createZipFile(VorgangHeaderTestFactory.ID);
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment