diff options
author | Vincent Lucas <chenzo@jitsi.org> | 2008-07-03 11:54:04 +0000 |
---|---|---|
committer | Vincent Lucas <chenzo@jitsi.org> | 2008-07-03 11:54:04 +0000 |
commit | 07bca42bed14970accdaee7605fa208d64dfde3c (patch) | |
tree | 9eb3af413b447bc52e41b07c1221884d4c9a99b4 | |
parent | 7d31f0d49868e3f46f1b0be59bde1bd2af4f617d (diff) | |
download | jitsi-07bca42bed14970accdaee7605fa208d64dfde3c.zip jitsi-07bca42bed14970accdaee7605fa208d64dfde3c.tar.gz jitsi-07bca42bed14970accdaee7605fa208d64dfde3c.tar.bz2 |
Applied patch for renaming automatically DICT new accounts with thier descriptions in order of their codes ID (originaly proposed by Damien Roth).
-rw-r--r-- | src/net/java/sip/communicator/impl/protocol/dict/ContactDictImpl.java | 30 | ||||
-rw-r--r-- | src/net/java/sip/communicator/impl/protocol/dict/DictAdapter.java | 1099 |
2 files changed, 601 insertions, 528 deletions
diff --git a/src/net/java/sip/communicator/impl/protocol/dict/ContactDictImpl.java b/src/net/java/sip/communicator/impl/protocol/dict/ContactDictImpl.java index cd1b2d9..ffd19b1 100644 --- a/src/net/java/sip/communicator/impl/protocol/dict/ContactDictImpl.java +++ b/src/net/java/sip/communicator/impl/protocol/dict/ContactDictImpl.java @@ -7,6 +7,7 @@ package net.java.sip.communicator.impl.protocol.dict; import net.java.sip.communicator.service.protocol.*; +import net.java.sip.communicator.util.*; /** * An implementation of a Dict contact @@ -17,6 +18,8 @@ import net.java.sip.communicator.service.protocol.*; public class ContactDictImpl implements Contact { + private Logger logger = Logger.getLogger(ContactDictImpl.class); + /** * The id of the contact. */ @@ -50,9 +53,10 @@ public class ContactDictImpl private boolean isResolved = true; /** - * Socket connection associated with the contact + * The string in a "humain readable and understandable representation" of + * the dictionnaire. In brief this is a short description of the dictionary. */ - private DictAdapter dictAdapter = null; + private String dictName = null; /** * Creates an instance of a meta contact with the specified string used @@ -111,7 +115,27 @@ public class ContactDictImpl */ public String getDisplayName() { - return contactID; + if (dictName == null) + { + try + { + dictName = getDictAdapter().getDictionaryName(contactID); + + // If the dict name is still null, set it to the contact ID + if (dictName == null) + { + dictName = contactID; + } + } + catch (Exception e) + { + // Can't read data + logger.error("Error while getting dictionary long name", e); + dictName = contactID; + } + } + + return dictName; } /** diff --git a/src/net/java/sip/communicator/impl/protocol/dict/DictAdapter.java b/src/net/java/sip/communicator/impl/protocol/dict/DictAdapter.java index f9c780c..b458e42 100644 --- a/src/net/java/sip/communicator/impl/protocol/dict/DictAdapter.java +++ b/src/net/java/sip/communicator/impl/protocol/dict/DictAdapter.java @@ -21,577 +21,626 @@ public class DictAdapter /** * The host name of the server: i.e. "dict.org" */ - private String host; + private String host; /** * The port used by the server. The default one for the DICT protocol is * 2628. */ - private int port; + private int port; /** * The name of the strategy used for searching words with command MATCH: * i.e. the strategie can de "prefix", "suffix", "soundex", "levenshtein", etc. */ - private String strategy; + private String strategy; /** * A string representation used to identify the client to the serveur. In * our case we will use the "SIP Communicator" string for the client name. */ - private String clientName = ""; - - // Status + private String clientName = ""; + + // Status /** * The socket used to connect to the DICT server. */ - private Socket socket; + private Socket socket; /** * A output stream piped to the socket in order to send command to the server. */ - private PrintWriter out; + private PrintWriter out; /** * A input stream piped to the socket in order to receive messages from the server. */ - private BufferedReader in; + private BufferedReader in; /** * A boolean telling if we are currently connected to the DICT server. */ - private boolean connected; - + private boolean connected; + /** * The list of all the databases hosted by the server. Each database * correspond to a dictionnary. */ - Vector<String> databasesList; - - /** - * Initialize a basic instance with predefined settings - */ - public DictAdapter() - { - this.host = "dict.org"; - this.port = 2628; - this.strategy = "prefix"; - this.connected = false; - this.socket = null; - this.out = null; - this.in = null; - } - - /** - * Initialize a basic instance and set th host - * @param host Host - */ - public DictAdapter(String host) - { - this.host = host; - this.port = 2628; - this.strategy = "prefix"; - this.connected = false; - this.socket = null; - this.out = null; - this.in = null; - } - - /** - * Initialize an instance and set the host and the port - * @param host Host - * @param port Port - */ - public DictAdapter(String host, int port) - { - this.host = host; - this.port = port; - this.strategy = "prefix"; - this.connected = false; - this.socket = null; - this.out = null; - this.in = null; - } - - /** - * Initialize an instance and set the host, port and strategy - * @param host Host - * @param port Port - * @param strategy Match strategy - */ - public DictAdapter(String host, int port, String strategy) - { - this.host = host; - this.port = port; - this.strategy = strategy; - this.connected = false; - this.socket = null; - this.out = null; - this.in = null; - } - - - /** - * Establish a connexion to the dict server - * @throws Exception - * @return DictResultset containing the error - null otherwise - */ - private void connect() throws Exception - { - String fromServer; - - if (this.isConnected()) - { - return; - } - - try - { - this.socket = new Socket(this.host, this.port); - this.out = new PrintWriter(new OutputStreamWriter(this.socket.getOutputStream(), - "UTF-8"), true); - this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), - "UTF-8")); - - fromServer = this.in.readLine(); // Server banner - - if (fromServer.startsWith("220")) - { // 220 = connect ok - this.connected = true; - this.client("SIP Communicator"); - return; - } - else - { - throw new DictException(fromServer.substring(0, 3)); - } - } - catch(UnknownHostException uhe) - { - throw new DictException(uhe); - } - catch(IOException ioe) - { - throw new DictException(ioe); - } - } - - /** - * Close the actual connexion - * @throws Exception - */ - public void close() throws Exception - { - String fromServer; - boolean quit = false; - - if (!this.isConnected()) - { - return; - } - - try - { - this.out.println("QUIT"); - - // Clean the socket buffer - while (quit == false && (fromServer = this.in.readLine()) != null) - { - if (fromServer.startsWith("221")) - { // Quit response - quit = true; - } - } - - this.out.close(); - this.in.close(); - this.socket.close(); - - this.connected = false; - } - catch (IOException ioe) - { - throw new DictException(ioe); - } - } - - /** - * Get the database list from the server - * @throws Exception - * @return a DictResultset containing the database list - otherwise the error code - */ - public DictResultset showDB() throws Exception - { - String fromServer; - boolean quit = false; - DictResultset result = new DictResultset(); - this.connect(); - - try - { - fromServer = this.query("SHOW DB"); - - if (fromServer.startsWith("110")) - { // OK - getting responses from the server - result.newResultset(); - while (quit == false && (fromServer = this.in.readLine()) != null) - { - if (fromServer.startsWith("250")) - { - quit = true; - } - else if (!fromServer.equals(".")) - { - result.addResult(fromServer); - } - } - } - else - { - throw new DictException(fromServer.substring(0,3)); - } - } - catch (IOException ioe) - { - throw new DictException(ioe); - } - - return result; - } - - /** - * Get the strategies allowed by the server for the MATCH command - * @throws Exception - * @return a DictResultset containing the database list - otherwise the error code - */ - public DictResultset showStrat() throws Exception - { - String fromServer; - boolean quit = false; - DictResultset result = new DictResultset(); - this.connect(); - - try - { - fromServer = this.query("SHOW STRAT"); - - if (fromServer.startsWith("111")) - { // OK - getting responses from the server - result.newResultset(); - while (quit == false && (fromServer = this.in.readLine()) != null) - { - if (fromServer.startsWith("250")) - { - quit = true; - } - else if (!fromServer.equals(".")) - { - result.addResult(fromServer); - } - } - } - else - { - throw new DictException(fromServer.substring(0,3)); - } - } - catch (IOException ioe) - { - throw new DictException(ioe); - } - - return result; - } - - - /** - * Get the definition of a word - * @param database the database in which the word will be searched - * @param word the search word - * @throws Exception - * @return a DictResultset containing the database list - otherwise the error code - */ - public DictResultset define(String database, String word) throws Exception - { - String fromServer; - boolean quit = false; - DictResultset result = new DictResultset(); - String[] test; - this.connect(); - - try - { - fromServer = this.query("DEFINE " + database + " " + word); - - if (fromServer.startsWith("150")) - { - while (quit == false && (fromServer = this.in.readLine()) != null) - { - if (fromServer.startsWith("151")) - { // First line - Contains the DB Name - test = fromServer.split(" ", 4); - result.newResultset(test[3].substring(1, test[3].length() - 1)); - continue; - } - else if (fromServer.startsWith("250")) - { // End of the request - quit = true; - } - else if (!fromServer.equals(".")) - { - result.addResult(fromServer); - } - } - } - else - { - throw new DictException(fromServer.substring(0,3)); - } - } - catch (IOException ioe) - { - throw new DictException(ioe); - } - - return result; - } - - /** - * Get words that match with a strategie form a word with the stored strategy - * @param database The database in which the words will be searched - * @param word The base word - * @return a DictResultset containing the words list - otherwise throw an exception - * @throws Exception - */ - public DictResultset match(String database, String word) throws Exception - { - return this.match(database, this.strategy, word); - } - - /** - * Get words that match with a strategie from a word - * @param database the database in which the words will be searched - * @param strat the strategies used - * @param word the base word - * @throws Exception - * @return a DictResultset containing the words list - otherwise the error code - */ - public DictResultset match(String database, String strat, String word) throws Exception - { - String fromServer; - boolean quit = false; - DictResultset result = new DictResultset(); - this.connect(); - - try - { - fromServer = this.query("MATCH " + database + " " + strat + " " + word); - - if (fromServer.startsWith("152")) - { - result.newResultset(); - while (quit == false && (fromServer = this.in.readLine()) != null) - { - if (fromServer.startsWith("250")) - { - quit = true; - } - else if (!fromServer.equals(".")) - { - result.addResult(fromServer); - } - } - } - else - { - throw new DictException(fromServer.substring(0,3)); - } - } - catch (IOException ioe) - { - throw new DictException(ioe); - } - - return result; - } + Vector<String> databasesList; + + /** + * Initialize a basic instance with predefined settings + */ + public DictAdapter() + { + this.host = "dict.org"; + this.port = 2628; + this.strategy = "prefix"; + this.connected = false; + this.socket = null; + this.out = null; + this.in = null; + } + + /** + * Initialize a basic instance and set th host + * @param host Host + */ + public DictAdapter(String host) + { + this.host = host; + this.port = 2628; + this.strategy = "prefix"; + this.connected = false; + this.socket = null; + this.out = null; + this.in = null; + } + + /** + * Initialize an instance and set the host and the port + * @param host Host + * @param port Port + */ + public DictAdapter(String host, int port) + { + this.host = host; + this.port = port; + this.strategy = "prefix"; + this.connected = false; + this.socket = null; + this.out = null; + this.in = null; + } + + /** + * Initialize an instance and set the host, port and strategy + * @param host Host + * @param port Port + * @param strategy Match strategy + */ + public DictAdapter(String host, int port, String strategy) + { + this.host = host; + this.port = port; + this.strategy = strategy; + this.connected = false; + this.socket = null; + this.out = null; + this.in = null; + } + + + /** + * Establish a connexion to the dict server + * @throws Exception + * @return DictResultset containing the error - null otherwise + */ + private void connect() throws Exception + { + String fromServer; + + if (this.isConnected()) + { + return; + } + + try + { + this.socket = new Socket(this.host, this.port); + this.out = new PrintWriter(new OutputStreamWriter(this.socket.getOutputStream(), + "UTF-8"), true); + this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), + "UTF-8")); + + fromServer = this.in.readLine(); // Server banner + + if (fromServer.startsWith("220")) + { // 220 = connect ok + this.connected = true; + this.client("SIP Communicator"); + return; + } + else + { + throw new DictException(fromServer.substring(0, 3)); + } + } + catch(UnknownHostException uhe) + { + throw new DictException(uhe); + } + catch(IOException ioe) + { + throw new DictException(ioe); + } + } + + /** + * Close the actual connexion + * @throws Exception + */ + public void close() throws Exception + { + String fromServer; + boolean quit = false; + + if (!this.isConnected()) + { + return; + } + + try + { + this.out.println("QUIT"); + + // Clean the socket buffer + while (quit == false && (fromServer = this.in.readLine()) != null) + { + if (fromServer.startsWith("221")) + { // Quit response + quit = true; + } + } + + this.out.close(); + this.in.close(); + this.socket.close(); + + this.connected = false; + } + catch (IOException ioe) + { + throw new DictException(ioe); + } + } + + /** + * Get the database list from the server + * @throws Exception + * @return a DictResultset containing the database list - otherwise the error code + */ + public DictResultset showDB() throws Exception + { + String fromServer; + boolean quit = false; + DictResultset result = new DictResultset(); + this.connect(); + + try + { + fromServer = this.query("SHOW DB"); + + if (fromServer.startsWith("110")) + { // OK - getting responses from the server + result.newResultset(); + while (quit == false && (fromServer = this.in.readLine()) != null) + { + if (fromServer.startsWith("250")) + { + quit = true; + } + else if (!fromServer.equals(".")) + { + result.addResult(fromServer); + } + } + } + else + { + throw new DictException(fromServer.substring(0,3)); + } + } + catch (IOException ioe) + { + throw new DictException(ioe); + } + + return result; + } + + /** + * Get the strategies allowed by the server for the MATCH command + * @throws Exception + * @return a DictResultset containing the database list - otherwise the error code + */ + public DictResultset showStrat() throws Exception + { + String fromServer; + boolean quit = false; + DictResultset result = new DictResultset(); + this.connect(); + + try + { + fromServer = this.query("SHOW STRAT"); + + if (fromServer.startsWith("111")) + { // OK - getting responses from the server + result.newResultset(); + while (quit == false && (fromServer = this.in.readLine()) != null) + { + if (fromServer.startsWith("250")) + { + quit = true; + } + else if (!fromServer.equals(".")) + { + result.addResult(fromServer); + } + } + } + else + { + throw new DictException(fromServer.substring(0,3)); + } + } + catch (IOException ioe) + { + throw new DictException(ioe); + } + + return result; + } + + + /** + * Get the definition of a word + * @param database the database in which the word will be searched + * @param word the search word + * @throws Exception + * @return a DictResultset containing the database list - otherwise the error code + */ + public DictResultset define(String database, String word) throws Exception + { + String fromServer; + boolean quit = false; + DictResultset result = new DictResultset(); + String[] test; + this.connect(); + + try + { + fromServer = this.query("DEFINE " + database + " " + word); + + if (fromServer.startsWith("150")) + { + while (quit == false && (fromServer = this.in.readLine()) != null) + { + if (fromServer.startsWith("151")) + { // First line - Contains the DB Name + test = fromServer.split(" ", 4); + result.newResultset(test[3].substring(1, test[3].length() - 1)); + continue; + } + else if (fromServer.startsWith("250")) + { // End of the request + quit = true; + } + else if (!fromServer.equals(".")) + { + result.addResult(fromServer); + } + } + } + else + { + throw new DictException(fromServer.substring(0,3)); + } + } + catch (IOException ioe) + { + throw new DictException(ioe); + } + + return result; + } + + /** + * Get words that match with a strategie form a word with the stored strategy + * @param database The database in which the words will be searched + * @param word The base word + * @return a DictResultset containing the words list - otherwise throw an exception + * @throws Exception + */ + public DictResultset match(String database, String word) throws Exception + { + return this.match(database, this.strategy, word); + } + + /** + * Get words that match with a strategie from a word + * @param database the database in which the words will be searched + * @param strat the strategies used + * @param word the base word + * @throws Exception + * @return a DictResultset containing the words list - otherwise the error code + */ + public DictResultset match(String database, String strat, String word) throws Exception + { + String fromServer; + boolean quit = false; + DictResultset result = new DictResultset(); + this.connect(); + + try + { + fromServer = this.query("MATCH " + database + " " + strat + " " + word); + + if (fromServer.startsWith("152")) + { + result.newResultset(); + while (quit == false && (fromServer = this.in.readLine()) != null) + { + if (fromServer.startsWith("250")) + { + quit = true; + } + else if (!fromServer.equals(".")) + { + result.addResult(fromServer); + } + } + } + else + { + throw new DictException(fromServer.substring(0,3)); + } + } + catch (IOException ioe) + { + throw new DictException(ioe); + } + + return result; + } - /** - * Provide information to the server about the clientname, for logging and statistical purposes - * @param clientname Client name - * @throws Exception - */ - public void client(String clientname) throws Exception - { - String fromServer; - this.connect(); - - fromServer = this.query("CLIENT " + clientname); - - // 250 code is the only possible answer - if (!fromServer.startsWith("250")) - { - throw new DictException(fromServer.substring(0, 3)); - } - } - - /** - * Set the host - * @param newHost host address - */ - public void setHost(String newHost) throws Exception - { - if (isUrl(newHost)) - { - this.host = newHost; - } - else - { - throw new DictException(900, "Host URL is incorrect"); - } - } - - /** - * Set the host port - * @param newPort Port - */ - public void setPort(int newPort) - { - this.port = newPort; - } + /** + * Provide information to the server about the clientname, for logging and statistical purposes + * @param clientname Client name + * @throws Exception + */ + public void client(String clientname) throws Exception + { + String fromServer; + this.connect(); + + fromServer = this.query("CLIENT " + clientname); + + // 250 code is the only possible answer + if (!fromServer.startsWith("250")) + { + throw new DictException(fromServer.substring(0, 3)); + } + } + + /** + * Set the host + * @param newHost host address + */ + public void setHost(String newHost) throws Exception + { + if (isUrl(newHost)) + { + this.host = newHost; + } + else + { + throw new DictException(900, "Host URL is incorrect"); + } + } + + /** + * Set the host port + * @param newPort Port + */ + public void setPort(int newPort) + { + this.port = newPort; + } - /** - * Set the strategy - * @param newStrat Strategy - */ - public void setStrategy(String newStrat) - { - this.strategy = newStrat; - } - - /** - * Set the client name which is communicated to the server - * @param cn Client name - */ - public void setClientName(String cn) - { - this.clientName = cn; - } + /** + * Set the strategy + * @param newStrat Strategy + */ + public void setStrategy(String newStrat) + { + this.strategy = newStrat; + } + + /** + * Set the client name which is communicated to the server + * @param cn Client name + */ + public void setClientName(String cn) + { + this.clientName = cn; + } - /** - * Return the host - * @return return the host - */ - public String getHost() - { - return this.host; - } - - /** - * Return the port - * @return return the port - */ - public int getPort() - { - return this.port; - } - - /** - * Return the strategy - * @return return the strategy - */ - public String getStrategy() - { - return this.strategy; - } - - /** - * Return the client name - * @return return the client name - */ - public String getClientName() - { - return this.clientName; - } - - /** - * Gets the database's list from the server - * @return List of the databases - * @throws Exception - */ - public Vector<String> getDatabases() throws Exception - { - if (this.databasesList == null) - { - DictResultset drs = this.showDB(); - DictResult list = drs.getResultset(0); - - this.databasesList = new Vector<String>(); - - while(list.hasNext()) - { - this.databasesList.add(list.next()); - } - } - return this.databasesList; - } - - /** - * Check if we are connected to the server - * @return true if we are connected - false otherwise - */ - public boolean isConnected() - { - return this.connected; - } + /** + * Return the host + * @return return the host + */ + public String getHost() + { + return this.host; + } + + /** + * Return the port + * @return return the port + */ + public int getPort() + { + return this.port; + } + + /** + * Return the strategy + * @return return the strategy + */ + public String getStrategy() + { + return this.strategy; + } + + /** + * Return the client name + * @return return the client name + */ + public String getClientName() + { + return this.clientName; + } + + /** + * Gets the database's list from the server + * @return List of the databases + * @throws Exception + */ + public Vector<String> getDatabases() throws Exception + { + if (this.databasesList == null) + { + DictResultset drs = this.showDB(); + DictResult list = drs.getResultset(0); + + this.databasesList = new Vector<String>(); + + while(list.hasNext()) + { + this.databasesList.add(list.next()); + } + } + return this.databasesList; + } + + /** + * Gets the dictionary name from the databases list + * @param code Dictionary code + * @return the dictionary name + * @throws Exception + */ + public String getDictionaryName(String code) throws Exception + { + int dictionary_id_and_description_separator; + String dictionary_id_and_description; + String dictionary_id; + String dictionary_description; + + // First, we check if the code is a special code + // Checks the RFC-2229 for more details + if (code.equals("*")) + { + return "Any dictionary"; + } + else if (code.equals("!")) + { + return "First match"; + } + + // Gets the databases list + if (this.databasesList == null) + { + getDatabases(); + + } + + // Look down the databases list to get the name + for (int i=0; i<this.databasesList.size(); i++) + { + dictionary_id_and_description = this.databasesList.get(i); + dictionary_id_and_description_separator = dictionary_id_and_description.indexOf(' '); + dictionary_id = dictionary_id_and_description.substring(0, dictionary_id_and_description_separator); + + if (dictionary_id.equals(code)) + { + dictionary_description = dictionary_id_and_description.substring(dictionary_id_and_description_separator + 1); + return dictionary_description.replace("\"", ""); + } + } + + // If the name isn't in the list, return null + return null; + } + + /** + * Check if we are connected to the server + * @return true if we are connected - false otherwise + */ + public boolean isConnected() + { + return this.connected; + } + + /** + * Check if the URL is correct and a server exists + * @param url an Url + * @return true if everything is ok - false otherwise + */ + public static boolean isUrl(String url) + { + boolean ok; + if (url == null) + { + return false; + } + + try + { + InetAddress.getByName(url); + ok = true; + } + catch (UnknownHostException uhex) + { + ok = false; + } + + return ok; + } + + /** + * Executes a query and deals with the automatic deconnexion + * @param query A query to send to the server + * @return The first ligne of the response from the server + * @throws Exception IOException and DictException + */ + private String query(String query) throws Exception + { + String result = null; - /** - * Check if the URL is correct and a server exists - * @param url an Url - * @return true if everything is ok - false otherwise - */ - public static boolean isUrl(String url) - { - boolean ok; - if (url == null) - { - return false; - } - - try - { - InetAddress.getByName(url); - ok = true; - } - catch (UnknownHostException uhex) - { - ok = false; - } - - return ok; - } - - /** - * Executes a query and deals with the automatic deconnexion - * @param query A query to send to the server - * @return The first ligne of the response from the server - * @throws Exception IOException and DictException - */ - private String query(String query) throws Exception - { - String result = null; - this.out.println(query); result = in.readLine(); - + if (result == null) { // The connexion may be close, reconnexion this.connected = false; this.connect(); - + this.out.println(query); result = in.readLine(); - + if (result == null) { // If result is still equal to null, the server is unavailable @@ -601,5 +650,5 @@ public class DictAdapter } return result; - } + } } |