use of io.milton.common.Path in project lobcder by skoulouzis.
the class WebDataDirResource method createAndLock.
/**
* This means to just lock the name Not to create the resource.
*
* @param name
* @param timeout
* @param lockInfo
* @return
* @throws NotAuthorizedException
*/
@Override
public LockToken createAndLock(String name, LockTimeout timeout, LockInfo lockInfo) throws NotAuthorizedException {
try (Connection connection = getCatalogue().getConnection()) {
Path newPath = Path.path(getPath(), name);
// If the resource exists
LogicalData fileLogicalData = getCatalogue().getLogicalDataByPath(newPath, connection);
if (fileLogicalData != null) {
throw new PreConditionFailedException(new WebDataFileResource(fileLogicalData, Path.path(getPath(), name), getCatalogue(), authList));
}
LockToken lockToken = new LockToken(UUID.randomUUID().toString(), lockInfo, timeout);
return lockToken;
} catch (SQLException | PreConditionFailedException ex) {
Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, ex);
if (ex instanceof PreConditionFailedException) {
throw new RuntimeException(ex);
}
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
use of io.milton.common.Path in project lobcder by skoulouzis.
the class LockHandler method processNonExistingResource.
private void processNonExistingResource(HttpManager manager, Request request, Response response, String host, String url) throws NotAuthorizedException, BadRequestException {
String name;
Path parentPath = Path.path(url);
name = parentPath.getName();
parentPath = parentPath.getParent();
url = parentPath.toString();
Resource r = manager.getResourceFactory().getResource(host, url);
if (r != null) {
if (!handlerHelper.checkAuthorisation(manager, r, request)) {
responseHandler.respondUnauthorised(r, response, request);
return;
} else {
processCreateAndLock(manager, request, response, r, name);
}
} else {
// log.debug("couldnt find parent to execute lock-null, returning not found");
// respondNotFound(response,request);
response.setStatus(Status.SC_CONFLICT);
}
}
use of io.milton.common.Path 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.common.Path in project lobcder by skoulouzis.
the class CopyJsonResource method processForm.
public String processForm(Map<String, String> parameters, Map<String, FileItem> files) throws BadRequestException, NotAuthorizedException {
String dest = parameters.get("destination");
Path pDest = Path.path(dest);
Resource rDestParent = resourceFactory.getResource(host, pDest.getParent().toString());
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.copyTo(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());
throw new BadRequestException(rDestParent, "File already exists");
}
} else {
throw new BadRequestException(wrapped, "The destination parent is not a collection resource");
}
}
use of io.milton.common.Path in project lobcder by skoulouzis.
the class ClassPathResourceFactory method getResource.
@Override
public Resource getResource(String host, String path) {
Path p = Path.path(path);
if (basePath != null) {
if (p.getFirst().equals(basePath)) {
p = p.getStripFirst();
} else {
return null;
}
}
// try to locate a resource with the given path
InputStream content = this.getClass().getResourceAsStream(p.toString());
if (content == null) {
return null;
} else {
log.trace("return class path resource");
return new ClassPathResource(host, p, content);
}
}
Aggregations