Coverage Report - org.apache.camel.component.file.remote.FtpProducer
 
Classes in this File Line Coverage Branch Coverage Complexity
FtpProducer
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.IOException;
 20  
 import java.io.InputStream;
 21  
 
 22  
 import org.apache.camel.Exchange;
 23  
 import org.apache.camel.RuntimeCamelException;
 24  
 import org.apache.commons.net.ftp.FTPClient;
 25  
 
 26  
 public class FtpProducer extends RemoteFileProducer<RemoteFileExchange> {
 27  
     FtpEndpoint endpoint;
 28  
     private final FTPClient client;
 29  
 
 30  
     public FtpProducer(FtpEndpoint endpoint, FTPClient client) {
 31  0
         super(endpoint);
 32  0
         this.endpoint = endpoint;
 33  0
         this.client = client;
 34  0
     }
 35  
 
 36  
     public void process(Exchange exchange) throws Exception {
 37  0
         process(endpoint.toExchangeType(exchange));
 38  0
     }
 39  
 
 40  
     public void process(RemoteFileExchange exchange) throws Exception {
 41  
         final String fileName;
 42  0
         InputStream payload = exchange.getIn().getBody(InputStream.class);
 43  0
         final String endpointFile = endpoint.getConfiguration().getFile();
 44  0
         client.changeWorkingDirectory(endpointFile); // TODO this line might
 45  
                                                         // not be needed...
 46  
                                                         // check after finish
 47  
                                                         // writing unit tests
 48  0
         if (endpointFile == null) {
 49  0
             throw new NullPointerException("Null Endpoint File");
 50  
         } else {
 51  0
             if (endpoint.getConfiguration().isDirectory()) {
 52  0
                 fileName = endpointFile + "/" + exchange.getIn().getMessageId();
 53  0
             } else {
 54  0
                 fileName = endpointFile;
 55  
             }
 56  
         }
 57  0
         buildDirectory(client, fileName.substring(0, fileName.lastIndexOf('/')));
 58  0
         final boolean success = client.storeFile(fileName, payload);
 59  0
         if (!success) {
 60  0
             throw new RuntimeCamelException("error sending file");
 61  
         }
 62  0
     }
 63  
 
 64  
     @Override
 65  
     protected void doStart() throws Exception {
 66  0
         super.doStart();
 67  
         // client.connect(endpoint.getConfiguration().getHost());
 68  
         // client.login(endpoint.getConfiguration().getUsername(),
 69  
         // endpoint.getConfiguration().getPassword());
 70  
         // client.setFileType(endpoint.getConfiguration().isBinary() ?
 71  
         // FTPClient.BINARY_FILE_TYPE : FTPClient.ASCII_FILE_TYPE);
 72  0
     }
 73  
 
 74  
     @Override
 75  
     protected void doStop() throws Exception {
 76  0
         client.disconnect();
 77  0
         super.doStop();
 78  0
     }
 79  
 
 80  
     protected static boolean buildDirectory(FTPClient ftpClient, String dirName) throws IOException {
 81  0
         boolean atLeastOneSuccess = false;
 82  0
         final StringBuilder sb = new StringBuilder(dirName.length());
 83  0
         final String[] dirs = dirName.split("\\/");
 84  0
         for (String dir : dirs) {
 85  0
             sb.append('/').append(dir);
 86  0
             final boolean success = ftpClient.makeDirectory(sb.toString());
 87  0
             System.out.println(sb.toString() + " = " + success);
 88  0
             if (!atLeastOneSuccess && success) {
 89  0
                 atLeastOneSuccess = true;
 90  
             }
 91  
         }
 92  0
         return atLeastOneSuccess;
 93  
     }
 94  
 }