Apache MINA SFTP - limit the directory structure that the user sees - java

Am using Apache MINA SSHD to build my own custom SFTP Server.
I want to limit the file system my user sees. I just want them to see the directory structure under /aa/bb/cc
I do not want them to be able to see or navigate any other folder.
And from the directories under /aa/bb/cc, a user will have read access to some directories and write access to only a selected few. How do I achive this ?

The FileSystemView has been introduced for that very purpose. If you're using version 0.14.0, the following will work:
sshServer.setFileSystemFactory(new VirtualFileSystemFactory(new File("admin").getAbsolutePath()));
I have also almost got a working example here. I just have to figure out how to set the home directory dynamically.

Related

Create a file link on the fly in wildfly

I have some questions about Wildfly deployment
1.An ear, when deployed in wildfly, is extracted within standalone/tmp/vfs/deployment/ directory. Can I place a file there manually and still access it from web. (I can check it, but as of now I do not have any machine to test it).
Can I create a file and place it there via some program. The reason I am asking this question is that I need to generate some files based on user input and provide the user with a link to that file. One way to do this is to statically link a directory in JBOSS and create the file there(access it using file handlers see this). I just want to know if it can be done at all using something like VFS.
If you need to persist to a file you'd want to create a new file handler, like the link you provided describes, and write the file to that folder. You don't want to try to use that temporary deployment directory. The content is not exploded by default so writing to it would likely fail.
If you don't need to persist to a file you can just use an output stream of some sort and the user will be able to download the file.

Moving Files between Servers

I was able to implement moving files from one directory to other in the same system using JAVA URL Connection.But I have to move files from a directory of one server to another(linux or windows) , and I should not use the third party Java APIs. Is there a way how to implement this? If yes , what configuration details are required in the program.please let me know how to implement it.
What about writing a very small script like below..
SOURCEDIR=/home/subodhr/e_books/
DESTDIR=user#server:home/subodhr/Destination/
rsync -avh --exclude="*.bak" $SOURCEDIR $DESTDIR
save this file using .sh extension
like moveFile.sh
then execute the script as ./moveFile.sh
Read the contents into serializable objects and pass them across different machines. Once received, just write them to the disk. You should be able to do this using core Java API.

Browse Directory option for sftp and ftp

Currently I'm doing the functionality for sftp using jsch-0.1.44. I need to add option for the user to browse the directory. So how to get the list of files from the remote server. Is there any other open source exists for this functionality ? Please help me
The ChannelSftp class provides the necessary methods to browse a remote directory.
For listing the directory, use channel.ls("."). This returns a vector of LsEntry objects, which you can traverse, print, show in a window, etc.
This example from jsch examples list shows one way to do this.
Look at the code starting from the following line...
if(cmd.equals("ls") || cmd.equals("dir")){
You can use Apache Virtual File System. If you are creating GUI application you can use OtrosVfsBrowser or VFSJFileChooser.

Attachments in Play! Framework on dotcloud

I uploaded files to the server using BLOB db type on play framework. In the application.conf file I have
attachments.path=home/dotcloud/uploads
But I couldn't find the files on the server.
The issue is that if I restart my www service, then I lose all my files, I only have the db records.
I believe that there are two issues in here. First your path lack a initial '/' to be a full path (I'm assuming that was your intention):
attachments.path=/home/dotcloud/uploads
Second, I'm not sure that your Play server will have rights to write to that folder, as it's outside the application context path. Default folder is local to the application and Play can write it, not so sure about other folders though. You should double check that.

Download a directory over HTTP in Java

I have some files in a directory tree which is being served over HTTP.
Given some sub-directory A, in that directory tree I want to be able to download directory A and all containing subdirectories and files.
It seems likely that a simple/direct/atomic solution exists in the some dark corner of Java. Does anyone know how to do this?
A webcrawler will not solve my problem since files in sub-directories may link to directories that are not subdirectories.
==Update==
The directories and files must be hosted in static manner.
The server is statically hosting files in a directory tree, the client is running Java and attempting to copy some branch of the directory tree using HTTP.
VFS is the answer to this, unfortunately I answered the question myself and so can't choose it as the answer until two days from now. If someone would write up of my answer I would be happy to mark their write up as the answer.
==Further Update==
VFS is in fact not the answer. VFS will not list directories over HTTP, as stated here. There does seem to be a few people that are interested in that functionality.
My first suggestion would be to create a servlet/jsp which recursiveley reads the directory structure (using java.io.File), reads all files, puts them in one zip (java.util.zip), and sends it to the browers for download.
I don't know of an atomic solution, but the most straightforward one would be using a URLConnection to fetch the sub-directory (assuming the server lists the directory) and then parse the response, look for contents of that directory and use URLConnection again to fetch each of the files under it.
Based on these answers, now I am wondering if you meant the Java to be on the client side or server side!
So you want from the client side on retrieve a list of all files and directores for the particular URL of the server side as if it is a local disk file system folder? That's usually not possible when the server doesn't have directory indexing enabled. And even then, you still need to parse the HTML page which represents the directory index and parse all <a> elements representing the files and folders yourself. There's no normal java.io.File approach for this. That would have been a huge security hole. One would for example be able to download all source files from http://gmail.com. HTTP is not meant as a file transfer protocol. Use FTP. That's where it stands for.
Assuming you have control over both the server and client, I would write a page (in your favorite technology of your choice; ASP, JSP, PHP, etc) that reads the server directory structure, and dynamically returns a page that consists of a bunch of links to each file to be downloaded.
Then client side you can trigger a download of each link.
What is the client side technology? is the thing doing the downloading an application of some sort, or a web browser? Does it have to have a client interface?
If this is some sort of in-house utility program, maybe you can just FTP instead? Having FTP access open on a server and downloading a directory would be easy...
Adding another possible answer:
If the server does not have directory listings turned on, then you basically have to make a modification server side. The easiest thing would be to just make a page that returns the dir structure to the client in a known format (see my 1st answer above).
If you control the server and have directory listings on, and you are always using the same server program (IIS, Tomcat, JBoss, etc) then you might be able to just make the client webcrawl the directory listings. For example, in a directory listing from IIS, you can tell which links are directories and which are files because it always puts a '/' at the end of a directory link, and shows 'dir' instead of a file size:
Friday, October 16, 2009 03:55 PM <dir> Unity
Thursday, July 02, 2009 10:42 AM 95 Global.asax
You can tell here that the 1st link is a directory, and the 2nd is an actual file.
So if you are using a consistent server app, just take a look at how the directory listing is returned. Maybe you'll get lucky.
If I am not terribly mistaken, HTTP does not tell you anything about the "structure" of the server side - if such a thing even exists.
Think about REST where the URI does not really tell you where to find a file on the server, but could merely trigger some action, retrieve data or the like.
So I do not think what you are trying to achieve can be done reliably, be it with Java or any other language. Or maybe I am getting you wrong here?
Talk about low-hanging fruit ;-) Thanks for the offer, e5!
Commons VFS provides a single API for accessing various different file systems. It presents a uniform view of the files from various different sources, such as the files on local disk, on an HTTP server, or inside a Zip archive.
http://commons.apache.org/vfs/
For the first time in a while google beat stackoverflow, Apache commons VFS does exactly what I need.
Commons VFS provides a single API for
accessing various different file
systems. It presents a uniform view of
the files from various different
sources, such as the files on local
disk, on an HTTP server, or inside a
Zip archive.
http://commons.apache.org/vfs/
==Update==
As stated in the question VFS only pretends to solve this problem, since it doesn't allow the listing of http directories.

Categories

Resources