Skip to content
Snippets Groups Projects
Commit 995e58da authored by Ferhat Hocine's avatar Ferhat Hocine
Browse files

rendu intermédiaire5

parent 1d335aa3
Branches
Tags
No related merge requests found
Showing
with 191 additions and 48 deletions
package systemes.repartie.tree.ftp; package systemes.repartie.tree.ftp.Arbre;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import systemes.repartie.tree.ftp.FtpConnection.Connection;
public class Arbre { public class Arbre {
private Connection connect; private Connection connect;
public Arbre(Connection connect) { public Arbre(Connection connect) {
...@@ -13,7 +15,7 @@ public class Arbre { ...@@ -13,7 +15,7 @@ public class Arbre {
} }
public void getTree() throws IOException { public void getTree(String space) throws IOException {
this.connect.envoyerType(); this.connect.envoyerType();
String mySTring = this.connect.envoyerPASV(); String mySTring = this.connect.envoyerPASV();
String adrs= this.connect.getAdressePasv(mySTring); String adrs= this.connect.getAdressePasv(mySTring);
...@@ -21,11 +23,37 @@ public class Arbre { ...@@ -21,11 +23,37 @@ public class Arbre {
ArrayList<String> Readl=this.connect.envoyerList(adrs,port); ArrayList<String> Readl=this.connect.envoyerList(adrs,port);
this.connect.envoyerPWD(); this.connect.envoyerPWD();
for(int i=0; i<Readl.size();i++) { for(int i=0; i<Readl.size();i++) {
TypeOf(Readl.get(i)); if(Readl.get(i)!=null) {
TypeOf(Readl.get(i),space);
}
}
}
public void TypeOf(String s,String space) throws IOException {
String[] myline=NameOperation(s);
if (myline[0].startsWith("d")) {
System.out.println("* "+space+"D--->>> " +myline[8]);
boolean enter=this.connect.envoyerCWD(myline[8]);
if (enter) {
getTree(space+" ");
this.connect.envoyerCdup();
}
}
else if (myline[0].startsWith("l")) {
System.out.println("* "+space+"L------->> " +myline[myline.length-1]);
}
else {
System.out.println("* "+space+"F-------> " +myline[8]);
} }
} }
public void TypeOf(String s) {
public String[] NameOperation(String s){
s=s.replaceAll(" ",",");
while(s.indexOf(",,")!=-1) {
s=s.replaceAll(",,",",");
}
return s.split(",");
} }
} }
package systemes.repartie.tree.ftp; package systemes.repartie.tree.ftp.FtpConnection;
import java.net.Socket; import java.net.Socket;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.io.InputStream; import java.io.InputStream;
...@@ -9,14 +9,22 @@ import java.io.BufferedWriter; ...@@ -9,14 +9,22 @@ import java.io.BufferedWriter;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import systemes.repartie.tree.ftp.ConnectionParametre;
import java.util.ArrayList; import java.util.ArrayList;
import systemes.repartie.tree.ftp.ParametreConnection.ConnectionParametre;
/**
* Connexion class with ftp server
* @author hocine
*
*/
public class Connection { public class Connection {
// parames
private ConnectionParametre connect; private ConnectionParametre connect;
private Socket socket; private Socket socket;
private BufferedReader bfR; private BufferedReader bfR;
private OutputStream outPut; private OutputStream outPut;
private PrintWriter printer; private PrintWriter printer;
//Constructor
public Connection(ConnectionParametre connect) { public Connection(ConnectionParametre connect) {
this.connect=connect; this.connect=connect;
this.socket=null; this.socket=null;
...@@ -24,41 +32,69 @@ public class Connection { ...@@ -24,41 +32,69 @@ public class Connection {
} }
/*
* return the instance of ConnectionParametre used by our class
*/
public ConnectionParametre getConnectionParam() { public ConnectionParametre getConnectionParam() {
return this.connect; return this.connect;
} }
/*
* Connexion to ftp server methode
* this methode uses our instance of ConnectionParametre to connect to ftp server
* @throws IOException
* @return void
*/
public void OperationConnection() throws IOException{ public void OperationConnection() throws IOException{
//creating a new instance of socket using ConnectionParametre adress and port
this.socket= new Socket(this.connect.getAdresse(),this.connect.getPort()); this.socket= new Socket(this.connect.getAdresse(),this.connect.getPort());
//creating a new instance of Buffered Reader
this.bfR = new BufferedReader(new InputStreamReader(this.socket.getInputStream())); this.bfR = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
//creating a new instance of printer of Buffered writer
printer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()))); printer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream())));
String Errors="";
//Connecting
String r = this.bfR.readLine(); String r = this.bfR.readLine();
System.out.println(r); //checking errors
if (!r.startsWith("220")) { Errors=Errors+" FTP Error --> "+r;}
printer.write("AUTH TLS\r\n"); printer.write("AUTH TLS\r\n");
printer.flush(); printer.flush();
r = this.bfR.readLine(); r = this.bfR.readLine();
//checking errors
if (!r.startsWith("530")&& Errors=="") { Errors=Errors+" AUTH TLS Error --> "+r;}
System.out.println(r);
printer.write("AUTH SSL\r\n"); printer.write("AUTH SSL\r\n");
printer.flush(); printer.flush();
r = this.bfR.readLine(); r = this.bfR.readLine();
//checking errors
System.out.println(r); if (!r.startsWith("530")&& Errors=="") { Errors=Errors+" AUTH SSL Error --> "+r;}
//identification using ConnectionParametre user and passWord
printer.write("USER "+this.connect.getIdentifiant()+"\r\n"); printer.write("USER "+this.connect.getIdentifiant()+"\r\n");
printer.flush(); printer.flush();
r = this.bfR.readLine(); r = this.bfR.readLine();
//checking errors
if (!r.startsWith("331")&& Errors=="") { Errors=Errors+" USER Error --> "+r;}
System.out.println(r);
printer.write("PASS "+this.connect.getMotdepasse()+"\r\n"); printer.write("PASS "+this.connect.getMotdepasse()+"\r\n");
printer.flush(); printer.flush();
r = this.bfR.readLine(); r = this.bfR.readLine();
System.out.println(r); //checking errors
if (!r.startsWith("230")&& Errors=="") { Errors=Errors+" PASS Error --> "+r;}
//Print result if succeed or throwing exception if failed
if(Errors=="") {
System.out.println("Login successful.");
}
else {
throw new IOException("Errors: "+Errors);
}
} }
/*
* executing PWD commande to get repertory root
* @throws IOException
* @return void
*/
public void envoyerPWD() throws IOException { public void envoyerPWD() throws IOException {
if (this.socket==null) { if (this.socket==null) {
System.out.println("Erreur connection FTP Server !!"); System.out.println("Erreur connection FTP Server !!");
...@@ -67,11 +103,16 @@ public class Connection { ...@@ -67,11 +103,16 @@ public class Connection {
printer.write("PWD\r\n"); printer.write("PWD\r\n");
printer.flush(); printer.flush();
String r = this.bfR.readLine(); String r = this.bfR.readLine();
System.out.println(r); //System.out.println(r);
} }
} }
/*
* executing CWD command using the directory given in param to access to it
* @param String : dossier
* @throws IOException
* @return boolean : True if it succeed to access to the directory or False if it failed
*/
public boolean envoyerCWD(String dossier) throws IOException { public boolean envoyerCWD(String dossier) throws IOException {
if (this.socket==null) { if (this.socket==null) {
System.out.println("Erreur connection FTP Server !!"); System.out.println("Erreur connection FTP Server !!");
...@@ -85,6 +126,11 @@ public class Connection { ...@@ -85,6 +126,11 @@ public class Connection {
return false; return false;
} }
/*
* executing PASV command to enter to passive mode
* @throws IOException
* @return String : the information received in passive mode
*/
public String envoyerPASV() throws IOException { public String envoyerPASV() throws IOException {
if (this.socket==null) { if (this.socket==null) {
System.out.println("Erreur connection FTP Server !!"); System.out.println("Erreur connection FTP Server !!");
...@@ -94,10 +140,16 @@ public class Connection { ...@@ -94,10 +140,16 @@ public class Connection {
printer.write("PASV\r\n"); printer.write("PASV\r\n");
printer.flush(); printer.flush();
String r = this.bfR.readLine(); String r = this.bfR.readLine();
System.out.println(r); //System.out.println(r);
return r; return r;
} }
} }
/*
* getting Ip address from passive mode information
* String String : PASV
* @return String : the Ip address extracted from passive mode information
*/
public String getAdressePasv(String PASV) { public String getAdressePasv(String PASV) {
int i = PASV.indexOf("("); int i = PASV.indexOf("(");
int j = PASV.indexOf(")"); int j = PASV.indexOf(")");
...@@ -111,11 +163,22 @@ public class Connection { ...@@ -111,11 +163,22 @@ public class Connection {
return res; return res;
} }
/*
* calculation of the Port from passive mode information
* String String : PASV
* @return int : Port extracted then calculated from passive mode information
*/
public int getPORTPasv(String PASV) { public int getPORTPasv(String PASV) {
String [] str=PASV.split(","); String [] str=PASV.split(",");
return (Integer.parseInt(str[str.length-2])*256 )+ Integer.parseInt(str[str.length-1].substring(0,str[str.length-1].length()-2)); return (Integer.parseInt(str[str.length-2])*256 )+ Integer.parseInt(str[str.length-1].substring(0,str[str.length-1].length()-2));
} }
/*
* executing TYPE I command to switch to the binary mode
* @throws IOException
* @return void
*/
public void envoyerType() throws IOException { public void envoyerType() throws IOException {
if (this.socket==null) { if (this.socket==null) {
System.out.println("Erreur connection FTP Server !!"); System.out.println("Erreur connection FTP Server !!");
...@@ -124,11 +187,15 @@ public class Connection { ...@@ -124,11 +187,15 @@ public class Connection {
printer.write("TYPE I\r\n"); printer.write("TYPE I\r\n");
printer.flush(); printer.flush();
String r = this.bfR.readLine(); String r = this.bfR.readLine();
System.out.println(r); //System.out.println(r);
} }
} }
/*
* executing LIST command to get the List of file,directory and link in our repertory
* @throws IOException
* @return ArrayList<String> : the List of file,directory and link in our repertory
*/
public ArrayList<String> envoyerList(String adrs, int port) throws IOException { public ArrayList<String> envoyerList(String adrs, int port) throws IOException {
ArrayList<String> Readl=new ArrayList<String>(); ArrayList<String> Readl=new ArrayList<String>();
if (this.socket==null) { if (this.socket==null) {
...@@ -145,7 +212,11 @@ public class Connection { ...@@ -145,7 +212,11 @@ public class Connection {
} }
/*
* executing Cdup To change directory
* @throws IOException
* @return void
*/
public void envoyerCdup() throws IOException { public void envoyerCdup() throws IOException {
if (this.socket==null) { if (this.socket==null) {
System.out.println("Erreur connection FTP Server !!"); System.out.println("Erreur connection FTP Server !!");
...@@ -154,10 +225,15 @@ public class Connection { ...@@ -154,10 +225,15 @@ public class Connection {
printer.write("CDUP\r\n"); printer.write("CDUP\r\n");
printer.flush(); printer.flush();
String r = this.bfR.readLine(); String r = this.bfR.readLine();
System.out.println(r); //System.out.println(r);
} }
} }
/*
* executing Quit To QUIT socket
* @throws IOException
* @return void
*/
public void envoyerQuit() throws IOException { public void envoyerQuit() throws IOException {
if (this.socket==null) { if (this.socket==null) {
System.out.println("Erreur connection FTP Server !!"); System.out.println("Erreur connection FTP Server !!");
...@@ -166,10 +242,17 @@ public class Connection { ...@@ -166,10 +242,17 @@ public class Connection {
printer.write("QUIT\r\n"); printer.write("QUIT\r\n");
printer.flush(); printer.flush();
String r = this.bfR.readLine(); String r = this.bfR.readLine();
System.out.println(r); //System.out.println(r);
} }
} }
/*
* Staring new socket the to get the List of file,directory and link in our repertory from IP Address and Port
* @param String Adresse : IP address
* @param STring Port : port
* @throws IOException
* @return ArrayList<String> : The List of file,directory and link in our repertory from IP Address and Port
*/
public ArrayList<String> newConnection(String Adresse,int Port) throws UnknownHostException, IOException{ public ArrayList<String> newConnection(String Adresse,int Port) throws UnknownHostException, IOException{
ArrayList<String> liste=new ArrayList<String>(); ArrayList<String> liste=new ArrayList<String>();
Socket socket1= new Socket(Adresse,Port); Socket socket1= new Socket(Adresse,Port);
...@@ -186,10 +269,11 @@ public class Connection { ...@@ -186,10 +269,11 @@ public class Connection {
return liste; return liste;
} }
/*
* Close Socket
*/
public void closeSocket() throws IOException { public void closeSocket() throws IOException {
this.socket.close(); this.socket.close();
} }
public void readLine() throws IOException {
this.bfR.readLine();
}
} }
...@@ -9,26 +9,21 @@ import java.io.OutputStreamWriter; ...@@ -9,26 +9,21 @@ import java.io.OutputStreamWriter;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.net.Socket; import java.net.Socket;
import systemes.repartie.tree.ftp.Arbre.Arbre;
import systemes.repartie.tree.ftp.FtpConnection.Connection;
import systemes.repartie.tree.ftp.ParametreConnection.ConnectionParametre;
public class Main { public class Main {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
ConnectionParametre connectPar= new ConnectionParametre("anonymous","anonymous",21,args[0]); ConnectionParametre connectPar= new ConnectionParametre("anonymous","anonymous",21,args[0]);
Connection connect= new Connection(connectPar); Connection connect= new Connection(connectPar);
System.out.println("-------------Connection-------------"); System.out.println("-------------Connection-------------");
connect.OperationConnection(); connect.OperationConnection();
System.out.println("\n");
System.out.println("--------------* Tree *--------------");
Arbre ar= new Arbre(connect);
ar.getTree("");
System.out.println("------------------------------------"); System.out.println("------------------------------------");
//connect.envoyerPWD();
//connect.envoyerCWD("/cdimage");
//connect.envoyerPWD();
//connect.envoyerCWD("bionic");
//connect.envoyerPWD();
//String a=connect.envoyerPASV();
//int b= connect.getPORTPasv(a);
//System.out.println(b);
//String s= connect.getAdressePasv(a);
//System.out.println(s);
//connect.closeSocket();
Arbre ar= new Arbre(connect);
ar.getTree();
} }
} }
package systemes.repartie.tree.ftp; package systemes.repartie.tree.ftp.ParametreConnection;
public class ConnectionParametre { public class ConnectionParametre {
private String identifiant; private String identifiant;
......
File deleted
File added
File deleted
File added
No preview for this file type
systemes/repartie/tree/ftp/FtpConnection/Connection.class
systemes/repartie/tree/ftp/Arbre/Arbre.class
systemes/repartie/tree/ftp/Main.class
systemes/repartie/tree/ftp/ParametreConnection/ConnectionParametre.class
/home/hocine/eclipse-workspace/projet1.sr/src/main/java/systemes/repartie/tree/ftp/Main.java /home/hocine/eclipse-workspace/projet1.sr/src/main/java/systemes/repartie/tree/ftp/Main.java
/home/hocine/eclipse-workspace/projet1.sr/src/main/java/systemes/repartie/tree/ftp/Connection.java /home/hocine/eclipse-workspace/projet1.sr/src/main/java/systemes/repartie/tree/ftp/ParametreConnection/ConnectionParametre.java
/home/hocine/eclipse-workspace/projet1.sr/src/main/java/systemes/repartie/tree/ftp/ConnectionParametre.java /home/hocine/eclipse-workspace/projet1.sr/src/main/java/systemes/repartie/tree/ftp/FtpConnection/Connection.java
/home/hocine/eclipse-workspace/projet1.sr/src/main/java/systemes/repartie/tree/ftp/Arbre.java /home/hocine/eclipse-workspace/projet1.sr/src/main/java/systemes/repartie/tree/ftp/Arbre/Arbre.java
systemes/repartie/tree/ftp/AppTest.class
No preview for this file type
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="systemes.repartie.tree.ftp.AppTest" time="0.028" tests="1" errors="0" skipped="0" failures="0"> <testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="systemes.repartie.tree.ftp.AppTest" time="0.029" tests="1" errors="0" skipped="0" failures="0">
<properties> <properties>
<property name="sun.desktop" value="gnome"/> <property name="sun.desktop" value="gnome"/>
<property name="awt.toolkit" value="sun.awt.X11.XToolkit"/> <property name="awt.toolkit" value="sun.awt.X11.XToolkit"/>
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
<property name="sun.java.launcher" value="SUN_STANDARD"/> <property name="sun.java.launcher" value="SUN_STANDARD"/>
<property name="user.country" value="FR"/> <property name="user.country" value="FR"/>
<property name="sun.boot.library.path" value="/usr/lib/jvm/java-11-openjdk-amd64/lib"/> <property name="sun.boot.library.path" value="/usr/lib/jvm/java-11-openjdk-amd64/lib"/>
<property name="sun.java.command" value="/home/hocine/eclipse-workspace/projet1.sr/target/surefire/surefirebooter5024289365485832660.jar /home/hocine/eclipse-workspace/projet1.sr/target/surefire 2021-02-02T22-33-31_904-jvmRun1 surefire18031108180273195258tmp surefire_010353060393872191906tmp"/> <property name="sun.java.command" value="/home/hocine/eclipse-workspace/projet1.sr/target/surefire/surefirebooter12294673392553916757.jar /home/hocine/eclipse-workspace/projet1.sr/target/surefire 2021-02-03T12-42-24_160-jvmRun1 surefire7600803984115398973tmp surefire_012456435499636066475tmp"/>
<property name="jdk.debug" value="release"/> <property name="jdk.debug" value="release"/>
<property name="surefire.test.class.path" value="/home/hocine/eclipse-workspace/projet1.sr/target/test-classes:/home/hocine/eclipse-workspace/projet1.sr/target/classes:/home/hocine/.m2/repository/junit/junit/4.11/junit-4.11.jar:/home/hocine/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:"/> <property name="surefire.test.class.path" value="/home/hocine/eclipse-workspace/projet1.sr/target/test-classes:/home/hocine/eclipse-workspace/projet1.sr/target/classes:/home/hocine/.m2/repository/junit/junit/4.11/junit-4.11.jar:/home/hocine/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:"/>
<property name="sun.cpu.endian" value="little"/> <property name="sun.cpu.endian" value="little"/>
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
<property name="java.specification.name" value="Java Platform API Specification"/> <property name="java.specification.name" value="Java Platform API Specification"/>
<property name="java.vm.specification.vendor" value="Oracle Corporation"/> <property name="java.vm.specification.vendor" value="Oracle Corporation"/>
<property name="java.awt.graphicsenv" value="sun.awt.X11GraphicsEnvironment"/> <property name="java.awt.graphicsenv" value="sun.awt.X11GraphicsEnvironment"/>
<property name="surefire.real.class.path" value="/home/hocine/eclipse-workspace/projet1.sr/target/surefire/surefirebooter5024289365485832660.jar"/> <property name="surefire.real.class.path" value="/home/hocine/eclipse-workspace/projet1.sr/target/surefire/surefirebooter12294673392553916757.jar"/>
<property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/> <property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
<property name="java.runtime.version" value="11.0.9.1+1-Ubuntu-0ubuntu1.20.04"/> <property name="java.runtime.version" value="11.0.9.1+1-Ubuntu-0ubuntu1.20.04"/>
<property name="user.name" value="hocine"/> <property name="user.name" value="hocine"/>
...@@ -57,5 +57,5 @@ ...@@ -57,5 +57,5 @@
<property name="sun.io.unicode.encoding" value="UnicodeLittle"/> <property name="sun.io.unicode.encoding" value="UnicodeLittle"/>
<property name="java.class.version" value="55.0"/> <property name="java.class.version" value="55.0"/>
</properties> </properties>
<testcase name="shouldAnswerWithTrue" classname="systemes.repartie.tree.ftp.AppTest" time="0.001"/> <testcase name="shouldAnswerWithTrue" classname="systemes.repartie.tree.ftp.AppTest" time="0.002"/>
</testsuite> </testsuite>
\ No newline at end of file
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Test set: systemes.repartie.tree.ftp.AppTest Test set: systemes.repartie.tree.ftp.AppTest
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.028 s - in systemes.repartie.tree.ftp.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.029 s - in systemes.repartie.tree.ftp.AppTest
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment