Coverage Report - org.apache.camel.component.file.remote.FtpConsumer
 
Classes in this File Line Coverage Branch Coverage Complexity
FtpConsumer
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 org.apache.camel.Processor;
 24  
 import org.apache.commons.net.ftp.FTPClient;
 25  
 import org.apache.commons.net.ftp.FTPFile;
 26  
 
 27  
 public class FtpConsumer extends RemoteFileConsumer<RemoteFileExchange> {
 28  0
     private boolean recursive = true;
 29  0
     private String regexPattern = "";
 30  
     private long lastPollTime;
 31  
     private final FtpEndpoint endpoint;
 32  
     private FTPClient client;
 33  
 
 34  
     public FtpConsumer(FtpEndpoint endpoint, Processor processor, FTPClient client) {
 35  0
         super(endpoint, processor);
 36  0
         this.endpoint = endpoint;
 37  0
         this.client = client;
 38  0
     }
 39  
 
 40  
     public FtpConsumer(FtpEndpoint endpoint, Processor processor, FTPClient client, ScheduledExecutorService executor) {
 41  0
         super(endpoint, processor, executor);
 42  0
         this.endpoint = endpoint;
 43  0
         this.client = client;
 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
             client.changeWorkingDirectory(fileName.substring(0, fileName.lastIndexOf('/')));
 52  0
             final FTPFile[] files = client.listFiles(fileName.substring(fileName.lastIndexOf('/') + 1));
 53  0
             pollFile(files[0]);
 54  
         }
 55  0
         lastPollTime = System.currentTimeMillis();
 56  0
     }
 57  
 
 58  
     protected void pollDirectory(String dir) throws Exception {
 59  0
         client.changeWorkingDirectory(dir);
 60  0
         for (FTPFile ftpFile : client.listFiles()) {
 61  0
             if (ftpFile.isFile()) {
 62  0
                 pollFile(ftpFile);
 63  0
             } else if (ftpFile.isDirectory()) {
 64  0
                 if (isRecursive()) {
 65  0
                     pollDirectory(getFullFileName(ftpFile));
 66  0
                 }
 67  
             } else {
 68  0
                 throw new RuntimeException("");
 69  
             }
 70  
         }
 71  0
     }
 72  
 
 73  
     protected String getFullFileName(FTPFile ftpFile) throws IOException {
 74  0
         return client.printWorkingDirectory() + "/" + ftpFile.getName();
 75  
     }
 76  
 
 77  
     private void pollFile(FTPFile ftpFile) throws Exception {
 78  0
         if (ftpFile.getTimestamp().getTimeInMillis() > lastPollTime) { // TODO
 79  
                                                                         // do we
 80  
                                                                         // need
 81  
                                                                         // to
 82  
                                                                         // adjust
 83  
                                                                         // the
 84  
                                                                         // TZ?
 85  
                                                                         // can
 86  
                                                                         // we?
 87  0
             if (isMatched(ftpFile)) {
 88  0
                 final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 89  0
                 client.retrieveFile(ftpFile.getName(), byteArrayOutputStream);
 90  0
                 getProcessor().process(endpoint.createExchange(getFullFileName(ftpFile), byteArrayOutputStream));
 91  
             }
 92  
         }
 93  0
     }
 94  
 
 95  
     protected boolean isMatched(FTPFile file) {
 96  0
         boolean result = true;
 97  0
         if (regexPattern != null && regexPattern.length() > 0) {
 98  0
             result = file.getName().matches(getRegexPattern());
 99  
         }
 100  0
         return result;
 101  
     }
 102  
 
 103  
     public boolean isRecursive() {
 104  0
         return recursive;
 105  
     }
 106  
 
 107  
     public void setRecursive(boolean recursive) {
 108  0
         this.recursive = recursive;
 109  0
     }
 110  
 
 111  
     public long getLastPollTime() {
 112  0
         return lastPollTime;
 113  
     }
 114  
 
 115  
     public void setLastPollTime(long lastPollTime) {
 116  0
         this.lastPollTime = lastPollTime;
 117  0
     }
 118  
 
 119  
     public String getRegexPattern() {
 120  0
         return regexPattern;
 121  
     }
 122  
 
 123  
     public void setRegexPattern(String regexPattern) {
 124  0
         this.regexPattern = regexPattern;
 125  0
     }
 126  
 }