Example usage for org.apache.commons.net.ftp FTPSClient FTPSClient

List of usage examples for org.apache.commons.net.ftp FTPSClient FTPSClient

Introduction

In this page you can find the example usage for org.apache.commons.net.ftp FTPSClient FTPSClient.

Prototype

public FTPSClient() 

Source Link

Document

Constructor for FTPSClient, calls #FTPSClient(String,boolean) .

Usage

From source file:password.pwm.svc.telemetry.FtpTelemetrySender.java

private void ftpPut(final TelemetryPublishBean telemetryPublishBean) throws PwmUnrecoverableException {
    final FTPClient ftpClient;
    switch (settings.getFtpMode()) {
    case ftp:/*from  w ww.j  av a 2 s  .c o m*/
        ftpClient = new FTPClient();
        break;

    case ftps:
        ftpClient = new FTPSClient();
        break;

    default:
        JavaHelper.unhandledSwitchStatement(settings.getFtpMode());
        throw new UnsupportedOperationException();
    }

    // connect
    try {
        LOGGER.trace(SessionLabel.TELEMETRY_SESSION_LABEL,
                "establishing " + settings.getFtpMode() + " connection to " + settings.getHost());
        ftpClient.connect(settings.getHost());

        final int reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            disconnectFtpClient(ftpClient);
            final String msg = "error " + reply + " connecting to " + settings.getHost();
            throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_TELEMETRY_SEND_ERROR, msg));
        }

        LOGGER.trace(SessionLabel.TELEMETRY_SESSION_LABEL, "connected to " + settings.getHost());
    } catch (IOException e) {
        disconnectFtpClient(ftpClient);
        final String msg = "unable to connect to " + settings.getHost() + ", error: " + e.getMessage();
        throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_TELEMETRY_SEND_ERROR, msg));
    }

    // set modes
    try {
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        final int reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            disconnectFtpClient(ftpClient);
            final String msg = "error setting file type mode to binary, error=" + reply;
            throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_TELEMETRY_SEND_ERROR, msg));
        }
    } catch (IOException e) {
        disconnectFtpClient(ftpClient);
        final String msg = "unable to connect to " + settings.getHost() + ", error: " + e.getMessage();
        throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_TELEMETRY_SEND_ERROR, msg));
    }

    // authenticate
    try {
        ftpClient.login(settings.getUsername(), settings.getPassword());

        final int reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            disconnectFtpClient(ftpClient);
            final String msg = "error authenticating as " + settings.getUsername() + " to " + settings.getHost()
                    + ", error=" + reply;
            throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_TELEMETRY_SEND_ERROR, msg));
        }

        LOGGER.trace(SessionLabel.TELEMETRY_SESSION_LABEL,
                "authenticated to " + settings.getHost() + " as " + settings.getUsername());
    } catch (IOException e) {
        disconnectFtpClient(ftpClient);
        final String msg = "error authenticating as " + settings.getUsername() + " to " + settings.getHost()
                + ", error: " + e.getMessage();
        throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_TELEMETRY_SEND_ERROR, msg));
    }

    // upload
    try {
        final String filePath = settings.getPath() + "/" + telemetryPublishBean.getId() + ".zip";
        final byte[] fileBytes = dataToJsonZipFile(telemetryPublishBean);
        final ByteArrayInputStream fileStream = new ByteArrayInputStream(fileBytes);

        LOGGER.trace(SessionLabel.TELEMETRY_SESSION_LABEL,
                "preparing to transfer " + fileBytes.length + " bytes to file path " + filePath);

        final Instant startTime = Instant.now();
        ftpClient.storeFile(filePath, fileStream);

        final int reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            disconnectFtpClient(ftpClient);
            final String msg = "error uploading file  to " + settings.getHost() + ", error=" + reply;
            throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_TELEMETRY_SEND_ERROR, msg));
        }

        LOGGER.trace(SessionLabel.TELEMETRY_SESSION_LABEL, "completed transfer of " + fileBytes.length + " in "
                + TimeDuration.compactFromCurrent(startTime));
    } catch (IOException e) {
        disconnectFtpClient(ftpClient);
        final String msg = "error uploading file  to " + settings.getHost() + ", error: " + e.getMessage();
        throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_TELEMETRY_SEND_ERROR, msg));
    }
}

From source file:pl.edu.icm.maven.wagon.provider.FTPSWagon.java

protected void openConnectionInternal() throws ConnectionException, AuthenticationException {
    AuthenticationInfo authInfo = getAuthenticationInfo();

    if (authInfo == null) {
        throw new IllegalArgumentException("Authentication Credentials cannot be null for FTP protocol");
    }//  w  w w  .  ja v a2s  .  c  o m

    if (authInfo.getUserName() == null) {
        authInfo.setUserName(System.getProperty("user.name"));
    }

    String username = authInfo.getUserName();

    String password = authInfo.getPassword();

    if (username == null) {
        throw new AuthenticationException("Username not specified for repository " + getRepository().getId());
    }
    if (password == null) {
        throw new AuthenticationException("Password not specified for repository " + getRepository().getId());
    }

    String host = getRepository().getHost();

    FTPSClient ftps = new FTPSClient();
    ftps.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
    ftp = ftps;
    ftp.setDefaultTimeout(getTimeout());
    ftp.setDataTimeout(getTimeout());
    ftp.setControlEncoding(getControlEncoding());

    ftp.addProtocolCommandListener(new PrintCommandListener(this));

    try {
        if (getRepository().getPort() != WagonConstants.UNKNOWN_PORT) {
            ftp.connect(host, getRepository().getPort());
        } else {
            ftp.connect(host);
        }

        // After connection attempt, you should check the reply code to
        // verify
        // success.
        int reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();

            throw new AuthenticationException("FTP server refused connection.");
        }
    } catch (IOException e) {
        if (ftp.isConnected()) {
            try {
                fireSessionError(e);

                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }

        throw new AuthenticationException("Could not connect to server.", e);
    }

    try {
        if (!ftp.login(username, password)) {
            throw new AuthenticationException("Cannot login to remote system");
        }

        fireSessionDebug("Remote system is " + ftp.getSystemType());

        // Set to binary mode.
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.setListHiddenFiles(true);

        // Use passive mode as default because most of us are
        // behind firewalls these days.
        if (isPassiveMode()) {
            ftp.enterLocalPassiveMode();
        }
    } catch (IOException e) {
        throw new ConnectionException("Cannot login to remote system", e);
    }
}