/** * Get the container which security callback affects this item. This searches up the path of * parents to see wether it can find any container with a callback. If no callback can be found, * null is returned. * * @param vfsItem * @return */ public static VFSContainer findInheritingSecurityCallbackContainer(VFSItem vfsItem) { if (vfsItem == null) return null; // first resolve delegates of any NamedContainers to get the actual container (might be a // MergeSource) if (vfsItem instanceof NamedContainerImpl) return findInheritingSecurityCallbackContainer(((NamedContainerImpl) vfsItem).getDelegate()); // special treatment for MergeSource if (vfsItem instanceof MergeSource) { MergeSource mergeSource = (MergeSource) vfsItem; VFSContainer rootWriteContainer = mergeSource.getRootWriteContainer(); if (rootWriteContainer != null && rootWriteContainer.getLocalSecurityCallback() != null) { // if the root write container has a security callback set, it will always override // any local securitycallback set on the mergesource return rootWriteContainer; } else if (mergeSource.getLocalSecurityCallback() != null) { return mergeSource; } else if (mergeSource.getParentContainer() != null) { return findInheritingSecurityCallbackContainer(mergeSource.getParentContainer()); } } else { if ((vfsItem instanceof VFSContainer) && (vfsItem.getLocalSecurityCallback() != null)) return (VFSContainer) vfsItem; if (vfsItem.getParentContainer() != null) return findInheritingSecurityCallbackContainer(vfsItem.getParentContainer()); } return null; }
/** * Check if the file exist or not * * @param item * @return */ public static boolean exists(VFSItem item) { if (item instanceof NamedContainerImpl) { item = ((NamedContainerImpl) item).getDelegate(); } if (item instanceof MergeSource) { MergeSource source = (MergeSource) item; item = source.getRootWriteContainer(); if (item == null) { // no write container, but the virtual container exist return true; } } if (item instanceof LocalImpl) { LocalImpl localFile = (LocalImpl) item; return localFile.getBasefile() != null && localFile.getBasefile().exists(); } return false; }
private static ContainerAndFile findWritableRootFolderForRecursion( VFSContainer rootDir, String relFilePath, int recursionLevel) { recursionLevel++; if (recursionLevel > 20) { // Emergency exit condition: a directory hierarchy that has more than 20 // levels? Probably not.. log.warn( "Reached recursion level while finding writable root Folder - most likely a bug. rootDir::" + rootDir + " relFilePath::" + relFilePath); return null; } if (rootDir instanceof NamedContainerImpl) { rootDir = ((NamedContainerImpl) rootDir).getDelegate(); } if (rootDir instanceof MergeSource) { MergeSource mergedDir = (MergeSource) rootDir; // first check if the next level is not a second MergeSource int stop = relFilePath.indexOf("/", 1); if (stop > 0) { String nextLevel = extractChild(relFilePath); VFSItem item = mergedDir.resolve(nextLevel); if (item instanceof NamedContainerImpl) { item = ((NamedContainerImpl) item).getDelegate(); } if (item instanceof MergeSource) { rootDir = (MergeSource) item; relFilePath = relFilePath.substring(stop); return findWritableRootFolderForRecursion(rootDir, relFilePath, recursionLevel); } } VFSContainer rootWriteContainer = mergedDir.getRootWriteContainer(); if (rootWriteContainer == null) { // we have a merge source without a write container, try it one higher, // go through all children of this one and search the correct child in // the path List<VFSItem> children = rootDir.getItems(); if (children.isEmpty()) { // ups, a merge source without children, no good, return null return null; } String nextChildName = relFilePath.substring(1, relFilePath.indexOf("/", 1)); for (VFSItem child : children) { // look up for the next child in the path if (child.getName().equals(nextChildName)) { // use this child as new root and remove the child name from the rel // path if (child instanceof VFSContainer) { rootDir = (VFSContainer) child; relFilePath = relFilePath.substring(relFilePath.indexOf("/", 1)); break; } else { // ups, a merge source with a child that is not a VFSContainer - // no good, return null return null; } } } } else { // ok, we found a merge source with a write container rootDir = rootWriteContainer; } } if (rootDir != null && rootDir instanceof LocalFolderImpl) { // finished, we found a local folder we can use to write return new ContainerAndFile(rootDir, relFilePath); } else { // do recursion return findWritableRootFolderForRecursion(rootDir, relFilePath, recursionLevel); } }