com.japp.FtpConfigTest.java Source code

Java tutorial

Introduction

Here is the source code for com.japp.FtpConfigTest.java

Source

/*
 * Copyright 2002-2013 the original author or authors.
 *
 * Licensed 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 com.japp;

import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.File;
import java.io.IOException;

@ContextConfiguration(locations = { "classpath:/context-test.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class FtpConfigTest {

    private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(FtpConfigTest.class);

    @Value("#{test['ftp.serverAddress']}")
    private String serverAddress;
    @Value("#{test['ftp.userId']}")
    private String userId;
    @Value("#{test['ftp.password']}")
    private String password;
    @Value("#{test['ftp.remoteDirectory']}")
    private String remoteDirectory;
    @Value("#{batch['ftp.localDirectory']}")
    private String localDirectory;
    @Value("#{batch['ftp.localOutputDirectory']}")
    private String localOutputDirectory;
    @Value("#{batch['ftp.isSFTP']}")
    private String isSFTP;

    FTPClient ftp;
    final long pollingInterval = 5 * 1000;
    File folder;
    File[] listOfFiles;

    private int downloadFileAttempts = 12;

    private long retryIntervalMilliseconds = 300000;

    private boolean retryIfNotFound = true;

    @Before
    public void setUp() throws Exception {

        folder = new File(localOutputDirectory);

        ftp = new FTPClient();
        ftp.connect(serverAddress, 3333);

        //login to server
        if (!ftp.login(userId, password)) {
            ftp.logout();
        }

        int reply = ftp.getReplyCode();
        //FTPReply stores a set of constants for FTP reply codes.
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
        }

        //enter passive mode
        ftp.enterLocalPassiveMode();
        //get system name
        LOGGER.info("Remote system is " + ftp.getSystemType());
        ftp.changeWorkingDirectory(remoteDirectory);
        LOGGER.info("Current directory is " + ftp.printWorkingDirectory());

    }

    @Test
    public void fTPClientPushTest() throws Exception {

        FileAlterationObserver observer = new FileAlterationObserver(folder);
        FileAlterationMonitor monitor = new FileAlterationMonitor(pollingInterval);

        observer.addListener(listener);
        monitor.addObserver(observer);
        monitor.start();

    }

    FileAlterationListener listener = new FileAlterationListenerAdaptor() {
        // Is triggered when a file is created in the monitored folder
        @Override
        public void onFileCreate(File file) {
            try {
                // "file" is the reference to the newly created file
                LOGGER.info("File created: " + file.getCanonicalPath());
                LOGGER.info("will now process fttp push");
            } catch (IOException e) {
                e.printStackTrace(System.err);
            }
        }

        // do nothing
        @Override
        public void onFileDelete(File file) {
        }
    };

}