From d5cbc5ed53c772bbd51ceb98b1b2cddb1737e230 Mon Sep 17 00:00:00 2001 From: Daniel Scalzi Date: Mon, 10 Feb 2020 16:06:40 -0500 Subject: [PATCH] Add pdf + general file endpoint. --- README.md | 5 +++ .../controller/ResourceController.java | 41 +++++++++++++++---- .../service/ResourceService.java | 11 +++-- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9708184..8e4a0a6 100644 --- a/README.md +++ b/README.md @@ -23,3 +23,8 @@ The application can be run and debugged through Intellij. To run standalone: The following test endpoint should be live after running. http://localhost:8080/3dsysnavbackend/api/test + +#### Test Resources + +* http://localhost:8080/3dsysnavbackend/api/resources/pdf/radar.pdf +* http://localhost:8080/3dsysnavbackend/api/resources/test.txt diff --git a/src/main/java/com/lmco/spectrum/systemnavigation3d/controller/ResourceController.java b/src/main/java/com/lmco/spectrum/systemnavigation3d/controller/ResourceController.java index bceb072..3ab619b 100644 --- a/src/main/java/com/lmco/spectrum/systemnavigation3d/controller/ResourceController.java +++ b/src/main/java/com/lmco/spectrum/systemnavigation3d/controller/ResourceController.java @@ -1,14 +1,22 @@ package com.lmco.spectrum.systemnavigation3d.controller; +import com.amazonaws.services.s3.model.AmazonS3Exception; +import com.amazonaws.util.IOUtils; import com.lmco.spectrum.systemnavigation3d.service.ResourceService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.lang.Nullable; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import java.io.IOException; +import java.io.InputStream; + @Controller @RequestMapping("api/resources") public class ResourceController { @@ -16,14 +24,33 @@ public class ResourceController { @Autowired ResourceService resourceService; - @RequestMapping(value = "/pdf/{id}", method = RequestMethod.GET) - public ResponseEntity testEndpoint(@PathVariable("id") Long id){ - // TODO call ResourceService - String x = resourceService.getFileTest(); - if(x == null) { - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + private ResponseEntity handleS3Input(String key, @Nullable HttpHeaders headers) { + try (InputStream is = resourceService.getFileAsStream(key)) { + return new ResponseEntity<>(IOUtils.toByteArray(is), headers, HttpStatus.OK); + } catch (AmazonS3Exception e) { + if(e.getErrorCode().equals("NoSuchKey")) { + return new ResponseEntity<>(e.getErrorMessage(), HttpStatus.NOT_FOUND); + } else { + e.printStackTrace(); + return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } + } catch (IOException e) { + e.printStackTrace(); + return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } - return ResponseEntity.ok(x); + } + + @RequestMapping(value = "/pdf/{key}", method = RequestMethod.GET) + public ResponseEntity getResourcePDF(@PathVariable("key") String key){ + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_PDF); + // headers.setContentDispositionFormData(key, key); // Auto download + return handleS3Input(key, headers); + } + + @RequestMapping(value = "/{key}", method = RequestMethod.GET) + public ResponseEntity getResource(@PathVariable("key") String key){ + return handleS3Input(key, null); } } diff --git a/src/main/java/com/lmco/spectrum/systemnavigation3d/service/ResourceService.java b/src/main/java/com/lmco/spectrum/systemnavigation3d/service/ResourceService.java index 602528d..f70bf9a 100644 --- a/src/main/java/com/lmco/spectrum/systemnavigation3d/service/ResourceService.java +++ b/src/main/java/com/lmco/spectrum/systemnavigation3d/service/ResourceService.java @@ -1,6 +1,7 @@ package com.lmco.spectrum.systemnavigation3d.service; import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.model.AmazonS3Exception; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.lang.Nullable; import org.springframework.stereotype.Service; @@ -16,10 +17,14 @@ public class ResourceService { @Autowired AmazonClient amazonClient; - @Nullable - public String getFileTest() { + public InputStream getFileAsStream(String key) throws AmazonS3Exception { AmazonS3 amazonS3 = amazonClient.getAmazonS3Connection(); - try (InputStream is = amazonS3.getObject(amazonClient.getS3Bucket(), "test.txt").getObjectContent()) { + return amazonS3.getObject(amazonClient.getS3Bucket(), key).getObjectContent(); + } + + @Nullable + public String getFileAsString(String key) { + try (InputStream is = getFileAsStream(key)) { return StreamUtils.copyToString(is, StandardCharsets.UTF_8); } catch (IOException e) { e.printStackTrace();