|
17 | 17 |
|
18 | 18 | import edu.kit.datamanager.mappingservice.dao.IMappingRecordDao;
|
19 | 19 | import edu.kit.datamanager.mappingservice.domain.MappingRecord;
|
| 20 | +import edu.kit.datamanager.mappingservice.exception.MappingException; |
| 21 | +import edu.kit.datamanager.mappingservice.exception.MappingExecutionException; |
| 22 | +import edu.kit.datamanager.mappingservice.exception.MappingNotFoundException; |
20 | 23 | import edu.kit.datamanager.mappingservice.impl.MappingService;
|
21 | 24 | import edu.kit.datamanager.mappingservice.plugins.MappingPluginException;
|
22 | 25 | import edu.kit.datamanager.mappingservice.plugins.MappingPluginState;
|
|
38 | 41 | import javax.servlet.http.HttpServletResponse;
|
39 | 42 | import java.io.File;
|
40 | 43 | import java.io.IOException;
|
| 44 | +import java.net.URI; |
41 | 45 | import java.nio.file.Files;
|
42 | 46 | import java.nio.file.Path;
|
| 47 | +import java.nio.file.Paths; |
| 48 | +import java.nio.file.StandardCopyOption; |
43 | 49 | import java.util.Optional;
|
44 | 50 |
|
45 | 51 | /**
|
|
49 | 55 | */
|
50 | 56 | @Controller
|
51 | 57 | @RequestMapping(value = "/api/v1/mappingExecution")
|
52 |
| -public class MappingExecutionController implements IMappingExecutionController { |
| 58 | +public class MappingExecutionController implements IMappingExecutionController{ |
53 | 59 |
|
54 |
| - private static final Logger LOG = LoggerFactory.getLogger(MappingExecutionController.class); |
| 60 | + private static final Logger LOG = LoggerFactory.getLogger(MappingExecutionController.class); |
55 | 61 |
|
56 |
| - private final MappingService mappingService; |
| 62 | + private final MappingService mappingService; |
57 | 63 |
|
58 |
| - private final IMappingRecordDao mappingRecordDao; |
| 64 | + private final IMappingRecordDao mappingRecordDao; |
59 | 65 |
|
60 |
| - public MappingExecutionController(MappingService mappingService, IMappingRecordDao mappingRecordDao) { |
61 |
| - this.mappingService = mappingService; |
62 |
| - this.mappingRecordDao = mappingRecordDao; |
63 |
| - } |
| 66 | + public MappingExecutionController(MappingService mappingService, IMappingRecordDao mappingRecordDao){ |
| 67 | + this.mappingService = mappingService; |
| 68 | + this.mappingRecordDao = mappingRecordDao; |
| 69 | + } |
64 | 70 |
|
65 |
| - @Override |
66 |
| - public ResponseEntity mapDocument(MultipartFile document, String mappingID, HttpServletRequest request, HttpServletResponse response, UriComponentsBuilder uriBuilder) { |
67 |
| - LOG.trace("Performing mapDocument(File#{}, {})", document.getOriginalFilename(), mappingID); |
68 |
| - |
69 |
| - Optional<Path> resultPath; |
70 |
| - if (!document.isEmpty() && !mappingID.isBlank()) { |
71 |
| - LOG.trace("Obtaining mapping for id {}.", mappingID); |
72 |
| - Optional<MappingRecord> record = mappingRecordDao.findByMappingId(mappingID); |
73 |
| - if (record.isEmpty()) { |
74 |
| - String message = String.format("No mapping found for mapping id %s.", mappingID); |
75 |
| - LOG.error(message + " Returning HTTP 404."); |
76 |
| - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(message); |
77 |
| - } |
78 |
| - |
79 |
| - LOG.trace("Receiving mapping input file."); |
80 |
| - String extension = "." + FilenameUtils.getExtension(document.getOriginalFilename()); |
81 |
| - LOG.trace("Found file extension: {}", extension); |
82 |
| - Path inputPath = FileUtil.createTempFile("inputMultipart", extension); |
83 |
| - LOG.trace("Writing user upload to: {}", inputPath); |
84 |
| - File inputFile = inputPath.toFile(); |
85 |
| - try { |
86 |
| - document.transferTo(inputFile); |
87 |
| - LOG.trace("Successfully received user upload."); |
88 |
| - } catch (IOException e) { |
89 |
| - LOG.error("Failed to receive upload from user.", e); |
90 |
| - return ResponseEntity.internalServerError().body("Unable to write user upload to disk."); |
91 |
| - } |
92 |
| - |
93 |
| - try { |
94 |
| - LOG.trace("Performing mapping process of file {} via mapping service", inputPath.toString()); |
95 |
| - |
96 |
| - resultPath = mappingService.executeMapping(inputFile.toURI(), mappingID); |
97 |
| - if (resultPath.isPresent()) { |
98 |
| - LOG.trace("Mapping process finished. Output written to {}.", resultPath.toString()); |
99 |
| - } else { |
100 |
| - throw new MappingPluginException(MappingPluginState.UNKNOWN_ERROR, "Mapping process finished, but no result was returned."); |
101 |
| - } |
102 |
| - } catch (MappingPluginException e) { |
103 |
| - LOG.error("Failed to execute mapping.", e); |
104 |
| - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to execute mapping with id " + mappingID + " on provided input document."); |
105 |
| - } |
106 |
| - LOG.trace("Removing user upload at {}.", inputFile); |
107 |
| - FileUtil.removeFile(inputPath); |
108 |
| - LOG.trace("User upload successfully removed."); |
109 |
| - } else { |
110 |
| - String message = "Either mapping id or input document are missing. Unable to perform mapping."; |
111 |
| - LOG.error(message); |
112 |
| - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(message); |
113 |
| - } |
| 71 | + @Override |
| 72 | + public void mapDocument(MultipartFile document, String mappingID, HttpServletRequest request, HttpServletResponse response, UriComponentsBuilder uriBuilder){ |
| 73 | + LOG.trace("Performing mapDocument(File#{}, {})", document.getOriginalFilename(), mappingID); |
| 74 | + |
| 75 | + Optional<Path> resultPath; |
| 76 | + if(!document.isEmpty() && !mappingID.isBlank()){ |
| 77 | + LOG.trace("Obtaining mapping for id {}.", mappingID); |
| 78 | + Optional<MappingRecord> record = mappingRecordDao.findByMappingId(mappingID); |
| 79 | + if(record.isEmpty()){ |
| 80 | + String message = String.format("No mapping found for mapping id %s.", mappingID); |
| 81 | + LOG.error(message + " Returning HTTP 404."); |
| 82 | + throw new MappingNotFoundException(message); |
| 83 | + //return ResponseEntity.status(HttpStatus.NOT_FOUND).body(message); |
| 84 | + } |
114 | 85 |
|
115 |
| - Path result = resultPath.get(); |
116 |
| - if (!Files.exists(result) || !Files.isRegularFile(result) || !Files.isReadable(result)) { |
117 |
| - String message = "The mapping result expected at path " + result + " is not accessible. This indicates an error of the mapper implementation."; |
118 |
| - LOG.trace(message); |
119 |
| - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(message); |
| 86 | + LOG.trace("Receiving mapping input file."); |
| 87 | + String extension = "." + FilenameUtils.getExtension(document.getOriginalFilename()); |
| 88 | + LOG.trace("Found file extension: {}", extension); |
| 89 | + Path inputPath = FileUtil.createTempFile("inputMultipart", extension); |
| 90 | + LOG.trace("Writing user upload to: {}", inputPath); |
| 91 | + File inputFile = inputPath.toFile(); |
| 92 | + try{ |
| 93 | + document.transferTo(inputFile); |
| 94 | + LOG.trace("Successfully received user upload."); |
| 95 | + } catch(IOException e){ |
| 96 | + LOG.error("Failed to receive upload from user.", e); |
| 97 | + throw new MappingExecutionException("Unable to write user upload to disk."); |
| 98 | + } |
| 99 | + |
| 100 | + try{ |
| 101 | + LOG.trace("Performing mapping process of file {} via mapping service", inputPath.toString()); |
| 102 | + |
| 103 | + resultPath = mappingService.executeMapping(inputFile.toURI(), mappingID); |
| 104 | + if(resultPath.isPresent()){ |
| 105 | + LOG.trace("Mapping process finished. Output written to {}.", resultPath.toString()); |
| 106 | + } else{ |
| 107 | + throw new MappingPluginException(MappingPluginState.UNKNOWN_ERROR, "Mapping process finished, but no result was returned."); |
120 | 108 | }
|
| 109 | + } catch(MappingPluginException e){ |
| 110 | + LOG.error("Failed to execute mapping.", e); |
| 111 | + throw new MappingExecutionException("Failed to execute mapping with id " + mappingID + " on provided input document."); |
| 112 | + } finally{ |
| 113 | + LOG.trace("Removing user upload at {}.", inputFile); |
| 114 | + FileUtil.removeFile(inputPath); |
| 115 | + LOG.trace("User upload successfully removed."); |
| 116 | + } |
| 117 | + } else{ |
| 118 | + String message = "Either mapping id or input document are missing. Unable to perform mapping."; |
| 119 | + LOG.error(message); |
| 120 | + throw new MappingException(message); |
| 121 | + } |
| 122 | + |
| 123 | + Path result = resultPath.get(); |
| 124 | + if(!Files.exists(result) || !Files.isRegularFile(result) || !Files.isReadable(result)){ |
| 125 | + String message = "The mapping result expected at path " + result + " is not accessible. This indicates an error of the mapper implementation."; |
| 126 | + LOG.error(message); |
| 127 | + throw new MappingExecutionException(message); |
| 128 | + } |
| 129 | + |
| 130 | + LOG.trace("Determining mime type for mapping result."); |
| 131 | + result = FileUtil.fixFileExtension(result); |
| 132 | + |
| 133 | + String mimeType = FileUtil.getMimeType(result); |
| 134 | + LOG.trace("Determining file extension for mapping result."); |
| 135 | + String extension = FileUtil.getExtensionForMimeType(mimeType); |
| 136 | + |
| 137 | + LOG.trace("Using mime type {} and extension {}.", mimeType, extension); |
| 138 | + |
| 139 | + response.setStatus(HttpStatus.OK.value()); |
| 140 | + response.setHeader("Content-Type", mimeType); |
| 141 | + response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(result.toFile().length())); |
| 142 | + response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); |
| 143 | + response.setHeader("Pragma", "no-cache"); |
| 144 | + response.setHeader("Expires", "0"); |
| 145 | + response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;" + "filename=result" + extension); |
| 146 | + try{ |
| 147 | + Files.copy(result, response.getOutputStream()); |
121 | 148 |
|
122 |
| - LOG.trace("Determining mime type for mapping result."); |
123 |
| - String mimeType = FileUtil.getMimeType(result); |
124 |
| - LOG.trace("Determining file extension for mapping result."); |
125 |
| - String extension = FileUtil.getExtensionForMimeType(mimeType); |
126 |
| - |
127 |
| - LOG.trace("Using mime type {} and extension {}.", mimeType, extension); |
128 |
| - return ResponseEntity.ok(). |
129 |
| - header(HttpHeaders.CONTENT_LENGTH, String.valueOf(result.toFile().length())). |
130 |
| - header(HttpHeaders.CONTENT_TYPE, mimeType). |
131 |
| - header(HttpHeaders.CONTENT_DISPOSITION, String.valueOf("attachment;" + "result" + extension)). |
132 |
| - body(new FileSystemResource(result.toFile())); |
| 149 | + } catch(IOException ex){ |
| 150 | + String message = "Failed to write mapping result file to stream."; |
| 151 | + LOG.error(message, ex); |
| 152 | + throw new MappingExecutionException(message); |
| 153 | + } finally{ |
| 154 | + LOG.trace("Result file successfully transferred to client. Removing file {} from disk.", result); |
| 155 | + try{ |
| 156 | + Files.delete(result); |
| 157 | + LOG.trace("Result file successfully removed."); |
| 158 | + } catch(IOException ignored){ |
| 159 | + LOG.warn("Failed to remove result file. Please remove manually."); |
| 160 | + } |
133 | 161 | }
|
| 162 | + } |
134 | 163 | }
|
0 commit comments