Awake File 3.0: access remote files securely using java.io.File APIs
Join the DZone community and get the full member experience.
Join For FreeAwake FILE is a secure Open Source framework which enables Android and Java Desktop developers to very easily implement file uploads/downloads, RPC and remote file access through HTTP. File transfers include powerful features like file chunking and automatic recovery mechanism.
The new 3.0 version include zero-learning APIs to access remote files, upload or download files (see examples below).
Awake FILE is licensed through the GNU Lesser General Public License (LGPL v2.1): you can use it for free and without any constraints in your open source projects as well as in your commercial applications.
Why Awake FILE?
Why is it so complicated to code the upload of a file from an Android device or a PC Desktop to a remote Tomcat or other JavaEE server for treatment by a Servlet? Java developers have no easy built in APIs or tools to handle this. There is the solution to set up a FTP server but it’s cumbersome: it requires amendments to security rules and new developments to link the uploaded file to Tomcat or other JavaEE Servlet Server. Another solution is to use client and server libraries (Apache Commons HttpClient, FileUpload, etc.), but you have to write, test and maintain the code, define the security rules, handle proxy considerations, manage the errors, etc.
Awake FILE is our attempt to offer an easy solution for:
- Uploading files to a remote servlet server.
- Downloading files from a remote servlet server.
- Creating/deleting/accessing remote files and directories.
- Calling a remote java method without complicated setup.
- Defining strong security rules for all these operations.
v3.0 APis: Code Examples
Session Establishment
RemoteSession is the only API class you’ll have to learn. It is used to establish a session with the remote server:
// Define URL of the path to the Server File Manager servlet String url = "https://www.acme.org/ServerFileManager"; // The login info for strong authentication on server side: String username = "myUsername"; char[] password = { 'm', 'y', 'P', 'a', 's', 's', 'w', 'o', 'r', 'd' }; // Establish a session with the remote server RemoteSession remoteSession = new RemoteSession(url, username, password);
The client side talks to the server via a servlet named Server File Manager.
There is no complicated setup, “registration” or programming on the server side (except few lines of code for files location and security settings if required.)
Uploading a File
Just use RemoteOutputStream that implements OutputStream:
// Establish a session with the remote server RemoteSession remoteSession = new RemoteSession(url, username, password); // Upload a file using a RemoteOutputStream InputStream in = null; OutputStream out = null; File localFile = new File("C:\\Users\\Mike\\Koala.jpg"); String remotePath = "/Koala.jpg"; try { in = new FileInputStream(localFile); // Create an OutputStream that maps a remote file on the host out = new RemoteOutputStream(remoteSession, remotePath, localFile.length()); // Copy the bytes to create our remote file byte[] buffer = new byte[1024 * 4]; int n = 0; while ((n = in.read(buffer)) != -1) { out.write(buffer, 0, n); } } finally { if (in != null) in.close(); if (out != null) out.close(); }
Downloading a Remote File
Just use RemoteInputStream that implements InputStream:
// Establish a session with the remote server RemoteSession remoteSession = new RemoteSession(url, username, password); File file = new File("C:\\Users\\Mike\\Koala_DOWNLOADED.jpg"); String remotePath = "/Koala.jpg"; InputStream in = null; OutputStream out = null; try { // Get an InputStream from the file located on our server in = new RemoteInputStream(remoteSession, remotePath); out = new FileOutputStream(file); // Copy the remote bytes to create our local file byte[] buffer = new byte[1024 * 4]; int n = 0; while ((n = in.read(buffer)) != -1) { out.write(buffer, 0, n); } } finally { if (in != null)in.close(); if (out != null) out.close(); }
Awake File includes Apache Commons IO. We can simplify the 2 examples (if we don’t need to setup a GUI Progress Bar).
Example for file upload:
// Establish a session with the remote server RemoteSession remoteSession = new RemoteSession(url, username, password); File file = new File("C:\\Users\\Mike\\Koala.jpg"); String remotePath= "/Koala.jpg"; InputStream in = null; OutputStream out = null; try { in = new FileInputStream(file); // Create an OutputStream that maps a remote file on the host out = new RemoteOutputStream(remoteSession, remotePath, file.length()); // upload file IOUtils.copy(in, out); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); }
Accessing Remote Files
Awake FILE 3.0 gives now the power to access remote files using well known java.io.File methods. If you know to use java.io.File you know how to use RemoteFile to access remote files and directories:
// Establish a session with the remote server RemoteSession remoteSession = new RemoteSession(url, username, password); // Create a new RemoteFile that maps a file on remote server RemoteFile remoteFile = new RemoteFile(remoteSession, "/Koala.jpg"); // RemoteFile methods have the same names, signatures and behaviors // as java.io.File methods: a RemoteFile method is a File method that // is executed on the remote host if (remoteFile.exists()) { System.out.println(remoteFile.getName() + " length : " + remoteFile.length()); System.out.println(remoteFile.getName() + " canWrite: " + remoteFile.canWrite()); } // List files on our remote root directory remoteFile = new RemoteFile(remoteSession, "/"); RemoteFile[] files = remoteFile.listFiles(); for (RemoteFile file : files) { System.out.println("Remote file: " + file); } // List all text files in out root directory // using an Apache Commons IO 2.4 FileFiter FileFilter fileFilter = new SuffixFileFilter(".txt"); files = remoteFile.listFiles(fileFilter); for (RemoteFile file : files) { System.out.println("Remote text file: " + file); } // Create a new remote directory new RemoteFile(remoteSession, "/my_new_dir").mkdirs();
Remote Java Method Call
This snippet shows how to call a remote Java method. There is no setup or “registration” on server side:
// Establish a session with the remote server RemoteSession remoteSession = new RemoteSession(url, username, password); // OK: call the add(int a, int b) remote method that returns a + b: String result = remoteSession.call( "org.kawanfw.examples.Calculator.add", 33, 44); System.out.println("Calculator Result: " + result);
Documentation, Tutorial, Source Code & Binaries
We will be very happy to have your comments and reviews!
Opinions expressed by DZone contributors are their own.
Comments