Get file(s) content from a pull event in ScmRequestCheck

Hello,

I am working on developing a plugin that allows me approve or deny events on the repository that come from pull, fetch, or clone operations. The ScmRequestCheck provides me with a

public boolean check( @Nonnull ScmRequest scmRequest ) throws IOException

where I can check whether the scmRequest is a write or a read from the repository.

So far I have the following

public class PullEventHandler implements ScmRequestCheck {

   private final RefService refService;
   private final ContentService contentService;

   class ContentTreeCallBackImpl extends AbstractContentTreeCallback {

      List<String> files = new ArrayList<>();
      ContentTreeSummary fileSummary;


      @Override
      public boolean onTreeNode( @Nonnull ContentTreeNode node ) throws IOException {

         System.out.println( "1. Name of Node: " + node.getPath() );
         files.add( node.getPath().toString() );
         return super.onTreeNode( node );
      }


      public List<String> getFiles() {
         return files;
      }

   }


   public PullEventHandler( @ComponentImport RefService refService,
      @ComponentImport ContentService contentService ) {
      this.refService = refService;
      this.contentService = contentService;
   }


   @Override
   public boolean check( @Nonnull ScmRequest scmRequest ) throws IOException {

      if ( scmRequest.isWrite() ) {
         return true;
      }
      return verifyFilesInRequest( scmRequest );
   }


   private boolean verifyFilesInRequest( ScmRequest scmRequest ) {
      RepositoryBranchesRequest repositoryBranchesRequest = new RepositoryBranchesRequest.Builder( scmRequest.
         getRepository() ).build();
      PageRequest pageRequest = new PageRequestImpl();
      Page<Branch> branches = refService.getBranches( repositoryBranchesRequest, pageRequest );

      Repository repo = scmRequest.getRepository();
      AbstractContentTreeCallback callBack = new ContentTreeCallBackImpl();

      branches.stream().forEach( branch -> contentService.streamDirectory( repo, branch.getId(), "", true,
         callBack, pageRequest ) );

      File logFile = new File( "C:\\Users\\<user>\\Documents\\Dev\\atlassian\\log.txt" );
      FileOutputStream fos = null;
      try {
         fos = new FileOutputStream( logFile );
      } catch ( FileNotFoundException ex ) {
         System.out.println( ex.getLocalizedMessage() );
      }
      for ( String path : ( ( ContentTreeCallBackImpl ) callBack ).getFiles() ) {
         try {

            byte[] buff = new byte[4096];
            FileInputStream fis = new FileInputStream( new File( path ) );
            int len;
            while ( ( len = ( fis.read() ) ) > 0 ) {
               fis.read( buff, 0, len );
            }
            fos.write( buff );

         } catch ( Exception e ) {
            System.out.println( e.getLocalizedMessage() );
         }
      }
      try {
         fos.close();
      } catch ( IOException ex ) {
         System.out.println( ex.getLocalizedMessage() );
      }
      return true;
   }
}

when I execute my code above on the test server, I receieve the following errors:

[INFO] An error occurred while executing an external process: add_file\add_file.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: basic_branching\file.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: binary\B.zip (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: binary\C.zip (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: binary\D.zip (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: branch_mod_merge\file.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: modification\mod_file.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: moved_dir\file.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: mv_file\moved_file.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: out_of_order\file.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: symlink\link.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: symlink\target.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: add_file\add_file.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: basic_branching\file.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: branch_mod_merge\file.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: modification\mod_file.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: moved_dir\file.txt (The system cannot find the path specified)
[INFO] 2019-08-02 10:57:16,553 WARN [Caesium-1-3] c.a.a.c.h.r.RemoteHashingInstructionsReader Unable to read remote instructions with key ‘uid.onewayhash’.
[INFO] 2019-08-02 10:57:16,553 WARN [Caesium-1-3] c.a.a.c.h.BcryptAnalyticsEmailHasher No instructions for hashing could be found.
[INFO] An error occurred while executing an external process: mv_file\moved_file.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: out_of_order\file.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: symlink\link.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: symlink\target.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: add_file\add_file.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: basic_branching\file.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: modification\mod_file.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: moved_dir\file.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: mv_file\moved_file.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: out_of_order\file.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: symlink\link.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: symlink\target.txt (The system cannot find the path specified)
[INFO] An error occurred while executing an external process: out_of_order\file.txt (The system cannot find the path specified)

I have been at this for a few days now and I cant find anything specific to my case.

Any help is appreciated.

I have also tried the code snippet below:

public class PullEventHandler implements ScmRequestCheck {

   private final RefService refService;
   private final ContentService contentService;

   class ContentTreeCallBackImpl extends AbstractContentTreeCallback {

      List<ContentTreeNode> nodes = new ArrayList<>();
      ContentTreeSummary fileSummary;


      @Override
      public boolean onTreeNode( @Nonnull ContentTreeNode node ) throws IOException {

         System.out.println( "1. Name of Node: " + node.getPath() );
         nodes.add( node );
         return super.onTreeNode( node );
      }


      public List<ContentTreeNode> getNodes() {
         return nodes;
      }

   }


   public PullEventHandler( @ComponentImport RefService refService,
      @ComponentImport ContentService contentService ) {
      this.refService = refService;
      this.contentService = contentService;
   }


   @Override
   public boolean check( @Nonnull ScmRequest scmRequest ) throws IOException {

      if ( scmRequest.isWrite() ) {
         return true;
      }
      return verifyFilesInRequest( scmRequest );
   }


   private boolean verifyFilesInRequest( ScmRequest scmRequest ) {
      RepositoryBranchesRequest repositoryBranchesRequest = new RepositoryBranchesRequest.Builder( scmRequest.
         getRepository() ).build();
      PageRequest pageRequest = new PageRequestImpl();
      Page<Branch> branches = refService.getBranches( repositoryBranchesRequest, pageRequest );

      Repository repo = scmRequest.getRepository();
      AbstractContentTreeCallback callBack = new ContentTreeCallBackImpl();
      List<String> branchIds = new ArrayList<>();

      branches.stream().forEach( branch -> contentService.streamDirectory( repo, branch.getId(), "", true,
         callBack, pageRequest ) );

      branches.stream().forEach( branch -> branchIds.add( branch.getId() ) );

      for ( ContentTreeNode node : ( ( ContentTreeCallBackImpl ) callBack ).getNodes() ) {
         try {
            Map<String, FileOutputStream> mapOfStreams = new HashMap<>();
            contentService.streamFile( repo, node.getContentId(), node.getPath().toString(),
               new TypeAwareOutputSupplier() {
               @Override
               public OutputStream getStream( String string ) throws IOException {
                  FileOutputStream fos = new FileOutputStream( new File( node.getPath().toString() ) );
                  mapOfStreams.put( node.getPath().toString(), fos );
                  System.out.println( "File: " + node.getPath().toString() + " at " + fos.toString() );
                  return fos;
               }
            } );

         } catch ( Exception e ) {
            System.out.println( e.getLocalizedMessage() );
         }
      }

      return true;
   }
}

But I get a similar buy slightly different set of errors:

[INFO] The path “add_file/add_file.txt” does not exist at revision “493021b1c9e0ecfebcd5ebce646a44138bc60b39”
[INFO] The path “basic_branching/file.txt” does not exist at revision “d8c00a7fe6eb6a00378e103a46e3c472c084a949”
[INFO] The path “binary/B.zip” does not exist at revision “927b01c060cf0dc5036e19abeab8e332d9075ea0”
[INFO] The path “binary/C.zip” does not exist at revision “72555d02a139700be930a6734b57c5fab84512b9”
[INFO] The path “binary/D.zip” does not exist at revision “72555d02a139700be930a6734b57c5fab84512b9”
[INFO] The path “branch_mod_merge/file.txt” does not exist at revision “668076c308990933c2df66efb87b946704252486”
[INFO] The path “modification/mod_file.txt” does not exist at revision “b1d99e581f0bb8138b8997f9268294e41faee05b”
[INFO] The path “moved_dir/file.txt” does not exist at revision “46fa903c8af8a92c2714a47ae5a341a99c2ee3b2”
[INFO] The path “mv_file/moved_file.txt” does not exist at revision “f49abb7d6c366e6f83470e36439bc162234d7963”
[INFO] The path “out_of_order/file.txt” does not exist at revision “2e6b03cbbe90d3c9afcfc98967a5a71b4c9cff1a”
[INFO] The path “symlink/link.txt” does not exist at revision “4cbb553f3f4ac2ee7b01ff6c951d6bf583c39c15”
[INFO] The path “symlink/target.txt” does not exist at revision “57a3592bb2cf8e76e2faef7660945a2fe0e24360”
[INFO] The path “add_file/add_file.txt” does not exist at revision “493021b1c9e0ecfebcd5ebce646a44138bc60b39”
[INFO] The path “basic_branching/file.txt” does not exist at revision “d8c00a7fe6eb6a00378e103a46e3c472c084a949”
[INFO] The path “branch_mod_merge/file.txt” does not exist at revision “668076c308990933c2df66efb87b946704252486”
[INFO] The path “modification/mod_file.txt” does not exist at revision “b1d99e581f0bb8138b8997f9268294e41faee05b”
[INFO] The path “moved_dir/file.txt” does not exist at revision “46fa903c8af8a92c2714a47ae5a341a99c2ee3b2”
[INFO] The path “mv_file/moved_file.txt” does not exist at revision “f49abb7d6c366e6f83470e36439bc162234d7963”
[INFO] The path “out_of_order/file.txt” does not exist at revision “2e6b03cbbe90d3c9afcfc98967a5a71b4c9cff1a”
[INFO] The path “symlink/link.txt” does not exist at revision “4cbb553f3f4ac2ee7b01ff6c951d6bf583c39c15”
[INFO] The path “symlink/target.txt” does not exist at revision “57a3592bb2cf8e76e2faef7660945a2fe0e24360”
[INFO] The path “add_file/add_file.txt” does not exist at revision “493021b1c9e0ecfebcd5ebce646a44138bc60b39”
[INFO] The path “basic_branching/file.txt” does not exist at revision “94efe92828e29523d68baaff145f81a680bacad4”
[INFO] The path “modification/mod_file.txt” does not exist at revision “b1d99e581f0bb8138b8997f9268294e41faee05b”
[INFO] The path “moved_dir/file.txt” does not exist at revision “46fa903c8af8a92c2714a47ae5a341a99c2ee3b2”
[INFO] The path “mv_file/moved_file.txt” does not exist at revision “f49abb7d6c366e6f83470e36439bc162234d7963”
[INFO] The path “out_of_order/file.txt” does not exist at revision “2e6b03cbbe90d3c9afcfc98967a5a71b4c9cff1a”
[INFO] The path “symlink/link.txt” does not exist at revision “4cbb553f3f4ac2ee7b01ff6c951d6bf583c39c15”
[INFO] The path “symlink/target.txt” does not exist at revision “57a3592bb2cf8e76e2faef7660945a2fe0e24360”
[INFO] The path “out_of_order/file.txt” does not exist at revision “2e6b03cbbe90d3c9afcfc98967a5a71b4c9cff1a”