001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2.libcheck;
018
019import java.io.OutputStream;
020import java.nio.charset.Charset;
021
022import org.apache.commons.net.ftp.FTPClient;
023import org.apache.commons.net.ftp.FTPFile;
024import org.apache.commons.net.ftp.FTPReply;
025
026/**
027 * Basic check for FTP.
028 */
029public final class FtpCheck {
030
031    private static final int MAX_ARG_COUNT = 4;
032
033    /**
034     * Invokes this example from the command line.
035     *
036     * @param args Arguments TODO
037     * @throws Exception If anything goes wrong.
038     */
039    public static void main(final String[] args) throws Exception {
040        if (args.length < 3) {
041            throw new IllegalArgumentException("Usage: FtpCheck user pass host dir");
042        }
043        final String user = args[0];
044        final String pass = args[1];
045        final String host = args[2];
046        String dir = null;
047        if (args.length == MAX_ARG_COUNT) {
048            dir = args[3];
049        }
050
051        final FTPClient client = new FTPClient();
052        client.connect(host);
053        final int reply = client.getReplyCode();
054        if (!FTPReply.isPositiveCompletion(reply)) {
055            throw new IllegalArgumentException("cant connect: " + reply);
056        }
057        if (!client.login(user, pass)) {
058            throw new IllegalArgumentException("login failed");
059        }
060        client.enterLocalPassiveMode();
061
062        final OutputStream os = client.storeFileStream(dir + "/test.txt");
063        if (os == null) {
064            throw new IllegalStateException(client.getReplyString());
065        }
066        os.write("test".getBytes(Charset.defaultCharset()));
067        os.close();
068        client.completePendingCommand();
069
070        if (dir != null && !client.changeWorkingDirectory(dir)) {
071            throw new IllegalArgumentException("change dir to '" + dir + "' failed");
072        }
073
074        System.err.println("System: " + client.getSystemType());
075
076        final FTPFile[] files = client.listFiles();
077        for (int i = 0; i < files.length; i++) {
078            final FTPFile file = files[i];
079            if (file == null) {
080                System.err.println("#" + i + ": " + null);
081            } else {
082                System.err.println("#" + i + ": " + file.getRawListing());
083                System.err.println("#" + i + ": " + file);
084                System.err.println("\t name:" + file.getName() + " type:" + file.getType());
085            }
086        }
087        client.disconnect();
088    }
089
090    private FtpCheck() {
091        /* main class not instantiated. */
092    }
093}