@Override public void afterPropertiesSet() throws Exception { if (ftpClient == null) { ftpClient = new FTPClient(); } ftpClient.configure(config); ftpClient.addProtocolCommandListener( new ProtocolCommandListener() { @Override public void protocolCommandSent(ProtocolCommandEvent event) { log.info("Send FTP command: " + event.getCommand()); } @Override public void protocolReplyReceived(ProtocolCommandEvent event) { log.info("Received FTP command reply: " + event.getReplyCode()); } }); }
/** * Opens a new connection and performs login with user name and password if set. * * @throws IOException */ protected void connectAndLogin() throws IOException { if (!ftpClient.isConnected()) { ftpClient.connect(getEndpointConfiguration().getHost(), getEndpointConfiguration().getPort()); log.info("Connected to FTP server: " + ftpClient.getReplyString()); int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { throw new CitrusRuntimeException("FTP server refused connection."); } log.info("Successfully opened connection to FTP server"); if (getEndpointConfiguration().getUser() != null) { log.info(String.format("Login as user: '******'", getEndpointConfiguration().getUser())); boolean login = ftpClient.login( getEndpointConfiguration().getUser(), getEndpointConfiguration().getPassword()); if (!login) { throw new CitrusRuntimeException( String.format( "Failed to login to FTP server using credentials: %s:%s", getEndpointConfiguration().getUser(), getEndpointConfiguration().getPassword())); } } } }
@Override public void destroy() throws Exception { if (ftpClient.isConnected()) { ftpClient.logout(); try { ftpClient.disconnect(); } catch (IOException e) { log.warn("Failed to disconnect from FTP server", e); } log.info("Successfully closed connection to FTP server"); } }
@Override public void send(Message message, TestContext context) { FtpMessage ftpMessage; if (message instanceof FtpMessage) { ftpMessage = (FtpMessage) message; } else { ftpMessage = new FtpMessage(message); } String correlationKeyName = getEndpointConfiguration().getCorrelator().getCorrelationKeyName(getName()); String correlationKey = getEndpointConfiguration().getCorrelator().getCorrelationKey(ftpMessage); correlationManager.saveCorrelationKey(correlationKeyName, correlationKey, context); log.info( String.format( "Sending FTP message to: ftp://'%s:%s'", getEndpointConfiguration().getHost(), getEndpointConfiguration().getPort())); if (log.isDebugEnabled()) { log.debug("Message to be sent:\n" + ftpMessage.getPayload().toString()); } try { connectAndLogin(); int reply = ftpClient.sendCommand(ftpMessage.getCommand(), ftpMessage.getArguments()); if (!FTPReply.isPositiveCompletion(reply) && !FTPReply.isPositivePreliminary(reply)) { throw new CitrusRuntimeException( String.format( "Failed to send FTP command - reply is: %s:%s", reply, ftpClient.getReplyString())); } log.info( String.format( "FTP message was successfully sent to: '%s:%s'", getEndpointConfiguration().getHost(), getEndpointConfiguration().getPort())); correlationManager.store( correlationKey, new FtpMessage(ftpMessage.getCommand(), ftpMessage.getArguments()) .replyCode(reply) .replyString(ftpClient.getReplyString())); } catch (IOException e) { throw new CitrusRuntimeException("Failed to execute ftp command", e); } }