Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
3DSystemNavigationBackend/src/main/java/com/lmco/spectrum/systemnavigation3d/controller/ResourceController.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
101 lines (83 sloc)
3.62 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 com.lmco.spectrum.systemnavigation3d.service.SmgService; | |
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.GetMapping; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import java.io.IOException; | |
import java.io.InputStream; | |
@Controller | |
@RequestMapping("api/resources") | |
public class ResourceController { | |
private final HttpHeaders PDF_HEADERS; | |
@Autowired | |
ResourceService resourceService; | |
@Autowired | |
SmgService smgService; | |
public ResourceController() { | |
this.PDF_HEADERS = new HttpHeaders(); | |
PDF_HEADERS.setContentType(MediaType.APPLICATION_PDF); | |
} | |
private HttpHeaders getAutoDownloadHeaders(String key) { | |
HttpHeaders headers = new HttpHeaders(); | |
headers.setContentDispositionFormData(key, key); | |
return headers; | |
} | |
private ResponseEntity<?> respondToException(Exception e) { | |
if (e instanceof AmazonS3Exception) { | |
AmazonS3Exception ex = (AmazonS3Exception)e; | |
if (ex.getErrorCode().equals("NoSuchKey")) { | |
return new ResponseEntity<>(ex.getErrorMessage(), HttpStatus.NOT_FOUND); | |
} else { | |
ex.printStackTrace(); | |
return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); | |
} | |
} else if(e instanceof IOException) { | |
e.printStackTrace(); | |
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); | |
} | |
// Return 500 as default. | |
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); | |
} | |
private ResponseEntity<?> fetchResponseFromS3(String key, @Nullable HttpHeaders headers) { | |
try (InputStream is = resourceService.getFileAsStream(key)) { | |
return new ResponseEntity<>(IOUtils.toByteArray(is), headers, HttpStatus.OK); | |
} catch (Exception e) { | |
return respondToException(e); | |
} | |
} | |
@GetMapping("/pdf/{key}") | |
public ResponseEntity<?> getResourcePDF(@PathVariable("key") String key){ | |
return fetchResponseFromS3(key, this.PDF_HEADERS); | |
} | |
@GetMapping("/{key}") | |
public ResponseEntity<?> getResource(@PathVariable("key") String key){ | |
return fetchResponseFromS3(key, null); | |
} | |
// TODO what input data? | |
@GetMapping("/smg/{key}") | |
public ResponseEntity<?> getResourceSMG(@PathVariable("key") String key) { | |
try(InputStream is = resourceService.getFileAsStream(String.format("smg/%s", key))) { | |
byte[] data = IOUtils.toByteArray(is); | |
byte[] processed = smgService.processSmg(data); | |
if(processed == null) { | |
// TODO failed to process, error? | |
// TODO maybe return error list | |
return new ResponseEntity<>("Failed to process SMG file.", HttpStatus.INTERNAL_SERVER_ERROR); | |
} else { | |
return new ResponseEntity<>(processed, getAutoDownloadHeaders(key), HttpStatus.OK); | |
} | |
} catch(Exception e) { | |
return respondToException(e); | |
} | |
} | |
} |