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.net.URI;
020    import java.util.Map;
021    
022    import org.apache.camel.CamelContext;
023    import org.apache.camel.component.file.GenericFileEndpoint;
024    import org.apache.camel.util.IntrospectionSupport;
025    import org.apache.commons.net.ftp.FTPFile;
026    
027    /**
028     * FTP Component
029     */
030    public class FtpComponent extends RemoteFileComponent<FTPFile> {
031    
032        public FtpComponent() {
033        }
034    
035        public FtpComponent(CamelContext context) {
036            super(context);
037        }
038    
039        @Override
040        protected GenericFileEndpoint<FTPFile> buildFileEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
041            String baseUri = getBaseUri(uri);
042    
043            // lets make sure we create a new configuration as each endpoint can customize its own version
044            // must pass on baseUri to the configuration (see above)
045            FtpConfiguration config = new FtpConfiguration(new URI(baseUri));
046    
047            FtpEndpoint<FTPFile> answer = new FtpEndpoint<FTPFile>(uri, this, config);
048            extractAndSetFtpClientConfigParameters(parameters, answer);
049            extractAndSetFtpClientParameters(parameters, answer);
050    
051            return answer;
052        }
053    
054        /**
055         * get the base uri part before the options as they can be non URI valid such as the expression using $ chars
056         * and the URI constructor will regard $ as an illegal character and we dont want to enforce end users to
057         * to escape the $ for the expression (file language)
058         */
059        protected String getBaseUri(String uri) {
060            String baseUri = uri;
061            if (uri.indexOf("?") != -1) {
062                baseUri = uri.substring(0, uri.indexOf("?"));
063            }
064            return baseUri;
065        }
066    
067        /**
068         * Extract additional ftp client configuration options from the parameters map (parameters starting with 
069         * 'ftpClientConfig.'). To remember these parameters, we set them in the endpoint and we can use them 
070         * when creating a client.
071         */
072        protected void extractAndSetFtpClientConfigParameters(Map<String, Object> parameters, FtpEndpoint<FTPFile> answer) {
073            if (IntrospectionSupport.hasProperties(parameters, "ftpClientConfig.")) {
074                Map<String, Object> param = IntrospectionSupport.extractProperties(parameters, "ftpClientConfig.");
075                answer.setFtpClientConfigParameters(param);
076            }
077        }
078        
079        /**
080         * Extract additional ftp client options from the parameters map (parameters starting with 
081         * 'ftpClient.'). To remember these parameters, we set them in the endpoint and we can use them 
082         * when creating a client.
083         */
084        protected void extractAndSetFtpClientParameters(Map<String, Object> parameters, FtpEndpoint<FTPFile> answer) {
085            if (IntrospectionSupport.hasProperties(parameters, "ftpClient.")) {
086                Map<String, Object> param = IntrospectionSupport.extractProperties(parameters, "ftpClient.");
087                answer.setFtpClientParameters(param);
088            }
089        }
090    
091        protected void afterPropertiesSet(GenericFileEndpoint<FTPFile> endpoint) throws Exception {
092            // noop
093        }
094    }