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.GenericFileConfiguration;
025    import org.apache.camel.component.file.GenericFileProducer;
026    import org.apache.camel.util.IntrospectionSupport;
027    import org.apache.commons.net.ftp.FTPClient;
028    import org.apache.commons.net.ftp.FTPClientConfig;
029    import org.apache.commons.net.ftp.FTPFile;
030    
031    /**
032     * FTP endpoint
033     */
034    public class FtpEndpoint<T extends FTPFile> extends RemoteFileEndpoint<FTPFile> {
035    
036        protected FTPClient ftpClient;
037        protected FTPClientConfig ftpClientConfig;
038        protected Map<String, Object> ftpClientParameters;
039        protected Map<String, Object> ftpClientConfigParameters;
040        protected int soTimeout;
041        protected int dataTimeout;
042    
043        public FtpEndpoint() {
044            super();
045        }
046    
047        public FtpEndpoint(String uri, RemoteFileComponent<FTPFile> component, RemoteFileConfiguration configuration) {
048            super(uri, component, configuration);
049        }
050    
051        @Override
052        public String getScheme() {
053            return "ftp";
054        }
055    
056        @Override
057        protected RemoteFileConsumer<FTPFile> buildConsumer(Processor processor) {
058            try {
059                return new FtpConsumer(this, processor, createRemoteFileOperations());
060            } catch (Exception e) {
061                throw new FailedToCreateConsumerException(this, e);
062            }
063        }
064    
065        protected GenericFileProducer<FTPFile> buildProducer() {
066            try {
067                return new RemoteFileProducer<FTPFile>(this, createRemoteFileOperations());
068            } catch (Exception e) {
069                throw new FailedToCreateProducerException(this, e);
070            }
071        }
072        
073        public RemoteFileOperations<FTPFile> createRemoteFileOperations() throws Exception {
074            // configure ftp client
075            FTPClient client = ftpClient;
076            
077            if (client == null) {
078                // must use a new client if not explicit configured to use a custom client
079                client = createFtpClient();
080            }
081    
082            // set any endpoint configured timeouts
083            if (getConfiguration().getConnectTimeout() > -1) {
084                client.setConnectTimeout(getConfiguration().getConnectTimeout());
085            }
086            if (getConfiguration().getSoTimeout() > -1) {
087                soTimeout = getConfiguration().getSoTimeout();
088            }
089            dataTimeout = getConfiguration().getTimeout();
090    
091            // then lookup ftp client parameters and set those
092            if (ftpClientParameters != null) {
093                // setting soTimeout has to be done later on FTPClient (after it has connected)
094                Object timeout = ftpClientParameters.remove("soTimeout");
095                if (timeout != null) {
096                    soTimeout = getCamelContext().getTypeConverter().convertTo(int.class, timeout);
097                }
098                // and we want to keep data timeout so we can log it later
099                timeout = ftpClientParameters.remove("dataTimeout");
100                if (timeout != null) {
101                    dataTimeout = getCamelContext().getTypeConverter().convertTo(int.class, dataTimeout);
102                }
103                IntrospectionSupport.setProperties(client, ftpClientParameters);
104            }
105            
106            if (ftpClientConfigParameters != null) {
107                // client config is optional so create a new one if we have parameter for it
108                if (ftpClientConfig == null) {
109                    ftpClientConfig = new FTPClientConfig();
110                }
111                IntrospectionSupport.setProperties(ftpClientConfig, ftpClientConfigParameters);
112            }
113    
114            if (dataTimeout > 0) {
115                client.setDataTimeout(dataTimeout);
116            }
117    
118            if (log.isDebugEnabled()) {
119                log.debug("Created FTPClient [connectTimeout: " + client.getConnectTimeout() + ", soTimeout: " + getSoTimeout() + ", dataTimeout: " + dataTimeout + "]: " + client);
120            }
121    
122            FtpOperations operations = new FtpOperations(client, getFtpClientConfig());
123            operations.setEndpoint(this);
124            return operations;
125        }
126    
127        protected FTPClient createFtpClient() throws Exception {
128            return new FTPClient();
129        }
130    
131        @Override
132        public FtpConfiguration getConfiguration() {
133            if (configuration == null) {
134                configuration = new FtpConfiguration();
135            }
136            return (FtpConfiguration)configuration;
137        }
138    
139        @Override
140        public void setConfiguration(GenericFileConfiguration configuration) {
141            setConfiguration((FtpConfiguration)configuration);
142        }
143    
144        public void setConfiguration(FtpConfiguration configuration) {
145            if (configuration == null) {
146                throw new IllegalArgumentException("FtpConfiguration expected");
147            }
148            this.configuration = configuration;
149        }
150    
151        public FTPClient getFtpClient() {
152            return ftpClient;
153        }
154    
155        public void setFtpClient(FTPClient ftpClient) {
156            this.ftpClient = ftpClient;
157        }
158    
159        public FTPClientConfig getFtpClientConfig() {
160            return ftpClientConfig;
161        }
162    
163        public void setFtpClientConfig(FTPClientConfig ftpClientConfig) {
164            this.ftpClientConfig = ftpClientConfig;
165        }
166    
167        /**
168         * Used by FtpComponent to provide additional parameters for the FTPClient
169         */
170        void setFtpClientParameters(Map<String, Object> ftpClientParameters) {
171            this.ftpClientParameters = ftpClientParameters;
172        }
173    
174        /**
175         * Used by FtpComponent to provide additional parameters for the FTPClientConfig
176         */
177        void setFtpClientConfigParameters(Map<String, Object> ftpClientConfigParameters) {
178            this.ftpClientConfigParameters = ftpClientConfigParameters;
179        }
180    
181        public int getSoTimeout() {
182            return soTimeout;
183        }
184    
185        /**
186         * Sets the soTimeout option.
187         * <p/>
188         * Used by FTPClient
189         */
190        public void setSoTimeout(int soTimeout) {
191            this.soTimeout = soTimeout;
192        }
193    }