dossier test : code source du server externe

This commit is contained in:
Elise Barnabe 2021-01-15 14:02:31 +01:00
parent db688b27a1
commit f5b3031ec4
20 changed files with 583 additions and 0 deletions

23
test/.classpath Normal file
View file

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre">
<attributes>
<attribute name="module" value="true"/>
<attribute name="owner.project.facets" value="java"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/Users/elise/OneDrive/Bureau/ELISE/INSA/4A/POO COO/mysql-connector-java-5.0.5/mysql-connector-java-5.0.5/mysql-connector-java-5.0.5-bin.jar">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v9.0">
<attributes>
<attribute name="owner.project.facets" value="jst.web"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="output" path="WebContent/WEB-INF/classes"/>
</classpath>

31
test/.project Normal file
View file

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>test</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>

12
test/.settings/.jsdtscope Normal file
View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="**/bower_components/*|**/node_modules/*|**/*.min.js" kind="src" path="WebContent"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
<attributes>
<attribute name="hide" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
<classpathentry kind="output" path=""/>
</classpath>

View file

@ -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

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="test">
<wb-resource deploy-path="/" source-path="/WebContent" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src"/>
<property name="context-root" value="test"/>
<property name="java-output-path" value="/test/build/classes"/>
</wb-module>
</project-modules>

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<runtime name="Apache Tomcat v9.0"/>
<fixed facet="wst.jsdt.web"/>
<fixed facet="jst.web"/>
<fixed facet="java"/>
<installed facet="jst.web" version="4.0"/>
<installed facet="wst.jsdt.web" version="1.0"/>
<installed facet="java" version="11"/>
</faceted-project>

View file

@ -0,0 +1 @@
org.eclipse.wst.jsdt.launching.baseBrowserLibrary

View file

@ -0,0 +1 @@
Window

View file

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Class-Path:

View file

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.sdzee.servlets.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/toto</url-pattern>
</servlet-mapping>
</web-app>

10
test/WebContent/test.html Normal file
View file

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test</title>
</head>
<body>
<p>Ceci est une page HTML.</p>
</body>
</html>

View file

@ -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";
}
}

View file

@ -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();
}
}
}

View file

@ -0,0 +1,91 @@
package com.controller;
import java.net.InetAddress;
import java.util.ArrayList;
public class ListeContacts {
private static Contact user;
private ArrayList<Contact> listeContact;
//la liste doit être un singleton
private static final ListeContacts instance = new ListeContacts();
public ListeContacts() {
this.listeContact = new ArrayList<Contact>();
}
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<Contact> getListe() {
return listeContact;
}
public void setListeContact(ArrayList<Contact> listeContact) {
this.listeContact = listeContact;
}
}

View file

@ -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;
}
}
}