Search in sources :

Example 1 with FTPConnectionClosedException

use of org.apache.commons.net.ftp.FTPConnectionClosedException in project ddf by codice.

the class TestFtp method setClientAuthConfiguration.

/**
     * Sets the clientAuth configuration in catalog-ftp feature.
     * <p/>
     * An FTPS client without a certificate is used to indicate when the clientAuth configuration
     * has taken effect at the FTP server level.
     * -    When the clientAuth is set to "want", this FTPS client without a certificate should be
     * able to connect to the FTP server and successfully complete the SSL handshake.
     * -    When the clientAuth is set to "need", this FTPS client without a certificate should be
     * able to connect to the FTP server but fail to complete the SSL handshake.
     * <p/>
     * SocketException and FTPConnectionClosedException are thrown when the client cannot connect to
     * the server or the connection was closed unexpectedly. These exceptions are thrown when the
     * server is being updated. SSLException is thrown only after a client has successfully
     * connected and when the SSL handshake between the client and server fails.
     *
     * @throws Exception
     */
private void setClientAuthConfiguration(String clientAuth) throws Exception {
    Configuration config = getAdminConfig().getConfiguration("ddf.catalog.ftp.FtpServerStarter");
    config.setBundleLocation("mvn:ddf.catalog/ftp/" + System.getProperty("ddf.version"));
    Dictionary properties = new Hashtable<>();
    properties.put(CLIENT_AUTH, clientAuth);
    config.update(properties);
    //wait until the clientAuth configuration has taken effect at the FTP server level
    switch(clientAuth) {
        case WANT:
            expect("SSL handshake to succeed with FTPS client without certificate because clientAuth = \"want\"").within(SET_CLIENT_AUTH_TIMEOUT_SEC, TimeUnit.SECONDS).until(() -> {
                FTPSClient client = null;
                try {
                    client = createSecureClient(false);
                    disconnectClient(client);
                    return true;
                } catch (SSLException e) {
                    //SSL handshake failed
                    return false;
                } catch (SocketException | FTPConnectionClosedException e) {
                    //connection failed
                    return false;
                }
            });
            break;
        case NEED:
            expect("SSL handshake to fail with FTPS client without certificate because clientAuth = \"need\"").within(SET_CLIENT_AUTH_TIMEOUT_SEC, TimeUnit.SECONDS).until(() -> {
                FTPSClient client = null;
                try {
                    client = createSecureClient(false);
                    disconnectClient(client);
                    return false;
                } catch (SSLException e) {
                    //SSL handshake failed
                    return true;
                } catch (SocketException | FTPConnectionClosedException e) {
                    //connection failed
                    return false;
                }
            });
    }
}
Also used : Dictionary(java.util.Dictionary) SocketException(java.net.SocketException) Configuration(org.osgi.service.cm.Configuration) FTPConnectionClosedException(org.apache.commons.net.ftp.FTPConnectionClosedException) Hashtable(java.util.Hashtable) FTPSClient(org.apache.commons.net.ftp.FTPSClient) SSLException(javax.net.ssl.SSLException)

Aggregations

SocketException (java.net.SocketException)1 Dictionary (java.util.Dictionary)1 Hashtable (java.util.Hashtable)1 SSLException (javax.net.ssl.SSLException)1 FTPConnectionClosedException (org.apache.commons.net.ftp.FTPConnectionClosedException)1 FTPSClient (org.apache.commons.net.ftp.FTPSClient)1 Configuration (org.osgi.service.cm.Configuration)1