001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.file.remote;
018    
019    import java.util.Map;
020    
021    import org.apache.camel.FailedToCreateConsumerException;
022    import org.apache.camel.FailedToCreateProducerException;
023    import org.apache.camel.Processor;
024    import org.apache.camel.component.file.GenericFileProducer;
025    import org.apache.camel.util.IntrospectionSupport;
026    import org.apache.commons.net.ftp.FTPClient;
027    import org.apache.commons.net.ftp.FTPClientConfig;
028    import org.apache.commons.net.ftp.FTPFile;
029    
030    /**
031     * FTP endpoint
032     */
033    public class FtpEndpoint extends RemoteFileEndpoint<FTPFile> {
034    
035        private FTPClient ftpClient;
036        private FTPClientConfig ftpClientConfig;
037        private Map<String, Object> ftpClientParameters;
038        private Map<String, Object> ftpClientConfigParameters;
039    
040        public FtpEndpoint() {
041        }
042    
043        public FtpEndpoint(String uri, FtpComponent component, RemoteFileConfiguration configuration) {
044            super(uri, component, configuration);
045        }
046    
047        @Override
048        public String getScheme() {
049            return "ftp";
050        }
051    
052        @Override
053        protected RemoteFileConsumer<FTPFile> buildConsumer(Processor processor) {
054            try {
055                return new FtpConsumer(this, processor, createRemoteFileOperations());
056            } catch (Exception e) {
057                throw new FailedToCreateConsumerException(this, e);
058            }
059        }
060    
061        protected GenericFileProducer<FTPFile> buildProducer() {
062            try {
063                return new RemoteFileProducer<FTPFile>(this, createRemoteFileOperations());
064            } catch (Exception e) {
065                throw new FailedToCreateProducerException(this, e);
066            }
067        }
068        
069        protected RemoteFileOperations<FTPFile> createRemoteFileOperations() throws Exception {
070            // configure ftp client
071            FTPClient client = ftpClient;
072            if (client == null) {
073                // must use a new client if not explicit configured to use a custom client
074                client = new FTPClient();
075            }
076    
077            if (ftpClientParameters != null) {
078                IntrospectionSupport.setProperties(client, ftpClientParameters);
079            }
080            if (ftpClientConfigParameters != null) {
081                // client config is optional so create a new one if we have parameter for it
082                if (ftpClientConfig == null) {
083                    ftpClientConfig = new FTPClientConfig();
084                }
085                IntrospectionSupport.setProperties(ftpClientConfig, ftpClientConfigParameters);
086            }
087    
088            FtpOperations operations = new FtpOperations(client, getFtpClientConfig());
089            operations.setEndpoint(this);
090            return operations;
091        }
092    
093        public FTPClient getFtpClient() {
094            return ftpClient;
095        }
096    
097        public void setFtpClient(FTPClient ftpClient) {
098            this.ftpClient = ftpClient;
099        }
100    
101        public FTPClientConfig getFtpClientConfig() {
102            return ftpClientConfig;
103        }
104    
105        public void setFtpClientConfig(FTPClientConfig ftpClientConfig) {
106            this.ftpClientConfig = ftpClientConfig;
107        }
108    
109        /**
110         * Used by FtpComponent to provide additional parameters for the FTPClient
111         */
112        void setFtpClientParameters(Map<String, Object> ftpClientParameters) {
113            this.ftpClientParameters = ftpClientParameters;
114        }
115    
116        /**
117         * Used by FtpComponent to provide additional parameters for the FTPClientConfig
118         */
119        void setFtpClientConfigParameters(Map<String, Object> ftpClientConfigParameters) {
120            this.ftpClientConfigParameters = ftpClientConfigParameters;
121        }
122    }