use of de.tudarmstadt.ukp.clarin.webanno.tsv.WebAnnoTsv3FormatSupport in project webanno by webanno.
the class AeroRemoteApiController method readAnnotation.
private ResponseEntity<byte[]> readAnnotation(long aProjectId, long aDocumentId, String aAnnotatorId, Mode aMode, Optional<String> aFormat) throws RemoteApiException, ClassNotFoundException, IOException, UIMAException {
// Get project (this also ensures that it exists and that the current user can access it
Project project = getProject(aProjectId);
SourceDocument doc = getDocument(project, aDocumentId);
// Check format
String formatId;
if (aFormat.isPresent()) {
if (VAL_ORIGINAL.equals(aFormat.get())) {
formatId = doc.getFormat();
} else {
formatId = aFormat.get();
}
} else {
formatId = doc.getFormat();
}
// Determine the format
FormatSupport format = importExportService.getWritableFormatById(formatId).orElseGet(() -> {
LOG.info("[{}] Format [{}] is not writable - exporting as WebAnno TSV3 instead.", doc.getName(), formatId);
return new WebAnnoTsv3FormatSupport();
});
// annotation document entry is actually properly set up in the database.
if (Mode.ANNOTATION.equals(aMode)) {
getAnnotation(doc, aAnnotatorId, false);
}
// Create a temporary export file from the annotations
File exportedAnnoFile = null;
byte[] resource;
try {
exportedAnnoFile = importExportService.exportAnnotationDocument(doc, aAnnotatorId, format, doc.getName(), Mode.ANNOTATION);
resource = FileUtils.readFileToByteArray(exportedAnnoFile);
} finally {
if (exportedAnnoFile != null) {
FileUtils.forceDelete(exportedAnnoFile);
}
}
String filename = FilenameUtils.removeExtension(doc.getName());
filename += "-" + aAnnotatorId;
// Actually, exportedAnnoFile cannot be null here - the warning can be ignored.
filename += "." + FilenameUtils.getExtension(exportedAnnoFile.getName());
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentLength(resource.length);
httpHeaders.set("Content-Disposition", "attachment; filename=\"" + filename + "\"");
return new ResponseEntity<>(resource, httpHeaders, OK);
}
Aggregations