Skip to content

Commit

Permalink
Add pdf + general file endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
dds14002 committed Feb 10, 2020
1 parent a46f8ed commit d5cbc5e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,29 +1,56 @@
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 {

@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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand Down

0 comments on commit d5cbc5e

Please sign in to comment.