use of io.milton.resource.Resource in project lobcder by skoulouzis.
the class LockHandler method process.
@Override
public void process(HttpManager httpManager, Request request, Response response) throws ConflictException, NotAuthorizedException, BadRequestException, NotFoundException {
if (!handlerHelper.checkExpects(responseHandler, request, response)) {
return;
}
String host = request.getHostHeader();
String url = HttpManager.decodeUrl(request.getAbsolutePath());
Resource r = httpManager.getResourceFactory().getResource(host, url);
try {
// Find a resource if it exists
if (r != null) {
processExistingResource(httpManager, request, response, r);
} else {
processNonExistingResource(httpManager, request, response, host, url);
}
} catch (IOException ex) {
throw new BadRequestException(r);
} catch (SAXException ex) {
throw new BadRequestException(r);
} catch (LockedException ex) {
responseHandler.respondLocked(request, response, r);
} catch (PreConditionFailedException ex) {
responseHandler.respondPreconditionFailed(request, response, r);
}
}
use of io.milton.resource.Resource 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.Resource in project lobcder by skoulouzis.
the class MoveJsonResource method processForm.
@Override
public String processForm(Map<String, String> parameters, Map<String, FileItem> files) throws BadRequestException, NotAuthorizedException {
String dest = parameters.get("destination");
if (dest == null) {
throw new BadRequestException(this, "The destination parameter is null");
} else if (dest.trim().length() == 0) {
throw new BadRequestException(this, "The destination parameter is empty");
}
Path pDest = Path.path(dest);
if (pDest == null) {
throw new BadRequestException(this, "Couldnt parse the destination header, returned null from: " + dest);
}
String parentPath = "/";
if (pDest.getParent() != null) {
parentPath = pDest.getParent().toString();
}
Resource rDestParent = resourceFactory.getResource(host, parentPath);
if (rDestParent == null)
throw new BadRequestException(wrapped, "The destination parent does not exist");
if (rDestParent instanceof CollectionResource) {
CollectionResource colDestParent = (CollectionResource) rDestParent;
if (colDestParent.child(pDest.getName()) == null) {
try {
wrapped.moveTo(colDestParent, pDest.getName());
} catch (ConflictException ex) {
log.warn("Exception copying to: " + pDest.getName(), ex);
throw new BadRequestException(rDestParent, "conflict: " + ex.getMessage());
}
return null;
} else {
log.warn("destination already exists: " + pDest.getName() + " in folder: " + colDestParent.getName());
throw new BadRequestException(rDestParent, "File already exists");
}
} else {
throw new BadRequestException(wrapped, "The destination parent is not a collection resource");
}
}
use of io.milton.resource.Resource 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.Resource in project lobcder by skoulouzis.
the class DefaultPropFindPropertyBuilder method processResource.
@Override
public void processResource(List<PropFindResponse> responses, PropFindableResource resource, PropertiesRequest parseResult, String href, int requestedDepth, int currentDepth, String collectionHref) throws NotAuthorizedException, BadRequestException {
final LinkedHashMap<QName, ValueAndType> knownProperties = new LinkedHashMap<QName, ValueAndType>();
final ArrayList<NameAndError> unknownProperties = new ArrayList<NameAndError>();
if (resource instanceof CollectionResource) {
if (!href.endsWith("/")) {
href = href + "/";
}
}
Set<QName> requestedFields;
if (parseResult.isAllProp()) {
requestedFields = findAllProps(resource);
} else {
requestedFields = parseResult.getNames();
}
Iterator<QName> it = requestedFields.iterator();
while (it.hasNext()) {
QName field = it.next();
LogUtils.trace(log, "processResoource: find property:", field);
if (field.getLocalPart().equals("href")) {
knownProperties.put(field, new ValueAndType(href, String.class));
} else {
boolean found = false;
for (PropertySource source : propertySources) {
LogUtils.trace(log, "look for field", field, " in property source", source.getClass());
PropertyMetaData meta = source.getPropertyMetaData(field, resource);
if (meta != null && !meta.isUnknown()) {
Object val;
try {
val = source.getProperty(field, resource);
LogUtils.trace(log, "processResource: got value", val, "from source", source.getClass());
if (val == null) {
// null, but we still need type information to write it so use meta
knownProperties.put(field, new ValueAndType(val, meta.getValueType()));
} else {
// non-null, so use more robust class info
knownProperties.put(field, new ValueAndType(val, val.getClass()));
}
} catch (NotAuthorizedException ex) {
unknownProperties.add(new NameAndError(field, "Not authorised"));
}
found = true;
break;
}
}
if (!found) {
if (log.isDebugEnabled()) {
log.debug("property not found in any property source: " + field.toString());
}
unknownProperties.add(new NameAndError(field, null));
}
}
}
if (log.isDebugEnabled()) {
if (unknownProperties.size() > 0) {
log.debug("some properties could not be resolved. Listing property sources:");
for (PropertySource ps : propertySources) {
log.debug(" - " + ps.getClass().getCanonicalName());
}
}
}
// Map<Status, List<NameAndError>> errorProperties = new HashMap<Status, List<NameAndError>>();
Map<Status, List<NameAndError>> errorProperties = new EnumMap<Status, List<NameAndError>>(Status.class);
errorProperties.put(Status.SC_NOT_FOUND, unknownProperties);
PropFindResponse r = new PropFindResponse(href, knownProperties, errorProperties);
responses.add(r);
if (requestedDepth > currentDepth && resource instanceof CollectionResource) {
CollectionResource col = (CollectionResource) resource;
List<? extends Resource> list = col.getChildren();
list = new ArrayList<Resource>(list);
for (Resource child : list) {
if (child instanceof PropFindableResource) {
String childName = child.getName();
if (childName == null) {
log.warn("null name for resource of type: " + child.getClass() + " in folder: " + href + " WILL NOT be returned in PROPFIND response!!");
} else {
String childHref = href + Utils.percentEncode(childName);
// Note that the new collection href, is just the current href
processResource(responses, (PropFindableResource) child, parseResult, childHref, requestedDepth, currentDepth + 1, href);
}
}
}
}
}
Aggregations