/*
* $Id: AnyFtpClient.java,v 1.15 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.net;
import anvil.core.Any;
import anvil.core.AnyAbstractClass;
import anvil.core.io.AnyInputStream;
import anvil.core.io.AnyOutputStream;
import anvil.script.Context;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.net.InetAddress;
import sun.net.ftp.FtpClient;
///
/// @class FtpClient
/// Embedded FTP client.
///
/**
* class AnyFtpClient
*
* @author Jani Lehtim�ki
*/
public class AnyFtpClient extends AnyAbstractClass
{
///
/// @constructor FtpClient
/// Creates and returns FtpClient.
/// Optionally opens connection to given host and port.
/// If error occured null is returned.
/// @synopsis FtpClient( [string host, int port] )
/// @synopsis FtpClient( [InetAddress host, int port] )
/// @throws IOError if an IO error occured
public static final Object[] newInstance = { null, "*address", null, "*port", new Integer(0) };
public static final Any newInstance(Context context, Any host, int port)
{
try {
FtpClient client;
if (host != null) {
String hostname;
if (host instanceof AnyInetAddress) {
hostname = ((InetAddress)host.toObject()).getHostName();
} else {
hostname = host.toString();
}
if (port == 0) {
port = FtpClient.FTP_PORT;
}
context.checkConnect(hostname, port);
client = new FtpClient(hostname, port);
} else {
client = new FtpClient();
}
return new AnyFtpClient(client);
} catch (IOException e) {
throw context.exception(e);
}
}
private FtpClient _client;
public AnyFtpClient(FtpClient client)
{
_client = client;
}
public final anvil.script.ClassType classOf() {
return __class__;
}
public Object toObject()
{
return _client;
}
/// @method isOpen
/// Checks if connection is open.
/// @synopsis boolean isOpen()
public Any m_isOpen()
{
return _client.serverIsOpen() ? TRUE : FALSE;
}
/// @method close
/// Closes connection.
/// @synopsis boolean close()
/// @return true if ok
/// @throws IOError If operation failed
public Any m_close(Context context)
{
try {
_client.closeServer();
return TRUE;
} catch (IOException e) {
throw context.exception(e);
}
}
/// @method open
/// Opens connection to given 'host' (and 'port').
/// @synopsis boolean open(string host)
/// @synopsis boolean open(string host, int port )
/// @param host host name
/// @param port port number
/// @throws IOError If operation failed
/// @throws AccessDenied If security policy denies this operation
public static final Object[] p_open = { null, "host", "*port", new Integer(22) };
public Any m_open(Context context, String host, int port)
{
context.checkConnect(host, port);
try {
_client.openServer(host, port);
return this;
} catch (IOException e) {
throw context.exception(e);
}
}
/// @method login
/// Logs into server with given 'username' and 'password'.
/// @synopsis boolean login(string username, string password)
/// @param username
/// @param password
/// @throws IOError if login was not succesful
public static final Object[] p_login = { null, "username", "password" };
public Any m_login(Context context, String username, String password)
{
try {
_client.login(username, password);
return this;
} catch (IOException e) {
throw context.exception(e);
}
}
/// @method cd
/// Changes remote directory.
/// @synopsis void cd(string dir)
/// @param dir remote directory
/// @throws IOError If operation failed
public static final Object[] p_cd = { null, "dir" };
public Any m_cd(Context context, String dir)
{
try {
_client.cd(dir);
return this;
} catch (IOException e) {
throw context.exception(e);
}
}
/// @method cdUp
/// Moves to parent directory.
/// @synopsis void cdUp()
/// @throws IOError If operation failed
public Any m_cdUp(Context context)
{
try {
_client.cdUp();
return this;
} catch (IOException e) {
throw context.exception(e);
}
}
/// @method pwd
/// Returns the current directory.
/// @synopsis string pwd()
/// @throws IOError If operation failed
public Any m_pwd(Context context)
{
try {
return Any.create(_client.pwd());
} catch (IOException e) {
throw context.exception(e);
}
}
/// @method asc
/// Sets transfer type to 'A'.
/// @synopsis boolean asc()
/// @throws IOError If operation failed
public Any m_asc(Context context)
{
try {
_client.ascii();
return this;
} catch (IOException e) {
throw context.exception(e);
}
}
/// @method bin
/// Sets transfer type to 'B'.
/// @synopsis boolean bin()
/// @throws IOError If operation failed
public Any m_bin(Context context)
{
try {
_client.binary();
return this;
} catch (IOException e) {
throw context.exception(e);
}
}
/// @method list
/// Lists files (and information in current remote directory.
/// @synopsis InputStream list()
/// @return InputStream containing directory list
/// @throws IOError If operation failed
public Any m_list(Context context)
{
try {
return new AnyInputStream(_client.list());
} catch (IOException e) {
throw context.exception(e);
}
}
/// @method get
/// Gets and returns the input stream to given file.
/// Returned stream should not be closed because that
/// will close the connection as well.
/// @synopsis InputStream get(string fileName)
/// @param fileName file name
/// @return input stream to given file
/// @throws IOError if error occurred
public static final Object[] p_get = { null, "filename" };
public Any m_get(Context context, String filename)
{
try {
return new AnyInputStream(_client.get(filename));
} catch (IOException e) {
throw context.exception(e);
}
}
/// @method put
/// Puts a file into server and returns output stream to write to
/// @synopsis OutputStream put(string fileName)
/// @return output stream
/// @throws IOError If operation failed
public static final Object[] p_put = { null, "filename" };
public Any m_put(Context context, String filename)
{
try {
return new AnyOutputStream(_client.put(filename));
} catch (IOException e) {
throw context.exception(e);
}
}
/// @method append
/// Appends to a file in server and returns output stream to write to
/// @synopsis OutputStream append(string fileName)
/// @return output stream
/// @throws IOError If operation failed
public static final Object[] p_append = { null, "filename" };
public Any m_append(Context context, String filename)
{
try {
return new AnyOutputStream(_client.append(filename));
} catch (IOException e) {
throw context.exception(e);
}
}
/// @method rename
/// Renames a file in server.
/// @synopsis void rename(string oldName, string newName)
/// @throws IOError If operation failed
public static final Object[] p_rename = { null, "oldName", "newName" };
public Any m_rename(Context context, String oldName, String newName)
{
try {
_client.rename(oldName, newName);
return this;
} catch (IOException e) {
throw context.exception(e);
}
}
public static final anvil.script.compiler.NativeClass __class__ =
new anvil.script.compiler.NativeClass("FtpClient", AnyFtpClient.class,
//DOC{{
""+
"\n" +
" @class FtpClient\n" +
" Embedded FTP client.\n" +
"\n" +
"\n" +
" @constructor FtpClient\n" +
" Creates and returns FtpClient.\n" +
" Optionally opens connection to given host and port. \n" +
" If error occured null is returned. \n" +
" @synopsis FtpClient( [string host, int port] )\n" +
" @synopsis FtpClient( [InetAddress host, int port] )\n" +
" @throws IOError if an IO error occured\n" +
" @method isOpen\n" +
" Checks if connection is open.\n" +
" @synopsis boolean isOpen()\n" +
" @method close\n" +
" Closes connection. \n" +
" @synopsis boolean close()\n" +
" @return true if ok\n" +
" @throws IOError If operation failed\n" +
" @method open\n" +
" Opens connection to given 'host' (and 'port').\n" +
" @synopsis boolean open(string host) \n" +
" @synopsis boolean open(string host, int port ) \n" +
" @param host host name\n" +
" @param port port number\n" +
" @throws IOError If operation failed\n" +
" @throws AccessDenied If security policy denies this operation\n" +
" @method login\n" +
" Logs into server with given 'username' and 'password'.\n" +
" @synopsis boolean login(string username, string password)\n" +
" @param username\n" +
" @param password\n" +
" @throws IOError if login was not succesful\n" +
" @method cd\n" +
" Changes remote directory.\n" +
" @synopsis void cd(string dir)\n" +
" @param dir remote directory\n" +
" @throws IOError If operation failed\n" +
" @method cdUp\n" +
" Moves to parent directory.\n" +
" @synopsis void cdUp()\n" +
" @throws IOError If operation failed\n" +
" @method pwd\n" +
" Returns the current directory.\n" +
" @synopsis string pwd()\n" +
" @throws IOError If operation failed\n" +
" @method asc\n" +
" Sets transfer type to 'A'.\n" +
" @synopsis boolean asc() \n" +
" @throws IOError If operation failed\n" +
" @method bin\n" +
" Sets transfer type to 'B'.\n" +
" @synopsis boolean bin() \n" +
" @throws IOError If operation failed\n" +
" @method list\n" +
" Lists files (and information in current remote directory.\n" +
" @synopsis InputStream list()\n" +
" @return InputStream containing directory list\n" +
" @throws IOError If operation failed\n" +
" @method get\n" +
" Gets and returns the input stream to given file.\n" +
" Returned stream should not be closed because that\n" +
" will close the connection as well.\n" +
" @synopsis InputStream get(string fileName)\n" +
" @param fileName file name\n" +
" @return input stream to given file\n" +
" @throws IOError if error occurred\n" +
" @method put\n" +
" Puts a file into server and returns output stream to write to\n" +
" @synopsis OutputStream put(string fileName) \n" +
" @return output stream\n" +
" @throws IOError If operation failed\n" +
" @method append\n" +
" Appends to a file in server and returns output stream to write to\n" +
" @synopsis OutputStream append(string fileName) \n" +
" @return output stream\n" +
" @throws IOError If operation failed\n" +
" @method rename\n" +
" Renames a file in server.\n" +
" @synopsis void rename(string oldName, string newName) \n" +
" @throws IOError If operation failed\n"
//}}DOC
);
static {
NetModule.class.getName();
}
}