Difference between revisions of "Home Library FTPClient"

From Gcube Wiki
Jump to: navigation, search
m (Valentina.marioli moved page Home Library Client FTP to Home Library FTPClient)
(Upload a file)
 
(47 intermediate revisions by the same user not shown)
Line 8: Line 8:
 
This library has following features:
 
This library has following features:
 
* '''Current Directory''': returns the pathname of the current working directory.
 
* '''Current Directory''': returns the pathname of the current working directory.
* '''Change Directory''': to create a directory in the root or in a subfolder.
+
* '''Change Directory''': to changes the current working directory.
 +
* '''Change Directory Up''': to changes the current working directory to the parent one.
 
* '''Create Directory''': to create a directory in the root or in a subfolder.
 
* '''Create Directory''': to create a directory in the root or in a subfolder.
 
* '''List Directory''': to list the content of a directory.
 
* '''List Directory''': to list the content of a directory.
Line 32: Line 33:
 
= Usage =
 
= Usage =
 
== Prerequisites ==
 
== Prerequisites ==
Home Library CLI is a jar-with-depencencies, you only need a Java 7 JDK.  
+
Home Library FTPClient is Java library, you only need a Java 7 JDK.  
  
Download the latest release from here:  
+
=== Download the library ===
 +
The library is available here:  
 +
* [http://maven.research-infrastructures.eu:8081/nexus/content/repositories/gcube-snapshots/org/gcube/data/access/home-library-ftpclient/1.0.0-SNAPSHOT/ home-library-ftpclient]
 +
Download the jar to your local repository and add its dependencies:
 +
* [http://central.maven.org/maven2/com/thoughtworks/xstream/xstream/1.4.4/xstream-1.4.4.jar xstream-1.4.4.jar]
 +
* [http://central.maven.org/maven2/junit/junit/4.10/junit-4.10.jar junit-4.10.jar]
  
http://maven.research-infrastructures.eu:8081/nexus/content/repositories/gcube-snapshots/org/gcube/common/home-library-cli/.
+
==== Version ====
 +
The current release of the Home Library FTPClient is 1.0.0.
  
== Start ==
+
=== Maven Artifact ===
  
After downloading the Home Library CLI jar just start it as follows:
+
The Home Library FTPClient library is available as a Maven artifact.
  
<source lang="bash">
+
To include it in your project, add this dependency to your pom:
java -jar home-library-cli-<$version>-jar-with-dependencies.jar
+
 
 +
<source lang="java">
 +
<dependency>
 +
<groupId>org.gcube.data.access</groupId>
 +
<artifactId>home-library-ftpclient</artifactId>
 +
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
 +
</dependency>
 
</source>
 
</source>
'''NOTE''': Replace '''<$version>''' with the last version available.
 
  
This starts the library and writes some help lines:
+
== FTPClient ==
  
<source lang="bash">
+
A FTPClient is created with:
user@my-pc:~$ java -jar home-library-cli-1.0.0-20160324.155028-1-jar-with-dependencies.jar
+
  
Usage: java -jar home-library-cli-1.0.0-20160324.155028-1-jar-with-dependencies.jar <command>
+
<source lang="java">
Where <command> is:
+
FTPClient client = new FTPClient();
mkdir
+
ls
+
rm
+
put
+
test
+
 
</source>
 
</source>
  
== Run the test ==
+
=== Run the test ===
 
To check the correct behavior of all features:
 
To check the correct behavior of all features:
<source lang="bash">
+
<source lang="java">
java -jar home-library-cli-<$version>-jar-with-dependencies.jar test
+
void test()
 
</source>
 
</source>
  
=== Example ===
+
Example:
<source lang="bash">
+
<source lang="java">
user@my-pc:~$ java -jar home-library-cli-1.0.0-20160324.155028-1-jar-with-dependencies.jar test
+
client.test();
 +
</source>
  
param N.0: test
+
=== Current Directory ===
List Folder /
+
To know the current working directory:
Response Code : 200
+
0 elements found
+
  
Create Folder myFolder-7cf437b6-e21f-4f87-a4a3-73f0b2a515c2
+
<source lang="java">
Response Code : 200
+
public String currentDirectory()
myFolder-7cf437b6-e21f-4f87-a4a3-73f0b2a515c2 created in /  
+
</source>
  
Save images in myFolder-7cf437b6-e21f-4f87-a4a3-73f0b2a515c2
+
Example:
  
List Folder /
+
<source lang="java">
Response Code : 200
+
client.currentDirectory();
0 elements found
+
</source>
  
Response Code : 200
+
=== Change directory ===
myFolder-7cf437b6-e21f-4f87-a4a3-73f0b2a515c2 deleted.
+
  
HL CLI test successfully completed.
+
To changes the current working directory.
 +
<source lang="java">
 +
void changeDirectory(String path)
 
</source>
 
</source>
  
== Create a directory ==
+
Where:
 +
* '''path''': the path to the new working directory.
  
To create a directory, if it does not already exist.
+
 
<source lang="bash">
+
Example:
java -jar home-library-cli-<$version>-jar-with-dependencies.jar mkdir name [path] [description]
+
 
 +
<source lang="java">
 +
String mySubFolder = ..
 +
client.changeDirectory(mySubFolder);
 
</source>
 
</source>
  
Mandatory arguments:
+
=== Change directory up ===
* '''name''': the name of the new folder.
+
  
Optional arguments:
+
To changes the current working directory to the parent one.
* '''path''': the path where the directory will be created (by default the directory will be created in the '''ROOT_PATH''').
+
<source lang="java">
* '''description''': the description of the new folder.
+
void changeDirectoryUp()
 +
</source>
  
=== Example ===
+
Example:
<source lang="bash">
+
user@my-pc:~$ java -jar home-library-cli-1.0.0-20160324.155028-1-jar-with-dependencies.jar mkdir myFolder
+
  
param N.0: mkdir
+
<source lang="java">
param N.1: myFolder
+
client.changeDirectoryUp();
Create Folder myFolder
+
Response Code : 200
+
myFolder created.
+
 
</source>
 
</source>
  
== Upload a file ==
+
=== Create a directory ===
To upload a file, if it does not already exist.
+
 
<source lang="bash">
+
To create a new remote directory in the current working one.
java -jar home-library-cli-<$version>-jar-with-dependencies.jar put fileURL [destinationPath] [name]
+
<source lang="java">
 +
void createDirectory(String directoryName, String description)
 
</source>
 
</source>
  
Mandatory arguments:
+
Where:
* '''fileURL''': the URL of the file to save.
+
* '''directoryName''': the name of the new folder.
 +
* '''description''': the description for the new directory.
  
Optional arguments:
 
* '''destinationPath''': the path of an existing directory where the file will be uploaded (by default the file will be saved in the '''ROOT_PATH''').
 
* '''name''': the description of the new folder (by default the original name will be preserved)
 
  
=== Example ===
+
Example:
<source lang="bash">
+
user@my-pc:~$ java -jar home-library-cli-1.0.0-20160324.155028-1-jar-with-dependencies.jar put /home/user/Downloads/image.jpg myFolder
+
  
param N.0: put
+
<source lang="java">
param N.1: /home/user/Downloads/image.jpg
+
String myDir = ..
param N.2: myFolder
+
String description = ..
/myFolder/image.jpg saved.
+
client.createDirectory(myDir, description);
 
</source>
 
</source>
  
== List a directory ==
+
=== Upload a file ===
To list the content of a directory.
+
To upload a file, if it does not already exist:
<source lang="bash">
+
<source lang="java">
java -jar home-library-cli-<$version>-jar-with-dependencies.jar ls [directory]
+
void upload(File file, String name, String description)
 
</source>
 
</source>
  
Optional arguments:
+
Where:
* '''directory''': the absolute path of the directory to list (by default the content of the '''ROOT_PATH''' will be shown).
+
* '''file''': the file to upload
 +
* '''name''': a name for the file
 +
* '''description''': a description for the file
  
=== Example ===
+
OR
<source lang="bash">
+
user@my-pc:~$ java -jar home-library-cli-1.0.0-20160324.155028-1-jar-with-dependencies.jar ls myFolder
+
  
param N.0: ls
+
To upload an array of bytes that represents a file:
param N.1: myFolder
+
<source lang="java">
Response Code : 200
+
void upload(byte[] bFile, String name, String description)
1 items found.
+
myFolder/image.jpg
+
 
</source>
 
</source>
  
== Remove files or directories ==
+
Where:
To remove files or directories:
+
* '''bFile''': an array of bytes that represents a file
<source lang="bash">
+
* '''name''': a name for the file
java -jar home-library-cli-<$version>-jar-with-dependencies.jar rm fileOrDirectory
+
* '''description''': a description for the file
 +
 
 +
 
 +
Example:
 +
 
 +
<source lang="java">
 +
File myFile = ..
 +
client.upload(myFile, myFile.getName(), "my new file");
 
</source>
 
</source>
  
Mandatory arguments:
+
=== List a directory ===
* '''fileOrDirectory''': the absolute path of the directory or file to remove.
+
To list the content of the current directory:
 +
<source lang="java">
 +
FTPFile[] list()
 +
</source>
  
=== Example ===
+
Example:
 +
 
 +
<source lang="java">
 +
FTPFile[] myFiles = client.list();
 +
</source>
 +
 
 +
FTPFile represents the files in a remote FTP directory, as described in [[#FTPFile | FTPFile]].
 +
 
 +
=== Remove files ===
 +
To remove files:
 +
<source lang="java">
 +
void deleteFile(String path)
 +
</source>
 +
 
 +
Where:
 +
* '''path''': the path of the file to remove.
 +
 
 +
 
 +
Example:
 +
 
 +
<source lang="java">
 +
String myFilename = ..
 +
client.deleteFile(myFilename);
 +
</source>
 +
 
 +
=== Remove directories ===
 +
To remove directories:
 +
<source lang="java">
 +
void deleteDirectory(String path)
 +
</source>
 +
 
 +
Where:
 +
* '''path''': the path of the directory to remove.
 +
 
 +
 
 +
Example:
 +
 
 +
<source lang="java">
 +
FTPClient client = ..
 +
String myDir = ..
 +
client.deleteDirectory(myDir);
 +
</source>
 +
 
 +
== FTPFile ==
 +
 
 +
A FTPFile represents the files in a remote FTP directory.
 +
 
 +
=== Get Path ===
 +
 
 +
To get the path of the current file or folder:
 +
<source lang="java">
 +
String getPath()
 +
</source>
 +
 
 +
Example:
 +
 
 +
<source lang="java">
 +
FTPFile file = ..
 +
file.getPath();
 +
</source>
 +
 
 +
=== Get Name ===
 +
 
 +
To get the name of the current file or folder
 +
<source lang="java">
 +
String getName()
 +
</source>
 +
 
 +
Example:
 +
 
 +
<source lang="java">
 +
FTPFile file = ..
 +
file.getName();
 +
</source>
  
Remove a file:
+
=== Is Folder ===
<source lang="bash">
+
user@my-pc:~$ java -jar home-library-cli-1.0.0-20160324.155028-1-jar-with-dependencies.jar rm myFolder/image.jpg
+
  
param N.0: rm
+
To know if the current file is a folder:
param N.1: myFolder/image.jpg
+
<source lang="java">
Response Code : 200
+
Boolean isFolder()
/myFolder/image.jpg deleted.
+
 
</source>
 
</source>
  
Remove a directory:
+
Example:
<source lang="bash">
+
user@my-pc:~$ java -jar home-library-cli-1.0.0-20160324.155028-1-jar-with-dependencies.jar rm myFolder
+
  
param N.0: rm
+
<source lang="java">
param N.1: myFolder
+
FTPFile file = ..
Response Code : 200
+
file.isFolder();
myFolder deleted.
+
 
</source>
 
</source>

Latest revision as of 18:33, 4 May 2016

Introduction

Home Library FTPClient is a Java library providing a simple interface to Home Library WebApp.

This library has following features:

  • Current Directory: returns the pathname of the current working directory.
  • Change Directory: to changes the current working directory.
  • Change Directory Up: to changes the current working directory to the parent one.
  • Create Directory: to create a directory in the root or in a subfolder.
  • List Directory: to list the content of a directory.
  • Upload file: to upload a file in a directory.
  • Remove files and folders: to remove a file or a directory (including subdirectories).
  • Test: a simple test to check the correct behavior of the features above.

The following sections will discuss each of these commands in turn, and discuss how to use them with Home Library FTPClient.

Configuration File

Home Library CLI is equipped with a simple configuration file to set the parameters and initial settings.

The fields are:

  • URL_REPOSITORY: the URL of Home Library WebApp.
  • USERNAME: username to connect to Home Library WebApp.
  • PASSWORD: password to connect to Home Library WebApp.
  • LOGIN: the portal login of the user who is using the library.
  • ROOT_PATH : the ROOT of the VRE folder where the files and directory will be created.

The Configuration File cannot be modified by the final user, but it allows to use the CLI in different context.

Usage

Prerequisites

Home Library FTPClient is Java library, you only need a Java 7 JDK.

Download the library

The library is available here:

Download the jar to your local repository and add its dependencies:

Version

The current release of the Home Library FTPClient is 1.0.0.

Maven Artifact

The Home Library FTPClient library is available as a Maven artifact.

To include it in your project, add this dependency to your pom:

<dependency>
	<groupId>org.gcube.data.access</groupId>
	<artifactId>home-library-ftpclient</artifactId>
	<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
</dependency>

FTPClient

A FTPClient is created with:

FTPClient client = new FTPClient();

Run the test

To check the correct behavior of all features:

void test()

Example:

client.test();

Current Directory

To know the current working directory:

public String currentDirectory()

Example:

client.currentDirectory();

Change directory

To changes the current working directory.

void changeDirectory(String path)

Where:

  • path: the path to the new working directory.


Example:

String mySubFolder = ..
client.changeDirectory(mySubFolder);

Change directory up

To changes the current working directory to the parent one.

void changeDirectoryUp()

Example:

client.changeDirectoryUp();

Create a directory

To create a new remote directory in the current working one.

void createDirectory(String directoryName, String description)

Where:

  • directoryName: the name of the new folder.
  • description: the description for the new directory.


Example:

String myDir = ..
String description = ..
client.createDirectory(myDir, description);

Upload a file

To upload a file, if it does not already exist:

void upload(File file, String name, String description)

Where:

  • file: the file to upload
  • name: a name for the file
  • description: a description for the file

OR

To upload an array of bytes that represents a file:

void upload(byte[] bFile, String name, String description)

Where:

  • bFile: an array of bytes that represents a file
  • name: a name for the file
  • description: a description for the file


Example:

File myFile = ..
client.upload(myFile, myFile.getName(), "my new file");

List a directory

To list the content of the current directory:

FTPFile[] list()

Example:

FTPFile[] myFiles = client.list();

FTPFile represents the files in a remote FTP directory, as described in FTPFile.

Remove files

To remove files:

void deleteFile(String path)

Where:

  • path: the path of the file to remove.


Example:

String myFilename = ..
client.deleteFile(myFilename);

Remove directories

To remove directories:

void deleteDirectory(String path)

Where:

  • path: the path of the directory to remove.


Example:

FTPClient client = ..
String myDir = ..
client.deleteDirectory(myDir);

FTPFile

A FTPFile represents the files in a remote FTP directory.

Get Path

To get the path of the current file or folder:

String getPath()

Example:

FTPFile file = ..
file.getPath();

Get Name

To get the name of the current file or folder

String getName()

Example:

FTPFile file = ..
file.getName();

Is Folder

To know if the current file is a folder:

Boolean isFolder()

Example:

FTPFile file = ..
file.isFolder();