use of io.milton.resource.MakeCollectionableResource in project lobcder by skoulouzis.
the class PutHandler method findOrCreateFolders.
private CollectionResource findOrCreateFolders(HttpManager manager, String host, Path path, Request request) throws NotAuthorizedException, ConflictException, BadRequestException {
if (path == null) {
return null;
}
Resource thisResource = manager.getResourceFactory().getResource(host, path.toString());
if (thisResource != null) {
// return the wrong resource for a given path
if (thisResource.getName() != null && !thisResource.getName().equals(path.getName())) {
log.warn("Your resource factory returned a resource with a different name to that requested!!! Requested: " + path.getName() + " returned: " + thisResource.getName() + " - resource factory: " + manager.getResourceFactory().getClass());
}
if (thisResource instanceof CollectionResource) {
return (CollectionResource) thisResource;
} else {
log.warn("parent is not a collection: " + path);
return null;
}
}
CollectionResource parent = findOrCreateFolders(manager, host, path.getParent(), request);
if (parent == null) {
log.warn("couldnt find parent: " + path);
// return null;
}
Resource r = parent.child(path.getName());
if (r == null) {
log.info("Could not find child: " + path.getName() + " in parent: " + parent.getName() + " - " + parent.getClass());
if (parent instanceof MakeCollectionableResource) {
MakeCollectionableResource mkcol = (MakeCollectionableResource) parent;
if (!handlerHelper.checkAuthorisation(manager, mkcol, request)) {
throw new NotAuthorizedException(mkcol);
}
log.info("autocreating new folder: " + path.getName());
CollectionResource newCol = mkcol.createCollection(path.getName());
manager.getEventManager().fireEvent(new NewFolderEvent(newCol));
return newCol;
} else {
log.info("parent folder isnt a MakeCollectionableResource: " + parent.getName() + " - " + parent.getClass());
return null;
}
} else if (r instanceof CollectionResource) {
return (CollectionResource) r;
} else {
log.info("parent in URL is not a collection: " + r.getName());
return null;
}
}
use of io.milton.resource.MakeCollectionableResource in project lobcder by skoulouzis.
the class FckQuickUploaderResource method processFileUpload.
private void processFileUpload(FileItem f, Map<String, String> params) throws BadRequestException, NotAuthorizedException {
CollectionResource target = null;
if (wrappedResource == null) {
throw new BadRequestException(this, "collection not found");
}
target = (CollectionResource) wrappedResource.child("uploads");
if (target == null) {
try {
if (wrappedResource instanceof MakeCollectionableResource) {
MakeCollectionableResource mk = (MakeCollectionableResource) wrappedResource;
target = mk.createCollection("uploads");
} else {
throw new BadRequestException(target, "Cant create subfolder");
}
} catch (ConflictException ex) {
throw new RuntimeException(ex);
} catch (NotAuthorizedException ex) {
throw new RuntimeException(ex);
} catch (BadRequestException ex) {
throw new RuntimeException(ex);
}
}
String name = FileUtils.sanitiseName(f.getName());
log.debug("processFileUpload: " + name);
boolean isFirst = true;
String newName = null;
while (target.child(name) != null) {
name = FileUtils.incrementFileName(name, isFirst);
newName = name;
isFirst = false;
}
long size = f.getSize();
try {
if (target instanceof PutableResource) {
PutableResource putable = (PutableResource) target;
Resource newRes = putable.createNew(name, f.getInputStream(), size, null);
if (newRes != null) {
log.trace("created: " + newRes.getName() + " of type: " + newRes.getClass());
} else {
log.trace("createNew returned null");
}
} else {
throw new BadRequestException(target, "Does not implement PutableResource");
}
} catch (ConflictException ex) {
throw new RuntimeException(ex);
} catch (NotAuthorizedException ex) {
throw new RuntimeException(ex);
} catch (BadRequestException ex) {
throw new RuntimeException(ex);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
try {
if (newName != null) {
// we renamed the file
uploadResponseOk(name);
} else {
uploadResponseOk();
}
} catch (Throwable ex) {
log.error("Exception saving new file", ex);
uploadResponseFailed(ex.getMessage());
}
}
use of io.milton.resource.MakeCollectionableResource in project lobcder by skoulouzis.
the class MkColHandler method processMakeCol.
private void processMakeCol(HttpManager manager, Request request, Response response, CollectionResource resource, String newName, CollectionResourceCreator creator) throws ConflictException, NotAuthorizedException, BadRequestException, IOException {
if (!handlerHelper.checkAuthorisation(manager, resource, request)) {
log.info("not authorised");
responseHandler.respondUnauthorised(resource, response, request);
return;
}
handlerHelper.checkExpects(responseHandler, request, response);
MakeCollectionableResource existingCol = (MakeCollectionableResource) resource;
if (!isCompatible(existingCol)) {
log.info("not compatible");
responseHandler.respondMethodNotImplemented(existingCol, response, request);
return;
}
Resource existingChild = existingCol.child(newName);
if (existingChild != null) {
log.warn("found already existing item: " + newName + " of type: " + existingChild.getClass() + " with actual name: " + existingChild.getName());
// throw new ConflictException( existingChild );
// See http://www.ettrema.com:8080/browse/MIL-86
// 405 (Method Not Allowed) - MKCOL can only be executed on a deleted/non-existent resource.
responseHandler.respondMethodNotAllowed(existingChild, response, request);
return;
}
CollectionResource made = creator.createResource(existingCol, newName, request);
if (made == null) {
log.warn("createCollection returned null. In resource class: " + existingCol.getClass());
response.setStatus(Response.Status.SC_METHOD_NOT_ALLOWED);
} else {
log.info("created item ok: " + made.getClass());
manager.getEventManager().fireEvent(new NewFolderEvent(resource));
response.setStatus(Response.Status.SC_CREATED);
}
}
Aggregations