Coverage Report - org.apache.camel.component.file.remote.SftpConsumer
 
Classes in this File Line Coverage Branch Coverage Complexity
SftpConsumer
0% 
0% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.component.file.remote;
 18  
 
 19  
 import java.io.ByteArrayOutputStream;
 20  
 import java.io.IOException;
 21  
 import java.util.concurrent.ScheduledExecutorService;
 22  
 
 23  
 import com.jcraft.jsch.ChannelSftp;
 24  
 
 25  
 import org.apache.camel.Processor;
 26  
 
 27  
 public class SftpConsumer extends RemoteFileConsumer<RemoteFileExchange> {
 28  0
     private boolean recursive = true;
 29  0
     private String regexPattern = "";
 30  
     private long lastPollTime;
 31  
     private final SftpEndpoint endpoint;
 32  
     private ChannelSftp channel;
 33  
 
 34  
     public SftpConsumer(SftpEndpoint endpoint, Processor processor, ChannelSftp channel) {
 35  0
         super(endpoint, processor);
 36  0
         this.endpoint = endpoint;
 37  0
         this.channel = channel;
 38  0
     }
 39  
 
 40  
     public SftpConsumer(SftpEndpoint endpoint, Processor processor, ChannelSftp channel, ScheduledExecutorService executor) {
 41  0
         super(endpoint, processor, executor);
 42  0
         this.endpoint = endpoint;
 43  0
         this.channel = channel;
 44  0
     }
 45  
 
 46  
     protected void poll() throws Exception {
 47  0
         final String fileName = endpoint.getConfiguration().getFile();
 48  0
         if (endpoint.getConfiguration().isDirectory()) {
 49  0
             pollDirectory(fileName);
 50  0
         } else {
 51  0
             channel.cd(fileName.substring(0, fileName.lastIndexOf('/')));
 52  0
             final ChannelSftp.LsEntry file = (ChannelSftp.LsEntry)channel.ls(fileName.substring(fileName.lastIndexOf('/') + 1)).get(0);
 53  0
             pollFile(file);
 54  
         }
 55  0
         lastPollTime = System.currentTimeMillis();
 56  0
     }
 57  
 
 58  
     protected void pollDirectory(String dir) throws Exception {
 59  0
         channel.cd(dir);
 60  0
         for (ChannelSftp.LsEntry sftpFile : (ChannelSftp.LsEntry[])channel.ls(".").toArray(new ChannelSftp.LsEntry[] {})) {
 61  0
             if (sftpFile.getFilename().startsWith(".")) {
 62  
                 // skip
 63  0
             } else if (sftpFile.getAttrs().isDir()) {
 64  0
                 if (isRecursive()) {
 65  0
                     pollDirectory(getFullFileName(sftpFile));
 66  0
                 }
 67  
             } else {
 68  0
                 pollFile(sftpFile);
 69  
             }
 70  
         }
 71  0
     }
 72  
 
 73  
     protected String getFullFileName(ChannelSftp.LsEntry sftpFile) throws IOException {
 74  0
         return channel.pwd() + "/" + sftpFile.getFilename();
 75  
     }
 76  
 
 77  
     private void pollFile(ChannelSftp.LsEntry sftpFile) throws Exception {
 78  0
         if (sftpFile.getAttrs().getMTime() * 1000 > lastPollTime) { // TODO do
 79  
                                                                     // we need
 80  
                                                                     // to adjust
 81  
                                                                     // the TZ?
 82  0
             if (isMatched(sftpFile)) {
 83  0
                 final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 84  0
                 channel.get(sftpFile.getFilename(), byteArrayOutputStream);
 85  0
                 getProcessor().process(endpoint.createExchange(getFullFileName(sftpFile), byteArrayOutputStream));
 86  
             }
 87  
         }
 88  0
     }
 89  
 
 90  
     protected boolean isMatched(ChannelSftp.LsEntry sftpFile) {
 91  0
         boolean result = true;
 92  0
         if (regexPattern != null && regexPattern.length() > 0) {
 93  0
             result = sftpFile.getFilename().matches(getRegexPattern());
 94  
         }
 95  0
         return result;
 96  
     }
 97  
 
 98  
     public boolean isRecursive() {
 99  0
         return recursive;
 100  
     }
 101  
 
 102  
     public void setRecursive(boolean recursive) {
 103  0
         this.recursive = recursive;
 104  0
     }
 105  
 
 106  
     public long getLastPollTime() {
 107  0
         return lastPollTime;
 108  
     }
 109  
 
 110  
     public void setLastPollTime(long lastPollTime) {
 111  0
         this.lastPollTime = lastPollTime;
 112  0
     }
 113  
 
 114  
     public String getRegexPattern() {
 115  0
         return regexPattern;
 116  
     }
 117  
 
 118  
     public void setRegexPattern(String regexPattern) {
 119  0
         this.regexPattern = regexPattern;
 120  0
     }
 121  
 }