This is version . It is not the current version, and thus it cannot be edited.
[Back to current version]   [Restore this version]

Example source code for a custom protocol handler for the virtual file system.

In the user manager, make a new VFS item. Set its protocol to be FILE. Save your changes, and close the user manager. Now go find the users folder, your server group, then your user, then the VFS folder, and finally the name of the VFS item you just saved. Edit it with a text editor, and set the URL to be:

CUSTOM_com.myprotocol.MyProtocol://any_data_you_want_on_the_url:port/

Now you are ready to login. This class must be on the class path for CrushFTP for it to work.

(Example: java -classpath plugins/lib -jar CrushFTP.jar)

package com.myprotocol;

import crushftp.handlers.*;
import java.util.*;
import java.io.*;
import java.text.*;
import crushftp.server.*;

public class MyProtocol
{
	VFS uVFS = null;
	VRL url = null;
	Properties vItem = null;
	
	public MyProtocol(VFS uVFS, String urlStr, Properties vItem)
	{
		this.uVFS = uVFS;
		this.url = new VRL(urlStr);
		this.vItem = vItem;
	}

	public Properties get_item(String path) throws Exception
	{
		if (path.endsWith("/")) path = path.substring(0,path.length()-1);
		File f = new File(url.getPath()+path);
		if (!f.exists()) return null;
		Properties dir_item = new Properties();
		dir_item.put("url",url+path+(f.isDirectory()?"/":""));
		dir_item.put("local","false");
		dir_item.put("protocol", "file");
		dir_item.put("type", f.isFile()?"FILE":"DIR");
		dir_item.put("permissions", (f.isFile()?"-":"d")+"rwxrwxrwx");
		dir_item.put("num_items", "0");
		dir_item.put("owner", "owner");
		dir_item.put("group", "group");
		dir_item.put("size", f.length()+"");
		dir_item.put("month", "1");
		dir_item.put("day", "1");
		dir_item.put("time_or_year", "12:00");
		dir_item.put("name", f.getName());
		dir_item.put("local", "true");
		dir_item.put("root_dir", path);
		return dir_item;
	}

	public void getListing(Vector list, String path) throws Exception
	{
		if (path.startsWith("/")) path = path.substring(1);
		Common.debug(0,"ls "+url+path);
		File fs[] = new File(url.getPath()+path).listFiles();
		if (fs != null)
		{
			for (int x=0; x<fs.length; x++)
			{
				File f = fs[x];
				Properties p = get_item(path+"/"+f.getName());
				list.addElement(p);
			}
		}
	}

	public InputStream getInputStream(Long seekPos, Properties item) throws Exception
	{
		Common.debug(0,"get "+item.getProperty("url"));
		VRL u = new VRL(item.getProperty("url"));
		InputStream inStream = new FileInputStream(u.getPath());
		inStream.skip(seekPos.longValue());
		return inStream;
	}

	public OutputStream getOutputStream(Long seekPos, final Properties item) throws Exception
	{
		Common.debug(0,"put "+item.getProperty("url"));
		VRL u = new VRL(item.getProperty("url"));
		RandomAccessFile raf = new RandomAccessFile(u.getPath(),"rw");
		if (raf.length() > seekPos.longValue())	raf.setLength(seekPos.longValue());
		raf.close();
		OutputStream outStream = new FileOutputStream(u.getPath(),true);
		return outStream;
	}

	public String doCommand(String command, Properties item) throws Exception
	{
		Common.debug(0,command +" "+item.getProperty("url"));
		VRL u = new VRL(item.getProperty("url"));
		String s = "";
		try
		{
			if (command.toUpperCase().startsWith("DELE"))
			{
				new File(u.getPath()).delete();
				s = "250 OK";
			}
			else if (command.toUpperCase().startsWith("RMD"))
			{
				new File(u.getPath()).delete();
				s = "250 OK";
			}
			else if (command.toUpperCase().startsWith("MKD"))
			{
				new File(u.getPath()).mkdirs();
				s = "257 OK";
			}
			else if (command.toUpperCase().startsWith("MDTM"))
			{
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
				if (new File(u.getPath()).exists()) s = "213 " + sdf.format(new Date(new File(u.getPath()).lastModified()));
				else throw new Exception("File does not exist.");
			}
			else if (command.toUpperCase().startsWith("SIZE"))
			{
				if (new File(u.getPath()).exists())
				{
					if (new File(u.getPath()).isDirectory() && new File(u.getPath()).listFiles() != null) s = "213 "+new File(u.getPath()).listFiles().length;
					else s = "213 "+new File(u.getPath()).length();
				}
				else throw new Exception("File does not exist.");
			}
			else s = "200 Ignoring command.";
		}
		catch(Exception e)
		{
			Common.debug(1, e);
			s = "550 "+e.toString();
		}
		return s;
	}

	public Boolean rename(Properties item1, Properties item2) throws Exception
	{
		Common.debug(0,"rename "+item1.getProperty("url") + "   to   " + item2.getProperty("url"));
		VRL u1 = new VRL(item1.getProperty("url"));
		VRL u2 = new VRL(item2.getProperty("url"));
		return new Boolean(new File(u1.getPath()).renameTo(new File(u2.getPath())));
	}

}

Add new attachment

Only authorized users are allowed to upload new attachments.
« This particular version was published on 09-Oct-2016 18:14 by Ben Spink.
G’day (anonymous guest)

OLD WIKI!!!#

New: CrushFTPv9#

OLD WIKI!!!#


CrushFTP8 | What's New

Referenced by
LeftMenu

JSPWiki