Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.wilkins.showcase.controllers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import static org.springframework.http.ResponseEntity.ok;

@RestController
@RequestMapping("/documents")
public class DocumentController {

private static final Logger log = LoggerFactory.getLogger(DocumentController.class);

@GetMapping(produces = "application/zip")
public ResponseEntity<StreamingResponseBody> download() {
return ok()
.header(HttpHeaders.CONTENT_DISPOSITION,
ContentDisposition.builder("zipfile").filename("documents.zip").build().toString())
.body(DocumentController::asZipFile);
}

private static void asZipFile(OutputStream outputStream) throws IOException {
try (var zip = new ZipOutputStream(outputStream)) {
files().forEach(file -> addToZip(file, zip));
}
}

private static List<String> files() {
return List.of("src/main/resources/test-doc-one.pdf",
"src/main/resources/test-doc-two.pdf");
}

private static void addToZip(String file, ZipOutputStream zip) {
var zipEntry = new ZipEntry(name());
try {
zip.putNextEntry(zipEntry);
readFileAndSendTo(zip, file);
} catch (IOException e) {
log.error("Exception adding files to zip", e);
}
}

private static void readFileAndSendTo(OutputStream outputStream, String fileName) throws IOException {
try (var inputStream = new FileInputStream(fileName)) {
inputStream.transferTo(outputStream);
}
}

private static String name() {
return UUID.randomUUID() + ".pdf";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class GreetingController {

@GetMapping("/greeting")
public Greeting getGreeting(@RequestParam(name = "salutation", required = false, defaultValue = "hello") String salutationParam,
@RequestParam(name = "name", required = false, defaultValue = "world") String nameParam) {
@RequestParam(name = "name", required = false, defaultValue = "world") String nameParam) {

log.info("A greeting was requested");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.wilkins.showcase.controllers;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import static org.hamcrest.CoreMatchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(controllers = DocumentController.class)
public class DocumentControllerTest {

@Autowired
MockMvc mockMvc;

@Test
void returnsZipFile() throws Exception {
mockMvc.perform(get("/documents"))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.header().string("Content-Disposition", is("zipfile; filename=\"documents.zip\"")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest
@WebMvcTest(controllers = GreetingController.class)
public class GreetingControllerTest {

@Autowired
Expand All @@ -23,5 +23,4 @@ void returnsGreeting() throws Exception {
.andExpect(jsonPath("$.salutation", is("hello")))
.andExpect(jsonPath("$.name", is("world")));
}

}