use of de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.exception.UnsupportedFormatException in project webanno by webanno.
the class AeroRemoteApiController method projectExport.
@ApiOperation(value = "Export a project to a ZIP file")
@RequestMapping(value = ("/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + EXPORT), method = RequestMethod.GET, produces = { "application/zip", APPLICATION_JSON_UTF8_VALUE })
public ResponseEntity<InputStreamResource> projectExport(@PathVariable(PARAM_PROJECT_ID) long aProjectId, @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);
// Check if the format is supported
if (aFormat.isPresent()) {
importExportService.getWritableFormatById(aFormat.get()).orElseThrow(() -> new UnsupportedFormatException("Format [%s] cannot be exported. Exportable formats are %s.", aFormat.get(), importExportService.getWritableFormats().stream().map(FormatSupport::getId).sorted().collect(Collectors.toList()).toString()));
}
ProjectExportRequest request = new ProjectExportRequest(project, aFormat.orElse(WebAnnoTsv3FormatSupport.ID), true);
ProjectExportTaskMonitor monitor = new ProjectExportTaskMonitor();
File exportedFile = exportService.exportProject(request, monitor);
// Turn the file into a resource and auto-delete the file when the resource closes the
// stream.
InputStreamResource result = new InputStreamResource(new FileInputStream(exportedFile) {
@Override
public void close() throws IOException {
super.close();
FileUtils.forceDelete(exportedFile);
}
});
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.valueOf("application/zip"));
httpHeaders.setContentLength(exportedFile.length());
httpHeaders.set("Content-Disposition", "attachment; filename=\"" + exportedFile.getName() + "\"");
return new ResponseEntity<>(result, httpHeaders, HttpStatus.OK);
}
use of de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.exception.UnsupportedFormatException in project webanno by webanno.
the class AeroRemoteApiController 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(@RequestPart(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", userRepository.isAdministrator(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);
ProjectImportRequest request = new ProjectImportRequest(false);
importedProject = exportService.importProject(request, new ZipFile(tempFile));
} finally {
tempFile.delete();
}
return ResponseEntity.ok(new RResponse<>(new RProject(importedProject)));
}
use of de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.exception.UnsupportedFormatException in project webanno by webanno.
the class AeroRemoteApiController method createCompatibleCas.
private CAS createCompatibleCas(long aProjectId, long aDocumentId, MultipartFile aFile, Optional<String> aFormatId) throws RemoteApiException, ClassNotFoundException, IOException, UIMAException {
Project project = getProject(aProjectId);
SourceDocument document = getDocument(project, aDocumentId);
// Check if the format is supported
String format = aFormatId.orElse(FORMAT_DEFAULT);
if (!importExportService.getReadableFormatById(format).isPresent()) {
throw new UnsupportedFormatException("Format [%s] not supported. Acceptable formats are %s.", format, importExportService.getReadableFormats().stream().map(FormatSupport::getId).sorted().collect(Collectors.toList()));
}
// Convert the uploaded annotation document into a CAS
File tmpFile = null;
CAS 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.
CAS 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!
forceOverwriteSofa(annotationCas, initialCas.getDocumentText());
Collection<AnnotationFS> annotationSentences = selectSentences(annotationCas);
Collection<AnnotationFS> initialSentences = selectSentences(initialCas);
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<AnnotationFS> annotationTokens = selectTokens(annotationCas);
Collection<AnnotationFS> initialTokens = selectTokens(initialCas);
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;
}
use of de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.exception.UnsupportedFormatException in project webanno by webanno.
the class AeroRemoteApiController method documentCreate.
@ApiOperation(value = "Create a new document in a project")
@ApiImplicitParams({ @ApiImplicitParam(name = PARAM_NAME, paramType = "form", required = true), @ApiImplicitParam(name = PARAM_FORMAT, paramType = "form", required = true), @ApiImplicitParam(name = PARAM_STATE, paramType = "form", required = true) })
@//
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(PARAM_CONTENT) MultipartFile aFile, @RequestParam(PARAM_NAME) String aName, @RequestParam(PARAM_FORMAT) String aFormat, @RequestParam(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
if (!importExportService.getReadableFormatById(aFormat).isPresent()) {
throw new UnsupportedFormatException("Format [%s] not supported. Acceptable formats are %s.", aFormat, importExportService.getReadableFormats().stream().map(FormatSupport::getId).sorted().collect(Collectors.toList()));
}
// 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);
}
use of de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.aero.exception.UnsupportedFormatException in project webanno by webanno.
the class AeroRemoteApiController 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 formatId;
if (aFormat.isPresent()) {
if (VAL_ORIGINAL.equals(aFormat.get())) {
formatId = doc.getFormat();
originalFile = true;
} else {
formatId = aFormat.get();
originalFile = doc.getFormat().equals(formatId);
}
} else {
formatId = 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
FormatSupport format = importExportService.getWritableFormatById(formatId).orElseThrow(() -> new UnsupportedFormatException("Format [%s] cannot be exported. Exportable formats are %s.", aFormat, importExportService.getWritableFormats().stream().map(FormatSupport::getId).sorted().collect(Collectors.toList()).toString()));
// Create a temporary export file from the annotations
CAS cas = documentService.createOrReadInitialCas(doc);
File exportedFile = null;
try {
// Load the converted file into memory
exportedFile = importExportService.exportCasToFile(cas, doc, doc.getName(), format, 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);
}
}
}
}
Aggregations