Skip to content
Snippets Groups Projects
Commit 079474c2 authored by mokeddes's avatar mokeddes
Browse files

readmeInprogress

parent 1cf23de2
No related branches found
No related tags found
No related merge requests found
Showing
with 1041 additions and 209 deletions
#TP1 SR2
#### 31/03/2021
## Mokeddes youva
## commande :
1 - http://localhost:8080/myapp/flopResource/connexionFlopBox/anonymous/anonymous => connexion a la plateforme
2 - http://localhost:8080/myapp/flopResource/connexionServer/127.0.0.1/2121/user/12345 => connexion au serveur
3 - http://localhost:8080/myapp/flopResource/putt/home/mokeddes/Bureau/exemple.txt/127.0.0.1/2121/user/12345 => mettre un fichier text dans le seveur
4 - http://localhost:8080/myapp/flopResource/putr/home/mokeddes/Bureau/monRep/127.0.0.1/2121/user/12345 => mettre un dossier dans le serveur
5 - http://localhost:8080/myapp/flopResource/putb/home/mokeddes/Bureau/t.png/127.0.0.1/2121/user/12345 => mettre un fichier binaire dans le serveur
6 - http://localhost:8080/myapp/flopResource/mkd/repertoirCurl12/127.0.0.1/2121/user/12345 => creer un repertoir
7 - http://localhost:8080/myapp/flopResource/gett/exemple.txt/127.0.0.1/2121/user/12345 => récuperer le fichier
8 - http://localhost:8080/myapp/flopResource/getb/t.png/127.0.0.1/2121/user/12345 => récuperer un fichier binaire du serveur
9 - http://localhost:8080/myapp/flopResource/list/repAlister/127.0.0.1/2121/user/12345 => lister un repertoir
10 - http://localhost:8080/myapp/flopResource/rmd/doss1/127.0.0.1/2121/user/12345 => supprimer un repertoir
11 - http://localhost:8080/myapp/flopResource/enr/127.0.0.1/2121/user/12345/toto //ajouter un serveur à la plateforme
12 - http://localhost:8080/myapp/flopResource/sup/127.0.0.1/toto // supprimer un serveur
13 - http://localhost:8080/myapp/flopResource/deconnexion/127.0.0.1/2121/user/12345 //se déconnecter proprement du serveur
### Execution :
...@@ -48,8 +48,8 @@ ...@@ -48,8 +48,8 @@
<version>2.5.1</version> <version>2.5.1</version>
<inherited>true</inherited> <inherited>true</inherited>
<configuration> <configuration>
<source>1.7</source> <source>1.8</source>
<target>1.7</target> <target>1.8</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
......
package fil.src.method; package fil.src.method;
/**
*
* @author mokeddes
*
*/
public class Save { public class Save {
/**
* cette classe nous aides a sauvgrader un serveur
*/
private String host; private String host;
private String mot; private String codeSecret;
private int port; private int port;
private String user;
private String pass;
public Save(String host,String mot,int port) { public Save(String host,String codeSecret,int port,String user,String pass) {
this.mot = mot; /**
*
*/
this.codeSecret = codeSecret;
this.host = host; this.host = host;
this.port = port; this.port = port;
this.user = user;
this.pass = pass;
} }
/**
*
* @return
*/
public String getHost() { public String getHost() {
return this.host; return this.host;
} }
public String getMot() { /**
return mot; *
* @return
*/
public String getcodeSecret() {
return codeSecret;
} }
public void setMot(String mot) { /**
this.mot = mot; *
* @param codeSecret
*/
public void setMot(String codeSecret) {
this.codeSecret = codeSecret;
} }
/**
*
* @return
*/
public int getPort() { public int getPort() {
return port; return port;
} }
/**
*
* @return
*/
public String getuser() {
return user;
}
public String getpass() {
return pass;
}
public void setPort(int port) { public void setPort(int port) {
this.port = port; this.port = port;
} }
......
package fil.src.resource;
import java.util.List;
import java.io.IOException;
import java.util.ArrayList;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import fil.src.method.Save;
import fil.src.service.FlopBoxService;
@Path("flopResource")
public class FlopBox_ressource {
private Save save;
public static List<Save> listServer = new ArrayList<>();;
public static boolean isConnectedToFlopBox = false;
private FlopBoxService flpservice;
public FlopBox_ressource(){
//FlopBox_ressource.isConnectedToFlopBox = false;
this.flpservice = new FlopBoxService();
}
@GET
@Produces(MediaType.TEXT_PLAIN )
@Path("/connexionFlopBox/{user}/{pass}")
public String connexionFlopBox(@PathParam("user") String username,@PathParam("pass") String passW) {
if(flpservice.conflopbox(username,passW)) {
FlopBox_ressource.isConnectedToFlopBox = true;
return "vous étes connecté";
}
else {
return "echec de connexion";
}
}
@POST
@Produces(MediaType.TEXT_PLAIN)
@Path("/{host}/{port}/{user}/{pass}/{mot}")
//il faut enlever le user et le pass
public String StockServer(@PathParam("host") String host,@PathParam("port") int port,@PathParam("user")
String username,@PathParam("pass") String passW,@PathParam("mot") String mot) {
if(FlopBox_ressource.isConnectedToFlopBox) {
FlopBox_ressource.listServer.add(new Save(host,mot,port));
return "serveur ajouté";
}
else {
return "echec de connexion";}
}
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/list")
public String ListServer() {
if(FlopBox_ressource.isConnectedToFlopBox) {
String res = "";
for(Save s: this.listServer) {
res += s.getHost()+" "+s.getPort()+"\n";
}
return res;
}
return "echec de connexion";
}
@DELETE
@Produces(MediaType.TEXT_PLAIN)
@Path("/SUP/{host}/{mot}")
public String deletServer(@PathParam("host") String host,@PathParam("mot") String mot) {
boolean pasTrouve = true;
int i = 0;
if(FlopBox_ressource.isConnectedToFlopBox) {
while (pasTrouve && i < FlopBox_ressource.listServer.size() ) {
if(FlopBox_ressource.listServer.get(i).getHost().equals(host)) {
pasTrouve = false;
}
i++;
}
i--;
if(!pasTrouve) {
if(FlopBox_ressource.listServer.get(i).getHost().equals(host) && FlopBox_ressource.listServer.get(i).getMot().equals(mot) )
{
FlopBox_ressource.listServer.remove(i);
//System.out.println(host);
return "le serveur a ete supprimé";
}
}
return "la plateform ne contient pas ce serveur";
}
return "echec de connexion deletserver";
}
@GET
@Path("/con/{hostServeur}/{portServeur}/{userServeur}/{passServeur}")
@Produces("text/html")
public String connexionServeur(@PathParam("hostServeur") String host,@PathParam("portServeur") int port,
@PathParam("userServeur") String user,@PathParam("passServeur") String pass) throws IOException {
if(FlopBox_ressource.isConnectedToFlopBox) {
System.out.println("je suis dans la condition");
boolean res = this.flpservice.connectToServerFTP(user, pass,host, port);
if(res) {
String data = this.flpservice.corps("/",host,port,user,pass);
return data;
}
else {
return "échec de connexion au serveur";}
}
else {
return "vous n'etes pas connecté à la platefrom";
}
}
}
\ No newline at end of file
package fil.src.resource;
import java.util.List;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HEAD;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import fil.src.method.Save;
import fil.src.service.ServiceFlop;
/**
*
* @author mokeddes
*
*/
@Path("flopResource")
public class ResourceFlop {
/**
*class flopboxressource
*/
private Save save;
public static List<Save> listServer = new ArrayList<>();
public static boolean isConnectedToFlopBox = false;
private ServiceFlop plateforme;
public ResourceFlop(){
this.plateforme = new ServiceFlop();
}
/**
* cette méthode nous permet de nous connecter à la plateforme
* @param user
* @param pass
* @return
* @throws IOException
*/
@GET
@Produces(MediaType.TEXT_PLAIN )
@Path("/connexionFlopBox/{user}/{pass}")
public String connexionFlopBox(@PathParam("user") String user,@PathParam("pass") String pass) throws IOException {
if(plateforme.connexionToflopbox(user,pass)) {
ResourceFlop.isConnectedToFlopBox = true;
return "vous étes connecté";
}
else {
return "echec de connexion";
}
}
/**
* cette méthode nous permet d'ajouter un serveur à notre plateforme
* @param host
* @param port
* @param user
* @param pass
* @param codeSecret
* @return
* @throws IOException
*/
@POST
@Produces(MediaType.TEXT_PLAIN)
@Path("enr/{host}/{port}/{user}/{pass}/{codeSecret}")
public String StockServer(@PathParam("host") String host,@PathParam("port") int port,@PathParam("user")
String user,@PathParam("pass") String pass,@PathParam("codeSecret") String codeSecret) throws IOException {
if(ResourceFlop.isConnectedToFlopBox) {
ResourceFlop.listServer.add(new Save(host,codeSecret,port,user,pass));
return "serveur ajouté";
}
else {
throw new IOException("vous étes pas connecté a la platrfome");}
}
/**
* cette méthode nous permet de lister tous les serveur enregistrés dans notre plateforme
* @return
*/
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/list")
public String ListServer() {
if(ResourceFlop.isConnectedToFlopBox) {
String res = "";
for(Save s: this.listServer) {
res += s.getHost()+" "+s.getPort()+"\n";
}
return res;
}
return "echec de connexion";
}
/**
* cette méthode nous permet de supprimer un serveur de la plateforme
* @param host
* @param codeSecret
* @return
* @throws IOException
*/
@DELETE
@Produces(MediaType.TEXT_PLAIN)
@Path("/sup/{host}/{codeSecret}")
public String deletServer(@PathParam("host") String host,@PathParam("codeSecret") String codeSecret) throws IOException {
boolean bool = true;
int compteur = 0;
if(ResourceFlop.isConnectedToFlopBox) {
//on vérifie la connection à la plateforme
while (bool && compteur < ResourceFlop.listServer.size() ) {
if(ResourceFlop.listServer.get(compteur).getHost().equals(host)) {
bool = false;
}
compteur++;
}
compteur--;
if(!bool) {
if(ResourceFlop.listServer.get(compteur).getHost().equals(host) && ResourceFlop.listServer.get(compteur).getcodeSecret().equals(codeSecret) )
{
ResourceFlop.listServer.remove(compteur);
return "le serveur a ete supprimé";
}
}
throw new IOException("le serveur n'existe pas");
}
return "echec de connexion";
}
/**
* cette méthode nous permet de nous connecter à un serveur et affiche son contenue.
* @param host
* @param port
* @param user
* @param pass
* @return
* @throws IOException
*/
@GET
@Path("/connexionServer/{host}/{port}/{user}/{pass}")
@Produces("text/html")
public String connexionServeur(@PathParam("host") String host,@PathParam("port") int port,
@PathParam("user") String user,@PathParam("pass") String pass) throws IOException {
if(ResourceFlop.isConnectedToFlopBox) {
System.out.println("je suis dans la condition !");
boolean res = this.plateforme.connectToServerFTP(user, pass,host, port);
if(res) {
String data = this.plateforme.print_data("/",host,port,user,pass);
return data;
}
else {
return "échec de connexion au serveur";}
}
else {
return "vous n'etes pas connecté à la platefrom";
}
}
/**
* cette methode nous permet de nous connecter à un serveur enregistré dans la plateform
* @return
* @throws IOException
* @throws SocketException
*/
@GET
@Path("/serveurplateform")
@Produces("text/html")
public String connectToserverINmyStock() throws SocketException, IOException {
if(ResourceFlop.isConnectedToFlopBox) {
Save server = ResourceFlop.listServer.get(0);
boolean res = this.plateforme.connectToServerFTP(server.getuser(),server.getpass(),server.getHost(),server.getPort());
if(res) {
return "vous etes connecté au serveur";
}
else {
return "erreur de connexion au serveur";
}
}
return "vous étes pas connecté à la plateform";
}
/**
* cette method nous permet de lister un repertoir spécifique dans un serveur distant
* @param document
* @param server
* @param port
* @param user
* @param pass
* @return
* @throws IOException
*/
@GET
@Produces("text/html")
@Path("/ls-al/{folder:.*}/{host}/{port}/{user}/{pass}")
public String cmdLsAl(@PathParam("folder") String document,@PathParam("host") String server,@PathParam("port") int port,@PathParam("user") String user,@PathParam("pass") String pass) throws IOException {
if(!ResourceFlop.isConnectedToFlopBox) {
return "vous n'etes pas connecté";
}
boolean reponse = this.plateforme.connectToServerFTP(user, pass, server, port);
if (reponse) {
try {
this.plateforme.cwdFtpClient(document,server,port,user,pass);
} catch (IOException e) {
return this.plateforme.print_data(document,server,port,user,pass);
}
String data = this.plateforme.print_data(document,server,port,user,pass);
this.plateforme.getMyFtpClient().disconnect();
return data;
}
return "connexion au serveur refusé vérifier votre login et mot de pass";
}
/**
* enregistrer un fichier ASCCI dans un serveur distant
* @param fichier
* @param server
* @param port
* @param user
* @param pass
* @return
* @throws IOException
*/
@POST
@Produces("text/html")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
@Path("/putt/{fichier:.*}/{nomServeur}/{portServeur}/{userFTP}/{passFTP}")
public String stockAscci(@PathParam("fichier") String fichier,@PathParam("nomServeur") String server,@PathParam("portServeur") int port,@PathParam("userFTP") String user,@PathParam("passFTP") String pass) throws IOException{
if(!ResourceFlop.isConnectedToFlopBox) {
return "Veuillez vous connecter";
}
boolean res = this.plateforme.connectToServerFTP(user, pass, server, port);
if (res) {
this.plateforme.stock_file(fichier,false);
String str = this.plateforme.stock_file(fichier,false);
//this.plateforme.getMyFtpClient().disconnect();
return "Fichier sauvegardé avec succée";
}
return "connexion au serveur refusé vérifier votre login et mot de pass";
}
/**
* cette methode nous permet de stocket une repertoir sur un serveur distant
* @param fichier
* @param server
* @param port
* @param user
* @param pass
* @return
* @throws IOException
*/
@POST
@Produces("text/html")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
@Path("/putr/{fichier:.*}/{nomServeur}/{portServeur}/{userFTP}/{passFTP}")
public String stockRep(@PathParam("fichier") String fichier,@PathParam("nomServeur") String server,@PathParam("portServeur") int port,@PathParam("userFTP") String user,@PathParam("passFTP") String pass) throws IOException{
if(!ResourceFlop.isConnectedToFlopBox) {
return "Veuillez vous connecter à la platforme";
}
boolean bool = this.plateforme.connectToServerFTP(user, pass, server, port);
if (bool) {
this.plateforme.stock_folder(fichier);
//String rep = this.plateforme.stock_folder(fichier);
this.plateforme.getMyFtpClient().disconnect();
return "sauvgarde du document réussie";
//return rep;
}
return "connexion au serveur refusé vérifier votre login et mot de pass";
}
/**
* stocket un fichier binaire sur un serveur distant
* @param fichier
* @param server
* @param port
* @param user
* @param pass
* @return
* @throws IOException
*/
@POST
@Produces("text/html")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
@Path("/putb/{fichier:.*}/{nomServeur}/{portServeur}/{userFTP}/{passFTP}")
public String put_b(@PathParam("fichier") String fichier,@PathParam("nomServeur") String server,@PathParam("portServeur") int port,@PathParam("userFTP") String user,@PathParam("passFTP") String pass) throws IOException{
if(!ResourceFlop.isConnectedToFlopBox) {
return "Veuillez vous connecter a la plateforme";
}
boolean bool = this.plateforme.connectToServerFTP(user, pass, server, port);
if (bool) {
this.plateforme.stock_file(fichier,true);
//String rep = this.plateforme.stock_file(fichier,true);
this.plateforme.getMyFtpClient().disconnect();
return "sauvgarde du document réussie";
}
return "connexion au serveur refusé vérifier votre login et votre mot de pass";
}
/**
* création d'un repertoir dans un serveur distant
* @param fichier
* @param server
* @param port
* @param user
* @param pass
* @return
* @throws IOException
* @throws SocketException
*/
@POST
@Produces("text/html")
@Path("/mkd/{fichier:.*}/{nomServeur}/{portServeur}/{userFTP}/{passFTP}")
public String createRep(@PathParam("fichier") String fichier,@PathParam("nomServeur") String server,@PathParam("portServeur") int port,@PathParam("userFTP") String user,@PathParam("passFTP") String pass) throws SocketException, IOException{
if(!ResourceFlop.isConnectedToFlopBox) {
return "Veuillez vous connecter a la platforme";
}
boolean bool = this.plateforme.connectToServerFTP(user, pass, server, port);
if (bool) {
try {
if (this.plateforme.makeDirectory_ftpClient(fichier)) {
this.plateforme.getMyFtpClient().disconnect();
return "le repertoire a été créé avec succée !";
}
} catch (IOException e) {
e.printStackTrace();
}
}
return "échec de création du répertoire";
}
/**
* récuperer un fichier text stocké dans un serveur distant
* @param fichier
* @param server
* @param port
* @param user
* @param pass
* @return
* @throws IOException
* @throws SocketException
*/
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/gett/{fichier:.*}/{nomServeur}/{portServeur}/{userFTP}/{passFTP}")
public InputStream RecupFilAscci(@PathParam("fichier") String fichier,@PathParam("nomServeur") String server,@PathParam("portServeur") int port,@PathParam("userFTP") String user,@PathParam("passFTP") String pass) throws SocketException, IOException{
if(ResourceFlop.isConnectedToFlopBox) {
boolean reponse = this.plateforme.connectToServerFTP(user, pass, server, port);
if (reponse) {
try {
return this.plateforme.recover_file(fichier,false);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
return null;
}
/**
* récuperer un fichier binaire stocké dans un serveur distant
* @param fichier
* @param server
* @param port
* @param user
* @param pass
* @return
* @throws IOException
* @throws SocketException
*/
@GET
@Produces("image/jpeg")
@Path("/getb/{fichier:.*}/{nomServeur}/{portServeur}/{userFTP}/{passFTP}")
public InputStream recupFileBinary(@PathParam("fichier") String fichier,@PathParam("nomServeur") String server,@PathParam("portServeur") int port,@PathParam("userFTP") String user,@PathParam("passFTP") String pass) throws SocketException, IOException{
if(ResourceFlop.isConnectedToFlopBox) {
boolean reponse = this.plateforme.connectToServerFTP(user, pass, server, port);
if (reponse) {
try {
return this.plateforme.recover_file(fichier,true);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
return null;
}
/**
* lister un repertoir d'un serveur distant
* @param document
* @param server
* @param port
* @param user
* @param pass
* @return
* @throws UnknownHostException
* @throws IOException
*/
@GET
@Path("/list/{folder:.*}/{nomServeur}/{portServeur}/{userFTP}/{passFTP}")
@Produces("text/html")
public String listSpecificFolder(@PathParam("folder") String document,@PathParam("nomServeur") String server,@PathParam("portServeur") int port,@PathParam("userFTP") String user,@PathParam("passFTP") String pass) throws UnknownHostException, IOException {
String data;
if(!ResourceFlop.isConnectedToFlopBox) {
return "Veuillez vous connecter";
}
boolean bool = this.plateforme.connectToServerFTP(user, pass, server, port);
if (bool) {
data = this.plateforme.print_data(document,server,port,user,pass);
this.plateforme.getMyFtpClient().disconnect();
return data;
}
return "connexion au serveur refusé vérifier votre login et votre mot de pass";
}
/**
* supprimer un répertoire d'un serveur distant
* @param file
* @param server
* @param port
* @param user
* @param pass
* @return
* @throws IOException
*/
@GET
@Produces("text/html")
@Path("/delete/{Rep:.*}/{nomServeur}/{portServeur}/{userFTP}/{passFTP}")
public String deletRep(@PathParam("Rep") String file,@PathParam("nomServeur") String server,@PathParam("portServeur") int port,@PathParam("userFTP") String user,@PathParam("passFTP") String pass) throws IOException {
if(!ResourceFlop.isConnectedToFlopBox) {
return "Veuillez vous connecter";
}
boolean bool = this.plateforme.connectToServerFTP(user, pass, server, port);
if (bool) {
try {
return this.plateforme.removeDirectory(file);
} catch (Exception e) {
e.printStackTrace();
}
}
return "connexion au serveur refusé vérifier votre login et votre mot de pass";
}
/**
* affecter un nouveau port a un serveur
* @param newPort
* @param server
* @param port
* @param user
* @param pass
* @return
* @throws IOException
*/
@HEAD
@Produces(MediaType.APPLICATION_XHTML_XML)
@Path("/chPort/{newPort}/{host}/{port}/{user}/{pass}")
public String chPort(@PathParam("newPort") int newPort,@PathParam("host") String server,@PathParam("port") int port,@PathParam("user") String user,@PathParam("pass") String pass) throws IOException {
if(!ResourceFlop.isConnectedToFlopBox) {
return "connectez vous d'abord au serveur";
}
boolean bool = this.plateforme.connectToServerFTP(user, pass, server, port);
if (bool) {
this.plateforme.port(newPort);
return "le port a été changé avec succées";
}
return "connexion au serveur refusé vérifier votre login et votre mot de pass";
}
/**
* deconnexion d'un serveur
* @param host
* @param port
* @param user
* @param pass
* @return
* @throws IOException
*/
@HEAD
@Produces("text/html")
@Path("/deconnexion/{host}/{port}/{user}/{pass}")
public String deconnexion(@PathParam("host") String host,@PathParam("port") int port,@PathParam("user") String user,@PathParam("pass") String pass) throws IOException {
if(!ResourceFlop.isConnectedToFlopBox) {
return "connectez vous d'abord au serveur";
}
boolean reponse = this.plateforme.connectToServerFTP(user, pass, host, port);
if (reponse) {
this.plateforme.deconnexion();
return "deconnexion du serveur";
}
return "connexion au serveur refusé vérifier votre login et votre mot de pass";
}
}
\ No newline at end of file
package fil.src.service;
import java.awt.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.io.InputStreamReader;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FlopBoxService {
public String nameServer;
private FTPClient ftp;
public boolean conflopbox(String username,String passW) {
if(username.equals("anonymous") && passW.equals("anonymous")) {
return true;
}
return false;
}
public boolean connectToServerFTP(String user, String pass,String server, int port) {
this.ftp = new FTPClient();
this.nameServer = server;
try {
System.out.println("juste avant le connect ");
ftp.connect(server,port);
System.out.println("juste avant login ");
ftp.login(user,pass);
int reply = ftp.getReplyCode();
if(FTPReply.isPositiveCompletion(reply)) {
System.out.println("je suis dans le true ");
return true;
}
System.out.println("je suis dans le false ");
return false;
} catch (IOException e) {
System.out.println("je suis dans l'exeption");
e.printStackTrace();
}
return false;
}
}
package fil.src.service;
import java.awt.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;
import java.io.InputStreamReader;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
/**
*
* @author mokeddes
*
*/
public class ServiceFlop {
/**
* flopboxserviecer
*/
public String nameServer;
private FTPClient myFtpClient;
private Socket mySocket;
/**
* connexion a la plateform
* @param username
* @param passW
* @return
* @throws IOException
*/
public boolean connexionToflopbox(String username,String passW) throws IOException {
if(username.equals("anonymous") && passW.equals("anonymous")) {
return true;
}
else {
throw new IOException("echec de connexion");
}
}
/**
* connexion à un serveur ftp
* @param user
* @param pass
* @param server
* @param port
* @return
* @throws IOException
* @throws SocketException
*/
public boolean connectToServerFTP(String user, String pass,String server, int port) throws SocketException, IOException {
this.myFtpClient = new FTPClient();
this.nameServer = server;
System.out.println("juste avant le connect ");
myFtpClient.connect(server,port);
System.out.println("juste avant login ");
myFtpClient.login(user,pass);
int reponse = myFtpClient.getReplyCode();
if(FTPReply.isPositiveCompletion(reponse)) {
return true;
}
else {
throw new IOException("echec de connexion");
}
}
/**
* executer la commande port
* @param port
* @throws IOException
*/
public void port(int port) throws IOException{
this.myFtpClient.port(this.myFtpClient.getRemoteAddress(),port);
}
/**
* retourner le contenue du serveur sous forme d'un string
* @return
*/
public String commande_list_content() {
try {
this.myFtpClient.enterLocalPassiveMode();
this.myFtpClient.pasv();
String res = this.myFtpClient.getReplyString();
int port = ServiceFlop.getPort(res);
this.mySocket =new Socket(this.myFtpClient.getRemoteAddress(), port);
this.myFtpClient.list();
BufferedReader MyBufferedReader = new BufferedReader(new InputStreamReader(this.mySocket.getInputStream()));
String result ="";
String myStr = MyBufferedReader.readLine();
while(myStr != null){
result += myStr+",";
myStr = MyBufferedReader.readLine();
}
myFtpClient.completePendingCommand();
return result;
} catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* récuperation du port
* @param myString
* @return
*/
public static int getPort(String myString) {
String [] myStr = myString.substring(myString.indexOf("(")).split(",");
return Integer.parseInt(myStr[4])*256+Integer.parseInt(myStr[5].trim().substring(0,myStr[5].trim().length()-2));
}
/**
* cette methode nous renvoie le contenu d'un repertoir specifique en respectant un format défini
* @param myFile
* @param server
* @param port
* @param user
* @param pass
* @return
* @throws IOException
*/
public String print_data(String myFile,String server,int port,String user,String pass) throws IOException {
String final_return="";
this.myFtpClient.changeWorkingDirectory(myFile);
String path="";
for(String folder : this.commande_list_content().split(",")){
String myStr = folder.split(" ")[0];
StringBuilder MyStringBuilder = new StringBuilder();
MyStringBuilder.append(folder);
MyStringBuilder.reverse();
String []tabS = MyStringBuilder.toString().split(" ");
MyStringBuilder = new StringBuilder();
MyStringBuilder.append(tabS[0]);
MyStringBuilder.reverse();
String document = MyStringBuilder.toString();
this.myFtpClient.pwd();
path = this.myFtpClient.getReplyString().split(" ")[1];
path.replace("\"", "");
if (!path.isEmpty()) path = path.substring(1,path.length()-1);
final_return += " "+myStr+"==> "+document+"\n";
}
if(final_return.equals("")){
final_return += "the folder is empty";
}
return final_return;
}
public static boolean isFolder(String data) {
String res =data.substring(0,1);
return res.equals("d");
}
public String cwdFtpClient(String folder,String serveur,int port,String user,String pass) throws IOException{
this.myFtpClient.cwd(folder);
return this.print_data(folder,serveur,port,user,pass);
}
public FTPClient getMyFtpClient() {
return this.myFtpClient;
}
public String stock_file(String fichier,boolean bool){
try {
this.myFtpClient.enterLocalPassiveMode();
if(bool) {
//dans le cas ou c'est un fichier binaire ex:image
this.myFtpClient.setFileType(FTPClient.BINARY_FILE_TYPE);}
else
//dans le cas ou ce n'est pas un fichier binaire ex : texrt
{this.myFtpClient.setFileType(FTPClient.ASCII_FILE_TYPE);
}
this.myFtpClient.pasv();
if (fichier.charAt(fichier.length()-1) == '/') {
fichier.substring(fichier.length()-1);
}
String[] parse = fichier.split("/");
int res = parse.length;
String nom = parse[res-1];
InputStream myInputstream = new FileInputStream(fichier);
String reponse = this.myFtpClient.getReplyString();
int port = ServiceFlop.getPort(reponse);
this.mySocket =new Socket(this.myFtpClient.getRemoteAddress(), port);
this.myFtpClient.storeFile(nom, myInputstream);
return this.myFtpClient.getReplyString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String stock_folder(String folder) throws IOException {
File fold = new File(folder);
File[] subFiles = fold.listFiles();
this.myFtpClient.pwd();
String chemin = this.myFtpClient.getReplyString().split(" ")[1];
chemin = chemin.replace("\"", "");
if(chemin.length() == 1 && chemin.charAt(0) ==('/')) chemin = "";
else {
if(chemin.charAt(chemin.length()-1) == '/') {
chemin = chemin.substring(0,chemin.length()-1);
}
}
String name = fold.getName();
this.makeDirectory_ftpClient(name);
this.myFtpClient.cwd(name);
if (subFiles != null && subFiles.length > 0) {
for (File item : subFiles) {
if(!item.getName().equals("..") && !item.getName().equals(".")) {
if (item.isFile()) {
this.stock_file(folder+"/"+item.getName(), false);
return this.myFtpClient.getReplyString();
} else {
//dans le cas ou c'est un autre dossier on refait les memes instructions
this.stock_folder(folder+"/"+item.getName());
this.myFtpClient.cwd("..");
}}}}
return this.myFtpClient.getReplyString();
}
public boolean makeDirectory_ftpClient(String file) throws IOException{
return this.myFtpClient.makeDirectory(file);
}
public InputStream recover_file(String filename,boolean bool) throws IOException {
try {
System.out.println("-------------+++++++++++++++++++-*/ ");
if(bool) {
//dans le cas ou c'est un fichier ASCII exemple fichier text.
this.myFtpClient.setFileType(FTPClient.ASCII_FILE_TYPE);
}
else {
//dans le cas ou c'est un fichier binaire ex image.
this.myFtpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
}
this.myFtpClient.enterLocalPassiveMode();
this.myFtpClient.pasv();
String myStr = this.myFtpClient.getReplyString();
int port = ServiceFlop.getPort(myStr);
this.mySocket =new Socket(this.myFtpClient.getRemoteAddress(), port);
return this.myFtpClient.retrieveFileStream(filename);
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* suppression d'un repertoire
* @param folder
* @return
* @throws IOException
*/
public String removeDirectory(String folder) throws IOException{
FTPFile[] data = null;
try {
data = this.myFtpClient.listFiles(folder);
}catch (IOException e) {
e.printStackTrace();
}
String myStr,chemin;
for(FTPFile ftpFile : data) {
myStr = ftpFile.getName();
chemin = folder+"/"+myStr;
if(ftpFile.isDirectory()) {
if(!myStr.equals(".")){
if(!myStr.equals("..")) {
removeDirectory(chemin);}
}
}
else {
this.deleteFileFtpClient(chemin);
}
}
this.myFtpClient.removeDirectory(folder);
return this.myFtpClient.getReplyString();
}
/**
* supprimer un fichier dans un serveur distant
* @param fichier
* @throws IOException
*/
public void deleteFileFtpClient(String fichier) throws IOException{
this.myFtpClient.deleteFile(fichier);
}
/**
* executer les commandes rnfr et rnto
* @param oldFile
* @param newfile
* @throws IOException
*/
public void rnto_rnfr(String ancien,String nouveau) throws IOException{
this.myFtpClient.rnfr(ancien);
this.myFtpClient.rnto(nouveau);
return;
}
/**
* deconnexion du serveur
* @throws IOException
*/
public void deconnexion() throws IOException {
if(this.myFtpClient.isConnected()) {
System.out.println("je suis dans le if");
this.myFtpClient.disconnect();
System.out.println("je suis dans le if 22");
}
}
}
package fil.src;
import static org.junit.Assert.*;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.junit.Before;
import org.junit.Test;
import fil.src.resource.ResourceFlop;
import fil.src.service.ServiceFlop;
public class TestFlopBox {
@Test(expected = IOException.class)
public void testconnexionWithWrogUser() throws IOException {
ServiceFlop service = new ServiceFlop();
service.connexionToflopbox("wrong", "anonnymous");
}
@Test(expected = IOException.class)
public void testconnexionWithWrogpassr() throws IOException {
ServiceFlop service = new ServiceFlop();
service.connexionToflopbox("anonymous", "wrong");
}
@Test()
public void testconnexionWithGoodData() throws IOException {
ServiceFlop service = new ServiceFlop();
service.connexionToflopbox("anonymous", "anonymous");
}
@Test(expected = IOException.class)
public void testconnectToServerFtpWithWrongData() throws IOException {
ServiceFlop service = new ServiceFlop();
service.connectToServerFTP("x", "x", "x", 0);
}
@Test()
public void testconnectToServerFtpWithGoodData() throws IOException {
ServiceFlop service = new ServiceFlop();
service.connectToServerFTP("anonymous", "anonymous", "ftp.ubuntu.com", 21);
}
}
No preview for this file type
No preview for this file type
No preview for this file type
File deleted
File added
File deleted
File added
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment