Universal storage provides you an interface for storing files according to your needs. With this Universal Storage Java API, you will be able to develop programs in Java and use an interface for storing your files within a FTP folder as storage.
This documentation has the following content:
This API follows the Maven structure to ease its installation within your project.
If you want to test the API, follow these steps:
- Open with a text editor the settings.json located on test/resources/settings.json
{
"provider": "ftp",
"root": "universalstorage",
"tmp": "src/test/resources/tmp",
"ftp": {
"ftp_user": "",
"ftp_password": "",
"ftp_host": "",
"ftp_port": 21,
"ftp_passive": true
}
}
- The root and tmp keys are the main data to be filled. Create a local folder called tmp and paste its path on the key tmp.
- Create a folder i.e: universalstorage in your FTP root folder, copy the name and then paste it on root attribute.
- Save the settings.json file.
Now execute the following command:
mvn clean test
These are the steps for setting up Universal Storage in your project:
- You must create a file called settings.json (can be any name) and paste the following.
{
"provider": "ftp",
"root": "universalstorage",
"tmp": "src/test/resources/tmp",
"ftp": {
"ftp_user": "",
"ftp_password": "",
"ftp_host": "",
"ftp_port": 21,
"ftp_passive": true
}
}
- The root and tmp keys are the main data to be filled. Create a local folder called tmp and paste its path on the key tmp.
- Create a folder i.e: universalstorage in your FTP root folder, copy the name and then paste it on root attribute.
- Save the file settings.json
- Add the maven dependency in your pom.xml file.
<dependency>
<groupId>org.dynamicloud.api</groupId>
<artifactId>universalstorage.ftp</artifactId>
<version>1.0.0</version>
</dependency>
ftp_user
ftp username.
ftp_password
ftp password.
ftp_host
fto host.
ftp_port
ftp port.
ftp_passive
this flag tells the server to open a data port to which the client will connect to conduct data transfers.
The root folder is the storage where the files will be stored.
The tmp folder is where temporary files will be stored.
This api will get the ftp_user, ftp_password and ftp_host through either this file or using the environment variable ftp_user
, ftp_password
and ftp_host
Examples for Storing files:
- Passing the settings programmatically
UniversalStorage us = null;
try {
us = UniversalStorage.Impl.
getInstance(new UniversalSettings(new File("/home/test/resources/settings.json")));
us.storeFile(new File("/home/test/resources/settings.json"), "myfolder/innerfolder");
us.storeFile(new File("/home/test/resources/settings.json"));
us.storeFile(new File("/home/test/resources/settings.json").getAbsolutePath(), "myfolder/innerfolder");
us.storeFile(new File("/home/test/resources/settings.json").getAbsolutePath());
} catch (UniversalStorageException e) {
fail(e.getMessage());
} finally {
us.close();
}
- The settings could be passed through either jvm parameter or environment variable.
- If you want to pass the settings.json path through jvm parameter, in your java command add the following parameter:
-Duniversal.storage.settings=/home/test/resources/settings.json
- If your want to pass the settings.json path through environment variable, add the following variable:
universal_storage_settings=/home/test/resources/settings.json
UniversalStorage us = null;
try {
us = UniversalStorage.Impl.getInstance();
us.storeFile(new File("/home/test/resources/settings.json"), "myfolder/innerfolder");
us.storeFile(new File("/home/test/resources/settings.json"));
us.storeFile(new File("/home/test/resources/settings.json").getAbsolutePath(), "myfolder/innerfolder");
us.storeFile(new File("/home/test/resources/settings.json").getAbsolutePath());
} catch (UniversalStorageException e) {
fail(e.getMessage());
} finally {
us.close();
}
Remove file:
UniversalStorage us = null;
try {
us = UniversalStorage.Impl.getInstance();
us.removeFile("/home/test/resources/settings.json");
} catch (UniversalStorageException e) {
e.printStackTrace();
} finally {
us.close();
}
Create folder:
UniversalStorage us = null;
try {
us = UniversalStorage.Impl.getInstance();
us.createFolder("/myNewFolder");
} catch (UniversalStorageException e) {
e.printStackTrace();
} finally {
us.close();
}
Remove folder:
UniversalStorage us = null;
try {
us = UniversalStorage.Impl.getInstance();
us.removeFolder("/myNewFolder");
} catch (UniversalStorageException e) {
e.printStackTrace();
} finally {
us.close();
}
Retrieve file:
This file will be stored into the tmp folder.
UniversalStorage us = null;
try {
us = UniversalStorage.Impl.getInstance();
File file = us.retrieveFile("myFolder/file.txt");
} catch (UniversalStorageException e) {
e.printStackTrace();
} finally {
us.close();
}
Retrieve file as InputStream:
This inputstream will use a file that was stored into the tmp folder.
UniversalStorage us = null;
try {
us = UniversalStorage.Impl.getInstance();
InputSstream stream = us.retrieveFileAsStream("myFolder/file.txt");
} catch (UniversalStorageException e) {
e.printStackTrace();
} finally {
us.close();
}
Clean up tmp folder:
UniversalStorage us = null;
try {
us = UniversalStorage.Impl.getInstance();
us.clean();
} catch (UniversalStorageException e) {
e.printStackTrace();
} finally {
us.close();
}
Wipe root folder:
try {
UniversalStorage us = UniversalStorage.Impl.getInstance();
us.wipe();
} catch (UniversalStorageException e) {
e.printStackTrace();
}
Register listeners
This API provides useful listeners for asynchronous situations.
Your custom listener must implement this interface. This interface provides a series of methods for every situation, for example, a listener when the method "storeFile" is either starting or ending, when error occurs during any kind of process, Etc.
public interface UniversalStorageListener {}
Register a listener
UniversalStorage us = UniversalStorage.Impl.getInstance();
us.registerListener(new UniversalStorageListenerAdapter() {
public void onFolderCreated(UniversalStorageData data) {
System.out.println(data.toString());
}
public void onFileStored(UniversalStorageData data) {
System.out.println(data.toString());
}
public void onError(UniversalIOException error) {
System.out.println(error.getMessage());
}
});
Listener adapter
This adapter is useful for situation where you're needing only one or two implementations of UniversalStorageListener interface.
public class UniversalStorageListenerAdapter implements UniversalStorageListener {
/**
* This method will be called just before storing process.
*/
public void onStoreFile() {
}
/**
* This method will be called just before creation process.
*/
public void onCreateFolder() {
}
/**
* This method will be called just before file removing process.
*/
public void onRemoveFile() {
}
/**
* This method will be called just before folder removing process.
*/
public void onRemoveFolder() {
}
/**
* This method will be called when an error occurs.
*/
public void onError(UniversalIOException error) {
}
/**
* This method will be called just after storing process.
*
* @param data contains data about the new file.
*/
public void onFileStored(UniversalStorageData data) {
}
/**
* This method will be called just after creation process.
*
* @param data contains data about the new folder.
*/
public void onFolderCreated(UniversalStorageData data) {
}
/**
* This method will be called just after file removing process.
*/
public void onFileRemoved() {
}
/**
* This method will be called just after folder removing process.
*/
public void onFolderRemoved() {
}
}