use of de.tudarmstadt.ukp.clarin.webanno.model.Project in project webanno by webanno.
the class RemoteApiController2 method documentList.
@ApiOperation(value = "List documents in a project")
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS, method = RequestMethod.GET, produces = APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<RResponse<List<RDocument>>> documentList(@PathVariable(PARAM_PROJECT_ID) long aProjectId) throws Exception {
// Get project (this also ensures that it exists and that the current user can access it
Project project = getProject(aProjectId);
List<SourceDocument> documents = documentService.listSourceDocuments(project);
List<RDocument> documentList = new ArrayList<>();
for (SourceDocument document : documents) {
documentList.add(new RDocument(document));
}
return ResponseEntity.ok(new RResponse<>(documentList));
}
use of de.tudarmstadt.ukp.clarin.webanno.model.Project in project webanno by webanno.
the class RemoteApiController2 method getProject.
private Project getProject(long aProjectId) throws ObjectNotFoundException, AccessForbiddenException {
// Get current user - this will throw an exception if the current user does not exit
User user = getCurrentUser();
// Get project
Project project;
try {
project = projectService.getProject(aProjectId);
} catch (NoResultException e) {
throw new ObjectNotFoundException("Project [" + aProjectId + "] not found.");
}
// Check for the access
assertPermission("User [" + user.getUsername() + "] is not allowed to access project [" + aProjectId + "]", isProjectAdmin(project, projectService, user) || isSuperAdmin(projectService, user));
return project;
}
use of de.tudarmstadt.ukp.clarin.webanno.model.Project in project webanno by webanno.
the class RemoteApiController2 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 format;
if (aFormat.isPresent()) {
if (VAL_ORIGINAL.equals(aFormat.get())) {
format = doc.getFormat();
} else {
format = aFormat.get();
}
} else {
format = doc.getFormat();
}
// Determine the format
Class<?> writer = importExportService.getWritableFormats().get(format);
if (writer == null) {
String msg = "[" + doc.getName() + "] No writer found for format [" + format + "] - exporting as WebAnno TSV instead.";
LOG.info(msg);
writer = WebannoTsv3XWriter.class;
}
// 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, writer, 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);
}
use of de.tudarmstadt.ukp.clarin.webanno.model.Project in project webanno by webanno.
the class RemoteApiController2 method projectDelete.
@ApiOperation(value = "Delete an existing project")
@RequestMapping(value = ("/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}"), method = RequestMethod.DELETE, produces = APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<RResponse<Void>> projectDelete(@PathVariable(PARAM_PROJECT_ID) long aProjectId) throws Exception {
// Get project (this also ensures that it exists and that the current user can access it
Project project = getProject(aProjectId);
projectService.removeProject(project);
return ResponseEntity.ok(new RResponse<>(INFO, "Project [" + aProjectId + "] deleted."));
}
use of de.tudarmstadt.ukp.clarin.webanno.model.Project 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);
}
}
}
}
Aggregations