package org.gcube.data.access.storagehub.fs; import java.awt.Container; import java.nio.file.Paths; import java.util.List; import org.cache2k.Cache; import org.gcube.common.storagehub.client.dsl.FolderContainer; import org.gcube.common.storagehub.client.dsl.ItemContainer; import org.gcube.common.storagehub.client.dsl.StorageHubClient; import org.gcube.common.storagehub.model.items.Item; import org.gcube.common.storagehub.model.items.SharedFolder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class PathUtils { public static Logger logger = LoggerFactory.getLogger(PathUtils.class); private Cache> cache; private FolderContainer rootDirectory; private StorageHubClient client; public PathUtils(Cache> cache, FolderContainer rootDirectory, StorageHubClient client) { super(); this.cache = cache; this.rootDirectory = rootDirectory; this.client = client; } public String getLastComponent(String path) { while (path.substring(path.length() - 1).equals("/")) { path = path.substring(0, path.length() - 1); } if (path.isEmpty()) { return ""; } return path.substring(path.lastIndexOf("/") + 1); } public String getParentPath(String path) { return Paths.get(path).getParent().toString(); } public ItemContainer getPath(String path) { if (path.equals("/")) return rootDirectory; if (cache.containsKey(path)) { ItemContainer cached = cache.peek(path); logger.trace("path "+path+" retrieved in cache with id "+cached.getId()); return cached; } else logger.trace("path "+path+" not in cache"); synchronized (this) { ItemContainer retrievedItem = getByPath(path, rootDirectory); if (retrievedItem!=null) { cache.put(path, (ItemContainer) retrievedItem); logger.trace("retrieved container is of type {}",retrievedItem.getType()); } return retrievedItem; } } public ItemContainer getByPath(String path, FolderContainer parentContainer) { try { if (path.startsWith("/")) { path = path.substring(1); } if (path.startsWith(StorageHubFS.VREFOLDERS_NAME)) { List> vreFolders = client.getVREFolders().getContainers(); String vreName = path.split("/")[1]; for (ItemContainer vreContainer : vreFolders) { SharedFolder veFolder = (SharedFolder)vreContainer.get(); if (veFolder.getDisplayName().equals(vreName)) { String nextPath = path.replace(StorageHubFS.VREFOLDERS_NAME+"/"+veFolder.getDisplayName(), ""); if(nextPath.isEmpty()) return vreContainer; else return ((FolderContainer)vreContainer).openByRelativePath(nextPath).resolve(); } } } else return parentContainer.openByRelativePath(path).resolve(); }catch(Exception e) { logger.error("error in gpath recursive",e); } return null; } }