packages database,session cleaned and commented + minor revision communication
This commit is contained in:
parent
15a618ec50
commit
dce7df5494
12 changed files with 991 additions and 554 deletions
|
@ -102,7 +102,7 @@ public class FileTransferUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param imageString The base64 encoded string of an image.
|
* @param imageString The base64 encoded string of an image.
|
||||||
* @return A buffered image corresponding to the given base64 encoded string.
|
* @return A buffered image corresponding to the given base64 encoded string.
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -74,7 +74,7 @@ public class TCPClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method used when the session is over. Set all attribute references to null,
|
* Method used when the session is over. Set all attributes' references to null,
|
||||||
* interrupt the inputThread and close the streams and the socket.
|
* interrupt the inputThread and close the streams and the socket.
|
||||||
*/
|
*/
|
||||||
public void destroyAll() {
|
public void destroyAll() {
|
||||||
|
|
|
@ -20,7 +20,7 @@ public class CommunicationUDP extends Thread {
|
||||||
private ObserverUserList obsList;
|
private ObserverUserList obsList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the class that will manage the userlist and contain a UDPClient and a
|
* Create the object that will manage the userlist and contain a UDPClient and a
|
||||||
* UDPServer. Since the applications will run on localhost, it needs to know
|
* UDPServer. Since the applications will run on localhost, it needs to know
|
||||||
* every UDPServer ports used in order to replicate a broadcast behaviour.
|
* every UDPServer ports used in order to replicate a broadcast behaviour.
|
||||||
*
|
*
|
||||||
|
@ -63,7 +63,7 @@ public class CommunicationUDP extends Thread {
|
||||||
this.obsList = o;
|
this.obsList = o;
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------- USER LIST UPDATE FUNCTION --------------//
|
// -------------- USER LIST UPDATE METHODS -------------- //
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a new user to the userlist and notify the observer.
|
* Add a new user to the userlist and notify the observer.
|
||||||
|
@ -122,13 +122,13 @@ public class CommunicationUDP extends Thread {
|
||||||
this.users.clear();
|
this.users.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------- CHECKERS --------------//
|
// -------------- CHECKERS -------------- //
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if there is an user in the list that has the given id.
|
* Check if there is an user in the list that has the given id.
|
||||||
*
|
*
|
||||||
* @param id The user's id.
|
* @param id The user's id.
|
||||||
* @return true if the user is in the list
|
* @return True if the user is in the list
|
||||||
* false otherwise.
|
* false otherwise.
|
||||||
*/
|
*/
|
||||||
protected boolean containsUserFromID(String id) {
|
protected boolean containsUserFromID(String id) {
|
||||||
|
@ -144,7 +144,7 @@ public class CommunicationUDP extends Thread {
|
||||||
* Check if there is an user in the list that has the given pseudo.
|
* Check if there is an user in the list that has the given pseudo.
|
||||||
*
|
*
|
||||||
* @param pseudo The user's pseudo.
|
* @param pseudo The user's pseudo.
|
||||||
* @return true if the user is in the list
|
* @return True if the user is in the list
|
||||||
* false otherwise.
|
* false otherwise.
|
||||||
*/
|
*/
|
||||||
public boolean containsUserFromPseudo(String pseudo) {
|
public boolean containsUserFromPseudo(String pseudo) {
|
||||||
|
@ -157,7 +157,7 @@ public class CommunicationUDP extends Thread {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------- GETTERS --------------//
|
// -------------- GETTERS -------------- //
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the user with the given pseudo if it exists in the list.
|
* Return the user with the given pseudo if it exists in the list.
|
||||||
|
@ -191,7 +191,7 @@ public class CommunicationUDP extends Thread {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------- SEND MESSAGES --------------//
|
// -------------- SEND MESSAGES METHODS -------------- //
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a message indicating this application's user is connected to every
|
* Send a message indicating this application's user is connected to every
|
||||||
|
@ -279,6 +279,9 @@ public class CommunicationUDP extends Thread {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -------------- OTHERS -------------- //
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Notify the observer with the updated list
|
* Notify the observer with the updated list
|
||||||
|
|
|
@ -14,7 +14,7 @@ class UDPClient {
|
||||||
private DatagramSocket sockUDP;
|
private DatagramSocket sockUDP;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a UDP client on the specified port. It will be used to notify the
|
* Create an UDP client on the specified port. It will be used to notify the
|
||||||
* other users of this application's user state (Connected/Disconnected/Pseudo
|
* other users of this application's user state (Connected/Disconnected/Pseudo
|
||||||
* changed).
|
* changed).
|
||||||
*
|
*
|
||||||
|
|
|
@ -17,7 +17,7 @@ class UDPServer extends Thread {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a UDP Server on the specified port. It will be used to read the
|
* Create an UDP Server on the specified port. It will be used to read the
|
||||||
* other users states (Connected/Disconnected/Pseudo).
|
* other users states (Connected/Disconnected/Pseudo).
|
||||||
*
|
*
|
||||||
* @param port
|
* @param port
|
||||||
|
|
|
@ -6,57 +6,101 @@ import java.sql.Statement;
|
||||||
|
|
||||||
class SQLiteCreateTables {
|
class SQLiteCreateTables {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the user table if it does not exist in the database.
|
||||||
|
* An user is characterized by :
|
||||||
|
* - an unique username,
|
||||||
|
* - a password salt,
|
||||||
|
* - a database_key salt,
|
||||||
|
* - a password hash encrypted by the database_key,
|
||||||
|
* - the encrypted database_key,
|
||||||
|
* - the initialization vector used to encrypt the database key.
|
||||||
|
*
|
||||||
|
* @param connec The opened connection to the database.
|
||||||
|
* @throws SQLException
|
||||||
|
*/
|
||||||
protected static void createTableUser(Connection connec) throws SQLException {
|
protected static void createTableUser(Connection connec) throws SQLException {
|
||||||
String createTableUser = "CREATE TABLE IF NOT EXISTS user (\r\n"
|
String createTableUser = "CREATE TABLE IF NOT EXISTS user (\r\n"
|
||||||
+ " id INTEGER PRIMARY KEY AUTOINCREMENT,\r\n"
|
+ " id INTEGER PRIMARY KEY AUTOINCREMENT,\r\n"
|
||||||
+ " username VARCHAR (50) NOT NULL\r\n"
|
+ " username VARCHAR (50) NOT NULL\r\n"
|
||||||
+ " UNIQUE ON CONFLICT ROLLBACK,\r\n"
|
+ " UNIQUE ON CONFLICT ROLLBACK,\r\n"
|
||||||
+ " pwd_salt BLOB,\r\n"
|
+ " pwd_salt BLOB,\r\n"
|
||||||
+ " db_datakey_salt BLOB,\r\n"
|
+ " db_datakey_salt BLOB,\r\n"
|
||||||
+ " encrypted_pwd_hashsalt BLOB,\r\n"
|
+ " encrypted_pwd_hashsalt BLOB,\r\n"
|
||||||
+ " encrypted_db_datakey BLOB,\r\n"
|
+ " encrypted_db_datakey BLOB,\r\n"
|
||||||
+ " iv_datakey BLOB\r\n"
|
+ " iv_datakey BLOB\r\n" + ");";
|
||||||
+ ");";
|
|
||||||
|
|
||||||
Statement stmt = connec.createStatement();
|
Statement stmt = connec.createStatement();
|
||||||
stmt.execute(createTableUser);
|
stmt.execute(createTableUser);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the conversation table if it does not exist in the database.
|
||||||
|
* A conversation is characterized by :
|
||||||
|
* - the id of the user who sends the messages,
|
||||||
|
* - the id of the user who receives the messages,
|
||||||
|
* - an initialization vector used to encrypt the conversation's messages.
|
||||||
|
*
|
||||||
|
* During an session between two users, two conversations are created.
|
||||||
|
*
|
||||||
|
* @param connec The opened connection to the database.
|
||||||
|
* @throws SQLException
|
||||||
|
*/
|
||||||
protected static void createTableConversation(Connection connec) throws SQLException {
|
protected static void createTableConversation(Connection connec) throws SQLException {
|
||||||
String createTableConversation = "CREATE TABLE IF NOT EXISTS conversation (\r\n"
|
String createTableConversation = "CREATE TABLE IF NOT EXISTS conversation (\r\n"
|
||||||
+ " id_conversation INTEGER PRIMARY KEY AUTOINCREMENT,\r\n"
|
+ " id_conversation INTEGER PRIMARY KEY AUTOINCREMENT,\r\n"
|
||||||
+ " id_emetteur INTEGER REFERENCES user (id) \r\n"
|
+ " id_sender INTEGER REFERENCES user (id) \r\n" + " NOT NULL,\r\n"
|
||||||
+ " NOT NULL,\r\n"
|
+ " id_receiver INTEGER REFERENCES user (id) \r\n" + " NOT NULL,\r\n"
|
||||||
+ " id_recepteur INTEGER REFERENCES user (id) \r\n"
|
+ " iv_conversation BLOB NOT NULL" + ");";
|
||||||
+ " NOT NULL,\r\n"
|
|
||||||
+" iv_conversation BLOB NOT NULL"
|
|
||||||
+ ");";
|
|
||||||
|
|
||||||
Statement stmt = connec.createStatement();
|
Statement stmt = connec.createStatement();
|
||||||
stmt.execute(createTableConversation);
|
stmt.execute(createTableConversation);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the message table if it does not exist in the database.
|
||||||
|
* A message is characterized by :
|
||||||
|
* - the id of the conversation it belongs,
|
||||||
|
* - the id of its type,
|
||||||
|
* - its content,
|
||||||
|
* - the date when it was emitted,
|
||||||
|
* - its extension if it is a file (text or image).
|
||||||
|
*
|
||||||
|
* @param connec The opened connection to the database.
|
||||||
|
* @throws SQLException
|
||||||
|
*/
|
||||||
protected static void createTableMessage(Connection connec) throws SQLException {
|
protected static void createTableMessage(Connection connec) throws SQLException {
|
||||||
String createTableMessage = "CREATE TABLE IF NOT EXISTS message (\r\n"
|
String createTableMessage = "CREATE TABLE IF NOT EXISTS message (\r\n"
|
||||||
+ " id_message INTEGER PRIMARY KEY AUTOINCREMENT,\r\n"
|
+ " id_message INTEGER PRIMARY KEY AUTOINCREMENT,\r\n"
|
||||||
+ " id_conversation INTEGER REFERENCES conversation (id_conversation) \r\n"
|
+ " id_conversation INTEGER REFERENCES conversation (id_conversation) \r\n"
|
||||||
+ " NOT NULL,\r\n"
|
+ " NOT NULL,\r\n"
|
||||||
+ " id_type INTEGER REFERENCES type (id_type) \r\n"
|
+ " id_type INTEGER REFERENCES type (id_type) \r\n"
|
||||||
+ " NOT NULL,\r\n"
|
+ " NOT NULL,\r\n"
|
||||||
+ " content BLOB,\r\n"
|
+ " content BLOB,\r\n"
|
||||||
+ " date INTEGER NOT NULL,\r\n"
|
+ " date INTEGER NOT NULL,\r\n"
|
||||||
+ " extension VARCHAR (20) \r\n"
|
+ " extension VARCHAR (20) \r\n" + ");\r\n";
|
||||||
+ ");\r\n";
|
|
||||||
|
|
||||||
Statement stmt = connec.createStatement();
|
Statement stmt = connec.createStatement();
|
||||||
stmt.execute(createTableMessage);
|
stmt.execute(createTableMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the (message) type table if it does not exist and insert the different
|
||||||
|
* types of message in the database.
|
||||||
|
* A type is characterized by :
|
||||||
|
* - a label.
|
||||||
|
*
|
||||||
|
* This table only exists because the type "enumeration" does not exist in SQLite.
|
||||||
|
* It is a static table that contains the different types of message stored in the database.
|
||||||
|
*
|
||||||
|
* @param connec The opened connection to the database.
|
||||||
|
* @throws SQLException
|
||||||
|
*/
|
||||||
protected static void createTableType(Connection connec) throws SQLException {
|
protected static void createTableType(Connection connec) throws SQLException {
|
||||||
String createTableType = "CREATE TABLE IF NOT EXISTS type (\r\n" + " id_type INTEGER PRIMARY KEY,\r\n"
|
String createTableType = "CREATE TABLE IF NOT EXISTS type (\r\n"
|
||||||
|
+ " id_type INTEGER PRIMARY KEY,\r\n"
|
||||||
+ " label VARCHAR (20) NOT NULL\r\n" + ");";
|
+ " label VARCHAR (20) NOT NULL\r\n" + ");";
|
||||||
|
|
||||||
Statement stmt = connec.createStatement();
|
Statement stmt = connec.createStatement();
|
||||||
|
|
|
@ -1,84 +0,0 @@
|
||||||
package database;
|
|
||||||
|
|
||||||
|
|
||||||
import java.util.Base64;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
import javax.crypto.*;
|
|
||||||
import javax.crypto.spec.*;
|
|
||||||
import java.security.*;
|
|
||||||
import java.security.spec.*;
|
|
||||||
|
|
||||||
class SQLiteEncprytion {
|
|
||||||
|
|
||||||
private static final Random RANDOM = new SecureRandom();
|
|
||||||
private static final int ITERATIONS = 10000;
|
|
||||||
private static final int KEY_LENGTH = 256;
|
|
||||||
protected static final String encryptAlgorithm = "AES/CBC/PKCS5Padding";
|
|
||||||
|
|
||||||
protected static byte[] getNextSalt() {
|
|
||||||
byte[] salt = new byte[24];
|
|
||||||
RANDOM.nextBytes(salt);
|
|
||||||
return salt;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected static byte[] hash(char[] password, byte[] salt) {
|
|
||||||
return SQLiteEncprytion.getKey(password, salt).getEncoded();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected static SecretKey getKey(char[] password, byte[] salt) {
|
|
||||||
PBEKeySpec saltpwd = new PBEKeySpec(password, salt, ITERATIONS, KEY_LENGTH);
|
|
||||||
|
|
||||||
try {
|
|
||||||
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
|
|
||||||
SecretKey tmp = skf.generateSecret(saltpwd);
|
|
||||||
SecretKey key = new SecretKeySpec(tmp.getEncoded(), "AES");
|
|
||||||
return key;
|
|
||||||
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
saltpwd.clearPassword();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static IvParameterSpec generateIv() {
|
|
||||||
byte[] iv = new byte[16];
|
|
||||||
new SecureRandom().nextBytes(iv);
|
|
||||||
return new IvParameterSpec(iv);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected static byte[] encrypt(String algorithm, byte[] input, SecretKey key, IvParameterSpec iv) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{
|
|
||||||
Cipher cipher = Cipher.getInstance(algorithm);
|
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
|
||||||
byte[] cipherText = cipher.doFinal(input);
|
|
||||||
return Base64.getEncoder().encode(cipherText);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected static byte[] decryptByte(String algorithm, byte[] cipherText, SecretKey key, IvParameterSpec iv) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{
|
|
||||||
Cipher cipher = Cipher.getInstance(algorithm);
|
|
||||||
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
|
||||||
byte[] plainText = cipher.doFinal(Base64.getDecoder().decode(cipherText));
|
|
||||||
return plainText;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static String decryptString(String algorithm, byte[] cipherText, SecretKey key, IvParameterSpec iv) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
|
|
||||||
return new String(SQLiteEncprytion.decryptByte(algorithm, cipherText, key, iv) );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static byte[] keyToByte(SecretKey key) {
|
|
||||||
return Base64.getEncoder().encode(key.getEncoded());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static SecretKey byteToKey(byte[] encodedKey) {
|
|
||||||
byte[] decodedKey = Base64.getDecoder().decode(encodedKey);
|
|
||||||
return new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
|
|
||||||
}
|
|
||||||
}
|
|
158
POO/src/database/SQLiteEncryption.java
Normal file
158
POO/src/database/SQLiteEncryption.java
Normal file
|
@ -0,0 +1,158 @@
|
||||||
|
package database;
|
||||||
|
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import javax.crypto.*;
|
||||||
|
import javax.crypto.spec.*;
|
||||||
|
import java.security.*;
|
||||||
|
import java.security.spec.*;
|
||||||
|
|
||||||
|
class SQLiteEncryption {
|
||||||
|
|
||||||
|
private static final Random RANDOM = new SecureRandom();
|
||||||
|
private static final int ITERATIONS = 10000;
|
||||||
|
private static final int KEY_LENGTH = 256;
|
||||||
|
protected static final String encryptAlgorithm = "AES/CBC/PKCS5Padding";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a 24 bytes salt.
|
||||||
|
*
|
||||||
|
* @return The salt in a byte array.
|
||||||
|
*/
|
||||||
|
protected static byte[] getNextSalt() {
|
||||||
|
byte[] salt = new byte[24];
|
||||||
|
RANDOM.nextBytes(salt);
|
||||||
|
return salt;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the hash of the given password with the given salt.
|
||||||
|
*
|
||||||
|
* @param password
|
||||||
|
* @param salt
|
||||||
|
* @return The hash in a byte array.
|
||||||
|
*/
|
||||||
|
protected static byte[] hash(char[] password, byte[] salt) {
|
||||||
|
return SQLiteEncryption.getKey(password, salt).getEncoded();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a secret key generated with the given password and salt.
|
||||||
|
*
|
||||||
|
* @param password
|
||||||
|
* @param salt
|
||||||
|
* @return The secret key.
|
||||||
|
*/
|
||||||
|
protected static SecretKey getKey(char[] password, byte[] salt) {
|
||||||
|
PBEKeySpec saltpwd = new PBEKeySpec(password, salt, ITERATIONS, KEY_LENGTH);
|
||||||
|
|
||||||
|
try {
|
||||||
|
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
|
||||||
|
SecretKey tmp = skf.generateSecret(saltpwd);
|
||||||
|
SecretKey key = new SecretKeySpec(tmp.getEncoded(), "AES");
|
||||||
|
return key;
|
||||||
|
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
saltpwd.clearPassword();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a 16 bytes Initialization vector.
|
||||||
|
*
|
||||||
|
* @return The Initialization vector.
|
||||||
|
*/
|
||||||
|
protected static IvParameterSpec generateIv() {
|
||||||
|
byte[] iv = new byte[16];
|
||||||
|
RANDOM.nextBytes(iv);
|
||||||
|
return new IvParameterSpec(iv);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encrypt the given input (byte array) with the given algorithm, secretKey and
|
||||||
|
* initialization vector.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param algorithm
|
||||||
|
* @param input
|
||||||
|
* @param key
|
||||||
|
* @param iv
|
||||||
|
* @return The encrypted input in a byte array.
|
||||||
|
*
|
||||||
|
* @throws NoSuchAlgorithmException
|
||||||
|
* @throws NoSuchPaddingException
|
||||||
|
* @throws InvalidKeyException
|
||||||
|
* @throws InvalidAlgorithmParameterException
|
||||||
|
* @throws IllegalBlockSizeException
|
||||||
|
* @throws BadPaddingException
|
||||||
|
*/
|
||||||
|
protected static byte[] encrypt(String algorithm, byte[] input, SecretKey key, IvParameterSpec iv)
|
||||||
|
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
|
||||||
|
InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
|
||||||
|
Cipher cipher = Cipher.getInstance(algorithm);
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
||||||
|
byte[] cipherText = cipher.doFinal(input);
|
||||||
|
return Base64.getEncoder().encode(cipherText);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decrypt the given input (byte array) with the given algorithm, secretKey and
|
||||||
|
* initialization vector.
|
||||||
|
*
|
||||||
|
* @param algorithm
|
||||||
|
* @param input
|
||||||
|
* @param key
|
||||||
|
* @param iv
|
||||||
|
* @return The decrypted input in a byte array.
|
||||||
|
*
|
||||||
|
* @throws NoSuchAlgorithmException
|
||||||
|
* @throws NoSuchPaddingException
|
||||||
|
* @throws InvalidKeyException
|
||||||
|
* @throws InvalidAlgorithmParameterException
|
||||||
|
* @throws IllegalBlockSizeException
|
||||||
|
* @throws BadPaddingException
|
||||||
|
*/
|
||||||
|
protected static byte[] decryptByte(String algorithm, byte[] input, SecretKey key, IvParameterSpec iv)
|
||||||
|
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
|
||||||
|
InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
|
||||||
|
Cipher cipher = Cipher.getInstance(algorithm);
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
||||||
|
byte[] plainText = cipher.doFinal(Base64.getDecoder().decode(input));
|
||||||
|
return plainText;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decrypt the given input (byte array) with the given algorithm, secretKey and
|
||||||
|
* initialization vector.
|
||||||
|
*
|
||||||
|
* @param algorithm
|
||||||
|
* @param input
|
||||||
|
* @param key
|
||||||
|
* @param iv
|
||||||
|
* @return The decrypted input as a String.
|
||||||
|
*
|
||||||
|
* @throws InvalidKeyException
|
||||||
|
* @throws NoSuchAlgorithmException
|
||||||
|
* @throws NoSuchPaddingException
|
||||||
|
* @throws InvalidAlgorithmParameterException
|
||||||
|
* @throws IllegalBlockSizeException
|
||||||
|
* @throws BadPaddingException
|
||||||
|
*/
|
||||||
|
protected static String decryptString(String algorithm, byte[] input, SecretKey key, IvParameterSpec iv)
|
||||||
|
throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
|
||||||
|
InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
|
||||||
|
return new String(SQLiteEncryption.decryptByte(algorithm, input, key, iv));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static byte[] keyToByte(SecretKey key) {
|
||||||
|
return Base64.getEncoder().encode(key.getEncoded());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static SecretKey byteToKey(byte[] encodedKey) {
|
||||||
|
byte[] decodedKey = Base64.getDecoder().decode(encodedKey);
|
||||||
|
return new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load diff
|
@ -38,18 +38,23 @@ public class ControleurSession implements ActionListener, ObserverInputMessage,
|
||||||
private ArrayList<Message> messagesOut;
|
private ArrayList<Message> messagesOut;
|
||||||
private SQLiteManager sqlManager;
|
private SQLiteManager sqlManager;
|
||||||
private ArrayList<File> files;
|
private ArrayList<File> files;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Create the controller for this session. It will manage all the objects used
|
||||||
|
* to send/receive messages and files as well as the ones interacting with the
|
||||||
|
* database. It will handle the actions performed on the view and call the
|
||||||
|
* appropriate methods to display messages and data on the view.
|
||||||
*
|
*
|
||||||
* @param vue
|
* @param vue The corresponding view
|
||||||
* @param socketComm
|
* @param socketComm The socket used to send/receive messages
|
||||||
* @param idOther
|
* @param idOther The other user's id
|
||||||
* @param pseudoOther
|
* @param pseudoOther The other user's pseudo
|
||||||
* @param sqlManager
|
* @param sqlManager The SQLManager instance to retrieve/insert
|
||||||
|
* users,conversations,messages from/into the database
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
protected ControleurSession(VueSession vue, Socket socketComm, String idOther, String pseudoOther, SQLiteManager sqlManager) throws IOException {
|
protected ControleurSession(VueSession vue, Socket socketComm, String idOther, String pseudoOther,
|
||||||
|
SQLiteManager sqlManager) throws IOException {
|
||||||
this.vue = vue;
|
this.vue = vue;
|
||||||
this.tcpClient = new TCPClient(socketComm);
|
this.tcpClient = new TCPClient(socketComm);
|
||||||
this.tcpClient.setObserverInputThread(this);
|
this.tcpClient.setObserverInputThread(this);
|
||||||
|
@ -57,173 +62,247 @@ public class ControleurSession implements ActionListener, ObserverInputMessage,
|
||||||
this.tcpClient.startInputThread();
|
this.tcpClient.startInputThread();
|
||||||
this.messagesIn = new ArrayList<Message>();
|
this.messagesIn = new ArrayList<Message>();
|
||||||
this.messagesOut = new ArrayList<Message>();
|
this.messagesOut = new ArrayList<Message>();
|
||||||
|
|
||||||
this.idOther = idOther;
|
this.idOther = idOther;
|
||||||
this.pseudoOther = pseudoOther;
|
this.pseudoOther = pseudoOther;
|
||||||
|
|
||||||
this.sqlManager = sqlManager;
|
this.sqlManager = sqlManager;
|
||||||
|
|
||||||
this.files = new ArrayList<File>();
|
this.files = new ArrayList<File>();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------- ACTION LISTENER OPERATIONS ----------//
|
|
||||||
|
// ---------- ACTION LISTENER OPERATIONS ---------- //
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
|
||||||
//If the button "Envoyer" is pressed
|
// If the button "Envoyer" is pressed
|
||||||
if ((JButton) e.getSource() == this.vue.getButtonEnvoyer()) {
|
if ((JButton) e.getSource() == this.vue.getButtonEnvoyer()) {
|
||||||
String messageContent = this.vue.getInputedText();
|
String messageContent = this.vue.getInputedText();
|
||||||
System.out.println(messageContent);
|
System.out.println(messageContent);
|
||||||
|
|
||||||
if(!this.files.isEmpty()) {
|
if (!this.files.isEmpty()) {
|
||||||
this.processSelectedFiles(messageContent);
|
this.processSelectedFiles(messageContent);
|
||||||
if(!this.files.isEmpty()) {
|
if (!this.files.isEmpty()) {
|
||||||
this.askFileTransfer();
|
this.askFileTransfer();
|
||||||
|
|
||||||
this.vue.resetZoneSaisie();
|
this.vue.resetZoneSaisie();
|
||||||
messageContent = "";
|
messageContent = "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the text field is not empty
|
||||||
//If the text field is not empty
|
|
||||||
if (!messageContent.equals("")) {
|
if (!messageContent.equals("")) {
|
||||||
|
|
||||||
//Retrieve the date and prepare the messages to send/display
|
// Retrieve the date and prepare the messages to send/display
|
||||||
MessageTexte messageOut = null;
|
MessageTexte messageOut = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
messageOut = new MessageTexte(TypeMessage.TEXTE, messageContent);
|
messageOut = new MessageTexte(TypeMessage.TEXTE, messageContent);
|
||||||
messageOut.setSender(Utilisateur.getSelf().getPseudo());
|
messageOut.setSender(Utilisateur.getSelf().getPseudo());
|
||||||
} catch (MauvaisTypeMessageException e2) {
|
} catch (MauvaisTypeMessageException e2) {
|
||||||
e2.printStackTrace();
|
e2.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.tcpClient.sendMessage(messageOut);
|
this.tcpClient.sendMessage(messageOut);
|
||||||
} catch (IOException e1) {
|
} catch (IOException e1) {
|
||||||
e1.printStackTrace();
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
messageOut.setSender("Moi");
|
messageOut.setSender("Moi");
|
||||||
this.vue.appendMessage(messageOut);
|
this.vue.appendMessage(messageOut);
|
||||||
this.vue.resetZoneSaisie();
|
this.vue.resetZoneSaisie();
|
||||||
|
|
||||||
this.messagesOut.add(messageOut);
|
this.messagesOut.add(messageOut);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//If the button "Importer" is pressed
|
// If the button "Importer" is pressed
|
||||||
if((JButton) e.getSource() == this.vue.getButtonImportFile()) {
|
if ((JButton) e.getSource() == this.vue.getButtonImportFile()) {
|
||||||
//Display a file chooser to select one or several files
|
|
||||||
|
// Display a file chooser to select one or several files
|
||||||
JFileChooser fc = new JFileChooser();
|
JFileChooser fc = new JFileChooser();
|
||||||
fc.setMultiSelectionEnabled(true);
|
fc.setMultiSelectionEnabled(true);
|
||||||
int returVal = fc.showDialog(this.vue, "Importer");
|
int returVal = fc.showDialog(this.vue, "Importer");
|
||||||
|
|
||||||
|
// If the user clicked on "Importer",
|
||||||
if(returVal == JFileChooser.APPROVE_OPTION) {
|
// Retrieve all the files he clicked on.
|
||||||
|
// The files are stored in this.files
|
||||||
|
// and their names are display in the ChatInput.
|
||||||
|
if (returVal == JFileChooser.APPROVE_OPTION) {
|
||||||
File[] files = fc.getSelectedFiles();
|
File[] files = fc.getSelectedFiles();
|
||||||
Collections.addAll(this.files, files);
|
Collections.addAll(this.files, files);
|
||||||
for(File file : files) {
|
for (File file : files) {
|
||||||
this.vue.appendInputedText(file.getName());
|
this.vue.appendInputedText(file.getName());
|
||||||
this.vue.appendInputedText(";");
|
this.vue.appendInputedText(";");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ---------- KEY LISTENER METHODS ---------- //
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void keyTyped(KeyEvent e) {}
|
public void keyTyped(KeyEvent e) {
|
||||||
|
|
||||||
@Override
|
|
||||||
public void keyPressed(KeyEvent e) {
|
|
||||||
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
|
|
||||||
if(!e.isShiftDown()) {
|
|
||||||
this.vue.getButtonEnvoyer().doClick();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void keyReleased(KeyEvent e) {}
|
public void keyPressed(KeyEvent e) {
|
||||||
|
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
|
||||||
|
if (!e.isShiftDown()) {
|
||||||
|
this.vue.getButtonEnvoyer().doClick();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyReleased(KeyEvent e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
protected ArrayList<Message> getHistorique(){
|
// ---------- OTHERS ---------- //
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create and send the message to ask for a file transfer.
|
||||||
|
*/
|
||||||
|
private void askFileTransfer() {
|
||||||
try {
|
try {
|
||||||
ArrayList<Message> historique = this.sqlManager.getHistoriquesMessages(idOther, pseudoOther);
|
MessageFichier messageOut = new MessageFichier(TypeMessage.FICHIER_INIT, "" + this.files.size(), "");
|
||||||
|
this.tcpClient.sendMessage(messageOut);
|
||||||
|
} catch (MauvaisTypeMessageException | IOException e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create and send the answer with the port on which the FileTransferServer is
|
||||||
|
* listening.
|
||||||
|
*
|
||||||
|
* @param port
|
||||||
|
*/
|
||||||
|
private void answerFileTransfer(int port) {
|
||||||
|
try {
|
||||||
|
MessageFichier messageOut = new MessageFichier(TypeMessage.FICHIER_ANSWER, "" + port, "");
|
||||||
|
this.tcpClient.sendMessage(messageOut);
|
||||||
|
} catch (MauvaisTypeMessageException | IOException e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the files' names from the given input using ";" as a separator
|
||||||
|
* Removes the files whose names are missing.
|
||||||
|
*
|
||||||
|
* This method is used to check if a file's name has been deleted/overwritten in
|
||||||
|
* the ChatInput. Indeed, the only way to cancel the import of a file is by
|
||||||
|
* deleting its name from the ChatInput.
|
||||||
|
*
|
||||||
|
* @param input
|
||||||
|
*/
|
||||||
|
private void processSelectedFiles(String input) {
|
||||||
|
String[] tmp = input.split(";");
|
||||||
|
ArrayList<String> potentialFiles = new ArrayList<String>();
|
||||||
|
Collections.addAll(potentialFiles, tmp);
|
||||||
|
|
||||||
|
for (File file : this.files) {
|
||||||
|
if (!potentialFiles.contains(file.getName())) {
|
||||||
|
this.files.remove(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the messages previously exchanged between the current user of the
|
||||||
|
* application and the other user of this session
|
||||||
|
*
|
||||||
|
* @return The ArrayList of all previous messages
|
||||||
|
*/
|
||||||
|
protected ArrayList<Message> getHistorique() {
|
||||||
|
try {
|
||||||
|
ArrayList<Message> historique = this.sqlManager.getMessageRecord(idOther, pseudoOther);
|
||||||
return historique;
|
return historique;
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return new ArrayList<Message>();
|
return new ArrayList<Message>();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void processSelectedFiles(String input) {
|
/**
|
||||||
String[] tmp = input.split(";");
|
* Method used when the session is over. Insert every message exchanged in the
|
||||||
ArrayList<String> potentialFiles = new ArrayList<String>();
|
* database, set all attributes' references to null, and call destroyAll() on
|
||||||
Collections.addAll(potentialFiles, tmp);
|
* the TCPClient.
|
||||||
|
*/
|
||||||
for(File file: this.files) {
|
protected void destroyAll() {
|
||||||
if(!potentialFiles.contains(file.getName()) ) {
|
String idSelf = Utilisateur.getSelf().getId();
|
||||||
this.files.remove(file);
|
String idOther = this.idOther;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void askFileTransfer() {
|
|
||||||
try {
|
try {
|
||||||
MessageFichier messageOut = new MessageFichier(TypeMessage.FICHIER_INIT, ""+this.files.size(), "");
|
this.sqlManager.insertAllMessages(messagesOut, idSelf, idOther);
|
||||||
this.tcpClient.sendMessage(messageOut);
|
this.sqlManager.insertAllMessages(messagesIn, idOther, idSelf);
|
||||||
} catch (MauvaisTypeMessageException | IOException e1) {
|
} catch (SQLException e) {
|
||||||
e1.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.vue = null;
|
||||||
|
this.tcpClient.destroyAll();
|
||||||
|
this.tcpClient = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void answerFileTransfer(int port) {
|
// ---------- OBSERVERS ---------- //
|
||||||
try {
|
|
||||||
MessageFichier messageOut = new MessageFichier(TypeMessage.FICHIER_ANSWER, ""+port, "");
|
// Method called when a message is received from the TCP socket
|
||||||
this.tcpClient.sendMessage(messageOut);
|
|
||||||
} catch (MauvaisTypeMessageException | IOException e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Method called when a message is received from the TCP socket
|
|
||||||
@Override
|
@Override
|
||||||
public void updateInput(Object o, Object arg) {
|
public void updateInput(Object o, Object arg) {
|
||||||
Message message = (Message) arg;
|
Message message = (Message) arg;
|
||||||
|
|
||||||
switch(message.getTypeMessage()) {
|
switch (message.getTypeMessage()) {
|
||||||
|
|
||||||
|
// If it is a simple text message, display it
|
||||||
case TEXTE:
|
case TEXTE:
|
||||||
System.out.println(message.toString());
|
System.out.println(message.toString());
|
||||||
this.vue.appendMessage(message);
|
this.vue.appendMessage(message);
|
||||||
this.messagesIn.add(message);
|
this.messagesIn.add(message);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
// If it is an image, display a thumbnail
|
||||||
case IMAGE:
|
case IMAGE:
|
||||||
this.vue.appendImage(message);
|
this.vue.appendImage(message);
|
||||||
|
|
||||||
if(message.getSender().equals("Moi")) {
|
if (message.getSender().equals("Moi")) {
|
||||||
this.messagesOut.add(message);
|
this.messagesOut.add(message);
|
||||||
}else {
|
} else {
|
||||||
this.messagesIn.add(message);
|
this.messagesIn.add(message);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
// If it is a file, display a message saying whether it has been sent/received.
|
||||||
case FICHIER:
|
case FICHIER:
|
||||||
this.vue.appendMessage(message);
|
this.vue.appendMessage(message);
|
||||||
|
|
||||||
if(message.getSender().equals("Moi")) {
|
if (message.getSender().equals("Moi")) {
|
||||||
this.messagesOut.add(message);
|
this.messagesOut.add(message);
|
||||||
}else {
|
} else {
|
||||||
this.messagesIn.add(message);
|
this.messagesIn.add(message);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
// If it is a demand for a file transfer, create a new FileTransferServer, start
|
||||||
|
// it
|
||||||
|
// and answer back with "FICHIER_ANSWER" message containing the port of the
|
||||||
|
// server.
|
||||||
case FICHIER_INIT:
|
case FICHIER_INIT:
|
||||||
try {
|
try {
|
||||||
MessageFichier mFichier = (MessageFichier) arg;
|
MessageFichier mFichier = (MessageFichier) arg;
|
||||||
|
@ -232,57 +311,42 @@ public class ControleurSession implements ActionListener, ObserverInputMessage,
|
||||||
int port = fts.getPort();
|
int port = fts.getPort();
|
||||||
fts.start();
|
fts.start();
|
||||||
this.answerFileTransfer(port);
|
this.answerFileTransfer(port);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
// If it is an answer for a file transfer, create a FilteTransferClient
|
||||||
|
// with the port received and the path of the file(s) to send,
|
||||||
|
// and send the files.
|
||||||
case FICHIER_ANSWER:
|
case FICHIER_ANSWER:
|
||||||
try {
|
try {
|
||||||
MessageFichier mFichier = (MessageFichier) arg;
|
MessageFichier mFichier = (MessageFichier) arg;
|
||||||
int port = Integer.parseInt(mFichier.getContenu());
|
int port = Integer.parseInt(mFichier.getContenu());
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
FileTransferClient ftc = new FileTransferClient(port ,(ArrayList<File>) this.files.clone(), this);
|
FileTransferClient ftc = new FileTransferClient(port, (ArrayList<File>) this.files.clone(), this);
|
||||||
|
|
||||||
ftc.sendFiles();
|
ftc.sendFiles();
|
||||||
this.files.clear();
|
this.files.clear();
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//Do nothing
|
// Do nothing
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//If the other user closes the session or the communication is broken
|
// If the other user closes the session or the communication is broken
|
||||||
//Disable the view (TextArea, Buttons..) and display a message
|
// Disable the view (TextArea, Buttons..) and display a message
|
||||||
@Override
|
@Override
|
||||||
public void updateSocketState(Object o, Object arg) {
|
public void updateSocketState(Object o, Object arg) {
|
||||||
this.vue.endSession(this.pseudoOther);
|
this.vue.endSession(this.pseudoOther);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
protected void destroyAll() {
|
|
||||||
String idSelf = Utilisateur.getSelf().getId();
|
|
||||||
String idOther = this.idOther;
|
|
||||||
|
|
||||||
try {
|
|
||||||
this.sqlManager.insertAllMessages(messagesOut, idSelf, idOther);
|
|
||||||
this.sqlManager.insertAllMessages(messagesIn, idOther, idSelf);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.vue = null;
|
|
||||||
this.tcpClient.destroyAll();
|
|
||||||
this.tcpClient = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -39,9 +39,6 @@ import messages.Message.TypeMessage;
|
||||||
|
|
||||||
public class VueSession extends JPanel {
|
public class VueSession extends JPanel {
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private JButton sendMessage;
|
private JButton sendMessage;
|
||||||
|
@ -58,12 +55,7 @@ public class VueSession extends JPanel {
|
||||||
this.setBorder(new EmptyBorder(5, 5, 5, 5));
|
this.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||||
this.setLayout(new BorderLayout(0, 0));
|
this.setLayout(new BorderLayout(0, 0));
|
||||||
|
|
||||||
this.chatInput = new JTextArea();
|
// Create the display zone
|
||||||
this.chatInput.setColumns(10);
|
|
||||||
this.chatInput.setLineWrap(true);
|
|
||||||
this.chatInput.setWrapStyleWord(true);
|
|
||||||
this.chatInput.addKeyListener(this.c);
|
|
||||||
|
|
||||||
this.chatWindow = new JTextPane();
|
this.chatWindow = new JTextPane();
|
||||||
this.chatWindow.setEditable(false);
|
this.chatWindow.setEditable(false);
|
||||||
this.chatWindow.setEditorKit(new WrapEditorKit());
|
this.chatWindow.setEditorKit(new WrapEditorKit());
|
||||||
|
@ -71,21 +63,32 @@ public class VueSession extends JPanel {
|
||||||
JScrollPane chatScroll = new JScrollPane(this.chatWindow);
|
JScrollPane chatScroll = new JScrollPane(this.chatWindow);
|
||||||
chatScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
|
chatScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
|
||||||
|
|
||||||
|
// Create the input zone
|
||||||
|
this.chatInput = new JTextArea();
|
||||||
|
this.chatInput.setColumns(10);
|
||||||
|
this.chatInput.setLineWrap(true);
|
||||||
|
this.chatInput.setWrapStyleWord(true);
|
||||||
|
this.chatInput.addKeyListener(this.c);
|
||||||
|
|
||||||
JPanel bottom = new JPanel();
|
JPanel bottom = new JPanel();
|
||||||
bottom.setLayout(new BorderLayout(0, 0));
|
bottom.setLayout(new BorderLayout(0, 0));
|
||||||
|
|
||||||
// remap "ENTER" to "none" to avoid "\n" in the input area after sending message
|
// Remap "ENTER" to "none" to avoid "\n" in the input area when pressing "ENTER"
|
||||||
|
// to send a message
|
||||||
KeyStroke enter = KeyStroke.getKeyStroke("ENTER");
|
KeyStroke enter = KeyStroke.getKeyStroke("ENTER");
|
||||||
this.chatInput.getInputMap().put(enter, "none");
|
this.chatInput.getInputMap().put(enter, "none");
|
||||||
KeyStroke shiftEnter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.SHIFT_DOWN_MASK);
|
KeyStroke shiftEnter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.SHIFT_DOWN_MASK);
|
||||||
this.chatInput.getInputMap().put(shiftEnter, "insert-break");
|
this.chatInput.getInputMap().put(shiftEnter, "insert-break");
|
||||||
|
|
||||||
this.importFile = new JButton("Importer..");
|
// Create a scroller to be able to send messages of several lines
|
||||||
this.importFile.addActionListener(this.c);
|
|
||||||
|
|
||||||
JScrollPane inputScroll = new JScrollPane(this.chatInput);
|
JScrollPane inputScroll = new JScrollPane(this.chatInput);
|
||||||
inputScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
|
inputScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
|
||||||
|
|
||||||
|
// Import file button
|
||||||
|
this.importFile = new JButton("Importer..");
|
||||||
|
this.importFile.addActionListener(this.c);
|
||||||
|
|
||||||
|
// Send message button
|
||||||
this.sendMessage = new JButton("Envoyer");
|
this.sendMessage = new JButton("Envoyer");
|
||||||
this.sendMessage.addActionListener(this.c);
|
this.sendMessage.addActionListener(this.c);
|
||||||
|
|
||||||
|
@ -93,6 +96,7 @@ public class VueSession extends JPanel {
|
||||||
bottom.add(inputScroll, BorderLayout.CENTER);
|
bottom.add(inputScroll, BorderLayout.CENTER);
|
||||||
bottom.add(this.sendMessage, BorderLayout.EAST);
|
bottom.add(this.sendMessage, BorderLayout.EAST);
|
||||||
|
|
||||||
|
// Add the components to the view
|
||||||
this.add(chatScroll, BorderLayout.CENTER);
|
this.add(chatScroll, BorderLayout.CENTER);
|
||||||
this.add(bottom, BorderLayout.SOUTH);
|
this.add(bottom, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
@ -102,32 +106,30 @@ public class VueSession extends JPanel {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -------------- GETTERS -------------- //
|
||||||
|
|
||||||
protected JButton getButtonEnvoyer() {
|
protected JButton getButtonEnvoyer() {
|
||||||
return this.sendMessage;
|
return this.sendMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected JButton getButtonImportFile() {
|
protected JButton getButtonImportFile() {
|
||||||
return this.importFile;
|
return this.importFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected String getInputedText() {
|
protected String getInputedText() {
|
||||||
return this.chatInput.getText();
|
return this.chatInput.getText();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void appendInputedText(String str) {
|
|
||||||
this.chatInput.append(str);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected void resetZoneSaisie() {
|
|
||||||
this.chatInput.setText("");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setCaretToEnd() {
|
|
||||||
this.chatWindow.setCaretPosition(this.chatWindow.getDocument().getLength());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
// -------------- DISPLAY METHODS -------------- //
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append the given string to the ChatWindow.
|
||||||
|
*
|
||||||
|
* @param str
|
||||||
|
*/
|
||||||
protected void appendString(String str) {
|
protected void appendString(String str) {
|
||||||
try {
|
try {
|
||||||
Document doc = this.chatWindow.getDocument();
|
Document doc = this.chatWindow.getDocument();
|
||||||
|
@ -137,12 +139,16 @@ public class VueSession extends JPanel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append the given Message to the ChatWindow.
|
||||||
|
*
|
||||||
|
* @param message
|
||||||
|
*/
|
||||||
protected void appendMessage(Message message) {
|
protected void appendMessage(Message message) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
StyledDocument sdoc = this.chatWindow.getStyledDocument();
|
StyledDocument sdoc = this.chatWindow.getStyledDocument();
|
||||||
// sdoc.setParagraphAttributes(sdoc.getLength(), message.toString().length()-1,
|
|
||||||
// style, false);
|
|
||||||
sdoc.insertString(sdoc.getLength(), message.toString(), null);
|
sdoc.insertString(sdoc.getLength(), message.toString(), null);
|
||||||
|
|
||||||
} catch (BadLocationException e) {
|
} catch (BadLocationException e) {
|
||||||
|
@ -150,9 +156,17 @@ public class VueSession extends JPanel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append an icon with the image contained in the given message. The message has
|
||||||
|
* to contain the base64 encoded bytes of the image as a String for it to be
|
||||||
|
* decoded and displayed.
|
||||||
|
*
|
||||||
|
* @param message
|
||||||
|
*/
|
||||||
protected void appendImage(Message message) {
|
protected void appendImage(Message message) {
|
||||||
this.setCaretToEnd();
|
this.setCaretToEnd();
|
||||||
|
|
||||||
String imgString = message.toString();
|
String imgString = message.toString();
|
||||||
Icon ic;
|
Icon ic;
|
||||||
try {
|
try {
|
||||||
|
@ -160,34 +174,52 @@ public class VueSession extends JPanel {
|
||||||
ic = new ImageIcon(img);
|
ic = new ImageIcon(img);
|
||||||
this.chatWindow.insertIcon(ic);
|
this.chatWindow.insertIcon(ic);
|
||||||
this.appendString("\n");
|
this.appendString("\n");
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void printLineSeparator() {
|
private void printLineSeparator() {
|
||||||
this.appendString("------------------------------------------\n");
|
this.appendString("------------------------------------------\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void endSession(String pseudoOther) {
|
/**
|
||||||
this.printLineSeparator();
|
* Append the given string to the ChatInput.
|
||||||
this.appendString(pseudoOther + " a mis fin à la session.");
|
*
|
||||||
this.chatInput.setEnabled(false);
|
* @param str
|
||||||
this.chatInput.setFocusable(false);
|
*/
|
||||||
this.sendMessage.setEnabled(false);
|
protected void appendInputedText(String str) {
|
||||||
this.importFile.setEnabled(false);
|
this.chatInput.append(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void resetZoneSaisie() {
|
||||||
|
this.chatInput.setText("");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -------------- OTHERS -------------- //
|
||||||
|
|
||||||
|
private void setCaretToEnd() {
|
||||||
|
this.chatWindow.setCaretPosition(this.chatWindow.getDocument().getLength());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve all the previous messages from the controller and display them by
|
||||||
|
* appending them one by one to the ChatWindow.
|
||||||
|
*/
|
||||||
private void displayHistorique() {
|
private void displayHistorique() {
|
||||||
ArrayList<Message> historique = this.c.getHistorique();
|
ArrayList<Message> historique = this.c.getHistorique();
|
||||||
|
|
||||||
for (Message m : historique) {
|
for (Message m : historique) {
|
||||||
if(m.getTypeMessage() == TypeMessage.IMAGE) {
|
if (m.getTypeMessage() == TypeMessage.IMAGE) {
|
||||||
this.appendImage(m);
|
this.appendImage(m);
|
||||||
}else {
|
} else {
|
||||||
this.appendMessage(m);
|
this.appendMessage(m);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -197,6 +229,27 @@ public class VueSession extends JPanel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disable the ChatInput, the buttons "Importer" and "Envoyer" and display a
|
||||||
|
* message indicating the other user ended the session.
|
||||||
|
*
|
||||||
|
* @param pseudoOther
|
||||||
|
*/
|
||||||
|
protected void endSession(String pseudoOther) {
|
||||||
|
this.printLineSeparator();
|
||||||
|
this.appendString(pseudoOther + " a mis fin à la session.");
|
||||||
|
this.chatInput.setEnabled(false);
|
||||||
|
this.chatInput.setFocusable(false);
|
||||||
|
this.sendMessage.setEnabled(false);
|
||||||
|
this.importFile.setEnabled(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method used when the user closes the session. Set all attributes' references
|
||||||
|
* to null, and call destroyAll() on the controller.
|
||||||
|
*/
|
||||||
public void destroyAll() {
|
public void destroyAll() {
|
||||||
if (this.c != null) {
|
if (this.c != null) {
|
||||||
this.c.destroyAll();
|
this.c.destroyAll();
|
||||||
|
@ -207,12 +260,13 @@ public class VueSession extends JPanel {
|
||||||
this.sendMessage = null;
|
this.sendMessage = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ------------- PRIVATE CLASS TO WRAP TEXT -------------//
|
// ------------- PRIVATE CLASS TO WRAP TEXT -------------//
|
||||||
|
|
||||||
class WrapEditorKit extends StyledEditorKit {
|
class WrapEditorKit extends StyledEditorKit {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
ViewFactory defaultFactory = new WrapColumnFactory();
|
ViewFactory defaultFactory = new WrapColumnFactory();
|
||||||
|
|
||||||
public ViewFactory getViewFactory() {
|
public ViewFactory getViewFactory() {
|
||||||
|
|
|
@ -242,7 +242,7 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
||||||
return input.readLine();
|
return input.readLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------OBSERVERS-------------//
|
// ------------OBSERVERS------------- //
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateInput(Object o, Object arg) {
|
public void updateInput(Object o, Object arg) {
|
||||||
|
|
Loading…
Reference in a new issue