diff --git a/test/.classpath b/test/.classpath new file mode 100644 index 0000000..c6bcd68 --- /dev/null +++ b/test/.classpath @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/.project b/test/.project new file mode 100644 index 0000000..9f028f3 --- /dev/null +++ b/test/.project @@ -0,0 +1,31 @@ + + + test + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.wst.common.project.facet.core.nature + org.eclipse.jdt.core.javanature + org.eclipse.wst.jsdt.core.jsNature + + diff --git a/test/.settings/.jsdtscope b/test/.settings/.jsdtscope new file mode 100644 index 0000000..92e666d --- /dev/null +++ b/test/.settings/.jsdtscope @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/test/.settings/org.eclipse.jdt.core.prefs b/test/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..a58ebdc --- /dev/null +++ b/test/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,15 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=11 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=11 diff --git a/test/.settings/org.eclipse.wst.common.component b/test/.settings/org.eclipse.wst.common.component new file mode 100644 index 0000000..ebe14cd --- /dev/null +++ b/test/.settings/org.eclipse.wst.common.component @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/test/.settings/org.eclipse.wst.common.project.facet.core.xml b/test/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000..0923cd2 --- /dev/null +++ b/test/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/test/.settings/org.eclipse.wst.jsdt.ui.superType.container b/test/.settings/org.eclipse.wst.jsdt.ui.superType.container new file mode 100644 index 0000000..3bd5d0a --- /dev/null +++ b/test/.settings/org.eclipse.wst.jsdt.ui.superType.container @@ -0,0 +1 @@ +org.eclipse.wst.jsdt.launching.baseBrowserLibrary \ No newline at end of file diff --git a/test/.settings/org.eclipse.wst.jsdt.ui.superType.name b/test/.settings/org.eclipse.wst.jsdt.ui.superType.name new file mode 100644 index 0000000..05bd71b --- /dev/null +++ b/test/.settings/org.eclipse.wst.jsdt.ui.superType.name @@ -0,0 +1 @@ +Window \ No newline at end of file diff --git a/test/WebContent/META-INF/MANIFEST.MF b/test/WebContent/META-INF/MANIFEST.MF new file mode 100644 index 0000000..254272e --- /dev/null +++ b/test/WebContent/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Class-Path: + diff --git a/test/WebContent/WEB-INF/classes/com/controller/Contact.class b/test/WebContent/WEB-INF/classes/com/controller/Contact.class new file mode 100644 index 0000000..9660d42 Binary files /dev/null and b/test/WebContent/WEB-INF/classes/com/controller/Contact.class differ diff --git a/test/WebContent/WEB-INF/classes/com/controller/DataBase.class b/test/WebContent/WEB-INF/classes/com/controller/DataBase.class new file mode 100644 index 0000000..6b1d17c Binary files /dev/null and b/test/WebContent/WEB-INF/classes/com/controller/DataBase.class differ diff --git a/test/WebContent/WEB-INF/classes/com/controller/ListeContacts.class b/test/WebContent/WEB-INF/classes/com/controller/ListeContacts.class new file mode 100644 index 0000000..9b4e06d Binary files /dev/null and b/test/WebContent/WEB-INF/classes/com/controller/ListeContacts.class differ diff --git a/test/WebContent/WEB-INF/classes/com/sdzee/servlets/Test.class b/test/WebContent/WEB-INF/classes/com/sdzee/servlets/Test.class new file mode 100644 index 0000000..e6c96d2 Binary files /dev/null and b/test/WebContent/WEB-INF/classes/com/sdzee/servlets/Test.class differ diff --git a/test/WebContent/WEB-INF/lib/mysql-connector-java-5.0.5-bin.jar b/test/WebContent/WEB-INF/lib/mysql-connector-java-5.0.5-bin.jar new file mode 100644 index 0000000..af8ec67 Binary files /dev/null and b/test/WebContent/WEB-INF/lib/mysql-connector-java-5.0.5-bin.jar differ diff --git a/test/WebContent/WEB-INF/web.xml b/test/WebContent/WEB-INF/web.xml new file mode 100644 index 0000000..7bdaacb --- /dev/null +++ b/test/WebContent/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + + Test + com.sdzee.servlets.Test + + + + Test + /toto + + \ No newline at end of file diff --git a/test/WebContent/test.html b/test/WebContent/test.html new file mode 100644 index 0000000..969cb6f --- /dev/null +++ b/test/WebContent/test.html @@ -0,0 +1,10 @@ + + + + + Test + + +

Ceci est une page HTML.

+ + \ No newline at end of file diff --git a/test/src/com/controller/Contact.java b/test/src/com/controller/Contact.java new file mode 100644 index 0000000..dd0b7ca --- /dev/null +++ b/test/src/com/controller/Contact.java @@ -0,0 +1,66 @@ +package com.controller; + +import java.io.*; +import java.net.InetAddress; + +public class Contact { + + private String pseudo; + private String status; + private int port; + private int id; + + private InetAddress address; + + + public Contact(String pseudo, InetAddress address, int port) { + this.pseudo = pseudo; + this.address = address; + this.status = ""; + this.port = port; + } + + public String getPseudo() { + return this.pseudo; + } + + public void setPseudo(String pseudo) { + this.pseudo = pseudo; + } + + public InetAddress getAddress() { + return this.address; + } + + public void setAddress(InetAddress address) { + this.address = address; + } + + public String getStatut() { + return this.status; + } + + public void setStatut(String status) { + this.status = status; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String toString() { + return "Pseudo : " + pseudo + " status : " + status + " \n"; + } +} diff --git a/test/src/com/controller/DataBase.java b/test/src/com/controller/DataBase.java new file mode 100644 index 0000000..dd1eef0 --- /dev/null +++ b/test/src/com/controller/DataBase.java @@ -0,0 +1,193 @@ +package com.controller; + +import java.sql.*; + +public class DataBase { + + //Connexion serveur GEI + //login : tp_servlet_018 + //password : Taey2Aje + //Plus d'infs connexion sur moodle + + private static DataBase instance = null; + + public Connection connexion; + + private DataBase() { + try { + Class.forName("com.mysql.jdbc.Driver"); + }catch (ClassNotFoundException e){ + e.printStackTrace(); + } + try { + connexion = DriverManager.getConnection("jdbc:mysql://srv-bdens.insa-toulouse.fr:3306/tp_servlet_018?useSSL=false", "tp_servlet_018", "Taey2Aje"); + //connexion = DriverManager.getConnection("jdbc:mysql://localhost/test?characterEncoding=utf8", "root","root"); + }catch(SQLException e) { + e.printStackTrace(); + } + } + + public static DataBase getInstance() { + synchronized (DataBase.class) { + DataBase db = instance; + if(db == null) { + db = new DataBase(); + } + return db; + } + } + + //retourne l'id associé au user ajouté + public int addUser(String pseudo, String login) { + String query = "INSERT INTO users (pseudo, statut, login) VALUES (?, ?, ?)"; + PreparedStatement pStat = null; + try { + pStat = connexion.prepareStatement(query); + pStat.setString(1, pseudo); + pStat.setString(2, "En ligne"); + pStat.setString(3, login); + pStat.executeUpdate(); + }catch (SQLException e) { + e.printStackTrace(); + } + query = "SELECT * FROM users WHERE pseudo=?"; + int id =0; + try { + pStat = connexion.prepareStatement(query); + pStat.setString(1, pseudo); + ResultSet resId = pStat.executeQuery(); + if(resId.first()) { + id = resId.getInt("id"); + } + }catch(SQLException e) { + e.printStackTrace(); + } + return id; + } + + public String getPseudoFromId(int id) { + String pseudo = null; + String query = "SELECT * FROM users WHERE id=?"; + PreparedStatement pStat = null; + try { + pStat = connexion.prepareStatement(query); + pStat.setInt(1, id); + ResultSet resPseudo = pStat.executeQuery(); + if(resPseudo.first()) { + pseudo = resPseudo.getString("pseudo"); + } + }catch(SQLException e) { + e.printStackTrace(); + } + return pseudo; + } + + public int getIdFromPseudo(String pseudo) { + int id = -1; + String query = "SELECT * FROM users WHERE pseudo='" + pseudo + "' AND statut = 'En ligne'"; + PreparedStatement pStat = null; + try { + pStat = connexion.prepareStatement(query); + //pStat.setString(1, pseudo); + ResultSet resId = pStat.executeQuery(); + if(resId.next()) { + id = resId.getInt(1); + } + }catch(SQLException e) { + e.printStackTrace(); + } + return id; + } + + public boolean loginExiste(String login) { + String loginAux = null; + String query = "SELECT * FROM users WHERE login =?"; + PreparedStatement pStat = null; + try { + pStat = connexion.prepareStatement(query); + pStat.setString(1, login); + ResultSet rs = pStat.executeQuery(); + if (rs.first()) { + loginAux = rs.getString("login"); + } + } catch (SQLException e) { + e.printStackTrace(); + } + //System.out.println("loginAux = "+loginAux); + return !(loginAux==null); + } + + public String getPseudoFromLogin(String login) { + String pseudo = null; + String query = "SELECT * FROM users WHERE login=?"; + PreparedStatement pStat = null; + try { + pStat = connexion.prepareStatement(query); + pStat.setString(1, login); + ResultSet resPseudo = pStat.executeQuery(); + if(resPseudo.first()) { + pseudo = resPseudo.getString("pseudo"); + } + }catch(SQLException e) { + e.printStackTrace(); + } + return pseudo; + } + + public int getIdFromLogin(String login) { + int id = -1; + String query = "SELECT * FROM users WHERE login=?"; + PreparedStatement pStat = null; + try { + pStat = connexion.prepareStatement(query); + pStat.setString(1, login); + ResultSet resPseudo = pStat.executeQuery(); + if(resPseudo.first()) { + id = resPseudo.getInt("id"); + } + }catch(SQLException e) { + e.printStackTrace(); + } + return id; + } + + public void deleteFromId(int id) { + String query = "DELETE FROM `users` WHERE `id` = ?"; + PreparedStatement pStat = null; + try { + pStat = connexion.prepareStatement(query); + pStat.setInt(1, id); + pStat.executeUpdate(); + }catch(SQLException e) { + e.printStackTrace(); + } + + } + + public void updatePseudo(int id, String pseudo) { + String query = "UPDATE `users` SET `pseudo`=? WHERE id=?"; + PreparedStatement pStat = null; + try { + pStat = connexion.prepareStatement(query); + pStat.setString(1, pseudo); + pStat.setInt(2, id); + pStat.executeUpdate(); + }catch(SQLException e) { + e.printStackTrace(); + } + } + + public void updateStatus(int id, String status) { + String query = "UPDATE `users` SET `statut`=? WHERE id=?"; + PreparedStatement pStat = null; + try { + pStat = connexion.prepareStatement(query); + pStat.setString(1, status); + pStat.setInt(2, id); + pStat.executeUpdate(); + }catch(SQLException e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file diff --git a/test/src/com/controller/ListeContacts.java b/test/src/com/controller/ListeContacts.java new file mode 100644 index 0000000..238a998 --- /dev/null +++ b/test/src/com/controller/ListeContacts.java @@ -0,0 +1,91 @@ +package com.controller; + +import java.net.InetAddress; +import java.util.ArrayList; + +public class ListeContacts { + private static Contact user; + private ArrayList listeContact; + + + //la liste doit être un singleton + + private static final ListeContacts instance = new ListeContacts(); + + public ListeContacts() { + this.listeContact = new ArrayList(); + } + + public static final ListeContacts getInstance() { + return instance; + } + + public boolean contactExist(Contact contact) { + for(Contact c : listeContact) { + if(c.getPseudo().equals(contact.getPseudo())){ + return true; + } + } + return false; + } + + public boolean pseudoExist(String pseudo) { + for(Contact c : listeContact) { + if(c.getPseudo().equals(pseudo)){ + return true; + } + } + return false; + } + + public void addContact(Contact contact) { + if(!contactExist(contact)){ + System.out.println("Pseudo : "+contact.getPseudo()+" ajouté\n"); + listeContact.add(contact); + } + } + + public void deleteContact(Contact contact) { + if(contactExist(contact)){ + System.out.println("Pseudo : "+contact.getPseudo()+"supprimé\n"); + listeContact.remove(contact); + } + } + + public int length() { + int n = 0; + while(!listeContact.isEmpty()) { + n++; + } + return n; + } + + public Contact findContact(String pseudo) { + Contact contact = null; + for(Contact c : listeContact) { + if(c.getPseudo().equals(pseudo)){ + contact = c; + break; + } + } + return contact; + } + + + public String actifUsers() { + String users = ""; + for(Contact c : this.listeContact) { + users += (c.getPseudo()+"\n"); + } + return users; + } + + public ArrayList getListe() { + return listeContact; + } + + public void setListeContact(ArrayList listeContact) { + this.listeContact = listeContact; + } + +} diff --git a/test/src/com/sdzee/servlets/Test.java b/test/src/com/sdzee/servlets/Test.java new file mode 100644 index 0000000..c6f6a02 --- /dev/null +++ b/test/src/com/sdzee/servlets/Test.java @@ -0,0 +1,103 @@ +package com.sdzee.servlets; + +import java.io.IOException; +import java.net.InetAddress; +import java.util.ArrayList; + +import javax.servlet.ServletException; +import javax.servlet.http.*; + +import com.controller.*; + +public class Test extends HttpServlet { + + private DataBase db; + private ListeContacts listActifs; + public void init() { + listActifs = ListeContacts.getInstance(); + db = DataBase.getInstance(); + } + + public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException{ + System.out.println("j'ai reçu la requete"); + String login; + String pseudo; + String res; + String adr; + String port; + String type = request.getParameter("type"); + + switch (type) { + case "loginExiste": + login = request.getParameter("login"); + boolean loginOK = DataBase.getInstance().loginExiste(login); + res = Boolean.toString(loginOK); + response.setHeader("result", res); + break; + case "pseudoOK": + boolean pseudoOK = true; + pseudo = request.getParameter("pseudo"); + for(Contact c : listActifs.getListe()) { + if(c.getPseudo().equals(pseudo)){ + pseudoOK = false; + } + } + res = Boolean.toString(pseudoOK); + response.setHeader("result", res); + break; + case "addUser": + pseudo = request.getParameter("pseudo"); + adr = request.getParameter("adr"); + String adr2= adr.substring(1); + System.out.println(adr2); + port = request.getParameter("port"); + InetAddress address = InetAddress.getByName(adr2); + int portInt = Integer.parseInt(port); + Contact user = new Contact(pseudo, address, portInt); + listActifs.addContact(user); + break; + + case "pseudoChanged": + pseudo = request.getParameter("pseudo"); + String ancienPseudo = request.getParameter("pseudoVieux"); + System.out.println("ancien pseudo :"+ancienPseudo+" et nouveau :"+pseudo); + for(Contact c : listActifs.getListe()) { + if(c.getPseudo().equals(ancienPseudo)){ + c.setPseudo(pseudo); + } + } + System.out.println("actifs :"+listActifs.actifUsers()); + break; + + case "deconnexion": + pseudo = request.getParameter("pseudo"); + String idRequest = request.getParameter("id"); + System.out.println("actifs :"+listActifs.actifUsers()); + int id = Integer.parseInt(idRequest); + Contact c = listActifs.findContact(pseudo); + listActifs.deleteContact(c); + db.updateStatus(id, "Hors Ligne"); + break; + + + + + case "getUser": + pseudo = request.getParameter("pseudo"); + Contact dest = listActifs.findContact(pseudo); + response.setHeader("address", dest.getAddress().toString()); + response.setHeader("port", Integer.toString(dest.getPort())); + + break; + + case "actifs": + String actifs = listActifs.actifUsers(); + //String actifs = "ceci sont les actifs"; + response.setHeader("actifs", actifs); + break; + + } + + + } +}