ContentService usage

Hey all,

I am working on a script that will utilize the ContentService class. In particular, the streamDirectory() method.

My usage is as follows:

def repo = repositoryService.getById(1930)
def callback = new AbstractContentTreeCallback()
contentService.streamDirectory(repo,"patch","files",false,callback,pageReq)

Syntax wise, this is correct. However I do not understand how to actually see the data that is returned. The API states that streamDirectory() calls methods for the AbstractContentTreeCallback object I pass in. The streamDirectory() method itself does not return anything.

I think I am not understanding the “callback” terminology in a Java sense. I’ve seen callbacks in javascript but not in Java.

Has anyone been successful with this class before?

1 Like

AbstractContentTreeCallback doesn’t actually do anything, you need to extend it. You might want to start by writing a subclass that calls System.out.println for each method, so you can understand when the methods will be called and what data they get passed.

1 Like

Ahhhhh that makes sense. Thanks Brent.

I will go ahead and try that out and report back.

EDIT: I got it. Added a few methods in my extended class to store data that was returned.

So, here is my updated code:

public class MyContentTreeCallback extends AbstractContentTreeCallback {
    
    ArrayList<String> files = new ArrayList<String>()
    Logger log;
    ContentTreeSummary fileSummary;
    public MyContentTreeCallback() {
        log = Logger.getLogger("com.onresolve.jira.groovy")
    log.setLevel(Level.DEBUG)
    }
    
    @Override
    public void onEnd(@Nonnull ContentTreeSummary summary) {
        log.debug(summary)
        fileSummary = summary
        log.debug("hello world")
    }
    
  @Override
    public void onStart(@Nonnull ContentTreeContext context) {
       log.debug(context)
       log.debug("hello world")
       
    }
    
  @Override
    public boolean onTreeNode(@Nonnull ContentTreeNode node) {
       
       String filePath = ""
       if(node.getPath().getComponents().size()>1){
            for(int i=0;i<node.getPath().getComponents().size();i++){
              filePath+=node.getPath().getComponents()[i]+"/"
                //filePath=filePath.substring(0,filePath.length() - 1)
          }
       } else {
           filePath+=node.getPath().getName()
       }
       
       String lastChar = filePath.charAt(filePath.length() - 1)
       if(lastChar.equals("/")){
           filePath=filePath.substring(0,filePath.length() - 1)
       }
       files.add(filePath)
       return true;
      
    }

    public ArrayList<String> getFiles(){
        return files
    }
    
}
//further down the file......
def repo = repositoryService.getById(1930)
MyContentTreeCallback callback = new MyContentTreeCallback()
contentService.streamDirectory(repo,"release/production","",true,callback,pageReq);

ArrayList<String> repoFiles = callback.getFiles()

Thanks @bplump

1 Like