001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.component.file.remote;
019    
020    import org.apache.camel.Exchange;
021    import org.apache.camel.RuntimeCamelException;
022    import org.apache.commons.net.ftp.FTPClient;
023    
024    import java.io.IOException;
025    import java.io.InputStream;
026    
027    public class FtpProducer extends RemoteFileProducer<RemoteFileExchange> {
028        FtpEndpoint endpoint;
029        private final FTPClient client;
030    
031        public FtpProducer(FtpEndpoint endpoint, FTPClient client) {
032            super(endpoint);
033            this.endpoint = endpoint;
034            this.client = client;
035        }
036    
037        public void process(Exchange exchange) throws Exception {
038            process(endpoint.toExchangeType(exchange));
039        }
040    
041        public void process(RemoteFileExchange exchange) throws Exception {
042            final String fileName;
043            InputStream payload = exchange.getIn().getBody(InputStream.class);
044            final String endpointFile = endpoint.getConfiguration().getFile();
045            client.changeWorkingDirectory(endpointFile); // TODO this line might not be needed... check after finish writing unit tests
046            if (endpointFile == null) {
047                throw new NullPointerException("Null Endpoint File");
048            }
049            else {
050                if (endpoint.getConfiguration().isDirectory()) {
051                    fileName = endpointFile + "/" + exchange.getIn().getMessageId();
052                }
053                else {
054                    fileName = endpointFile;
055                }
056            }
057            buildDirectory(client, fileName.substring(0, fileName.lastIndexOf('/')));
058            final boolean success = client.storeFile(fileName, payload);
059            if (success) {
060    
061            }
062            else {
063                throw new RuntimeCamelException("error sending file");
064            }
065        }
066    
067        @Override
068        protected void doStart() throws Exception {
069            super.doStart();
070    //        client.connect(endpoint.getConfiguration().getHost());
071    //        client.login(endpoint.getConfiguration().getUsername(), endpoint.getConfiguration().getPassword());
072    //        client.setFileType(endpoint.getConfiguration().isBinary() ? FTPClient.BINARY_FILE_TYPE : FTPClient.ASCII_FILE_TYPE);
073        }
074    
075        @Override
076        protected void doStop() throws Exception {
077            client.disconnect();
078            super.doStop();
079        }
080    
081        protected static boolean buildDirectory(FTPClient ftpClient, String dirName) throws IOException {
082            boolean atLeastOneSuccess = false;
083            final StringBuilder sb = new StringBuilder(dirName.length());
084            final String[] dirs = dirName.split("\\/");
085            for (String dir : dirs) {
086                sb.append('/').append(dir);
087                final boolean success = ftpClient.makeDirectory(sb.toString());
088                System.out.println(sb.toString() + " = " + success);
089                if (!atLeastOneSuccess && success) {
090                    atLeastOneSuccess = true;
091                }
092            }
093            return atLeastOneSuccess;
094        }
095    }