use of io.milton.resource.DeletableResource in project lobcder by skoulouzis.
the class DeleteHelperImpl method isLockedOut.
@Override
public boolean isLockedOut(Request req, Resource r) throws NotAuthorizedException, BadRequestException {
if (r instanceof DeletableCollectionResource) {
DeletableCollectionResource dcr = (DeletableCollectionResource) r;
boolean locked = dcr.isLockedOutRecursive(req);
if (locked && log.isInfoEnabled()) {
log.info("isLocked, as reported by DeletableCollectionResource: " + dcr.getName());
}
return locked;
} else if (r instanceof CollectionResource) {
CollectionResource col = (CollectionResource) r;
List<Resource> list = new ArrayList<Resource>();
list.addAll(col.getChildren());
for (Resource rChild : list) {
if (rChild instanceof DeletableResource) {
DeletableResource rChildDel = (DeletableResource) rChild;
if (isLockedOut(req, rChildDel)) {
if (log.isInfoEnabled()) {
log.info("isLocked: " + rChild.getName() + " type:" + rChild.getClass());
}
return true;
}
} else {
if (log.isInfoEnabled()) {
log.info("a child resource is not deletable: " + rChild.getName() + " type: " + rChild.getClass());
}
return true;
}
}
return false;
} else {
boolean locked = handlerHelper.isLockedOut(req, r);
if (locked && log.isInfoEnabled()) {
log.info("isLocked, as reported by handlerHelper on resource: " + r.getName());
}
return locked;
}
}
use of io.milton.resource.DeletableResource in project lobcder by skoulouzis.
the class CopyHandler method processExistingResource.
@Override
public void processExistingResource(HttpManager manager, Request request, Response response, Resource resource) throws NotAuthorizedException, BadRequestException, ConflictException {
CopyableResource r = (CopyableResource) resource;
Dest dest = Utils.getDecodedDestination(request.getDestinationHeader());
Resource rDest = manager.getResourceFactory().getResource(dest.host, dest.url);
log.debug("process: copying from: " + r.getName() + " -> " + dest.url + "/" + dest.name);
if (rDest == null) {
log.debug("process: destination parent does not exist: " + dest);
responseHandler.respondConflict(resource, response, request, "Destination does not exist: " + dest);
} else if (!(rDest instanceof CollectionResource)) {
log.debug("process: destination exists but is not a collection");
responseHandler.respondConflict(resource, response, request, "Destination exists but is not a collection: " + dest);
} else {
log.debug("process: copy resource to: " + rDest.getName());
Resource fDest = manager.getResourceFactory().getResource(dest.host, dest.url + "/" + dest.name);
if (handlerHelper.isLockedOut(request, fDest)) {
responseHandler.respondLocked(request, response, resource);
return;
} else {
boolean wasDeleted = false;
CollectionResource colDest = (CollectionResource) rDest;
Resource rExisting = colDest.child(dest.name);
if (rExisting != null) {
if (!canOverwrite(request)) {
// Exists, and overwrite = F, disallow - http://www.webdav.org/specs/rfc4918.html#rfc.section.9.8.4
log.info("destination resource exists, and overwrite header is not set. dest name: " + dest.name + " dest folder: " + colDest.getName());
responseHandler.respondPreconditionFailed(request, response, resource);
return;
} else {
// Overwrite is absent or T, so continue
if (deleteHelper.isLockedOut(request, rExisting)) {
log.info("destination resource exists, and overwrite header IS set, but destination is locked. dest name: " + dest.name + " dest folder: " + colDest.getName());
responseHandler.respondPreconditionFailed(request, response, resource);
return;
} else {
if (deleteExistingBeforeCopy) {
if (rExisting instanceof DeletableResource) {
log.debug("copy destination exists and is deletable, delete it..");
DeletableResource dr = (DeletableResource) rExisting;
// Check the user can delete
if (!handlerHelper.checkAuthorisation(manager, dr, request, Method.DELETE, request.getAuthorization())) {
responseHandler.respondUnauthorised(colDest, response, request);
return;
}
deleteHelper.delete(dr, manager.getEventManager());
wasDeleted = true;
} else {
log.warn("copy destination exists and is a collection so must be deleted, but does not implement: " + DeletableResource.class);
responseHandler.respondConflict(rExisting, response, request, dest.toString());
return;
}
}
}
}
}
// the resource identified in the dest header
if (!handlerHelper.checkAuthorisation(manager, colDest, request, request.getMethod(), request.getAuthorization())) {
responseHandler.respondUnauthorised(colDest, response, request);
return;
}
r.copyTo(colDest, dest.name);
// See http://www.ettrema.com:8080/browse/MIL-87
if (wasDeleted) {
responseHandler.respondNoContent(resource, response, request);
} else {
responseHandler.respondCreated(resource, response, request);
}
}
}
}
use of io.milton.resource.DeletableResource in project lobcder by skoulouzis.
the class MoveHandler method processExistingResource.
@Override
public void processExistingResource(HttpManager manager, Request request, Response response, Resource resource) throws NotAuthorizedException, BadRequestException, ConflictException {
MoveableResource r = (MoveableResource) resource;
Dest dest = Utils.getDecodedDestination(request.getDestinationHeader());
Resource rDest = manager.getResourceFactory().getResource(dest.host, dest.url);
log.debug("process: moving from: " + r.getName() + " -> " + dest.url + " with name: " + dest.name);
if (rDest == null) {
log.debug("process: destination parent does not exist: " + dest);
responseHandler.respondConflict(resource, response, request, "Destination parent does not exist: " + dest);
} else if (!(rDest instanceof CollectionResource)) {
log.debug("process: destination exists but is not a collection");
responseHandler.respondConflict(resource, response, request, "Destination exists but is not a collection: " + dest);
} else {
boolean wasDeleted = false;
CollectionResource colDest = (CollectionResource) rDest;
// check if the dest exists
Resource rExisting = colDest.child(dest.name);
if (rExisting != null) {
// check for overwrite header
if (!canOverwrite(request)) {
log.info("destination resource exists, and overwrite header is not set. dest name: " + dest.name + " dest folder: " + colDest.getName());
responseHandler.respondPreconditionFailed(request, response, rExisting);
return;
} else {
if (deleteExistingBeforeMove) {
if (rExisting instanceof DeletableResource) {
log.debug("deleting existing resource");
DeletableResource drExisting = (DeletableResource) rExisting;
if (deleteHelper.isLockedOut(request, drExisting)) {
log.debug("destination resource exists but is locked");
responseHandler.respondLocked(request, response, drExisting);
return;
}
log.debug("deleting pre-existing destination resource");
deleteHelper.delete(drExisting, manager.getEventManager());
wasDeleted = true;
} else {
log.warn("destination exists, and overwrite header is set, but destination is not a DeletableResource");
responseHandler.respondConflict(resource, response, request, "A resource exists at the destination, and it cannot be deleted");
return;
}
}
}
}
log.debug("process: moving resource to: " + rDest.getName());
try {
if (!handlerHelper.checkAuthorisation(manager, colDest, request, request.getMethod(), request.getAuthorization())) {
responseHandler.respondUnauthorised(colDest, response, request);
return;
}
manager.getEventManager().fireEvent(new MoveEvent(resource, colDest, dest.name));
r.moveTo(colDest, dest.name);
// See http://www.ettrema.com:8080/browse/MIL-87
if (wasDeleted) {
responseHandler.respondNoContent(resource, response, request);
} else {
responseHandler.respondCreated(resource, response, request);
}
} catch (ConflictException ex) {
log.warn("conflict", ex);
responseHandler.respondConflict(resource, response, request, dest.toString());
}
}
log.debug("process: finished");
}
use of io.milton.resource.DeletableResource in project lobcder by skoulouzis.
the class PutJsonResource method processForm.
@Override
public String processForm(Map<String, String> parameters, Map<String, FileItem> files) throws ConflictException, NotAuthorizedException, BadRequestException {
log.info("processForm: " + wrapped.getClass());
if (files.isEmpty()) {
log.debug("no files uploaded");
return null;
}
newFiles = new ArrayList<NewFile>();
for (FileItem file : files.values()) {
NewFile nf = new NewFile();
String ua = HttpManager.request().getUserAgentHeader();
String f = Utils.truncateFileName(ua, file.getName());
nf.setOriginalName(f);
nf.setContentType(file.getContentType());
nf.setLength(file.getSize());
newFiles.add(nf);
String newName = getName(f, parameters);
log.info("creating resource: " + newName + " size: " + file.getSize());
InputStream in = null;
Resource newResource;
try {
in = file.getInputStream();
Resource existing = wrapped.child(newName);
if (existing != null) {
if (existing instanceof ReplaceableResource) {
log.trace("existing resource is replaceable, so replace content");
ReplaceableResource rr = (ReplaceableResource) existing;
rr.replaceContent(in, null);
log.trace("completed POST processing for file. Updated: " + existing.getName());
eventManager.fireEvent(new PutEvent(rr));
newResource = rr;
} else {
log.trace("existing resource is not replaceable, will be deleted");
if (existing instanceof DeletableResource) {
DeletableResource dr = (DeletableResource) existing;
dr.delete();
newResource = wrapped.createNew(newName, in, file.getSize(), file.getContentType());
log.trace("completed POST processing for file. Deleted, then created: " + newResource.getName());
eventManager.fireEvent(new PutEvent(newResource));
} else {
throw new BadRequestException(existing, "existing resource could not be deleted, is not deletable");
}
}
} else {
newResource = wrapped.createNew(newName, in, file.getSize(), file.getContentType());
log.info("completed POST processing for file. Created: " + newResource.getName());
eventManager.fireEvent(new PutEvent(newResource));
}
String newHref = buildNewHref(href, newResource.getName());
nf.setHref(newHref);
} catch (NotAuthorizedException ex) {
throw new RuntimeException(ex);
} catch (BadRequestException ex) {
throw new RuntimeException(ex);
} catch (ConflictException ex) {
throw new RuntimeException(ex);
} catch (IOException ex) {
throw new RuntimeException("Exception creating resource", ex);
} finally {
FileUtils.close(in);
}
}
log.trace("completed all POST processing");
return null;
}
use of io.milton.resource.DeletableResource in project lobcder by skoulouzis.
the class DeleteHandler method processExistingResource.
@Override
public void processExistingResource(HttpManager manager, Request request, Response response, Resource resource) throws NotAuthorizedException, BadRequestException, ConflictException {
log.debug("DELETE: " + request.getAbsoluteUrl());
DeletableResource r = (DeletableResource) resource;
if (deleteHelper.isLockedOut(request, r)) {
log.info("Could not delete. Is locked");
responseHandler.respondDeleteFailed(request, response, r, Status.SC_LOCKED);
return;
}
deleteHelper.delete(r, manager.getEventManager());
log.debug("deleted ok");
responseHandler.respondNoContent(resource, response, request);
}
Aggregations