Apache camel Apache Camel检查目录读取权限(AWS环境)

Apache camel Apache Camel检查目录读取权限(AWS环境),apache-camel,Apache Camel,我有一个验证FTP目录的API,如下所示: public boolean validateDirectory(FtpLocation ftpLocation) throws CustomException{ FTPClient client = new FTPClient(); try { client.connect(ftpLocation.getSystem(), Integer.parseInt(ftpLocation.getPort()));

我有一个验证FTP目录的API,如下所示:

public boolean validateDirectory(FtpLocation ftpLocation) throws CustomException{
    FTPClient client = new FTPClient();
    try {
        client.connect(ftpLocation.getSystem(), Integer.parseInt(ftpLocation.getPort()));
        client.login(ftpLocation.getUser(), ftpLocation.getPassword());
        client.changeWorkingDirectory(ftpLocation.getDirPath());
        int returnCode = client.getReplyCode();
        if (returnCode == 550) {
            LOG.error("Directory ["+ ftpLocation.getDirPath() + "] does not exist");
            return false;
        }           
        File dir = new File(ftpLocation.getDirPath());
        if(dir!=null){
            if (!dir.canRead()) {
                LOG.error("Directory ["+ ftpLocation.getDirPath() + "] does not have read permission");
                return false;
            }
        }
        return true;
    } catch (NumberFormatException | IOException e) {
        LOG.error("Failed to validate Source Directory Path [" + ftpLocation.getDirPath() + "]", e);
        throw new CustomException("Failed to validate Source Directory Path [" + ftpLocation.getDirPath() + "]", e);
    } finally {
        try {
            client.disconnect();
        } catch (IOException e) {
            LOG.warn("Error occured while disconnecting FTPClient", e);
        }
    }   
}
public void validateDirectory(FtpLocation ftpLocation) throws CustomException {
    FTPClient client = new FTPClient();
    try {
        client.connect(ftpLocation.getSystem(), Integer.parseInt(ftpLocation.getPort()));
        client.login(ftpLocation.getUser(), ftpLocation.getPassword());
        client.enterLocalPassiveMode();
        client.changeWorkingDirectory(ftpLocation.getDirPath());
        int returnCode = client.getReplyCode();
        LOG.debug("FTP return code: "+ returnCode);
        if (returnCode == 550) {
            LOG.error("Directory ["+ ftpLocation.getDirPath() + "] does not exist");
            throw new CustomException("Directory ["+ ftpLocation.getDirPath() + "] does not exist");
        }
        String targetDir = client.printWorkingDirectory();
        client.cdup();
        targetDir = StringUtils.substring(targetDir, StringUtils.lastIndexOf(targetDir, File.separator) + 1);
        FTPFile ftpFiles[] = client.listFiles();
        for(FTPFile ftpFile : ftpFiles){
            if(StringUtils.equals(ftpFile.getName(), targetDir) && !ftpFile.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION)){
                LOG.error("No read permission for directory [" + ftpLocation.getDirPath() + "]");
                throw new CustomException("No read permission for directory [" + ftpLocation.getDirPath() + "]");
            }
        }
    } catch (NumberFormatException | IOException e) {
        LOG.error("Failed to validate Source Directory Path [" + ftpLocation.getDirPath() + "]", e);
        throw new CustomException("Failed to validate Source Directory Path [" + ftpLocation.getDirPath() + "]", e);
    } finally {
        try {
            client.disconnect();
        } catch (IOException e) {
            LOG.warn("Error occured while disconnecting FTPClient", e);
        }
    }   
}
我的FTP根目录是/(绝对路径/home/soumya)

我想从/source路由文件(绝对路径/home/soumya/source)

我在测试API时发现了以下行为:

  • 如果源具有权限RWXR-x,则抛出验证消息(实际上期望成功的文件路由)
  • 如果源具有权限-wx wx--x,则验证成功
  • 如果源不存在,则验证成功
  • 如果源不是目录(源是文件),则验证成功
  • 以上四种情况的日志如下:

    07:41:59.799 [pool-2-thread-1] ERROR c.n.d.m.b.f.Validator - Directory [/source] does not have read permission
    
    07:42:48.801 [pool-2-thread-1] ERROR c.n.d.m.b.f.Validator - Directory [/source] does not have read permission
    
    07:43:27.093 [pool-2-thread-1] ERROR c.n.d.m.b.f.Validator - Directory [/source] does not exist
    
    07:44:00.215 [pool-2-thread-1] ERROR c.n.d.m.b.f.Validator - Directory [/source] does not exist
    
    问题:

    07:41:59.799 [pool-2-thread-1] ERROR c.n.d.m.b.f.Validator - Directory [/source] does not have read permission
    
    07:42:48.801 [pool-2-thread-1] ERROR c.n.d.m.b.f.Validator - Directory [/source] does not have read permission
    
    07:43:27.093 [pool-2-thread-1] ERROR c.n.d.m.b.f.Validator - Directory [/source] does not exist
    
    07:44:00.215 [pool-2-thread-1] ERROR c.n.d.m.b.f.Validator - Directory [/source] does not exist
    
  • 有没有其他方法可以解决问题1,成功路由源文件
  • 如何验证目录以检查读取权限

  • 注意:骆驼版本是2.12.1

    我找到了解决问题的方法,如下所示:

    public boolean validateDirectory(FtpLocation ftpLocation) throws CustomException{
        FTPClient client = new FTPClient();
        try {
            client.connect(ftpLocation.getSystem(), Integer.parseInt(ftpLocation.getPort()));
            client.login(ftpLocation.getUser(), ftpLocation.getPassword());
            client.changeWorkingDirectory(ftpLocation.getDirPath());
            int returnCode = client.getReplyCode();
            if (returnCode == 550) {
                LOG.error("Directory ["+ ftpLocation.getDirPath() + "] does not exist");
                return false;
            }           
            File dir = new File(ftpLocation.getDirPath());
            if(dir!=null){
                if (!dir.canRead()) {
                    LOG.error("Directory ["+ ftpLocation.getDirPath() + "] does not have read permission");
                    return false;
                }
            }
            return true;
        } catch (NumberFormatException | IOException e) {
            LOG.error("Failed to validate Source Directory Path [" + ftpLocation.getDirPath() + "]", e);
            throw new CustomException("Failed to validate Source Directory Path [" + ftpLocation.getDirPath() + "]", e);
        } finally {
            try {
                client.disconnect();
            } catch (IOException e) {
                LOG.warn("Error occured while disconnecting FTPClient", e);
            }
        }   
    }
    
    public void validateDirectory(FtpLocation ftpLocation) throws CustomException {
        FTPClient client = new FTPClient();
        try {
            client.connect(ftpLocation.getSystem(), Integer.parseInt(ftpLocation.getPort()));
            client.login(ftpLocation.getUser(), ftpLocation.getPassword());
            client.enterLocalPassiveMode();
            client.changeWorkingDirectory(ftpLocation.getDirPath());
            int returnCode = client.getReplyCode();
            LOG.debug("FTP return code: "+ returnCode);
            if (returnCode == 550) {
                LOG.error("Directory ["+ ftpLocation.getDirPath() + "] does not exist");
                throw new CustomException("Directory ["+ ftpLocation.getDirPath() + "] does not exist");
            }
            String targetDir = client.printWorkingDirectory();
            client.cdup();
            targetDir = StringUtils.substring(targetDir, StringUtils.lastIndexOf(targetDir, File.separator) + 1);
            FTPFile ftpFiles[] = client.listFiles();
            for(FTPFile ftpFile : ftpFiles){
                if(StringUtils.equals(ftpFile.getName(), targetDir) && !ftpFile.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION)){
                    LOG.error("No read permission for directory [" + ftpLocation.getDirPath() + "]");
                    throw new CustomException("No read permission for directory [" + ftpLocation.getDirPath() + "]");
                }
            }
        } catch (NumberFormatException | IOException e) {
            LOG.error("Failed to validate Source Directory Path [" + ftpLocation.getDirPath() + "]", e);
            throw new CustomException("Failed to validate Source Directory Path [" + ftpLocation.getDirPath() + "]", e);
        } finally {
            try {
                client.disconnect();
            } catch (IOException e) {
                LOG.warn("Error occured while disconnecting FTPClient", e);
            }
        }   
    }
    
    不过,如果你有更好的解决方案,请张贴