Moving to root below branch label

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2005 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2005-12-08 07:13:07 +00:00
commit e1e6508fec
1095 changed files with 230566 additions and 0 deletions

View File

@@ -0,0 +1,120 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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 org.alfresco.filesys.ftp;
/**
* FTP Command Types Class
*
* @author GKSpencer
*/
public class FTPCommand
{
// Command ids
public final static int User = 0;
public final static int Pass = 1;
public final static int Acct = 2;
public final static int Cwd = 3;
public final static int Cdup = 4;
public final static int Smnt = 5;
public final static int Rein = 6;
public final static int Quit = 7;
public final static int Port = 8;
public final static int Pasv = 9;
public final static int Type = 10;
public final static int Stru = 11;
public final static int Mode = 12;
public final static int Retr = 13;
public final static int Stor = 14;
public final static int Stou = 15;
public final static int Appe = 16;
public final static int Allo = 17;
public final static int Rest = 18;
public final static int Rnfr = 19;
public final static int Rnto = 20;
public final static int Abor = 21;
public final static int Dele = 22;
public final static int Rmd = 23;
public final static int Mkd = 24;
public final static int Pwd = 25;
public final static int List = 26;
public final static int Nlst = 27;
public final static int Site = 28;
public final static int Syst = 29;
public final static int Stat = 30;
public final static int Help = 31;
public final static int Noop = 32;
public final static int Mdtm = 33;
public final static int Size = 34;
public final static int Opts = 35;
public final static int Feat = 36;
public final static int XPwd = 37;
public final static int XMkd = 38;
public final static int XRmd = 39;
public final static int XCup = 40;
public final static int XCwd = 41;
public final static int MaxId = 41;
public final static int InvalidCmd = -1;
// Command name strings
private static final String[] _cmds = { "USER", "PASS", "ACCT", "CWD", "CDUP", "SMNT", "REIN", "QUIT", "PORT",
"PASV", "TYPE", "STRU", "MODE", "RETR", "STOR", "STOU", "APPE", "ALLO", "REST", "RNFR", "RNTO", "ABOR",
"DELE", "RMD", "MKD", "PWD", "LIST", "NLST", "SITE", "SYST", "STAT", "HELP", "NOOP", "MDTM", "SIZE",
"OPTS", "FEAT", "XPWD", "XMKD", "XRMD", "XCUP", "XCWD" };
/**
* Convert an FTP command to an id
*
* @param cmd String
* @return int
*/
public final static int getCommandId(String cmd)
{
// Check if the command is valid
if (cmd == null)
return InvalidCmd;
// Convert to a command id
for (int i = 0; i <= MaxId; i++)
if (_cmds[i].equalsIgnoreCase(cmd))
return i;
// Command not found
return InvalidCmd;
}
/**
* Return the FTP command name for the specified id
*
* @param id int
* @return String
*/
public final static String getCommandName(int id)
{
if (id < 0 || id > MaxId)
return null;
return _cmds[id];
}
}

View File

@@ -0,0 +1,369 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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 org.alfresco.filesys.ftp;
import java.net.*;
import java.io.*;
/**
* FTP Data Session Class
* <p>
* A data connection is made when a PORT or PASV FTP command is received on the main control
* session.
* <p>
* The PORT command will actively connect to the specified address/port on the client. The PASV
* command will create a listening socket and wait for the client to connect.
*
* @author GKSpencer
*/
public class FTPDataSession implements Runnable
{
// FTP session that this data connection is associated with
private FTPSrvSession m_cmdSess;
// Connection details for active connection
private InetAddress m_clientAddr;
private int m_clientPort;
// Local port to use
private int m_localPort;
// Active data session socket
private Socket m_activeSock;
// Passive data session socket
private ServerSocket m_passiveSock;
// Adapter to bind the passive socket to
private InetAddress m_bindAddr;
// Transfer in progress and abort file transfer flags
private boolean m_transfer;
private boolean m_abort;
// Send/receive data byte count
private long m_bytCount;
/**
* Class constructor
* <p>
* Create a data connection that listens for an incoming connection.
*
* @param sess FTPSrvSession
* @exception IOException
*/
protected FTPDataSession(FTPSrvSession sess) throws IOException
{
// Set the associated command session
m_cmdSess = sess;
// Create a server socket to listen for the incoming connection
m_passiveSock = new ServerSocket(0, 1, null);
}
/**
* Class constructor
* <p>
* Create a data connection that listens for an incoming connection on the specified network
* adapter and local port.
*
* @param sess FTPSrvSession
* @param localPort int
* @param addr InetAddress
* @exception IOException
*/
protected FTPDataSession(FTPSrvSession sess, int localPort, InetAddress bindAddr) throws IOException
{
// Set the associated command session
m_cmdSess = sess;
// Create a server socket to listen for the incoming connection on the specified network
// adapter
m_localPort = localPort;
m_passiveSock = new ServerSocket(localPort, 1, bindAddr);
}
/**
* Class constructor
* <p>
* Create a data connection that listens for an incoming connection on the specified network
* adapter.
*
* @param sess FTPSrvSession
* @param addr InetAddress
* @exception IOException
*/
protected FTPDataSession(FTPSrvSession sess, InetAddress bindAddr) throws IOException
{
// Set the associated command session
m_cmdSess = sess;
// Create a server socket to listen for the incoming connection on the specified network
// adapter
m_passiveSock = new ServerSocket(0, 1, bindAddr);
}
/**
* Class constructor
* <p>
* Create a data connection to the specified client address and port.
*
* @param sess FTPSrvSession
* @param addr InetAddress
* @param port int
*/
protected FTPDataSession(FTPSrvSession sess, InetAddress addr, int port)
{
// Set the associated command session
m_cmdSess = sess;
// Save the client address/port details, the actual connection will be made later when
// the client requests/sends a file
m_clientAddr = addr;
m_clientPort = port;
}
/**
* Class constructor
* <p>
* Create a data connection to the specified client address and port, using the specified local
* port.
*
* @param sess FTPSrvSession
* @param localPort int
* @param addr InetAddress
* @param port int
*/
protected FTPDataSession(FTPSrvSession sess, int localPort, InetAddress addr, int port)
{
// Set the associated command session
m_cmdSess = sess;
// Save the local port
m_localPort = localPort;
// Save the client address/port details, the actual connection will be made later when
// the client requests/sends a file
m_clientAddr = addr;
m_clientPort = port;
}
/**
* Return the associated command session
*
* @return FTPSrvSession
*/
public final FTPSrvSession getCommandSession()
{
return m_cmdSess;
}
/**
* Return the local port
*
* @return int
*/
public final int getLocalPort()
{
if (m_passiveSock != null)
return m_passiveSock.getLocalPort();
else if (m_activeSock != null)
return m_activeSock.getLocalPort();
return -1;
}
/**
* Return the port that was allocated to the data session
*
* @return int
*/
protected final int getAllocatedPort()
{
return m_localPort;
}
/**
* Return the passive server socket address
*
* @return InetAddress
*/
public final InetAddress getPassiveAddress()
{
if (m_passiveSock != null)
{
// Get the server socket local address
InetAddress addr = m_passiveSock.getInetAddress();
if (addr.getHostAddress().compareTo("0.0.0.0") == 0)
{
try
{
addr = InetAddress.getLocalHost();
}
catch (UnknownHostException ex)
{
}
}
return addr;
}
return null;
}
/**
* Return the passive server socket port
*
* @return int
*/
public final int getPassivePort()
{
if (m_passiveSock != null)
return m_passiveSock.getLocalPort();
return -1;
}
/**
* Determine if a file transfer is active
*
* @return boolean
*/
public final boolean isTransferActive()
{
return m_transfer;
}
/**
* Abort an in progress file transfer
*/
public final void abortTransfer()
{
m_abort = true;
}
/**
* Return the transfer byte count
*
* @return long
*/
public final synchronized long getTransferByteCount()
{
return m_bytCount;
}
/**
* Return the data socket connected to the client
*
* @return Socket
* @exception IOException
*/
public final Socket getSocket() throws IOException
{
// Check for a passive connection, get the incoming socket connection
if (m_passiveSock != null)
m_activeSock = m_passiveSock.accept();
else
{
if (m_localPort != 0)
{
// Use the specified local port
m_activeSock = new Socket(m_clientAddr, m_clientPort, null, m_localPort);
}
else
m_activeSock = new Socket(m_clientAddr, m_clientPort);
}
// Set the socket to close immediately
m_activeSock.setSoLinger(false, 0);
m_activeSock.setTcpNoDelay(true);
// Return the data socket
return m_activeSock;
}
/**
* Close the data connection
*/
public final void closeSession()
{
// If the data connection is active close it
if (m_activeSock != null)
{
try
{
m_activeSock.close();
}
catch (Exception ex)
{
}
m_activeSock = null;
}
// Close the listening socket for a passive connection
if (m_passiveSock != null)
{
try
{
m_passiveSock.close();
}
catch (Exception ex)
{
}
m_passiveSock = null;
}
}
/**
* Run a file send/receive in a seperate thread
*/
public void run()
{
}
}

View File

@@ -0,0 +1,107 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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 org.alfresco.filesys.ftp;
import java.util.*;
/**
* FTP Date Utility Class
*
* @author GKSpencer
*/
public class FTPDate
{
// Constants
//
// Six months in ticks
protected final static long SIX_MONTHS = 183L * 24L * 60L * 60L * 1000L;
// Month names
protected final static String[] _months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
"Nov", "Dec" };
/**
* Pack a date string in Unix format The format is 'Mmm dd hh:mm' if the file is less than six
* months old, else the format is 'Mmm dd yyyy'.
*
* @param buf StringBuffer
* @param dt Date
*/
public final static void packUnixDate(StringBuffer buf, Date dt)
{
// Check if the date is valid
if (dt == null)
{
buf.append("------------");
return;
}
// Get the time raw value
long timeVal = dt.getTime();
if (timeVal < 0)
{
buf.append("------------");
return;
}
// Add the month name and date parts to the string
Calendar cal = new GregorianCalendar();
cal.setTime(dt);
buf.append(_months[cal.get(Calendar.MONTH)]);
buf.append(" ");
int dayOfMonth = cal.get(Calendar.DATE);
if (dayOfMonth < 10)
buf.append(" ");
buf.append(dayOfMonth);
buf.append(" ");
// If the file is less than six months old we append the file time, else we append the year
long timeNow = System.currentTimeMillis();
if (Math.abs(timeNow - timeVal) > SIX_MONTHS)
{
// Append the year
buf.append(cal.get(Calendar.YEAR));
}
else
{
// Append the file time as hh:mm
int hr = cal.get(Calendar.HOUR_OF_DAY);
if (hr < 10)
buf.append("0");
buf.append(hr);
buf.append(":");
int sec = cal.get(Calendar.SECOND);
if (sec < 10)
buf.append("0");
buf.append(sec);
}
}
}

View File

@@ -0,0 +1,590 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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 org.alfresco.filesys.ftp;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Enumeration;
import org.alfresco.filesys.server.ServerListener;
import org.alfresco.filesys.server.SrvSession;
import org.alfresco.filesys.server.config.ServerConfiguration;
import org.alfresco.filesys.server.core.SharedDeviceList;
import org.alfresco.filesys.server.filesys.NetworkFileServer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* <p>
* Create an FTP server on the specified port. The default server port is 21.
*
* @author GKSpencer
*/
public class FTPNetworkServer extends NetworkFileServer implements Runnable
{
// Debug logging
private static final Log logger = LogFactory.getLog("org.alfresco.ftp.protocol");
// Constants
//
// Server version
private static final String ServerVersion = "3.5.0";
// Listen backlog for the server socket
protected static final int LISTEN_BACKLOG = 10;
// Default FTP server port
protected static final int SERVER_PORT = 21;
// Server socket
private ServerSocket m_srvSock;
// Active session list
private FTPSessionList m_sessions;
// List of available shares
private SharedDeviceList m_shares;
// Next available session id
private int m_sessId;
// Root path for new sessions
private FTPPath m_rootPath;
// FTP server thread
private Thread m_srvThread;
// Local server address string, in FTP format (ie. n,n,n,n)
private String m_localFTPaddress;
/**
* Class constructor
*
* @param serviceResgistry ServiceRegistry
* @param config ServerConfiguration
*/
public FTPNetworkServer(ServerConfiguration config)
{
super("FTP", config);
// Set the server version
setVersion(ServerVersion);
// Allocate the session lists
m_sessions = new FTPSessionList();
// Enable debug
if (getConfiguration().getFTPDebug() != 0)
setDebug(true);
// Create the root path, if configured
if (getConfiguration().hasFTPRootPath())
{
try
{
// Create the root path
m_rootPath = new FTPPath(getConfiguration().getFTPRootPath());
}
catch (InvalidPathException ex)
{
logger.error(ex);
}
}
}
/**
* Add a new session to the server
*
* @param sess FTPSrvSession
*/
protected final void addSession(FTPSrvSession sess)
{
// Add the session to the session list
m_sessions.addSession(sess);
// Propagate the debug settings to the new session
if (hasDebug())
{
// Enable session debugging, output to the same stream as the server
sess.setDebug(getConfiguration().getFTPDebug());
}
}
/**
* emove a session from the server
*
* @param sess FTPSrvSession
*/
protected final void removeSession(FTPSrvSession sess)
{
// Remove the session from the active session list
if (m_sessions.removeSession(sess) != null)
{
// Inform listeners that a session has closed
fireSessionClosedEvent(sess);
}
}
/**
* Allocate a local port for a data session
*
* @param sess FTPSrvSession
* @param remAddr InetAddress
* @param remPort int
* @return FTPDataSession
* @exception IOException
*/
protected final FTPDataSession allocateDataSession(FTPSrvSession sess, InetAddress remAddr, int remPort)
throws IOException
{
// Create a new FTP data session
FTPDataSession dataSess = null;
if (remAddr != null)
{
// Create a normal data session
dataSess = new FTPDataSession(sess, remAddr, remPort);
}
else
{
// Create a passive data session
dataSess = new FTPDataSession(sess, getBindAddress());
}
// Return the data session
return dataSess;
}
/**
* Release a data session
*
* @param dataSess FTPDataSession
*/
protected final void releaseDataSession(FTPDataSession dataSess)
{
// Close the data session
dataSess.closeSession();
}
/**
* Get the shared device list
*
* @return SharedDeviceList
*/
public final SharedDeviceList getShareList()
{
// Check if the share list has been populated
if (m_shares == null)
m_shares = getConfiguration().getShareMapper()
.getShareList(getConfiguration().getServerName(), null, false);
// Return the share list
return m_shares;
}
/**
* Check if the FTP server is to be bound to a specific network adapter
*
* @return boolean
*/
public final boolean hasBindAddress()
{
return getConfiguration().getFTPBindAddress() != null ? true : false;
}
/**
* Return the address that the FTP server should bind to
*
* @return InetAddress
*/
public final InetAddress getBindAddress()
{
return getConfiguration().getFTPBindAddress();
}
/**
* Check if the root path is set
*
* @return boolean
*/
public final boolean hasRootPath()
{
return m_rootPath != null ? true : false;
}
/**
* Check if anonymous logins are allowed
*
* @return boolean
*/
public final boolean allowAnonymous()
{
return getConfiguration().allowAnonymousFTP();
}
/**
* Return the anonymous login user name
*
* @return String
*/
public final String getAnonymousAccount()
{
return getConfiguration().getAnonymousFTPAccount();
}
/**
* Return the local FTP server address string in n,n,n,n format
*
* @return String
*/
public final String getLocalFTPAddressString()
{
return m_localFTPaddress;
}
/**
* Return the next available session id
*
* @return int
*/
protected final synchronized int getNextSessionId()
{
return m_sessId++;
}
/**
* Return the FTP server port
*
* @return int
*/
public final int getPort()
{
return getConfiguration().getFTPPort();
}
/**
* Return the server socket
*
* @return ServerSocket
*/
protected final ServerSocket getSocket()
{
return m_srvSock;
}
/**
* Return the root path for new sessions
*
* @return FTPPath
*/
public final FTPPath getRootPath()
{
return m_rootPath;
}
/**
* Notify the server that a user has logged on.
*
* @param sess SrvSession
*/
protected final void sessionLoggedOn(SrvSession sess)
{
// Notify session listeners that a user has logged on.
fireSessionLoggedOnEvent(sess);
}
/**
* Start the SMB server.
*/
public void run()
{
// Debug
if (logger.isDebugEnabled() && hasDebug())
{
logger.debug("FTP Server starting on port " + getPort());
logger.debug("Version " + isVersion());
}
// Create a server socket to listen for incoming FTP session requests
try
{
// Create the server socket to listen for incoming FTP session requests
if (hasBindAddress())
m_srvSock = new ServerSocket(getPort(), LISTEN_BACKLOG, getBindAddress());
else
m_srvSock = new ServerSocket(getPort(), LISTEN_BACKLOG);
// DEBUG
if (logger.isDebugEnabled() && hasDebug())
{
String ftpAddr = "ALL";
if (hasBindAddress())
ftpAddr = getBindAddress().getHostAddress();
logger.debug("FTP Binding to local address " + ftpAddr);
}
// If a bind address is set then we can set the FTP local address
if (hasBindAddress())
m_localFTPaddress = getBindAddress().getHostAddress().replace('.', ',');
// Indicate that the server is active
setActive(true);
fireServerEvent(ServerListener.ServerActive);
// Wait for incoming connection requests
while (hasShutdown() == false)
{
// Wait for a connection
Socket sessSock = getSocket().accept();
// Set the local address string in FTP format (n,n,n,n), if not already set
if (m_localFTPaddress == null)
{
if (sessSock.getLocalAddress() != null)
m_localFTPaddress = sessSock.getLocalAddress().getHostAddress().replace('.', ',');
}
// Set socket options
sessSock.setTcpNoDelay(true);
// Debug
if (logger.isDebugEnabled() && hasDebug())
logger.debug("FTP session request received from "
+ sessSock.getInetAddress().getHostAddress());
// Create a server session for the new request, and set the session id.
FTPSrvSession srvSess = new FTPSrvSession(sessSock, this);
srvSess.setSessionId(getNextSessionId());
srvSess.setUniqueId("FTP" + srvSess.getSessionId());
srvSess.setDebugPrefix("[FTP" + srvSess.getSessionId() + "] ");
// Initialize the root path for the new session, if configured
if (hasRootPath())
srvSess.setRootPath(getRootPath());
// Add the session to the active session list
addSession(srvSess);
// Inform listeners that a new session has been created
fireSessionOpenEvent(srvSess);
// Start the new session in a seperate thread
Thread srvThread = new Thread(srvSess);
srvThread.setDaemon(true);
srvThread.setName("Sess_FTP" + srvSess.getSessionId() + "_"
+ sessSock.getInetAddress().getHostAddress());
srvThread.start();
// Sleep for a while
try
{
Thread.sleep(1000L);
}
catch (InterruptedException ex)
{
}
}
}
catch (SocketException ex)
{
// Do not report an error if the server has shutdown, closing the server socket
// causes an exception to be thrown.
if (hasShutdown() == false)
{
logger.error("FTP Socket error", ex);
// Inform listeners of the error, store the exception
setException(ex);
fireServerEvent(ServerListener.ServerError);
}
}
catch (Exception ex)
{
// Do not report an error if the server has shutdown, closing the server socket
// causes an exception to be thrown.
if (hasShutdown() == false)
{
logger.error("FTP Server error", ex);
}
// Inform listeners of the error, store the exception
setException(ex);
fireServerEvent(ServerListener.ServerError);
}
// Close the active sessions
Enumeration enm = m_sessions.enumerate();
while (enm.hasMoreElements())
{
// Get the session id and associated session
Integer sessId = (Integer) enm.nextElement();
FTPSrvSession sess = m_sessions.findSession(sessId);
// DEBUG
if (logger.isDebugEnabled() && hasDebug())
logger.debug("FTP Close session, id = " + sess.getSessionId());
// Close the session
sess.closeSession();
}
// Debug
if (logger.isDebugEnabled() && hasDebug())
logger.debug("FTP Server shutting down ...");
// Indicate that the server has shutdown, inform listeners
setActive(false);
fireServerEvent(ServerListener.ServerShutdown);
}
/**
* Shutdown the FTP server
*
* @param immediate boolean
*/
public void shutdownServer(boolean immediate)
{
// Set the shutdown flag
setShutdown(true);
// Close the FTP server listening socket to wakeup the main FTP server thread
try
{
if (getSocket() != null)
getSocket().close();
}
catch (IOException ex)
{
}
// Wait for the main server thread to close
if (m_srvThread != null)
{
try
{
m_srvThread.join(3000);
}
catch (Exception ex)
{
}
}
// Fire a shutdown notification event
fireServerEvent(ServerListener.ServerShutdown);
}
/**
* Start the FTP server in a seperate thread
*/
public void startServer()
{
// Create a seperate thread to run the FTP server
m_srvThread = new Thread(this);
m_srvThread.setName("FTP Server");
m_srvThread.start();
// Fire a server startup event
fireServerEvent(ServerListener.ServerStartup);
}
}

View File

@@ -0,0 +1,580 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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 org.alfresco.filesys.ftp;
import org.alfresco.filesys.server.filesys.*;
import org.alfresco.filesys.server.core.*;
/**
* FTP Path Class
* <p>
* Converts FTP paths to share/share relative paths.
*
* @author GKSpencer
*/
public class FTPPath
{
// FTP directory seperator
private static final String FTP_SEPERATOR = "/";
private static final char FTP_SEPERATOR_CHAR = '/';
// Share relative path directory seperator
private static final String DIR_SEPERATOR = "\\";
private static final char DIR_SEPERATOR_CHAR = '\\';
// FTP path
private String m_ftpPath;
// Share name nad share relative path
private String m_shareName;
private String m_sharePath;
// Shared device
private DiskSharedDevice m_shareDev;
// Flag to indicate if this is a directory or file path
private boolean m_dir = true;
/**
* Default constructor
*/
public FTPPath()
{
try
{
setFTPPath(null);
}
catch (Exception ex)
{
}
}
/**
* Class constructor
*
* @param ftpPath String
* @exception InvalidPathException
*/
public FTPPath(String ftpPath) throws InvalidPathException
{
setFTPPath(ftpPath);
}
/**
* Class constructor
*
* @param shrName String
* @param shrPath String
* @exception InvalidPathException
*/
public FTPPath(String shrName, String shrPath) throws InvalidPathException
{
setSharePath(shrName, shrPath);
}
/**
* Copy constructor
*
* @param ftpPath FTPPath
*/
public FTPPath(FTPPath ftpPath)
{
try
{
setFTPPath(ftpPath.getFTPPath());
m_shareDev = ftpPath.getSharedDevice();
}
catch (Exception ex)
{
}
}
/**
* Determine if the current FTP path is the root path
*
* @return boolean
*/
public final boolean isRootPath()
{
return m_ftpPath.compareTo(FTP_SEPERATOR) == 0 ? true : false;
}
/**
* Determine if the path is for a directory or file
*
* @return boolean
*/
public final boolean isDirectory()
{
return m_dir;
}
/**
* Check if the FTP path is valid
*
* @return boolean
*/
public final boolean hasFTPPath()
{
return m_ftpPath != null ? true : false;
}
/**
* Return the FTP path
*
* @return String
*/
public final String getFTPPath()
{
return m_ftpPath;
}
/**
* Check if the share name is valid
*
* @return boolean
*/
public final boolean hasShareName()
{
return m_shareName != null ? true : false;
}
/**
* Return the share name
*
* @return String
*/
public final String getShareName()
{
return m_shareName;
}
/**
* Check if the share path is the root path
*
* @return boolean
*/
public final boolean isRootSharePath()
{
if (m_sharePath == null || m_sharePath.compareTo(DIR_SEPERATOR) == 0)
return true;
return false;
}
/**
* Check if the share path is valid
*
* @reutrn boolean
*/
public final boolean hasSharePath()
{
return m_sharePath != null ? true : false;
}
/**
* Return the share relative path
*
* @reutrn String
*/
public final String getSharePath()
{
return m_sharePath;
}
/**
* Check if the shared device has been set
*
* @return boolean
*/
public final boolean hasSharedDevice()
{
return m_shareDev != null ? true : false;
}
/**
* Return the shared device
*
* @return DiskSharedDevice
*/
public final DiskSharedDevice getSharedDevice()
{
return m_shareDev;
}
/**
* Set the paths using the specified FTP path
*
* @param path String
* @exception InvalidPathException
*/
public final void setFTPPath(String path) throws InvalidPathException
{
// Check for a null path or the root path
if (path == null || path.length() == 0 || path.compareTo(FTP_SEPERATOR) == 0)
{
m_ftpPath = FTP_SEPERATOR;
m_shareName = null;
m_sharePath = null;
m_shareDev = null;
return;
}
// Check if the path starts with the FTP seperator
if (path.startsWith(FTP_SEPERATOR) == false)
throw new InvalidPathException("Invalid FTP path, should start with " + FTP_SEPERATOR);
// Save the FTP path
m_ftpPath = path;
// Get the first level directory from the path, this maps to the share name
int pos = path.indexOf(FTP_SEPERATOR, 1);
if (pos != -1)
{
m_shareName = path.substring(1, pos);
if (path.length() > pos)
m_sharePath = path.substring(pos).replace(FTP_SEPERATOR_CHAR, DIR_SEPERATOR_CHAR);
else
m_sharePath = DIR_SEPERATOR;
}
else
{
m_shareName = path.substring(1);
m_sharePath = DIR_SEPERATOR;
}
// Check if the share has changed
if (m_shareDev != null && m_shareName != null && m_shareDev.getName().compareTo(m_shareName) != 0)
m_shareDev = null;
}
/**
* Set the paths using the specified share and share relative path
*
* @param shr String
* @param path String
* @exception InvalidPathException
*/
public final void setSharePath(String shr, String path) throws InvalidPathException
{
// Save the share name and path
m_shareName = shr;
m_sharePath = path != null ? path : DIR_SEPERATOR;
// Build the FTP style path
StringBuffer ftpPath = new StringBuffer();
ftpPath.append(FTP_SEPERATOR);
if (hasShareName())
ftpPath.append(getShareName());
if (hasSharePath())
{
// Convert the share relative path to an FTP style path
String ftp = getSharePath().replace(DIR_SEPERATOR_CHAR, FTP_SEPERATOR_CHAR);
ftpPath.append(ftp);
}
else
ftpPath.append(FTP_SEPERATOR);
// Update the FTP path
m_ftpPath = ftpPath.toString();
}
/**
* Set the shared device
*
* @param shareList SharedDeviceList
* @param sess FTPSrvSession
* @return boolean
*/
public final boolean setSharedDevice(SharedDeviceList shareList, FTPSrvSession sess)
{
// Clear the current shared device
m_shareDev = null;
// Check if the share name is valid
if (hasShareName() == false || shareList == null)
return false;
// Find the required disk share
SharedDevice shr = shareList.findShare(getShareName());
if (shr != null && shr instanceof DiskSharedDevice)
m_shareDev = (DiskSharedDevice) shr;
// Return the status
return m_shareDev != null ? true : false;
}
/**
* Build an FTP path to the specified file
*
* @param fname String
* @return String
*/
public final String makeFTPPathToFile(String fname)
{
// Build the FTP path to a file
StringBuffer path = new StringBuffer(256);
path.append(m_ftpPath);
if (m_ftpPath.endsWith(FTP_SEPERATOR) == false)
path.append(FTP_SEPERATOR);
path.append(fname);
return path.toString();
}
/**
* Build a share relative path to the specified file
*
* @param fname String
* @return String
*/
public final String makeSharePathToFile(String fname)
{
// Build the share relative path to a file
StringBuilder path = new StringBuilder(256);
path.append(m_sharePath);
if (m_sharePath.endsWith(DIR_SEPERATOR) == false)
path.append(DIR_SEPERATOR);
path.append(fname);
return path.toString();
}
/**
* Add a directory to the end of the current path
*
* @param dir String
*/
public final void addDirectory(String dir)
{
// Check if the directory has a trailing seperator
if (dir.length() > 1 && dir.endsWith(FTP_SEPERATOR) || dir.endsWith(DIR_SEPERATOR))
dir = dir.substring(0, dir.length() - 1);
// Append the directory to the FTP path
StringBuilder str = new StringBuilder(256);
str.append(m_ftpPath);
if (m_ftpPath.endsWith(FTP_SEPERATOR) == false)
str.append(FTP_SEPERATOR);
str.append(dir);
if (m_ftpPath.endsWith(FTP_SEPERATOR) == false)
str.append(FTP_SEPERATOR);
m_ftpPath = str.toString();
// Check if there are any incorrect seperators in the FTP path
if (m_ftpPath.indexOf(DIR_SEPERATOR) != -1)
m_ftpPath = m_ftpPath.replace(FTP_SEPERATOR_CHAR, DIR_SEPERATOR_CHAR);
// Append the directory to the share relative path
str.setLength(0);
str.append(m_sharePath);
if (m_sharePath.endsWith(DIR_SEPERATOR) == false)
str.append(DIR_SEPERATOR);
str.append(dir);
m_sharePath = str.toString();
// Check if there are any incorrect seperators in the share relative path
if (m_sharePath.indexOf(FTP_SEPERATOR) != -1)
m_sharePath = m_sharePath.replace(FTP_SEPERATOR_CHAR, DIR_SEPERATOR_CHAR);
// Indicate that the path is to a directory
setDirectory(true);
}
/**
* Add a file to the end of the current path
*
* @param file String
*/
public final void addFile(String file)
{
// Append the file name to the FTP path
StringBuilder str = new StringBuilder(256);
str.append(m_ftpPath);
if (m_ftpPath.endsWith(FTP_SEPERATOR) == false)
str.append(FTP_SEPERATOR);
str.append(file);
m_ftpPath = str.toString();
// Check if there are any incorrect seperators in the FTP path
if (m_ftpPath.indexOf(DIR_SEPERATOR) != -1)
m_ftpPath = m_ftpPath.replace(FTP_SEPERATOR_CHAR, DIR_SEPERATOR_CHAR);
// Append the file name to the share relative path
str.setLength(0);
str.append(m_sharePath);
if (m_sharePath.endsWith(DIR_SEPERATOR) == false)
str.append(DIR_SEPERATOR);
str.append(file);
m_sharePath = str.toString();
// Check if there are any incorrect seperators in the share relative path
if (m_sharePath.indexOf(FTP_SEPERATOR) != -1)
m_sharePath = m_sharePath.replace(FTP_SEPERATOR_CHAR, DIR_SEPERATOR_CHAR);
// Indicate that the path is to a file
setDirectory(false);
}
/**
* Remove the last directory from the end of the path
*/
public final void removeDirectory()
{
// Check if the FTP path has a directory to remove
if (m_ftpPath != null && m_ftpPath.length() > 1)
{
// Find the last directory in the FTP path
int pos = m_ftpPath.length() - 1;
if (m_ftpPath.endsWith(FTP_SEPERATOR))
pos--;
while (pos > 0 && m_ftpPath.charAt(pos) != FTP_SEPERATOR_CHAR)
pos--;
// Set the new FTP path
m_ftpPath = m_ftpPath.substring(0, pos);
// Indicate that the path is to a directory
setDirectory(true);
// Reset the share/share path
try
{
setFTPPath(m_ftpPath);
}
catch (InvalidPathException ex)
{
}
}
}
/**
* Set/clear the directory path flag
*
* @param dir boolean
*/
protected final void setDirectory(boolean dir)
{
m_dir = dir;
}
/**
* Check if an FTP path string contains multiple directories
*
* @param path String
* @return boolean
*/
public final static boolean hasMultipleDirectories(String path)
{
if (path == null)
return false;
if (path.startsWith(FTP_SEPERATOR))
return true;
return false;
}
/**
* Check if the FTP path is a relative path, ie. does not start with a leading slash
*
* @param path String
* @return boolean
*/
public final static boolean isRelativePath(String path)
{
if (path == null)
return false;
return path.startsWith(FTP_SEPERATOR) ? false : true;
}
/**
* Return the FTP path as a string
*
* @return String
*/
public String toString()
{
StringBuilder str = new StringBuilder();
str.append("[");
str.append(getFTPPath());
str.append("=");
str.append(getShareName());
str.append(",");
str.append(getSharePath());
str.append("]");
return str.toString();
}
}

View File

@@ -0,0 +1,173 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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 org.alfresco.filesys.ftp;
/**
* FTP Request Class
* <p>
* Contains the details of an FTP request
*
* @author GKSpencer
*/
public class FTPRequest
{
// FTP command id
private int m_cmd;
// Command argument
private String m_arg;
/**
* Default constructor
*/
public FTPRequest()
{
m_cmd = FTPCommand.InvalidCmd;
}
/**
* Class constructor
*
* @param cmd int
* @param arg String
*/
public FTPRequest(int cmd, String arg)
{
m_cmd = cmd;
m_arg = arg;
}
/**
* Class constructor
*
* @param cmdLine String
*/
public FTPRequest(String cmdLine)
{
// Parse the FTP command record
parseCommandLine(cmdLine);
}
/**
* Return the command index
*
* @return int
*/
public final int isCommand()
{
return m_cmd;
}
/**
* Check if the request has an argument
*
* @return boolean
*/
public final boolean hasArgument()
{
return m_arg != null ? true : false;
}
/**
* Return the request argument
*
* @return String
*/
public final String getArgument()
{
return m_arg;
}
/**
* Set the command line for the request
*
* @param cmdLine String
* @return int
*/
public final int setCommandLine(String cmdLine)
{
// Reset the current values
m_cmd = FTPCommand.InvalidCmd;
m_arg = null;
// Parse the new command line
parseCommandLine(cmdLine);
return isCommand();
}
/**
* Parse a command string
*
* @param cmdLine String
*/
protected final void parseCommandLine(String cmdLine)
{
// Check if the command has an argument
int pos = cmdLine.indexOf(' ');
String cmd = null;
if (pos != -1)
{
cmd = cmdLine.substring(0, pos);
m_arg = cmdLine.substring(pos + 1);
}
else
cmd = cmdLine;
// Validate the FTP command
m_cmd = FTPCommand.getCommandId(cmd);
}
/**
* Update the command argument
*
* @param arg String
*/
protected final void updateArgument(String arg)
{
m_arg = arg;
}
/**
* Return the request as a string
*
* @return String
*/
public String toString()
{
StringBuilder str = new StringBuilder();
str.append("[");
str.append(FTPCommand.getCommandName(m_cmd));
str.append(":");
str.append(m_arg);
str.append("]");
return str.toString();
}
}

View File

@@ -0,0 +1,133 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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 org.alfresco.filesys.ftp;
import java.util.*;
/**
* FTP Server Session List Class
*
* @author GKSpencer
*/
public class FTPSessionList
{
// Session list
private Hashtable<Integer, FTPSrvSession> m_sessions;
/**
* Class constructor
*/
public FTPSessionList()
{
m_sessions = new Hashtable<Integer, FTPSrvSession>();
}
/**
* Return the number of sessions in the list
*
* @return int
*/
public final int numberOfSessions()
{
return m_sessions.size();
}
/**
* Add a session to the list
*
* @param sess FTPSrvSession
*/
public final void addSession(FTPSrvSession sess)
{
m_sessions.put(new Integer(sess.getSessionId()), sess);
}
/**
* Find the session using the unique session id
*
* @param id int
* @return FTPSrvSession
*/
public final FTPSrvSession findSession(int id)
{
return findSession(new Integer(id));
}
/**
* Find the session using the unique session id
*
* @param id Integer
* @return FTPSrvSession
*/
public final FTPSrvSession findSession(Integer id)
{
return m_sessions.get(id);
}
/**
* Remove a session from the list
*
* @param id int
* @return FTPSrvSession
*/
public final FTPSrvSession removeSession(int id)
{
return removeSession(new Integer(id));
}
/**
* Remove a session from the list
*
* @param sess FTPSrvSession
* @return FTPSrvSession
*/
public final FTPSrvSession removeSession(FTPSrvSession sess)
{
return removeSession(sess.getSessionId());
}
/**
* Remove a session from the list
*
* @param id Integer
* @return FTPSrvSession
*/
public final FTPSrvSession removeSession(Integer id)
{
// Find the required session
FTPSrvSession sess = findSession(id);
// Remove the session and return the removed session
m_sessions.remove(id);
return sess;
}
/**
* Enumerate the session ids
*
* @return Enumeration
*/
public final Enumeration enumerate()
{
return m_sessions.keys();
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,43 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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 org.alfresco.filesys.ftp;
/**
* Invalid FTP Path Exception Class
*
* @author GKSpencer
*/
public class InvalidPathException extends Exception {
private static final long serialVersionUID = -3298943582077910226L;
/**
* Default constructor
*/
public InvalidPathException() {
super();
}
/**
* Class constructor
*
* @param msg String
*/
public InvalidPathException(String msg) {
super(msg);
}
}