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