Search in sources :

Example 6 with WebAnnoTsv3FormatSupport

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);
}
Also used : WebAnnoTsv3FormatSupport(de.tudarmstadt.ukp.clarin.webanno.tsv.WebAnnoTsv3FormatSupport) FormatSupport(de.tudarmstadt.ukp.clarin.webanno.api.format.FormatSupport) RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.model.RProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) WebAnnoTsv3FormatSupport(de.tudarmstadt.ukp.clarin.webanno.tsv.WebAnnoTsv3FormatSupport) File(java.io.File) ZipFile(java.util.zip.ZipFile) MultipartFile(org.springframework.web.multipart.MultipartFile)

Aggregations

FormatSupport (de.tudarmstadt.ukp.clarin.webanno.api.format.FormatSupport)6 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)6 WebAnnoTsv3FormatSupport (de.tudarmstadt.ukp.clarin.webanno.tsv.WebAnnoTsv3FormatSupport)6 File (java.io.File)6 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)5 IOException (java.io.IOException)5 ZipFile (java.util.zip.ZipFile)5 User (de.tudarmstadt.ukp.clarin.webanno.security.model.User)3 MultipartFile (org.springframework.web.multipart.MultipartFile)3 ProjectExportException (de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExportException)2 ExportedProject (de.tudarmstadt.ukp.clarin.webanno.export.model.ExportedProject)2 AnnotationDocument (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)2 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 NoResultException (javax.persistence.NoResultException)2 Pair (org.apache.commons.lang3.tuple.Pair)2 UIMAException (org.apache.uima.UIMAException)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2