package org.gcube.contentmanagement.blobstorage.service.directoryOperation; import java.net.UnknownHostException; import java.util.Iterator; import java.util.Map; import java.util.Set; import org.gcube.contentmanagement.blobstorage.resource.MyFile; import org.gcube.contentmanagement.blobstorage.resource.StorageObject; import org.gcube.contentmanagement.blobstorage.transport.TransportManager; import org.gcube.contentmanagement.blobstorage.transport.TransportManagerFactory; import org.gcube.contentmanagement.blobstorage.transport.backend.util.Costants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.mongodb.MongoException; /** * * @author Roberto Cirillo (ISTI - CNR) * */ public class DirectoryBucket { /** * Logger for this class */ final Logger logger=LoggerFactory.getLogger(DirectoryBucket.class); String author; String fileName; String path; String[] server; String user, password; TransportManager tm; public DirectoryBucket(String[] server, String user, String password, String path, String author){ if(logger.isDebugEnabled()) logger.debug("DirectoryBucket PATH: "+path); //coding the path this.path=path; this.author=author; this.server=server; this.user=user; this.password=password; } /** * generate the names of the upper tree directory buckets * @return The list of tree directory buckets: ex: if the path is /this/is/my/path/myFile.txt * the list will contains: /this, /this/is, /this/is/my, this/is/my/path */ public String[] retrieveBucketsName(String path, String rootArea){ if (logger.isDebugEnabled()) { logger.debug("retrieveBucketsName() - start"); } String pathCoded=new BucketCoding().mergingPathAndDir(rootArea, path); String[] splits=pathCoded.split(Costants.FILE_SEPARATOR); String[] buckets=new String[splits.length]; for(int i=0;i map=null; try { map = tm.getValues(resource, bucketList[i], DirectoryEntity.class); } catch (MongoException e) { tm.close(); e.printStackTrace(); } Set keys=map.keySet(); for(Iterator it=keys.iterator(); it.hasNext();){ String key=(String)it.next(); if(key.equalsIgnoreCase(bucketDirCoded)){ if(logger.isDebugEnabled()) logger.debug("key matched: "+key+" remove"); //recursively remove try { map=tm.getValues(resource, key, DirectoryEntity.class); } catch (MongoException e) { tm.close(); e.printStackTrace(); } keys=map.keySet(); for(Iterator it2=keys.iterator(); it2.hasNext();){ String key2=(String)it2.next(); if(logger.isDebugEnabled()) logger.debug("the bucket: "+key+" have a son: "+key2); if(bc.isFileObject(key2)){ if(logger.isDebugEnabled()){ logger.debug("remove "+key2+" in the bucket: "+key); } if(logger.isDebugEnabled()) logger.debug("remove all keys in the bucket: "+key2); try { tm.removeRemoteFile(key2, resource); } catch (UnknownHostException e) { tm.close(); e.printStackTrace(); } catch (MongoException e) { tm.close(); e.printStackTrace(); } }else{ if(logger.isDebugEnabled()) logger.debug(key2+" is a directory"); String bucketDecoded=bc.bucketDirDecoding(key2, rootArea); removeDirBucket(resource, bucketDecoded, rootArea, backendType, dbNames); } } if(logger.isDebugEnabled()) logger.debug("remove "+key+" in the bucket: "+bucketList[i]); if(logger.isDebugEnabled()) logger.debug("remove all keys in the bucket: "+key); try { tm.removeRemoteFile(key, resource); } catch (UnknownHostException e) { tm.close(); e.printStackTrace(); } catch (MongoException e) { tm.close(); e.printStackTrace(); } } } } } return bucketDirCoded; } /** * recursively search on directories buckets, return a key if found else return null * @param name fileName * @param bucketCoded bucketName coded * @param tm a client for the cluster */ public String searchInBucket(MyFile resource, String name, String bucketCoded, TransportManager tm, String rootArea) { Map dirs=null; try{ dirs=tm.getValues(resource, bucketCoded, DirectoryEntity.class); }catch(Exception e){ tm.close(); logger.info("object not found"); return null; } Set set=dirs.keySet(); for(Iterator it= set.iterator(); it.hasNext();){ String key=(String)it.next(); if(logger.isDebugEnabled()) logger.debug("try in "+key); String nameDecoded = new BucketCoding().bucketFileDecoding(key, rootArea); if(logger.isDebugEnabled()) logger.debug("name decoded: "+nameDecoded+" name searched is: "+name); if((nameDecoded!=null ) && (nameDecoded.equalsIgnoreCase(name))){ if(logger.isDebugEnabled()) logger.debug("FOUND in "+bucketCoded+" objectId returned: "+key); return key; }else{ searchInBucket(resource,name, key, tm, rootArea); } } return null; } }