Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.camel.component.file.remote;
 
 import java.util.List;
 
Secure FTP consumer
 
 public class SftpConsumer extends RemoteFileConsumer<ChannelSftp.LsEntry> {
 
     private String endpointPath;
 
     public SftpConsumer(RemoteFileEndpoint<ChannelSftp.LsEntryendpointProcessor processorRemoteFileOperations<ChannelSftp.LsEntryoperations) {
         super(endpointprocessoroperations);
         this. = endpoint.getConfiguration().getDirectory();
     }
 
     @Override
     protected boolean pollDirectory(String fileNameList<GenericFile<ChannelSftp.LsEntry>> fileListint depth) {
         String currentDir = null;
         if (isStepwise()) {
             // must remember current dir so we stay in that directory after the poll
             currentDir = .getCurrentDirectory();
         }
 
         // strip trailing slash
         fileName = FileUtil.stripTrailingSeparator(fileName);
 
         boolean answer = doPollDirectory(fileNamenullfileListdepth);
         if (currentDir != null) {
             .changeCurrentDirectory(currentDir);
         }
 
         return answer;
     }
 
     protected boolean pollSubDirectory(String absolutePathString dirNameList<GenericFile<ChannelSftp.LsEntry>> fileListint depth) {
         boolean answer = doPollDirectory(absolutePathdirNamefileListdepth);
         // change back to parent directory when finished polling sub directory
         if (isStepwise()) {
             .changeToParentDirectory();
         }
         return answer;
     }
 
     protected boolean doPollDirectory(String absolutePathString dirNameList<GenericFile<ChannelSftp.LsEntry>> fileListint depth) {
         .trace("doPollDirectory from absolutePath: {}, dirName: {}"absolutePathdirName);
 
         depth++;
 
         // remove trailing /
         dirName = FileUtil.stripTrailingSeparator(dirName);
         // compute dir depending on stepwise is enabled or not
         String dir;
         if (isStepwise()) {
             dir = ObjectHelper.isNotEmpty(dirName) ? dirName : absolutePath;
             .changeCurrentDirectory(dir);
         } else {
             dir = absolutePath;
         }
 
         .trace("Polling directory: {}"dir);
         List<ChannelSftp.LsEntryfiles;
         if (isStepwise()) {
             files = .listFiles();
         } else {
             files = .listFiles(dir);
         }
         if (files == null || files.isEmpty()) {
             // no files in this directory to poll
             .trace("No files found in directory: {}"dir);
             return true;
         } else {
             // we found some files
             .trace("Found {} in directory: {}"files.size(), dir);
         }
 
         for (ChannelSftp.LsEntry file : files) {
            if (.isTraceEnabled()) {
                .trace("SftpFile[fileName={}, longName={}, dir={}]"new Object[]{file.getFilename(), file.getLongname(), file.getAttrs().isDir()});
            }
            // check if we can continue polling in files
            if (!canPollMoreFiles(fileList)) {
                return false;
            }
            if (file.getAttrs().isDir()) {
                RemoteFile<ChannelSftp.LsEntryremote = asRemoteFile(absolutePathfile);
                if (.isRecursive() && isValidFile(remotetruefiles) && depth < .getMaxDepth()) {
                    // recursive scan and add the sub files and folders
                    String subDirectory = file.getFilename();
                    String path = absolutePath + "/" + subDirectory;
                    boolean canPollMore = pollSubDirectory(pathsubDirectoryfileListdepth);
                    if (!canPollMore) {
                        return false;
                    }
                }
                // we cannot use file.getAttrs().isLink on Windows, so we dont invoke the method
                // just assuming its a file we should poll
            } else {
                RemoteFile<ChannelSftp.LsEntryremote = asRemoteFile(absolutePathfile);
                if (isValidFile(remotefalsefiles) && depth >= .getMinDepth()) {
                    if (isInProgress(remote)) {
                        if (.isTraceEnabled()) {
                            .trace("Skipping as file is already in progress: {}"remote.getFileName());
                        }
                    } else {
                        // matched file so add
                        fileList.add(remote);
                    }
                }
            }
        }
        return true;
    }
    @Override
    protected boolean isMatched(GenericFile<ChannelSftp.LsEntryfileString doneFileNameList<ChannelSftp.LsEntryfiles) {
        String onlyName = FileUtil.stripPath(doneFileName);
        for (ChannelSftp.LsEntry f : files) {
            if (f.getFilename().equals(onlyName)) {
                return true;
            }
        }
        .trace("Done file: {} does not exist"doneFileName);
        return false;
    }
    private RemoteFile<ChannelSftp.LsEntryasRemoteFile(String absolutePathChannelSftp.LsEntry file) {
        RemoteFile<ChannelSftp.LsEntryanswer = new RemoteFile<ChannelSftp.LsEntry>();
        answer.setEndpointPath();
        answer.setFile(file);
        answer.setFileNameOnly(file.getFilename());
        answer.setFileLength(file.getAttrs().getSize());
        answer.setLastModified(file.getAttrs().getMTime() * 1000L);
        answer.setDirectory(file.getAttrs().isDir());
        // absolute or relative path
        boolean absolute = FileUtil.hasLeadingSeparator(absolutePath);
        answer.setAbsolute(absolute);
        // create a pseudo absolute name
        String dir = FileUtil.stripTrailingSeparator(absolutePath);
        String absoluteFileName = FileUtil.stripLeadingSeparator(dir + "/" + file.getFilename());
        // if absolute start with a leading separator otherwise let it be relative
        if (absolute) {
            absoluteFileName = "/" + absoluteFileName;
        }
        answer.setAbsoluteFilePath(absoluteFileName);
        // the relative filename, skip the leading endpoint configured path
        String relativePath = ObjectHelper.after(absoluteFileName);
        // skip trailing /
        relativePath = FileUtil.stripLeadingSeparator(relativePath);
        answer.setRelativeFilePath(relativePath);
        // the file name should be the relative path
        answer.setFileName(answer.getRelativeFilePath());
        return answer;
    }
    private boolean isStepwise() {
        return config.isStepwise();
    }
New to GrepCode? Check out our FAQ X