Skip to content
Snippets Groups Projects
Verified Commit 64bec6db authored by Timm Fitschen's avatar Timm Fitschen
Browse files

WIP: handle retrival of orphaned files

parent 459e275f
No related branches found
No related tags found
No related merge requests found
...@@ -408,12 +408,8 @@ public abstract class AbstractCaosDBServerResource extends ServerResource { ...@@ -408,12 +408,8 @@ public abstract class AbstractCaosDBServerResource extends ServerResource {
try { try {
getRequest().getAttributes().put("THROWN", t); getRequest().getAttributes().put("THROWN", t);
throw t; throw t;
} catch (final AuthenticationException e) { } catch (final AuthenticationException | AuthorizationException e) {
getResponse().setStatus(Status.CLIENT_ERROR_FORBIDDEN); return error(ServerMessages.NOT_PERMITTED, Status.CLIENT_ERROR_FORBIDDEN);
return null;
} catch (final AuthorizationException e) {
getResponse().setStatus(Status.CLIENT_ERROR_FORBIDDEN);
return null;
} catch (final Message m) { } catch (final Message m) {
return error(m, Status.CLIENT_ERROR_BAD_REQUEST); return error(m, Status.CLIENT_ERROR_BAD_REQUEST);
} catch (final FileUploadException e) { } catch (final FileUploadException e) {
......
...@@ -59,6 +59,11 @@ import org.restlet.representation.Representation; ...@@ -59,6 +59,11 @@ import org.restlet.representation.Representation;
*/ */
public class FileSystemResource extends AbstractCaosDBServerResource { public class FileSystemResource extends AbstractCaosDBServerResource {
public static Message ORPHANED_FILE_WARNING =
new Message(
MessageType.Warning,
"Orphaned file. The file is not tracked. This is probably a harmless inconsistency but it might be a sign of other problems.");
/** /**
* Download a File from the CaosDBFileSystem. Only one File per Request. * Download a File from the CaosDBFileSystem. Only one File per Request.
* *
...@@ -127,6 +132,13 @@ public class FileSystemResource extends AbstractCaosDBServerResource { ...@@ -127,6 +132,13 @@ public class FileSystemResource extends AbstractCaosDBServerResource {
} else { } else {
try {
getEntity(specifier).checkPermission(EntityPermission.RETRIEVE_FILE);
} catch (EntityDoesNotExistException exception) {
// This file in the file system has no corresponding File record.
return error(ServerMessages.NOT_PERMITTED, Status.CLIENT_ERROR_FORBIDDEN);
}
final MediaType mt = MediaType.valueOf(FileUtils.getMimeType(file)); final MediaType mt = MediaType.valueOf(FileUtils.getMimeType(file));
final FileRepresentation ret = new FileRepresentation(file, mt); final FileRepresentation ret = new FileRepresentation(file, mt);
ret.setDisposition(new Disposition(Disposition.TYPE_ATTACHMENT)); ret.setDisposition(new Disposition(Disposition.TYPE_ATTACHMENT));
...@@ -160,29 +172,31 @@ public class FileSystemResource extends AbstractCaosDBServerResource { ...@@ -160,29 +172,31 @@ public class FileSystemResource extends AbstractCaosDBServerResource {
*/ */
Element getFileElement(final String directory, final File file) throws Exception { Element getFileElement(final String directory, final File file) throws Exception {
final Element celem = new Element("file"); final Element celem = new Element("file");
celem.setAttribute("name", file.getName());
try {
final String entId = final String entId =
getEntityID((directory.endsWith("/") ? directory : directory + "/") + file.getName()); getEntityID((directory.endsWith("/") ? directory : directory + "/") + file.getName());
if (entId == null) {
new Message(
MessageType.Warning,
"Orphaned file. The file is not tracked. This is probably a harmless inconsistency but it might be a sign of other problems.")
.addToElement(celem);
} else {
celem.setAttribute("id", entId); celem.setAttribute("id", entId);
} catch (EntityDoesNotExistException exception) {
// This file in the file system has no corresponding File record.
ORPHANED_FILE_WARNING.addToElement(celem);
} }
celem.setAttribute("name", file.getName());
return celem; return celem;
} }
protected String getEntityID(final String path) throws Exception { protected String getEntityID(final String path) throws Exception {
final Entity fileEnt = getEntity(path); final Entity fileEnt = getEntity(path);
if (fileEnt == null) {
return null;
}
return fileEnt.getId().toString(); return fileEnt.getId().toString();
} }
/**
* Throws EntityDoesNotExistException when there is not entity with that path.
*
* @param path
* @return
* @throws Exception
*/
private Entity getEntity(final String path) throws Exception { private Entity getEntity(final String path) throws Exception {
final long t1 = System.currentTimeMillis(); final long t1 = System.currentTimeMillis();
final TransactionContainer c = new TransactionContainer(); final TransactionContainer c = new TransactionContainer();
...@@ -191,12 +205,7 @@ public class FileSystemResource extends AbstractCaosDBServerResource { ...@@ -191,12 +205,7 @@ public class FileSystemResource extends AbstractCaosDBServerResource {
e.setFileProperties(fp); e.setFileProperties(fp);
c.add(e); c.add(e);
final Transaction<?> t = new RetrieveSparseEntityByPath(c); final Transaction<?> t = new RetrieveSparseEntityByPath(c);
try {
t.execute(); t.execute();
} catch (EntityDoesNotExistException exception) {
// This file in the file system has no corresponding File record.
return null;
}
final long t2 = System.currentTimeMillis(); final long t2 = System.currentTimeMillis();
getBenchmark().addMeasurement(this.getClass().getSimpleName() + ".getEntity", t2 - t1); getBenchmark().addMeasurement(this.getClass().getSimpleName() + ".getEntity", t2 - t1);
return e; return e;
...@@ -204,19 +213,9 @@ public class FileSystemResource extends AbstractCaosDBServerResource { ...@@ -204,19 +213,9 @@ public class FileSystemResource extends AbstractCaosDBServerResource {
protected File getFile(final String path) throws Exception { protected File getFile(final String path) throws Exception {
final File ret = getFromFileSystem(path); final File ret = getFromFileSystem(path);
if (ret != null && ret.isFile()) {
checkPermissions(path);
}
return ret; return ret;
} }
private final void checkPermissions(final String path) throws Exception {
final long t1 = System.currentTimeMillis();
getEntity(path).checkPermission(EntityPermission.RETRIEVE_FILE);
final long t2 = System.currentTimeMillis();
getBenchmark().addMeasurement(this.getClass().getSimpleName() + ".checkPermissions", t2 - t1);
}
@Override @Override
protected Representation httpPostInChildClass(final Representation entity) protected Representation httpPostInChildClass(final Representation entity)
throws ConnectionException, JDOMException { throws ConnectionException, JDOMException {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment