|
| 1 | +package nl.hackyourfuture.dojoserver.certificate; |
| 2 | + |
| 3 | +import io.swagger.v3.oas.annotations.Hidden; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
| 5 | +import nl.hackyourfuture.dojoserver.filestorage.FileStorageService; |
| 6 | +import nl.hackyourfuture.dojoserver.shared.exception.DojoNotFoundException; |
| 7 | +import org.springframework.core.io.ClassPathResource; |
| 8 | +import org.springframework.core.io.Resource; |
| 9 | +import org.springframework.http.HttpStatus; |
| 10 | +import org.springframework.http.MediaType; |
| 11 | +import org.springframework.http.MediaTypeFactory; |
| 12 | +import org.springframework.http.ResponseEntity; |
| 13 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 14 | +import org.springframework.web.bind.annotation.GetMapping; |
| 15 | +import org.springframework.web.bind.annotation.PathVariable; |
| 16 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 17 | +import org.springframework.web.bind.annotation.RestController; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | + |
| 23 | +/** The public site where anyone can check a certificate. nginx serves it on its own domain, without the prefix. */ |
| 24 | +@Hidden // A web site, not part of the API |
| 25 | +@RestController |
| 26 | +@RequestMapping("/api/public/certificates") |
| 27 | +@Slf4j |
| 28 | +public class CertificateController { |
| 29 | + // Letters, digits, - and _ only, so an id can't reach outside certificates/ in the bucket |
| 30 | + private static final String ID = "/{id:[a-zA-Z0-9_-]{1,10}}"; |
| 31 | + |
| 32 | + private final FileStorageService fileStorageService; |
| 33 | + private final String validPage; |
| 34 | + private final String notFoundPage; |
| 35 | + |
| 36 | + public CertificateController(FileStorageService fileStorageService) throws IOException { |
| 37 | + this.fileStorageService = fileStorageService; |
| 38 | + this.validPage = new ClassPathResource("certificates/pages/index.html") |
| 39 | + .getContentAsString(StandardCharsets.UTF_8); |
| 40 | + this.notFoundPage = |
| 41 | + new ClassPathResource("certificates/pages/404.html").getContentAsString(StandardCharsets.UTF_8); |
| 42 | + } |
| 43 | + |
| 44 | + @GetMapping(ID) |
| 45 | + public ResponseEntity<String> page( |
| 46 | + @PathVariable |
| 47 | + String id |
| 48 | + ) { |
| 49 | + if (!fileStorageService.exists("certificates/" + id)) { |
| 50 | + log.info("Certificate {} not found", id); |
| 51 | + return notFound(); |
| 52 | + } |
| 53 | + return html(HttpStatus.OK, validPage.replace("XXXXXXXX", id)); |
| 54 | + } |
| 55 | + |
| 56 | + @GetMapping(path = ID, params = "raw") |
| 57 | + public ResponseEntity<byte[]> pdf(@PathVariable |
| 58 | + String id) throws IOException { |
| 59 | + // Read it whole first, so a slow visitor never holds a bucket connection |
| 60 | + try (InputStream content = fileStorageService.download("certificates/" + id).content()) { |
| 61 | + return ResponseEntity.ok().contentType(MediaType.APPLICATION_PDF).body(content.readAllBytes()); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // The stylesheets, logo, icon and robots.txt. The page templates are in pages/, so they are never sent as they are |
| 66 | + @GetMapping("/{file:[a-zA-Z0-9_-]+\\.[a-z]+}") |
| 67 | + public ResponseEntity<Resource> file(@PathVariable |
| 68 | + String file) { |
| 69 | + var resource = new ClassPathResource("certificates/static/" + file); |
| 70 | + if (!resource.exists()) { |
| 71 | + throw new DojoNotFoundException("File not found"); |
| 72 | + } |
| 73 | + // Set from the file name, or content negotiation would pick JSON |
| 74 | + return ResponseEntity.ok() |
| 75 | + .contentType(MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM)) |
| 76 | + .body(resource); |
| 77 | + } |
| 78 | + |
| 79 | + // Anything else, like / or /a/b |
| 80 | + @GetMapping("/**") |
| 81 | + public ResponseEntity<String> notFound() { |
| 82 | + return html(HttpStatus.NOT_FOUND, notFoundPage); |
| 83 | + } |
| 84 | + |
| 85 | + // A missing PDF or file gets the not-found page too, not the API's JSON error |
| 86 | + @ExceptionHandler(DojoNotFoundException.class) |
| 87 | + public ResponseEntity<String> handleNotFound() { |
| 88 | + return notFound(); |
| 89 | + } |
| 90 | + |
| 91 | + private static ResponseEntity<String> html(HttpStatus status, String page) { |
| 92 | + return ResponseEntity.status(status).contentType(MediaType.TEXT_HTML).body(page); |
| 93 | + } |
| 94 | +} |
0 commit comments