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.io.IOException;
020    import javax.net.ssl.SSLException;
021    
022    import org.apache.camel.component.file.GenericFileOperationFailedException;
023    import org.apache.camel.util.ObjectHelper;
024    import org.apache.commons.net.ftp.FTPClientConfig;
025    import org.apache.commons.net.ftp.FTPSClient;
026    
027    /**
028     * FTP Secure (FTP over SSL/TLS) operations
029     *
030     * @version $Revision: 954879 $
031     */
032    public class FtpsOperations extends FtpOperations {
033    
034        public FtpsOperations(FTPSClient client, FTPClientConfig clientConfig) {
035            super(client, clientConfig);
036        }
037    
038        @Override
039        public boolean connect(RemoteFileConfiguration configuration) throws GenericFileOperationFailedException {
040            boolean answer = super.connect(configuration);
041    
042            FtpsConfiguration config = (FtpsConfiguration) configuration;
043            if (answer) {
044                try {
045                    String execProt = config.getExecProt();
046                    Long execPbsz = config.getExecPbsz();
047                    // use default values for prop and pbsz, unless told to not do so
048                    if (!config.isDisableSecureDataChannelDefaults()) {
049                        if (ObjectHelper.isEmpty(execProt)) {
050                            execProt = "P";
051                        }
052                        if (ObjectHelper.isEmpty(execPbsz)) {
053                            execPbsz = 0L;
054                        }
055                    }
056    
057                    if (execPbsz != null) {
058                        if (log.isDebugEnabled()) {
059                            log.debug("FTPClient initializing with execPbsz=" + execPbsz);
060                        }
061                        getFtpClient().execPBSZ(execPbsz);
062                    }
063                    if (execProt != null) {
064                        if (log.isDebugEnabled()) {
065                            log.debug("FTPClient initializing with execProt=" + execProt);
066                        }
067                        getFtpClient().execPROT(execProt);
068                    }
069                } catch (SSLException e) {
070                    throw new GenericFileOperationFailedException(client.getReplyCode(),
071                            client.getReplyString(), e.getMessage(), e);
072                } catch (IOException e) {
073                    throw new GenericFileOperationFailedException(client.getReplyCode(),
074                            client.getReplyString(), e.getMessage(), e);
075                }
076            }
077    
078            return answer;
079        }
080    
081        @Override
082        protected FTPSClient getFtpClient() {
083            return (FTPSClient) super.getFtpClient();
084        }
085    
086    }