package edu.sdsc.globusauth.action; import edu.sdsc.globusauth.util.OauthConstants; import org.apache.log4j.Logger; import org.globusonline.transfer.Authenticator; import org.globusonline.transfer.GoauthAuthenticator; import org.globusonline.transfer.JSONTransferAPIClient; import org.globusonline.transfer.BaseTransferAPIClient; import org.json.JSONArray; import org.json.JSONObject; import org.ngbw.web.actions.NgbwSupport; import java.util.HashMap; import java.util.Map; import java.util.List; import java.util.ArrayList; import edu.sdsc.globusauth.model.TreeNode; import edu.sdsc.globusauth.model.FileMetadata; /** * Created by cyoun on 11/09/17. * Updated by mzhuang */ // public class DynamicTreeAction extends NgbwSupport implements ServletContextAware { public class DynamicTreeAction extends NgbwSupport { private static final Logger logger = Logger.getLogger(DynamicTreeAction.class.getName()); private static final long serialVersionUID = -2886756982077980790L; private List nodes = new ArrayList(); private String id = ""; // private ServletContext servletContext; private JSONTransferAPIClient client; private static final String INBOUND = "inbound"; private static final String OUTBOUND = "outbound"; public DynamicTreeAction() { } public String file_tree() throws Exception { String accesstoken = (String) getSession().get(OauthConstants.CREDENTIALS); String username = (String) getSession().get(OauthConstants.PRIMARY_USERNAME); Authenticator authenticator = new GoauthAuthenticator(accesstoken); client = new JSONTransferAPIClient(username, null, null); client.setAuthenticator(authenticator); logger.info("accesstoken = : "+ accesstoken + " username = " + username); logger.info("Node ID: "+id); generateTree(id); return SUCCESS; } private List get_filelist(String endpointId, String path, String disp_name) throws Exception { List files = new ArrayList(); Map params = new HashMap(); if (path != null) { params.put("path", path); params.put("show_hidden","0"); } try { String resource = BaseTransferAPIClient.endpointPath(endpointId) + "/ls"; JSONTransferAPIClient.Result r = client.getResult(resource, params); //logger.info("Contents of " + path + " on " + endpointId + ":"); JSONArray fileArray = r.document.getJSONArray("DATA"); files = new ArrayList<>(); List session_files = new ArrayList<>(); List session_dirs = new ArrayList<>(); for (int i = 0; i < fileArray.length(); i++) { JSONObject fileObject = fileArray.getJSONObject(i); String f_name = fileObject.getString("name"); String f_type = fileObject.getString("type"); //logger.info(" " + f_name); //files.add(new FileMetadata(f_name, f_type, fileObject.getInt("size"))); files.add(new FileMetadata(f_name, f_type, Long.parseLong(fileObject.getString("size")))); } } catch (Exception e) { logger.error("Display file list: "+e.toString()); reportUserError("Error, unable to list files on the source endpoint ID, \""+disp_name+"\"."); return null; } return files; } private void generateTree(String nodeId) throws Exception { String s_epid = ((String) getSession().get(OauthConstants.SRC_ENDPOINT_ID)).trim(); String s_eppath = ((String) getSession().get(OauthConstants.SRC_ENDPOINT_PATH)).trim(); String s_dispname = ((String) getSession().get(OauthConstants.SRC_DISP_NAME)).trim(); String dsEndpiontID = ((String) getSession().get(OauthConstants.DATASET_ENDPOINT_ID)).trim(); if (s_epid.equals(dsEndpiontID) && nodeId.startsWith(INBOUND)) return; if (nodeId.equals("#")) { nodeId = ""; } else { String[] nodeIds = nodeId.split("\\|"); s_eppath = s_eppath + "/" + nodeIds[0]; nodeId = nodeIds[0] + "/"; } logger.info("dsEndpointID: " + dsEndpiontID); logger.info("SRC Endpoint ID: "+s_epid); logger.info("SRC Path: "+s_eppath); logger.info("nodeId = " + nodeId); List files = get_filelist(s_epid,s_eppath,s_dispname); if (files == null || files.size() <= 0) { logger.info("The returned file list for endpoint " + s_epid + " is null"); return; } for(FileMetadata fm:files) { //logger.info("file name = " + fm.getName()); if (s_epid.equals(dsEndpiontID) && fm.getName().equals(INBOUND)) continue; TreeNode node = new TreeNode(); //node.setId(nodeId + fm.getName()); node.getState().setOpened(false); if (fm.getType().equals("dir")) { String new_s_eppath = s_eppath + "/" + fm.getName(); logger.info("new_s_eppath = " + new_s_eppath); List newFiles = get_filelist(s_epid,new_s_eppath,s_dispname); String postFix = ""; if (newFiles == null || newFiles.size() <= 0) { postFix += "|-0"; //node.setId(nodeId + fm.getName()+"|-0"); node.setHasChildren(false); } else { postFix += "|0"; //node.setId(nodeId + fm.getName() + "|0"); node.setHasChildren(true); } node.setId(nodeId + fm.getName()+ postFix + getDirection(s_epid, dsEndpiontID)); //node.setId(nodeId + fm.getName()+"|0"); //node.setHasChildren(true); node.setIcon("folder"); node.setType("folder"); if (shouldFolderOpen(new_s_eppath)) node.getState().setOpened(true); } else { node.setId(nodeId + fm.getName()+ "|" + fm.getSize() + getDirection(s_epid, dsEndpiontID)); //node.getState().setDisabled(true); node.setIcon("file"); node.setType("file"); } StringBuilder sb = new StringBuilder(); sb.append(fm.getName()); sb.append("     ("); sb.append(humanReadableByteCount(fm.getSize(),true)); sb.append(")"); node.setText(sb.toString()); //node.setSize(""+fm.getSize()); nodes.add(node); } } private boolean shouldFolderOpen(String path) { if (path == null) return false; int outboundIdx = path.indexOf(OUTBOUND); if (outboundIdx == -1) return false; int lastIndex = outboundIdx; int count = 0; String findStr = "/"; while(lastIndex != -1){ lastIndex = path.indexOf(findStr,lastIndex); if(lastIndex != -1){ count ++; lastIndex += findStr.length(); } } if (count > 1) return false; return true; } private String getDirection(String s_epid, String dsEndpiontID) { String postFix = ""; if (s_epid.equals(dsEndpiontID)) postFix += "|" + OUTBOUND; else postFix += "|" + INBOUND; return postFix; } private TreeNode getNode(String nodeId) { TreeNode tn = null; if (nodes != null && nodes.size() > 0) { for (TreeNode n : nodes) { if (n.getId().equals(nodeId)) { tn = n; break; } } } return tn; } private String humanReadableByteCount(long bytes, boolean si) { int unit = si ? 1000 : 1024; if (bytes < unit) return bytes + " B"; int exp = (int) (Math.log(bytes) / Math.log(unit)); String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i"); return String.format("%.2f %sB", bytes / Math.pow(unit, exp), pre); } public String getJSON() throws Exception { return file_tree(); } public List getNodes() { return nodes; } public void setId(String id) { this.id = id; } // public void setServletContext(ServletContext servletContext) { // this.servletContext = servletContext; //} }