use of io.milton.common.Path in project lobcder by skoulouzis.
the class WebDataResource method createResouses.
private Map<Long, Pair<WebDataFileResource, Long>> createResouses(Request request) throws SQLException, UnsupportedEncodingException, NotAuthorizedException, NoSuchAlgorithmException, IOException {
Map<Long, Pair<WebDataFileResource, Long>> resources = null;
try (Connection connection = getCatalogue().getConnection()) {
Map<String, FileItem> files = request.getFiles();
Collection<FileItem> fileItems = files.values();
resources = new HashMap<>();
WebDataFileResource resource = null;
for (FileItem fi : fileItems) {
Long pdriGroupid;
Path newPath = Path.path(getPath(), fi.getName());
LogicalData fileLogicalData = getCatalogue().getLogicalDataByPath(newPath, connection);
String contentType = mimeTypeMap.get(FilenameUtils.getExtension(fi.getName()));
if (fileLogicalData != null) {
Permissions p = getCatalogue().getPermissions(fileLogicalData.getUid(), fileLogicalData.getOwner(), connection);
if (!getPrincipal().canWrite(p)) {
throw new NotAuthorizedException(this);
}
fileLogicalData.setLength(fi.getSize());
fileLogicalData.setModifiedDate(System.currentTimeMillis());
fileLogicalData.setLastAccessDate(fileLogicalData.getModifiedDate());
fileLogicalData.addContentType(contentType);
pdriGroupid = fileLogicalData.getPdriGroupId();
resource = new WebDataFileResource(fileLogicalData, Path.path(getPath(), fi.getName()), getCatalogue(), authList);
} else {
fileLogicalData = new LogicalData();
fileLogicalData.setName(fi.getName());
fileLogicalData.setParentRef(getLogicalData().getUid());
fileLogicalData.setType(Constants.LOGICAL_FILE);
fileLogicalData.setOwner(getPrincipal().getUserId());
fileLogicalData.setLength(fi.getSize());
fileLogicalData.setCreateDate(System.currentTimeMillis());
fileLogicalData.setModifiedDate(System.currentTimeMillis());
fileLogicalData.setLastAccessDate(System.currentTimeMillis());
fileLogicalData.setTtlSec(getLogicalData().getTtlSec());
fileLogicalData.addContentType(contentType);
pdriGroupid = getCatalogue().associateLogicalDataAndPdriGroup(fileLogicalData, connection);
getCatalogue().setPreferencesOn(fileLogicalData.getUid(), getLogicalData().getUid(), connection);
List<String> pref = getLogicalData().getDataLocationPreferences();
fileLogicalData.setDataLocationPreferences(pref);
resource = new WebDataFileResource(fileLogicalData, Path.path(getPath(), fi.getName()), getCatalogue(), authList);
}
MutablePair<WebDataFileResource, Long> pair = new MutablePair<>();
pair.setRight(pdriGroupid);
pair.setLeft(resource);
resources.put(Long.valueOf(resource.getUniqueId()), pair);
}
connection.commit();
connection.close();
}
return resources;
}
use of io.milton.common.Path in project lobcder by skoulouzis.
the class WebDataResourceFactory method getResource.
@Override
public Resource getResource(String host, String strPath) {
if (strPath.equals("/login.html")) {
return null;
}
Path ldri = Path.path(strPath);
String first;
do {
first = ldri.getFirst();
ldri = ldri.getStripFirst();
} while (!first.equals("dav"));
// try (Connection cn = catalogue.getConnection()) {
try {
Logger.getLogger(WebDataResourceFactory.class.getName()).log(Level.FINER, "getResource: strPath: {0} path: {1} ldri: {2}" + "\n" + "\tgetResource: host: {3} path: {4}", new Object[] { strPath, Path.path(strPath), ldri, host, ldri });
// LogicalData entry = catalogue.getLogicalDataByPath(ldri, cn);
LogicalData entry = getCatalogue().getLogicalDataByPath(ldri);
if (entry == null) {
return null;
}
if (entry.getType().equals(Constants.LOGICAL_FOLDER)) {
return new WebDataDirResource(entry, ldri, getCatalogue(), getAuthList());
}
if (entry.getType().equals(Constants.LOGICAL_FILE)) {
return new WebDataFileResource(entry, ldri, getCatalogue(), getAuthList());
}
attempts = 0;
// return null;
} catch (SQLException ex) {
if (attempts <= Constants.RECONNECT_NTRY) {
attempts++;
getResource(host, strPath);
} else {
Logger.getLogger(WebDataResourceFactory.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException(ex);
}
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(WebDataResourceFactory.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
use of io.milton.common.Path in project lobcder by skoulouzis.
the class PutHandler method process.
@Override
public void process(HttpManager manager, Request request, Response response) throws NotAuthorizedException, ConflictException, BadRequestException, NotFoundException {
if (!handlerHelper.checkExpects(responseHandler, request, response)) {
return;
}
String host = request.getHostHeader();
String urlToCreateOrUpdate = HttpManager.decodeUrl(request.getAbsolutePath());
LogUtils.debug(log, "PUT request. Host:", host, " Url:", urlToCreateOrUpdate, " content length header:", request.getContentLengthHeader());
Path path = Path.path(urlToCreateOrUpdate);
urlToCreateOrUpdate = path.toString();
Resource existingResource = manager.getResourceFactory().getResource(host, urlToCreateOrUpdate);
StorageErrorReason storageErr = null;
if (existingResource != null) {
// Make sure the parent collection is not locked by someone else
if (handlerHelper.isLockedOut(request, existingResource)) {
log.warn("resource is locked, but not by the current user");
String value = request.getIfHeader();
if (value != null) {
response.setStatus(Status.SC_PRECONDITION_FAILED);
return;
} else {
respondLocked(request, response, existingResource);
return;
}
}
// Check if the resource has been modified based on etags
if (!matchHelper.checkIfMatch(existingResource, request)) {
log.info("if-match comparison failed, aborting PUT request");
responseHandler.respondPreconditionFailed(request, response, existingResource);
return;
}
if (matchHelper.checkIfNoneMatch(existingResource, request)) {
log.info("if-none-match comparison failed, aborting PUT request");
responseHandler.respondPreconditionFailed(request, response, existingResource);
return;
}
Resource parent = manager.getResourceFactory().getResource(host, path.getParent().toString());
// }
if (parent instanceof CollectionResource) {
CollectionResource parentCol = (CollectionResource) parent;
storageErr = handlerHelper.checkStorageOnReplace(request, parentCol, existingResource, host);
} else {
log.warn("parent exists but is not a collection resource: " + path.getParent());
}
} else {
if (!matchHelper.checkIfMatch(null, request)) {
log.info("if-match comparison failed on null resource, aborting PUT request");
responseHandler.respondPreconditionFailed(request, response, existingResource);
return;
}
if (matchHelper.checkIfNoneMatch(null, request)) {
log.info("if-none-match comparison failed on null resource, aborting PUT request");
responseHandler.respondPreconditionFailed(request, response, existingResource);
return;
}
CollectionResource parentCol = putHelper.findNearestParent(manager, host, path);
storageErr = handlerHelper.checkStorageOnAdd(request, parentCol, path.getParent(), host);
}
if (storageErr != null) {
respondInsufficientStorage(request, response, storageErr);
return;
}
ReplaceableResource replacee;
if (existingResource != null && existingResource instanceof ReplaceableResource) {
replacee = (ReplaceableResource) existingResource;
} else {
replacee = null;
}
if (replacee != null) {
if (log.isTraceEnabled()) {
log.trace("replacing content in: " + replacee.getName() + " - " + replacee.getClass());
}
long t = System.currentTimeMillis();
try {
manager.onProcessResourceStart(request, response, replacee);
processReplace(manager, request, response, replacee);
manager.getEventManager().fireEvent(new PutEvent(replacee));
} finally {
t = System.currentTimeMillis() - t;
manager.onProcessResourceFinish(request, response, replacee, t);
}
} else {
// either no existing resource, or its not replaceable. check for folder
String nameToCreate = path.getName();
CollectionResource folderResource = findOrCreateFolders(manager, host, path.getParent(), request);
if (folderResource != null) {
long t = System.currentTimeMillis();
try {
if (folderResource instanceof PutableResource) {
// Make sure the parent collection is not locked by someone else
if (handlerHelper.isLockedOut(request, folderResource)) {
respondLocked(request, response, folderResource);
return;
}
PutableResource putableResource = (PutableResource) folderResource;
processCreate(manager, request, response, putableResource, nameToCreate);
} else {
LogUtils.debug(log, "method not implemented: PUT on class: ", folderResource.getClass(), folderResource.getName());
manager.getResponseHandler().respondMethodNotImplemented(folderResource, response, request);
}
} finally {
t = System.currentTimeMillis() - t;
manager.onProcessResourceFinish(request, response, folderResource, t);
}
} else {
responseHandler.respondNotFound(response, request);
}
}
}
use of io.milton.common.Path in project lobcder by skoulouzis.
the class PutHandler method processExistingResource.
public void processExistingResource(HttpManager manager, Request request, Response response, Resource resource) throws NotAuthorizedException, BadRequestException, ConflictException, NotFoundException {
String host = request.getHostHeader();
String urlToCreateOrUpdate = HttpManager.decodeUrl(request.getAbsolutePath());
log.debug("process request: host: " + host + " url: " + urlToCreateOrUpdate);
Path path = Path.path(urlToCreateOrUpdate);
urlToCreateOrUpdate = path.toString();
Resource existingResource = manager.getResourceFactory().getResource(host, urlToCreateOrUpdate);
ReplaceableResource replacee;
if (existingResource != null) {
// Make sure the parent collection is not locked by someone else
if (handlerHelper.isLockedOut(request, existingResource)) {
log.warn("resource is locked, but not by the current user");
// 423
response.setStatus(Status.SC_LOCKED);
return;
}
}
if (existingResource != null && existingResource instanceof ReplaceableResource) {
replacee = (ReplaceableResource) existingResource;
} else {
replacee = null;
}
if (replacee != null) {
processReplace(manager, request, response, (ReplaceableResource) existingResource);
} else {
// either no existing resource, or its not replaceable. check for folder
String urlFolder = path.getParent().toString();
String nameToCreate = path.getName();
CollectionResource folderResource = findOrCreateFolders(manager, host, path.getParent(), request);
if (folderResource != null) {
if (log.isDebugEnabled()) {
log.debug("found folder: " + urlFolder + " - " + folderResource.getClass());
}
if (folderResource instanceof PutableResource) {
// Make sure the parent collection is not locked by someone else
if (handlerHelper.isLockedOut(request, folderResource)) {
// 423
response.setStatus(Status.SC_LOCKED);
return;
}
PutableResource putableResource = (PutableResource) folderResource;
processCreate(manager, request, response, putableResource, nameToCreate);
} else {
responseHandler.respondMethodNotImplemented(folderResource, response, request);
}
} else {
responseHandler.respondNotFound(response, request);
}
}
}
use of io.milton.common.Path in project lobcder by skoulouzis.
the class FileSystemResourceFactory method resolvePath.
public File resolvePath(File root, String url) {
Path path = Path.path(url);
File f = root;
for (String s : path.getParts()) {
f = new File(f, s);
}
return f;
}
Aggregations