Search in sources :

Example 1 with UnsupportedFormatException

use of de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.UnsupportedFormatException in project webanno by webanno.

the class RemoteApiController2 method documentCreate.

@ApiOperation(value = "Create a new document in a project")
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS, method = RequestMethod.POST, consumes = MULTIPART_FORM_DATA_VALUE, produces = APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<RResponse<RDocument>> documentCreate(@PathVariable(PARAM_PROJECT_ID) long aProjectId, @RequestParam(value = PARAM_CONTENT) MultipartFile aFile, @RequestParam(value = PARAM_NAME) String aName, @RequestParam(value = PARAM_FORMAT) String aFormat, @RequestParam(value = PARAM_STATE) Optional<String> aState, UriComponentsBuilder aUcb) throws Exception {
    // Get project (this also ensures that it exists and that the current user can access it
    Project project = getProject(aProjectId);
    // Check if the format is supported
    Map<String, Class<CollectionReader>> readableFormats = importExportService.getReadableFormats();
    if (readableFormats.get(aFormat) == null) {
        throw new UnsupportedFormatException("Format [%s] not supported. Acceptable formats are %s.", aFormat, readableFormats.keySet());
    }
    // Meta data entry to the database
    SourceDocument document = new SourceDocument();
    document.setProject(project);
    document.setName(aName);
    document.setFormat(aFormat);
    // Set state if one was provided
    if (aState.isPresent()) {
        SourceDocumentState state = parseSourceDocumentState(aState.get());
        switch(state) {
            // fallthrough
            case NEW:
            // fallthrough
            case ANNOTATION_IN_PROGRESS:
            case // fallthrough
            ANNOTATION_FINISHED:
                document.setState(state);
                documentService.createSourceDocument(document);
                break;
            // fallthrough
            case CURATION_IN_PROGRESS:
            case CURATION_FINISHED:
            default:
                throw new IllegalObjectStateException("State [%s] not valid when uploading a document.", aState.get());
        }
    }
    // Import source document to the project repository folder
    try (InputStream is = aFile.getInputStream()) {
        documentService.uploadSourceDocument(is, document);
    }
    RResponse<RDocument> rDocument = new RResponse<>(new RDocument(document));
    if (aState.isPresent()) {
        rDocument.addMessage(INFO, "State of document [" + document.getId() + "] set to [" + aState.get() + "]");
    }
    return ResponseEntity.created(aUcb.path(API_BASE + "/" + PROJECTS + "/{pid}/" + DOCUMENTS + "/{did}").buildAndExpand(project.getId(), document.getId()).toUri()).body(rDocument);
}
Also used : RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) IllegalObjectStateException(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.IllegalObjectStateException) UnsupportedFormatException(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.UnsupportedFormatException) SourceDocumentState(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocumentState) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) RResponse(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RResponse) RDocument(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RDocument) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with UnsupportedFormatException

use of de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.UnsupportedFormatException in project webanno by webanno.

the class RemoteApiController2 method createCompatibleCas.

private JCas createCompatibleCas(long aProjectId, long aDocumentId, MultipartFile aFile, Optional<String> aFormat) throws RemoteApiException, ClassNotFoundException, IOException, UIMAException {
    Project project = getProject(aProjectId);
    SourceDocument document = getDocument(project, aDocumentId);
    // Check if the format is supported
    String format = aFormat.orElse(FORMAT_DEFAULT);
    Map<String, Class<CollectionReader>> readableFormats = importExportService.getReadableFormats();
    if (readableFormats.get(format) == null) {
        throw new UnsupportedFormatException("Format [%s] not supported. Acceptable formats are %s.", format, readableFormats.keySet());
    }
    // Convert the uploaded annotation document into a CAS
    File tmpFile = null;
    JCas annotationCas;
    try {
        tmpFile = File.createTempFile("upload", ".bin");
        aFile.transferTo(tmpFile);
        annotationCas = importExportService.importCasFromFile(tmpFile, project, format);
    } finally {
        if (tmpFile != null) {
            FileUtils.forceDelete(tmpFile);
        }
    }
    // Check if the uploaded file is compatible with the source document. They are compatible
    // if the text is the same and if all the token and sentence annotations have the same
    // offsets.
    JCas initialCas = documentService.createOrReadInitialCas(document);
    String initialText = initialCas.getDocumentText();
    String annotationText = annotationCas.getDocumentText();
    // If any of the texts contains tailing line breaks, we ignore that. We assume at the moment
    // that nobody will have created annotations over that trailing line breaks.
    initialText = StringUtils.chomp(initialText);
    annotationText = StringUtils.chomp(annotationText);
    if (ObjectUtils.notEqual(initialText, annotationText)) {
        int diffIndex = StringUtils.indexOfDifference(initialText, annotationText);
        String expected = initialText.substring(diffIndex, Math.min(initialText.length(), diffIndex + 20));
        String actual = annotationText.substring(diffIndex, Math.min(annotationText.length(), diffIndex + 20));
        throw new IncompatibleDocumentException("Text of annotation document does not match text of source document at offset " + "[%d]. Expected [%s] but found [%s].", diffIndex, expected, actual);
    }
    // Just in case we really had to chomp off a trailing line break from the annotation CAS,
    // make sure we copy over the proper text from the initial CAS
    // NOT AT HOME THIS YOU SHOULD TRY
    // SETTING THE SOFA STRING FORCEFULLY FOLLOWING THE DARK SIDE IS!
    forceSetFeatureValue(annotationCas.getSofa(), CAS.FEATURE_BASE_NAME_SOFASTRING, initialCas.getDocumentText());
    FSUtil.setFeature(annotationCas.getDocumentAnnotationFs(), CAS.FEATURE_BASE_NAME_END, initialCas.getDocumentText().length());
    Collection<Sentence> annotationSentences = select(annotationCas, Sentence.class);
    Collection<Sentence> initialSentences = select(initialCas, Sentence.class);
    if (annotationSentences.size() != initialSentences.size()) {
        throw new IncompatibleDocumentException("Expected [%d] sentences, but annotation document contains [%d] sentences.", initialSentences.size(), annotationSentences.size());
    }
    assertCompatibleOffsets(initialSentences, annotationSentences);
    Collection<Token> annotationTokens = select(annotationCas, Token.class);
    Collection<Token> initialTokens = select(initialCas, Token.class);
    if (annotationTokens.size() != initialTokens.size()) {
        throw new IncompatibleDocumentException("Expected [%d] sentences, but annotation document contains [%d] sentences.", initialSentences.size(), annotationSentences.size());
    }
    assertCompatibleOffsets(initialTokens, annotationTokens);
    return annotationCas;
}
Also used : SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) IncompatibleDocumentException(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.IncompatibleDocumentException) RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) UnsupportedFormatException(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.UnsupportedFormatException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)

Example 3 with UnsupportedFormatException

use of de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.UnsupportedFormatException in project webanno by webanno.

the class RemoteApiController2 method projectImport.

@ApiOperation(value = "Import a previously exported project")
@RequestMapping(value = ("/" + PROJECTS + "/" + IMPORT), method = RequestMethod.POST, consumes = MULTIPART_FORM_DATA_VALUE, produces = APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<RResponse<RProject>> projectImport(@RequestParam(PARAM_FILE) MultipartFile aFile) throws Exception {
    // Get current user - this will throw an exception if the current user does not exit
    User user = getCurrentUser();
    // Check for the access
    assertPermission("User [" + user.getUsername() + "] is not allowed to import projects", isSuperAdmin(projectService, user));
    Project importedProject;
    File tempFile = File.createTempFile("webanno-training", null);
    try (InputStream is = new BufferedInputStream(aFile.getInputStream());
        OutputStream os = new FileOutputStream(tempFile)) {
        if (!ZipUtils.isZipStream(is)) {
            throw new UnsupportedFormatException("Invalid ZIP file");
        }
        IOUtils.copyLarge(is, os);
        if (!ImportUtil.isZipValidWebanno(tempFile)) {
            throw new UnsupportedFormatException("Incompatible to webanno ZIP file");
        }
        importedProject = importService.importProject(tempFile, false);
    } finally {
        tempFile.delete();
    }
    return ResponseEntity.ok(new RResponse<>(new RProject(importedProject)));
}
Also used : RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) UnsupportedFormatException(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.UnsupportedFormatException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with UnsupportedFormatException

use of de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.UnsupportedFormatException in project webanno by webanno.

the class RemoteApiController2 method documentRead.

@ApiOperation(value = "Get a document from a project", response = byte[].class)
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS + "/{" + PARAM_DOCUMENT_ID + "}", method = RequestMethod.GET, produces = { APPLICATION_OCTET_STREAM_VALUE, APPLICATION_JSON_UTF8_VALUE })
public ResponseEntity documentRead(@PathVariable(PARAM_PROJECT_ID) long aProjectId, @PathVariable(PARAM_DOCUMENT_ID) long aDocumentId, @RequestParam(value = PARAM_FORMAT) Optional<String> aFormat) throws Exception {
    // 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);
    boolean originalFile;
    String format;
    if (aFormat.isPresent()) {
        if (VAL_ORIGINAL.equals(aFormat.get())) {
            format = doc.getFormat();
            originalFile = true;
        } else {
            format = aFormat.get();
            originalFile = doc.getFormat().equals(format);
        }
    } else {
        format = doc.getFormat();
        originalFile = true;
    }
    if (originalFile) {
        // Export the original file - no temporary file created here, we export directly from
        // the file system
        File docFile = documentService.getSourceDocumentFile(doc);
        FileSystemResource resource = new FileSystemResource(docFile);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentLength(resource.contentLength());
        httpHeaders.set("Content-Disposition", "attachment; filename=\"" + doc.getName() + "\"");
        return new ResponseEntity<org.springframework.core.io.Resource>(resource, httpHeaders, OK);
    } else {
        // Export a converted file - here we first export to a local temporary file and then
        // send that back to the client
        // Check if the format is supported
        Map<String, Class<JCasAnnotator_ImplBase>> writableFormats = importExportService.getWritableFormats();
        Class<JCasAnnotator_ImplBase> writer = writableFormats.get(format);
        if (writer == null) {
            throw new UnsupportedFormatException("Format [%s] cannot be exported. Exportable formats are %s.", aFormat, writableFormats.keySet());
        }
        // Create a temporary export file from the annotations
        JCas jcas = documentService.createOrReadInitialCas(doc);
        File exportedFile = null;
        try {
            // Load the converted file into memory
            exportedFile = importExportService.exportCasToFile(jcas.getCas(), doc, doc.getName(), writer, true);
            byte[] resource = FileUtils.readFileToByteArray(exportedFile);
            // Send it back to the client
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.setContentLength(resource.length);
            httpHeaders.set("Content-Disposition", "attachment; filename=\"" + exportedFile.getName() + "\"");
            return new ResponseEntity<>(resource, httpHeaders, OK);
        } finally {
            if (exportedFile != null) {
                FileUtils.forceDelete(exportedFile);
            }
        }
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) JCasAnnotator_ImplBase(org.apache.uima.analysis_component.JCasAnnotator_ImplBase) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) FileSystemResource(org.springframework.core.io.FileSystemResource) RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) ResponseEntity(org.springframework.http.ResponseEntity) UnsupportedFormatException(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.UnsupportedFormatException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)4 UnsupportedFormatException (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.UnsupportedFormatException)4 RProject (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject)4 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)3 ApiOperation (io.swagger.annotations.ApiOperation)3 File (java.io.File)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 MultipartFile (org.springframework.web.multipart.MultipartFile)3 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 JCas (org.apache.uima.jcas.JCas)2 SourceDocumentState (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocumentState)1 User (de.tudarmstadt.ukp.clarin.webanno.security.model.User)1 IllegalObjectStateException (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.IllegalObjectStateException)1 IncompatibleDocumentException (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.exception.IncompatibleDocumentException)1 RDocument (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RDocument)1 RResponse (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RResponse)1 Sentence (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)1 Token (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token)1