Search in sources :

Example 1 with FTPEndpointProperties

use of io.irontest.models.endpoint.FTPEndpointProperties in project irontest by zheng-wang.

the class FTPTeststepRunner method put.

private void put(Endpoint endpoint, FtpPutRequest ftpPutRequest) throws IOException {
    String username = StringUtils.trimToEmpty(endpoint.getUsername());
    String remoteFilePath = StringUtils.trimToEmpty(ftpPutRequest.getRemoteFilePath());
    byte[] fileBytes;
    // validate arguments
    if ("".equals(username)) {
        throw new IllegalArgumentException("Username not specified in Endpoint.");
    } else if ("".equals(remoteFilePath)) {
        throw new IllegalArgumentException("Target File Path not specified.");
    }
    if (ftpPutRequest instanceof FtpPutRequestFileFromText) {
        FtpPutRequestFileFromText ftpPutRequestFileFromText = (FtpPutRequestFileFromText) ftpPutRequest;
        String fileContent = ftpPutRequestFileFromText.getFileContent();
        // validate arguments
        if ("".equals(StringUtils.trimToEmpty(fileContent))) {
            throw new IllegalArgumentException("No file content.");
        }
        fileBytes = fileContent.getBytes();
    } else {
        FtpPutRequestFileFromFile ftpPutRequestFileFromFile = (FtpPutRequestFileFromFile) ftpPutRequest;
        fileBytes = ftpPutRequestFileFromFile.getFileContent();
        // validate arguments
        if (fileBytes == null || fileBytes.length == 0) {
            throw new IllegalArgumentException("No file content.");
        }
    }
    FTPEndpointProperties endpointProperties = (FTPEndpointProperties) endpoint.getOtherProperties();
    String password = getDecryptedEndpointPassword();
    FTPClient ftpClient;
    if (endpointProperties.isUseSSL()) {
        ftpClient = new FTPSClient();
        ((FTPSClient) ftpClient).setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
    } else {
        ftpClient = new FTPClient();
    }
    ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
    ftpClient.addProtocolCommandListener(new ProtocolCommandListener() {

        @Override
        public void protocolCommandSent(ProtocolCommandEvent event) {
        }

        @Override
        public void protocolReplyReceived(ProtocolCommandEvent event) {
            if (FTPReply.isNegativePermanent(event.getReplyCode())) {
                throw new RuntimeException("Failed to put the file. " + event.getMessage());
            }
        }
    });
    try {
        ftpClient.connect(endpoint.getHost(), endpoint.getPort());
        ftpClient.login(username, password);
        ftpClient.enterLocalPassiveMode();
        if (endpointProperties.isUseSSL()) {
            ((FTPSClient) ftpClient).execPROT("P");
        }
        ftpClient.storeFile(remoteFilePath, new ByteArrayInputStream(fileBytes));
    } finally {
        ftpClient.disconnect();
    }
}
Also used : ProtocolCommandEvent(org.apache.commons.net.ProtocolCommandEvent) FTPClient(org.apache.commons.net.ftp.FTPClient) ByteArrayInputStream(java.io.ByteArrayInputStream) FTPEndpointProperties(io.irontest.models.endpoint.FTPEndpointProperties) FTPSClient(org.apache.commons.net.ftp.FTPSClient) PrintCommandListener(org.apache.commons.net.PrintCommandListener) ProtocolCommandListener(org.apache.commons.net.ProtocolCommandListener) PrintWriter(java.io.PrintWriter)

Aggregations

FTPEndpointProperties (io.irontest.models.endpoint.FTPEndpointProperties)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 PrintWriter (java.io.PrintWriter)1 PrintCommandListener (org.apache.commons.net.PrintCommandListener)1 ProtocolCommandEvent (org.apache.commons.net.ProtocolCommandEvent)1 ProtocolCommandListener (org.apache.commons.net.ProtocolCommandListener)1 FTPClient (org.apache.commons.net.ftp.FTPClient)1 FTPSClient (org.apache.commons.net.ftp.FTPSClient)1