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<T extends FTPFile> extends RemoteFileEndpoint<FTPFile> {
034    
035        protected FTPClient ftpClient;
036        protected FTPClientConfig ftpClientConfig;
037        protected Map<String, Object> ftpClientParameters;
038        protected Map<String, Object> ftpClientConfigParameters;
039    
040        public FtpEndpoint() {
041        }
042    
043        public FtpEndpoint(String uri, RemoteFileComponent<FTPFile> 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            
073            if (client == null) {
074                // must use a new client if not explicit configured to use a custom client
075                client = createFtpClient();
076            }
077    
078            if (ftpClientParameters != null) {
079                IntrospectionSupport.setProperties(client, ftpClientParameters);
080            }
081            
082            if (ftpClientConfigParameters != null) {
083                // client config is optional so create a new one if we have parameter for it
084                if (ftpClientConfig == null) {
085                    ftpClientConfig = new FTPClientConfig();
086                }
087                IntrospectionSupport.setProperties(ftpClientConfig, ftpClientConfigParameters);
088            }
089    
090            FtpOperations operations = new FtpOperations(client, getFtpClientConfig());
091            operations.setEndpoint(this);
092            return operations;
093        }
094    
095        protected FTPClient createFtpClient() throws Exception {
096            return new FTPClient();
097        }
098    
099        public FTPClient getFtpClient() {
100            return ftpClient;
101        }
102    
103        public void setFtpClient(FTPClient ftpClient) {
104            this.ftpClient = ftpClient;
105        }
106    
107        public FTPClientConfig getFtpClientConfig() {
108            return ftpClientConfig;
109        }
110    
111        public void setFtpClientConfig(FTPClientConfig ftpClientConfig) {
112            this.ftpClientConfig = ftpClientConfig;
113        }
114    
115        /**
116         * Used by FtpComponent to provide additional parameters for the FTPClient
117         */
118        void setFtpClientParameters(Map<String, Object> ftpClientParameters) {
119            this.ftpClientParameters = ftpClientParameters;
120        }
121    
122        /**
123         * Used by FtpComponent to provide additional parameters for the FTPClientConfig
124         */
125        void setFtpClientConfigParameters(Map<String, Object> ftpClientConfigParameters) {
126            this.ftpClientConfigParameters = ftpClientConfigParameters;
127        }
128    }