Skip to content
Snippets Groups Projects
Commit 33f8b4f1 authored by Samy Meghari's avatar Samy Meghari
Browse files

livrable 3

parent 7e7053fa
Branches
Tags
No related merge requests found
Showing
with 351 additions and 259 deletions
/target/
File added
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
</dependencies> </dependencies>
<build> <build>
<finalName>Tree</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins> <plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
...@@ -51,6 +52,25 @@ ...@@ -51,6 +52,25 @@
<plugin> <plugin>
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version> <version>3.0.2</version>
<configuration>
<archive>
<manifest>
<mainClass>sr1.main.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
<reportOutputDirectory>${project.build.directory}/docs</reportOutputDirectory>
<destDir>docs</destDir>
<nohelp>true</nohelp>
</configuration>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-install-plugin</artifactId> <artifactId>maven-install-plugin</artifactId>
......
package sr1;
import java.io.IOException;
import java.net.UnknownHostException;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) throws UnknownHostException, IOException
{
ClientFTP myclient = new ClientFTP("anonymous", "");
Client2FTP myclient2 = new Client2FTP(myclient);
myclient2.init("ftp.free.fr", 21);
myclient2.myExplorer("/","");
}
}
package sr1;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
public class Client2FTP {
private Socket mysocket;
private ClientFTP myclient;
public Client2FTP(ClientFTP myclient) {
this.myclient = myclient;
}
public void init(String ip,int port)throws UnknownHostException, IOException {
mysocket = new Socket(ip,port);
OutputStream out = mysocket.getOutputStream();
PrintWriter printer = new PrintWriter(out, true);
InputStream in = mysocket.getInputStream();
InputStreamReader isr= new InputStreamReader(in);
BufferedReader reader = new BufferedReader(isr);
String content = reader.readLine();
System.out.println(content);
printer.println("USER "+ this.myclient.getUser().trim()+"\r\n");
content = reader.readLine();
System.out.println(content);
printer.println("PASS "+this.myclient.getPassword().trim()+"\r\n");
content = reader.readLine();
System.out.println(content);
}
public void myExplorer(String dir,String space) throws IOException {
List<String> myFolder = new ArrayList<>();
myFolder = displaymyFolder(dir);
System.out.println("");
System.out.println("Display the first folders : ");
space+=" ";
for (String sd : myFolder) {
System.out.println(space+"└──"+sd);
}
}
public List<String> displaymyFolder(String myDir) throws IOException {
List<String> myFolder = new ArrayList<>();
OutputStream out = mysocket.getOutputStream();
PrintWriter printer = new PrintWriter(out, true);
InputStream in = mysocket.getInputStream();
InputStreamReader isr= new InputStreamReader(in);
BufferedReader reader = new BufferedReader(isr);
printer.println("CWD "+myDir+"\r\n");
reader.readLine();
printer.println("PWD "+"\r\n");
reader.readLine();
printer.println("PASV "+"\r\n");
String res = "";
int newport = 0;
String newip = null;
res= reader.readLine();
if(res.toLowerCase().startsWith("227 entering passive mode")) {
String temp = res.substring(res.indexOf("(")+1,res.indexOf(")"));
newip = getIP(temp);
newport = getPort(temp);
myFolder = getData(newip, newport);
}
res= reader.readLine();
return myFolder;
}
private List<String> getData(String ip,int newport) throws UnknownHostException, IOException {
OutputStream out = mysocket.getOutputStream();
PrintWriter printer = new PrintWriter(out, true);
printer.println("NLST "+"\r\n");
Socket mySocket;
List<String> myFolder = new ArrayList<>();
mySocket = new Socket(ip,newport);
InputStream in = mySocket.getInputStream();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader reader2 = new BufferedReader(isr);
String dir;
while((dir = reader2.readLine())!=null){
myFolder.add(dir);
}
mySocket.close();
return myFolder;
}
public String getIP(String ip) {
String[] s = ip.split(",");
return s[0]+ "." + s[1]+ "." + s[2]+ "." + s[3];
}
public int getPort(String port) {
String[] s = port.split(",");
int i = Integer.parseInt(s[4])*256 + Integer.parseInt(s[5]);
return i;
}
}
package sr1; package sr1;
import java.io.BufferedReader; import java.io.*;
import java.io.IOException; import java.net.*;
import java.io.InputStream;
import java.io.InputStreamReader; /**
import java.io.OutputStream; * @author Samy MEGHARI
import java.io.PrintWriter; *
import java.net.Socket; * */
import java.net.UnknownHostException;
public class ClientFTP { public class ClientFTP {
private Socket mysocket;
private String user; private String user;
private String password; private String password;
private Socket mysocket;
public ClientFTP(String user, String password) { public ClientFTP(String user, String password) {
this.user=user; this.user=user;
this.password= password; this.password= password;
} }
public ClientFTP() {
this.user="anonymous";
this.password= "";
}
public String getUser() { public String getUser() {
return user; return user;
} }
public Socket getSocket() {
return mysocket;
}
public String getPassword() { public String getPassword() {
return password; return password;
} }
public String getIP(String ip) { /**
String[] s = ip.split(","); * Lance le client
return s[0]+ "." + s[1]+ "." + s[2]+ "." + s[3]; *
} * @param ip , l'ip du serveur
* @param port , le port du serveur
public int getPort(String port) { * @throws IOException
String[] s = port.split(","); * @throws ErrorException
int i = Integer.parseInt(s[4])*256 + Integer.parseInt(s[5]); */
return i; public void init(String ip, int port)throws IOException, ErrorException {
}
public void init(String ip, int port) throws UnknownHostException, IOException { mysocket = new Socket(ip, port);
this.mysocket = new Socket(ip,port);
OutputStream out = mysocket.getOutputStream(); OutputStream out = mysocket.getOutputStream();
PrintWriter printer = new PrintWriter(out, true); PrintWriter printer = new PrintWriter(out, true);
...@@ -53,7 +48,6 @@ public class ClientFTP { ...@@ -53,7 +48,6 @@ public class ClientFTP {
String content = reader.readLine(); String content = reader.readLine();
System.out.println(content); System.out.println(content);
//printer.println("USER "+ getUser() + "\r\n" + "PASS" + getPassword() + "\r\n" + "PWD" + "\r\n" +"PASV" + "\r\n"+ "LIST" + "\r\n");
printer.println("USER " + getUser().trim() + "\r\n"); printer.println("USER " + getUser().trim() + "\r\n");
content = reader.readLine(); content = reader.readLine();
...@@ -62,69 +56,28 @@ public class ClientFTP { ...@@ -62,69 +56,28 @@ public class ClientFTP {
printer.println("PASS " + getPassword().trim() + "\r\n"); printer.println("PASS " + getPassword().trim() + "\r\n");
content = reader.readLine(); content = reader.readLine();
System.out.println(content); System.out.println(content);
/*
content = reader.readLine();
System.out.println(content);
content = reader.readLine();
System.out.println(content);
content = reader.readLine();
System.out.println(content);
content = reader.readLine();
System.out.println(content);
String pasv = content.substring(content.indexOf("(")+1,content.indexOf(")"));
String [] mysplit = pasv.split(",");
String newip = mysplit[0]+"."+mysplit[1]+"."+mysplit[2]+"."+mysplit[3];
int newport = Integer.parseInt(mysplit[4])*256+ Integer.parseInt(mysplit[5]);
*/
int newport = 0;
String newip = null;
while((content = reader.readLine()) !=null) {
if (content.toLowerCase().startsWith("227 entering passive mode")) {
String temp = content.substring(content.indexOf("(")+1,content.indexOf(")"));
ip = getIP(temp);
newport = getPort(temp);
getData(newip, newport,printer,reader);
} }
/**
* Parse le retour de la commande PASV et recupere l'IP
* @param ip commande FTP PASV nettoye
* @return IP reconstruite
*/
public String getIP(String ip) {
String[] s = ip.split(",");
return s[0]+ "." + s[1]+ "." + s[2]+ "." + s[3];
} }
this.mysocket.close();
/*
Socket newsocket = new Socket(newip,newport);
OutputStream newout = newsocket.getOutputStream();
PrintWriter newprinter = new PrintWriter(newout, true);
InputStream newin = newsocket.getInputStream(); /**
InputStreamReader newisr= new InputStreamReader(newin); * Parse le retour de la commande PASV , recupere le port et le recalcul
BufferedReader newreader = new BufferedReader(newisr); *
* @param port retour de la commande PASV nettoye
String newcontent = newreader.readLine(); * @return le port recalculer
System.out.println(newcontent);
content = reader.readLine();
System.out.println(content);
*/ */
public int getPort(String port) {
String[] s = port.split(",");
int i = Integer.parseInt(s[4])*256 + Integer.parseInt(s[5]);
return i;
} }
private void getData(String ip,int newport,PrintWriter myprinter,BufferedReader myreader) throws UnknownHostException, IOException {
Socket mySocket;
mySocket = new Socket(ip,newport);
InputStream in = mySocket.getInputStream();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader myreader2 = new BufferedReader(isr);
String dir;
while((dir = myreader2.readLine())!=null){
System.out.println(dir);
}
mySocket.close();
}
} }
package sr1;
public class ErrorException extends Exception{
/**
*
*/
private static final long serialVersionUID = 1L;
public ErrorException() {
super("failed to connect, please check your ids");
}
public ErrorException(String msg) {
super(msg);
}
}
package sr1;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
/**
* @author Samy MEGHARI
*
* */
public class Tree {
private String myfile;
private ClientFTP myclient;
public Tree(ClientFTP client) {
myclient = client;}
/**
* Parse le retour de la commande LIST
*/
private String analyse(String myreponse) {
String reponse = myreponse.replaceAll("(\\s)+", " ");
String[] tab = reponse.split(" ");
if (tab.length > 9) {
for (int i = 8; i < tab.length; i++) {
this.myfile += " " + tab[i];
}
} else {
this.myfile = tab[8];
}
return myfile;
}
/**
* affiche l'arborescence d'un repertoir
*
* @param dir le repertoire a parcouru
* @param res espace pour distinguer l'arborescence
* @param profondeur profondeur maximale
* @throws IOException
* @throws @throws ErrorException
*/
private void myExplorer(String dir, String res, int profondeur)
throws IOException, ErrorException {
if (profondeur > 0) {
List<String> myFolder = new ArrayList<>();
myFolder = displaymyFolder(dir);
res += " ";
for (String S : myFolder) {
switch (S.charAt(0)) {
case ('d'):
System.out.println(res + " └── " + analyse(S));
myExplorer(dir + "/" + analyse(S), res, profondeur - 1);
case ('l'):
System.out.println(res + " └──" + analyse(S));
case ('-'):
System.out.println(res + " └── " + analyse(S));
default:
break;
}
}
} else {
return;
}
}
/**
* Affiche l'arborescence des dossiers
*
* @param dir le repertoire parcouru
* @param profondeur profondeur maximale
* @throws IOException
* @throws ErrorException
*/
public void displayFolders(String dir, int profondeur)throws IOException, ErrorException {
List<String> myFolder = new ArrayList<>();
myFolder = displaymyFolder(dir);
if (dir == "") {
System.out.println("/");
} else {
System.out.println(dir);
}
if (myFolder.isEmpty()) {
System.out.println("empty");
return;
}
for (String s : myFolder) {
switch (s.charAt(0)) {
case ('d'):
System.out.println(" └── " + s);
myExplorer(dir + "/" + analyse(s), "", profondeur - 1);
break;
case ('l'):
System.out.println(" └──" + s);
break;
case ('-'):
System.out.println(" └── " + s);
break;
default:
break;
}
}
}
/**
* Recupere la sortie de LIST
*
* @param ip , Ip du nouveau socket
* @param port, Port du nouveau socket
* @return transforme en liste la sortie de LIST
*/
private List<String> getData(String ip, int port) throws UnknownHostException, IOException {
OutputStream out = this.myclient.getSocket().getOutputStream();
PrintWriter printer = new PrintWriter(out, true);
printer.println("LIST " + "\r\n");
Socket mySocket;
List<String> myFolder = new ArrayList<>();
mySocket = new Socket(ip, port);
InputStream in = mySocket.getInputStream();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader reader2 = new BufferedReader(isr);
String dir;
while ((dir = reader2.readLine()) != null) {
myFolder.add(dir);
}
mySocket.close();
return myFolder;
}
/**
*Renvoie le contenu d'un dossier
*
* @param myDir le dossier parcouru
* @return la liste des elements du repertoire
* @throws IOException
*/
public List<String> displaymyFolder(String myDir) throws IOException {
List<String> myFolder = new ArrayList<>();
OutputStream out = this.myclient.getSocket().getOutputStream();
PrintWriter printer = new PrintWriter(out, true);
InputStream in = this.myclient.getSocket().getInputStream();
InputStreamReader isr= new InputStreamReader(in);
BufferedReader reader = new BufferedReader(isr);
printer.println("CWD "+myDir+"\r\n");
reader.readLine();
printer.println("PWD "+"\r\n");
reader.readLine();
printer.println("PASV "+"\r\n");
String res = "";
int newport = 0;
String newip = null;
res= reader.readLine();
if(res.toLowerCase().startsWith("227 entering passive mode")) {
String temp = res.substring(res.indexOf("(")+1,res.indexOf(")"));
newip = this.myclient.getIP(temp);
newport = this.myclient.getPort(temp);
myFolder = getData(newip, newport);
}
res= reader.readLine();
return myFolder;
}
}
package sr1.main;
import java.io.IOException;
import sr1.*;
/**
* @author Samy MEGHARI
*
* */
public class Main {
/**
*lancera Tree en fonction des arguments passés
*
*@param args les arguments passées
*@throws IOException
*@throws ErrorException
*/
public static void main(String[] args) throws IOException, ErrorException {
int len = args.length;
ClientFTP client;
String serveur = "";
Tree tree;
if(len>= 1 && len<=4) {
serveur = args[0];
client = new ClientFTP("anonymous","anonymous");
client.init(serveur, 21);
tree = new Tree(client);
if(len == 1) {
tree.displayFolders("", 3);
}
else if(len == 2) {
tree.displayFolders("", Integer.parseInt(args[1]));
}
else if(len == 3) {
tree.displayFolders("", 3);
}
else if(len == 4) {
tree.displayFolders("", Integer.parseInt(args[3]));
}
}
else {
System.out.println("please check the readme");
}
}
}
package sr1;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
}
package sr1;
import java.io.IOException;
import java.net.UnknownHostException;
import org.junit.Before;
import org.junit.Test;
public class ClientFTPTest {
private ClientFTP client;
@Before
public void setupBefore(){
client = new ClientFTP("test", "test");
}
@Test(expected = UnknownHostException.class)
public void testConnectWhenfalseAdress() throws UnknownHostException, IOException,ErrorException {
client.init("ftp.randomadress.com", 75);
}
}
/sr1/
File deleted
No preview for this file type
File deleted
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment