Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.camel.component.file.remote;
 
 import java.io.File;
 import java.util.Map;
 
 
FTP Secure (FTP over SSL/TLS) endpoint

Version:
 
 public class FtpsEndpoint extends FtpEndpoint<FTPFile> {
     
 
     public FtpsEndpoint() {
     }
 
     public FtpsEndpoint(String uriRemoteFileComponent<FTPFileremoteFileComponentRemoteFileConfiguration configuration) {
         super(uriremoteFileComponentconfiguration);
     }
 
     @Override
     public String getScheme() {
         return getFtpsConfiguration().getProtocol();
     }
    
    
Create the FTPS client.
 
     protected FTPClient createFtpClient() throws Exception {
         FTPSClient client = null;
         
         if ( != null) {
             SSLContext context = .createSSLContext();
 
             client = new FTPSClient(getFtpsConfiguration().isImplicit(), context);
             
             // The FTPSClient tries to manage the following SSLSocket related configuration options
             // on its own based on internal configuration options.  FTPSClient does not lend itself
             // to subclassing for the purpose of overriding this behavior (private methods, fields, etc.).
             // As such, we create a socket (preconfigured by SSLContextParameters) from the context
             // we gave to FTPSClient and then setup FTPSClient to reuse the already configured configuration
             // from the socket for all future sockets it creates.  Not sexy and a little brittle, but it works.
             SSLSocket socket = (SSLSocket)context.getSocketFactory().createSocket();
             client.setEnabledCipherSuites(socket.getEnabledCipherSuites());
             client.setEnabledProtocols(socket.getEnabledProtocols());
             client.setNeedClientAuth(socket.getNeedClientAuth());
             client.setWantClientAuth(socket.getWantClientAuth());
             client.setEnabledSessionCreation(socket.getEnableSessionCreation());
         } else {
             client = new FTPSClient(getFtpsConfiguration().getSecurityProtocol(),
                                                getFtpsConfiguration().isImplicit());
             
             if ( != null) {
                 String type = (.containsKey("type"))
                         ? (String.get("type") : KeyStore.getDefaultType();
                 String file = (String.get("file");
                 String password = (String.get("password");
                 String algorithm = (.containsKey("algorithm"))
                         ? (String.get("algorithm")
                         : KeyManagerFactory.getDefaultAlgorithm();
                 String keyPassword = (String.get("keyPassword");
                 
                 KeyStore keyStore = KeyStore.getInstance(type);
                 FileInputStream keyStoreFileInputStream = new FileInputStream(new File(file));
                 try {
                    keyStore.load(keyStoreFileInputStreampassword.toCharArray());
                } finally {
                    IOHelper.close(keyStoreFileInputStream"keyStore");
                }
    
                KeyManagerFactory keyMgrFactory = KeyManagerFactory.getInstance(algorithm);
                keyMgrFactory.init(keyStorekeyPassword.toCharArray());
                client.setNeedClientAuth(true);
                client.setKeyManager(keyMgrFactory.getKeyManagers()[0]);
            }
    
            if ( != null) {
                String type = (.containsKey("type"))
                        ? (String.get("type") : KeyStore.getDefaultType();
                String file = (String.get("file");
                String password = (String.get("password");
                String algorithm = (.containsKey("algorithm"))
                        ? (String.get("algorithm")
                        : TrustManagerFactory.getDefaultAlgorithm();
                        
                KeyStore trustStore = KeyStore.getInstance(type);
                FileInputStream trustStoreFileInputStream = new FileInputStream(new File(file));
                try {
                    trustStore.load(trustStoreFileInputStreampassword.toCharArray());
                } finally {
                    IOHelper.close(trustStoreFileInputStream"trustStore");
                }
    
                TrustManagerFactory trustMgrFactory = TrustManagerFactory.getInstance(algorithm);
                trustMgrFactory.init(trustStore);
                
                client.setTrustManager(trustMgrFactory.getTrustManagers()[0]);
            }
        }
        
        return client;
    }
    @Override
        // configure ftp client
        FTPSClient client = getFtpsClient();
        if (client == null) {
            // must use a new client if not explicit configured to use a custom client
            client = (FTPSClientcreateFtpClient();
        }
        // set any endpoint configured timeouts
        if (getConfiguration().getConnectTimeout() > -1) {
            client.setConnectTimeout(getConfiguration().getConnectTimeout());
        }
        if (getConfiguration().getSoTimeout() > -1) {
             = getConfiguration().getSoTimeout();
        }
         = getConfiguration().getTimeout();
        if ( != null) {
            // setting soTimeout has to be done later on FTPClient (after it has connected)
            Object timeout = .remove("soTimeout");
            if (timeout != null) {
                 = getCamelContext().getTypeConverter().convertTo(int.classtimeout);
            }
            // and we want to keep data timeout so we can log it later
            timeout = .remove("dataTimeout");
            if (timeout != null) {
                 = getCamelContext().getTypeConverter().convertTo(int.class);
            }
            IntrospectionSupport.setProperties(client);
        }
        if ( != null) {
            // client config is optional so create a new one if we have parameter for it
            if ( == null) {
                 = new FTPClientConfig();
            }
            IntrospectionSupport.setProperties();
        }
        if ( > 0) {
            client.setDataTimeout();
        }
        if (.isDebugEnabled()) {
            .debug("Created FTPSClient [connectTimeout: {}, soTimeout: {}, dataTimeout: {}]: {}",
                    new Object[]{client.getConnectTimeout(), getSoTimeout(), client});
        }
        FtpsOperations operations = new FtpsOperations(clientgetFtpClientConfig());
        operations.setEndpoint(this);
        return operations;
    }

    
Returns the FTPSClient. This method exists only for convenient.

Returns:
ftpsClient
    public FTPSClient getFtpsClient() {
        return (FTPSClientgetFtpClient();
    }
    
    
Returns the FtpsConfiguration. This method exists only for convenient.

Returns:
ftpsConfiguration
        return (FtpsConfigurationgetConfiguration();
    }

    
Set the key store parameters
    public void setFtpClientKeyStoreParameters(Map<StringObjectparam) {
        this. = param;
    }

    
Set the trust store parameters
    public void setFtpClientTrustStoreParameters(Map<StringObjectparam) {
        this. = param;
    }

    
Gets the JSSE configuration that overrides any settings in ftpClientKeyStoreParameters, ftpClientTrustStoreParameters, and FtpsConfiguration.getSecurityProtocol().
        return ;
    }

    
Gets the JSSE configuration that overrides any settings in ftpClientKeyStoreParameters, ftpClientTrustStoreParameters, and FtpsConfiguration.getSecurityProtocol().
    public void setSslContextParameters(SSLContextParameters sslContextParameters) {
        this. = sslContextParameters;
    }
New to GrepCode? Check out our FAQ X