use of de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument in project webanno by webanno.
the class RemoteApiController method sourceDocumentDelete.
/**
* Delete the source document in project if user has an ADMIN permission
*
* To test when running in Eclipse, use the Linux "curl" command.
*
* curl -v -X DELETE
* 'http://USERNAME:PASSWORD@localhost:8080/webanno-webapp/api/projects/{aProjectId}/sourcedocs/
* {aSourceDocumentId}'
*
* @param aProjectId
* {@link Project} ID.
* @param aSourceDocumentId
* {@link SourceDocument} ID.
* @throws Exception
* if there was an error.
*/
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS + "/{" + PARAM_DOCUMENT_ID + "}", method = RequestMethod.DELETE)
public ResponseEntity<String> sourceDocumentDelete(@PathVariable(PARAM_PROJECT_ID) long aProjectId, @PathVariable(PARAM_DOCUMENT_ID) long aSourceDocumentId) throws Exception {
// Get current user
String username = SecurityContextHolder.getContext().getAuthentication().getName();
User user = userRepository.get(username);
if (user == null) {
return ResponseEntity.badRequest().body("User [" + username + "] not found.");
}
// Get project
Project project;
try {
project = projectRepository.getProject(aProjectId);
} catch (NoResultException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Project [" + aProjectId + "] not found.");
}
// Check for the access
boolean hasAccess = SecurityUtil.isProjectAdmin(project, projectRepository, user) || SecurityUtil.isSuperAdmin(projectRepository, user);
if (!hasAccess) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User [" + username + "] is not allowed to access project [" + aProjectId + "]");
}
LOG.info("Deleting document [" + project.getName() + "]");
// Get source document
SourceDocument srcDocument;
try {
srcDocument = documentRepository.getSourceDocument(aProjectId, aSourceDocumentId);
} catch (NoResultException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Source document [" + aSourceDocumentId + "] not found in project [" + aProjectId + "] not found.");
}
documentRepository.removeSourceDocument(srcDocument);
LOG.info("Successfully deleted project : [" + aProjectId + "]");
return ResponseEntity.ok("Source document [" + aSourceDocumentId + "] in project [" + aProjectId + "] deleted.");
}
use of de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument in project webanno by webanno.
the class RemoteApiController method annotationDocumentList.
/**
* List annotation documents for a source document in a projects where user is ADMIN
*
* Test when running in Eclipse: Open your browser, paste following URL with appropriate values:
*
* http://USERNAME:PASSWORD@localhost:8080/webanno-webapp/api/projects/{aProjectId}/sourcedocs/{
* aSourceDocumentId}/annos
*
* @param aProjectId
* {@link Project} ID
* @param aSourceDocumentId
* {@link SourceDocument} ID
* @return JSON string of all the annotation documents with their projects.
* @throws Exception
* if there was an error.
*/
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS + "/{" + PARAM_DOCUMENT_ID + "}/" + ANNOTATIONS, method = RequestMethod.GET)
public ResponseEntity<String> annotationDocumentList(@PathVariable(PARAM_PROJECT_ID) long aProjectId, @PathVariable(PARAM_DOCUMENT_ID) long aSourceDocumentId) throws Exception {
// Get current user
String username = SecurityContextHolder.getContext().getAuthentication().getName();
User user = userRepository.get(username);
if (user == null) {
return ResponseEntity.badRequest().body("User [" + username + "] not found.");
}
// Get project
Project project;
try {
project = projectRepository.getProject(aProjectId);
} catch (NoResultException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Project [" + aProjectId + "] not found.");
}
// Check for the access
boolean hasAccess = SecurityUtil.isProjectAdmin(project, projectRepository, user) || SecurityUtil.isSuperAdmin(projectRepository, user);
if (!hasAccess) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User [" + username + "] is not allowed to access project [" + aProjectId + "]");
}
// Get source document
SourceDocument srcDocument;
try {
srcDocument = documentRepository.getSourceDocument(aProjectId, aSourceDocumentId);
} catch (NoResultException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Source document [" + aSourceDocumentId + "] not found in project [" + aProjectId + "] not found.");
}
List<AnnotationDocument> annList = documentRepository.listAllAnnotationDocuments(srcDocument);
JSONArray annDocArr = new JSONArray();
for (AnnotationDocument annDoc : annList) {
if (annDoc.getState().equals(AnnotationDocumentState.FINISHED) || annDoc.getState().equals(AnnotationDocumentState.IN_PROGRESS)) {
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ssZ");
JSONObject annDocObj = new JSONObject();
annDocObj.put("user", annDoc.getUser());
annDocObj.put("state", annDoc.getState().getId());
if (annDoc.getTimestamp() != null) {
annDocObj.put("timestamp", sdf.format(annDoc.getTimestamp()));
}
annDocArr.put(annDocObj);
}
}
JSONObject returnJSON = new JSONObject();
returnJSON.put(srcDocument.getName(), annDocArr);
return ResponseEntity.ok(returnJSON.toString());
}
use of de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument in project webanno by webanno.
the class RemoteApiController method sourceDocumentList.
/**
* Show source documents in given project where user has ADMIN access
*
* http://USERNAME:PASSWORD@localhost:8080/webanno-webapp/api/projects/{aProjectId}/sourcedocs
*
* @param aProjectId the project ID
* @return JSON with {@link SourceDocument} : id
*/
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS, method = RequestMethod.GET)
public ResponseEntity<String> sourceDocumentList(@PathVariable(PARAM_PROJECT_ID) long aProjectId) throws Exception {
// Get current user
String username = SecurityContextHolder.getContext().getAuthentication().getName();
User user = userRepository.get(username);
if (user == null) {
return ResponseEntity.badRequest().body("User [" + username + "] not found.");
}
// Get project
Project project;
try {
project = projectRepository.getProject(aProjectId);
} catch (NoResultException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Project [" + aProjectId + "] not found.");
}
// Check for the access
boolean hasAccess = SecurityUtil.isProjectAdmin(project, projectRepository, user) || SecurityUtil.isSuperAdmin(projectRepository, user);
if (!hasAccess) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User [" + username + "] is not allowed to access project [" + aProjectId + "]");
}
List<SourceDocument> srcDocumentList = documentRepository.listSourceDocuments(project);
JSONArray sourceDocumentJSONArr = new JSONArray();
for (SourceDocument s : srcDocumentList) {
JSONObject sourceDocumentJSONObj = new JSONObject();
sourceDocumentJSONObj.put("id", s.getId());
sourceDocumentJSONObj.put("name", s.getName());
sourceDocumentJSONObj.put("state", s.getState());
sourceDocumentJSONArr.put(sourceDocumentJSONObj);
}
return ResponseEntity.ok(sourceDocumentJSONArr.toString());
}
use of de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument in project webanno by webanno.
the class RemoteApiController method annotationDocumentRead.
/**
* Download annotation document with requested parameters
*
* Test when running in Eclipse: Open your browser, paste following URL with appropriate values:
*
* http://USERNAME:PASSWORD@localhost:8080/webanno-webapp/api/projects/{aProjectId}/sourcedocs/{
* aSourceDocumentId}/annos/{annotatorName}?format="text"
*
* @param response
* HttpServletResponse.
* @param aProjectId
* {@link Project} ID.
* @param aSourceDocumentId
* {@link SourceDocument} ID.
* @param annotatorName
* {@link User} name.
* @param format
* Export format.
* @throws Exception
* if there was an error.
*/
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS + "/{" + PARAM_DOCUMENT_ID + "}/" + ANNOTATIONS + "/{" + PARAM_USERNAME + "}", method = RequestMethod.GET)
public void annotationDocumentRead(HttpServletResponse response, @PathVariable(PARAM_PROJECT_ID) long aProjectId, @PathVariable(PARAM_DOCUMENT_ID) long aSourceDocumentId, @PathVariable(PARAM_USERNAME) String annotatorName, @RequestParam(value = PARAM_FORMAT, required = false) String format) throws Exception {
// Get current user
String username = SecurityContextHolder.getContext().getAuthentication().getName();
User user = userRepository.get(username);
if (user == null) {
response.sendError(HttpStatus.BAD_REQUEST.value(), "User [" + username + "] not found.");
return;
}
// Get project
Project project;
try {
project = projectRepository.getProject(aProjectId);
} catch (NoResultException e) {
response.sendError(HttpStatus.NOT_FOUND.value(), "Project" + aProjectId + "] not found.");
return;
}
// Check for the access
boolean hasAccess = SecurityUtil.isProjectAdmin(project, projectRepository, user) || SecurityUtil.isSuperAdmin(projectRepository, user);
if (!hasAccess) {
response.sendError(HttpStatus.FORBIDDEN.value(), "User [" + username + "] is not allowed to access project [" + aProjectId + "]");
return;
}
// Get annotator user
User annotator = userRepository.get(annotatorName);
if (annotator == null) {
response.sendError(HttpStatus.BAD_REQUEST.value(), "Annotator user [" + annotatorName + "] not found.");
return;
}
// Get source document
SourceDocument srcDoc;
try {
srcDoc = documentRepository.getSourceDocument(aProjectId, aSourceDocumentId);
} catch (NoResultException e) {
response.sendError(HttpStatus.NOT_FOUND.value(), "Document [" + aSourceDocumentId + "] not found in project [" + aProjectId + "].");
return;
}
// Get annotation document
AnnotationDocument annDoc;
try {
annDoc = documentRepository.getAnnotationDocument(srcDoc, annotator);
} catch (NoResultException e) {
response.sendError(HttpStatus.NOT_FOUND.value(), "Annotations for user [" + annotatorName + "] not found on document [" + aSourceDocumentId + "] in project [" + aProjectId + "].");
return;
}
String formatId;
if (format == null) {
formatId = srcDoc.getFormat();
} else {
formatId = format;
}
Class<?> writer = importExportService.getWritableFormats().get(formatId);
if (writer == null) {
String msg = "[" + srcDoc.getName() + "] No writer found for format [" + formatId + "] - exporting as WebAnno TSV instead.";
LOG.info(msg);
writer = WebannoTsv3XWriter.class;
}
// Temporary file of annotation document
File downloadableFile = importExportService.exportAnnotationDocument(srcDoc, annotatorName, writer, annDoc.getName(), Mode.ANNOTATION);
try {
// Set mime type
String mimeType = URLConnection.guessContentTypeFromName(downloadableFile.getName());
if (mimeType == null) {
LOG.info("mimetype is not detectable, will take default");
mimeType = "application/octet-stream";
}
// Set response
response.setContentType(mimeType);
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "inline; filename=\"" + downloadableFile.getName() + "\"");
response.setContentLength((int) downloadableFile.length());
InputStream inputStream = new BufferedInputStream(new FileInputStream(downloadableFile));
FileCopyUtils.copy(inputStream, response.getOutputStream());
} catch (Exception e) {
LOG.info("Exception occured" + e.getMessage());
} finally {
if (downloadableFile.exists()) {
downloadableFile.delete();
}
}
}
use of de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument 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));
}
Aggregations