Author: jflesch
Date: 2007-04-22 19:47:03 +0000 (Sun, 22 Apr 2007)
New Revision: 12873
Modified:
trunk/apps/Thaw/src/thaw/plugins/index/BlackList.java
trunk/apps/Thaw/src/thaw/plugins/index/Comment.java
trunk/apps/Thaw/src/thaw/plugins/index/CommentTab.java
trunk/apps/Thaw/src/thaw/plugins/index/DatabaseManager.java
trunk/apps/Thaw/src/thaw/plugins/index/DetailPanel.java
trunk/apps/Thaw/src/thaw/plugins/index/Index.java
trunk/apps/Thaw/src/thaw/plugins/index/IndexBrowserPanel.java
trunk/apps/Thaw/src/thaw/plugins/index/IndexManagementHelper.java
trunk/apps/Thaw/src/thaw/plugins/index/IndexTree.java
trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java
Log:
Thaw is now able to sign and check comments on the indexes
Modified: trunk/apps/Thaw/src/thaw/plugins/index/BlackList.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/BlackList.java 2007-04-22
18:30:51 UTC (rev 12872)
+++ trunk/apps/Thaw/src/thaw/plugins/index/BlackList.java 2007-04-22
19:47:03 UTC (rev 12873)
@@ -51,9 +51,9 @@
}
- public BlackList(Core core, Hsqldb db, IndexBrowserPanel indexBrowser) {
+ public BlackList(Core core, IndexBrowserPanel indexBrowser) {
this.core = core;
- this.db = db;
+ this.db = indexBrowser.getDb();
this.indexBrowser = indexBrowser;
Modified: trunk/apps/Thaw/src/thaw/plugins/index/Comment.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/Comment.java 2007-04-22 18:30:51 UTC
(rev 12872)
+++ trunk/apps/Thaw/src/thaw/plugins/index/Comment.java 2007-04-22 19:47:03 UTC
(rev 12873)
@@ -60,6 +60,16 @@
import java.sql.ResultSet;
import java.sql.SQLException;
+
+/* Base 64 */
+
+import freenet.support.Base64;
+
+/* a little bit of DSA */
+
+import freenet.crypt.DSASignature;
+
+
/* Thaw */
import thaw.core.Config;
@@ -73,8 +83,8 @@
import thaw.fcp.FCPQueueManager;
import thaw.plugins.Hsqldb;
+import thaw.plugins.signatures.Identity;
-
/**
* Will use, from the configuration:
* 'userNickname' as the author name
@@ -82,7 +92,7 @@
public class Comment extends Observable implements Observer {
public final static int MAX_SIZE = 16384;
- private String author;
+ private Identity author;
private String comment;
private Index index;
@@ -91,7 +101,14 @@
private int rev;
private boolean newComment = false;
+ private boolean valid = false;
+
+ /* needed to check the signature */
+ private byte[] r;
+ private byte[] s;
+
+
private Comment() {
}
@@ -102,7 +119,7 @@
* @param rev revision of the comment (-1) if not inserted at the moment
* @param comment comment inside the comment ... :)
*/
- public Comment(Hsqldb db, Index index, int rev, String author, String
comment) {
+ public Comment(Hsqldb db, Index index, int rev, Identity author, String
comment) {
this.db = db;
this.author = author;
this.comment = comment;
@@ -115,7 +132,7 @@
JPanel panel = new JPanel(new GridLayout(1, 1));
JTextArea text = new JTextArea(comment.trim());
-
panel.setBorder(BorderFactory.createTitledBorder(I18n.getMessage("thaw.plugin.index.comment.author")+"
: "+author));
+
panel.setBorder(BorderFactory.createTitledBorder(I18n.getMessage("thaw.plugin.index.comment.author")+"
: "+author.toString()));
text.setEditable(false);
text.setBackground(panel.getBackground());
@@ -179,14 +196,47 @@
Element authorTag = xmlDoc.createElement("author");
Element textTag = xmlDoc.createElement("text");
- Text authorTxt = xmlDoc.createTextNode(author);
+ Text authorTxt = xmlDoc.createTextNode(author.getNick());
Text textTxt = xmlDoc.createTextNode(comment);
authorTag.appendChild(authorTxt);
textTag.appendChild(textTxt);
+ Element signatureTag = xmlDoc.createElement("signature");
+
+ Element sigTag = xmlDoc.createElement("sig");
+ Element rTag = xmlDoc.createElement("r");
+ Element sTag = xmlDoc.createElement("s");
+
+ DSASignature sig = author.sign(index.getCommentPublicKey()+"-"+
+ author.getNick()+"-"+
+ comment);
+
+ Text rTxt =
xmlDoc.createTextNode(Base64.encode(sig.getR().toByteArray()));
+ Text sTxt =
xmlDoc.createTextNode(Base64.encode(sig.getS().toByteArray()));
+
+ rTag.appendChild(rTxt);
+ sTag.appendChild(sTxt);
+
+ sigTag.appendChild(rTag);
+ sigTag.appendChild(sTag);
+
+
+ Element publicKeyTag = xmlDoc.createElement("publicKey");
+
+ Element yTag = xmlDoc.createElement("y");
+
+ Text yTxt = xmlDoc.createTextNode(Base64.encode(author.getY()));
+
+ yTag.appendChild(yTxt);
+ publicKeyTag.appendChild(yTag);
+
+ signatureTag.appendChild(sigTag);
+ signatureTag.appendChild(publicKeyTag);
+
rootEl.appendChild(authorTag);
rootEl.appendChild(textTag);
+ rootEl.appendChild(signatureTag);
/** GENERATE THE FILE **/
@@ -293,7 +343,15 @@
private boolean authorTag;
private boolean textTag;
+ private boolean yTag;
+ private boolean rTag;
+ private boolean sTag;
+ /* needed to create / get the corresponding identity */
+ private String authorTxt;
+ private byte[] y;
+
+
/**
* Called when the parsed find an opening tag
* @param localName local tag name
@@ -314,6 +372,15 @@
if ("text".equals(rawName))
textTag = true;
+
+ if ("y".equals(rawName))
+ yTag = true;
+
+ if ("r".equals(rawName))
+ rTag = true;
+
+ if ("s".equals(rawName))
+ sTag = true;
}
@@ -335,8 +402,18 @@
if ("text".equals(rawName))
textTag = false;
+
+ if ("y".equals(rawName))
+ yTag = false;
+
+ if ("r".equals(rawName))
+ rTag = false;
+
+ if ("s".equals(rawName))
+ sTag = false;
}
+
/**
* Called when a text between two tag is met
* @param ch text
@@ -347,8 +424,24 @@
public void characters(char[] ch, int start, int end) throws
SAXException {
String txt = new String(ch, start, end);
- if (authorTag) author = txt;
- if (textTag) comment = txt;
+ if (authorTag)
+ authorTxt = txt;
+
+ if (textTag)
+ comment = txt;
+
+ try {
+ if (yTag)
+ y = Base64.decode(txt);
+
+ if (rTag)
+ r = Base64.decode(txt);
+
+ if (sTag)
+ s = Base64.decode(txt);
+ } catch(freenet.support.IllegalBase64Exception e) {
+ Logger.error(this, "IllegalBase64Exception =>
won't validate :P");
+ }
}
public void ignorableWhitespace(char[] ch, int start, int end)
throws SAXException {
@@ -371,7 +464,21 @@
* @see org.xml.sax.ContentHandler#endDocument()
*/
public void endDocument() throws SAXException {
+ if (comment != null && authorTxt != null
+ && y != null && r != null && s != null) {
+ author = Identity.getIdentity(db, authorTxt, y);
+ valid =
author.check(index.getCommentPublicKey()+"-"+
+ author.getNick()+"-"+
+ comment,
+ r, s);
+ if (!valid) {
+ Logger.notice(this, "Signature
validation failed !");
+ }
+ } else {
+ Logger.notice(this, "Signature validation
failed ! (missing elements)");
+ valid = false;
+ }
}
}
@@ -410,24 +517,13 @@
Logger.error(this, "Error (3) while parsing index:
"+e.toString());
}
- if (comment != null && author != null) {
+ if (comment != null && author != null && valid) {
Logger.info(this, "Parsing done");
try {
synchronized(db.dbLock) {
- PreparedStatement st;
- st =
db.getConnection().prepareStatement("SELECT id FROM indexComments "+
-
"WHERE indexId = ? AND author = ? "+
-
"AND text = ?");
-
- st.setInt(1, index.getId());
- st.setString(2, author);
- st.setString(3, comment);
-
- ResultSet set = st.executeQuery();
-
- if (set.next()) {
+ if (existsInTheBdd()) {
Logger.debug(this, "Comment
already in db");
return false;
}
@@ -436,13 +532,17 @@
newComment = true;
+ PreparedStatement st;
+
st =
db.getConnection().prepareStatement("INSERT INTO indexComments "+
-
"(author, text, rev, indexId) "+
-
"VALUES (?, ?, ?, ?)");
- st.setString(1, author);
+
"(authorId, text, rev, indexId, r, s) "+
+
"VALUES (?, ?, ?, ?, ?, ?)");
+ st.setInt(1, author.getId());
st.setString(2, comment);
st.setInt(3, rev);
st.setInt(4, index.getId());
+ st.setBytes(5, r);
+ st.setBytes(6, s);
st.execute();
@@ -452,6 +552,8 @@
Logger.error(this, "Unable to add comment in
the db because: "+e.toString());
}
}
+ else
+ Logger.notice(this, "Parsing failed !");
return false;
}
@@ -465,6 +567,32 @@
return (comment != null && author != null);
}
+
+ /**
+ * r and s must be set
+ * You have to do the synchronized(db.dbLock) !
+ */
+ private boolean existsInTheBdd() {
+ try {
+ PreparedStatement st;
+
+ st = db.getConnection().prepareStatement("SELECT id
FROM indexComments "+
+ "WHERE r = ? AND s = ?
");
+
+ st.setBytes(1, r);
+ st.setBytes(2, s);
+
+ ResultSet set = st.executeQuery();
+
+ return (set.next());
+ } catch(SQLException e) {
+ Logger.error(this, "Unable to check if the comment is
already in the bdd, because: "+e.toString());
+ }
+
+ return true;
+ }
+
+
public boolean isNew() {
return newComment;
}
Modified: trunk/apps/Thaw/src/thaw/plugins/index/CommentTab.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/CommentTab.java 2007-04-22
18:30:51 UTC (rev 12872)
+++ trunk/apps/Thaw/src/thaw/plugins/index/CommentTab.java 2007-04-22
19:47:03 UTC (rev 12873)
@@ -26,6 +26,7 @@
import thaw.gui.IconBox;
import thaw.core.I18n;
+import thaw.plugins.Hsqldb;
public class CommentTab implements ActionListener {
private boolean visible;
@@ -43,8 +44,7 @@
private IndexBrowserPanel indexBrowser;
- public CommentTab(Config config,
- FCPQueueManager queueManager,
+ public CommentTab(FCPQueueManager queueManager,
IndexBrowserPanel indexBrowser) {
this.indexBrowser = indexBrowser;
@@ -69,7 +69,7 @@
buttonActions = new Vector();
button = new
JButton(I18n.getMessage("thaw.plugin.index.comment.add"),
IconBox.minAddComment);
- buttonActions.add(new
IndexManagementHelper.IndexCommentAdder(config, queueManager, indexBrowser,
button));
+ buttonActions.add(new
IndexManagementHelper.IndexCommentAdder(queueManager, indexBrowser, button));
southPanel.add(new JLabel(""), BorderLayout.CENTER);
southPanel.add(button, BorderLayout.EAST);
Modified: trunk/apps/Thaw/src/thaw/plugins/index/DatabaseManager.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/DatabaseManager.java 2007-04-22
18:30:51 UTC (rev 12872)
+++ trunk/apps/Thaw/src/thaw/plugins/index/DatabaseManager.java 2007-04-22
19:47:03 UTC (rev 12873)
@@ -51,9 +51,6 @@
* Creates all the tables used to save the indexes,
* manages structure changes if needed, etc.
*
- * <br/> TAKE CARE: because of the conversion mechanisms, the field order is
NOT ALWAYS the same !
- * So DON'T DO "SELECT * [...]" !
- *
* <br/>
* "Comprenne qui pourra" :P
*
@@ -94,7 +91,7 @@
if (config.getValue("indexDatabaseVersion") == null) {
newDb = true;
- config.setValue("indexDatabaseVersion", "5");
+ config.setValue("indexDatabaseVersion", "6");
} else {
/* CONVERTIONS */
@@ -130,6 +127,13 @@
config.setValue("indexDatabaseVersion",
"5");
}
+ if
("5".equals(config.getValue("indexDatabaseVersion"))) {
+ if (splashScreen != null)
+ splashScreen.setStatus("Converting
database ...");
+ if (convertDatabase_5_to_6(db))
+ config.setValue("indexDatabaseVersion",
"6");
+ }
+
/* ... */
}
@@ -262,11 +266,14 @@
sendQuery(db,
"CREATE CACHED TABLE indexComments ("
+ "id INTEGER IDENTITY NOT NULL,"
- + "author VARCHAR(255) NOT NULL,"
+ + "authorId INTEGER NOT NULL,"
+ "text VARCHAR(16384) NOT NULL," /* 16 KB */
+ "rev INTEGER NOT NULL,"
+ "indexId INTEGER NOT NULL,"
- + "FOREIGN KEY (indexId) REFERENCES indexes (id))");
+ + "r VARBINARY(400) NOT NULL," /* signature */
+ + "s VARBINARY(400) NOT NULL," /* signature */
+ + "FOREIGN KEY (indexId) REFERENCES indexes (id),"
+ + "FOREIGN KEY (authorId) REFERENCES signatures
(id))");
}
@@ -917,4 +924,39 @@
return true;
}
+
+
+ public static boolean convertDatabase_5_to_6(Hsqldb db) {
+ if (!sendQuery(db, "DELETE FROM indexComments")) {
+ Logger.error(new DatabaseManager(), "Error while
removing all the known comments");
+ return false;
+ }
+
+ if (!sendQuery(db, "ALTER TABLE indexComments DROP COLUMN
author")) {
+ Logger.error(new DatabaseManager(), "Error while
altering the indexComments table (1)");
+ return false;
+ }
+
+ if (!sendQuery(db, "ALTER TABLE indexComments ADD COLUMN
authorId INTEGER NOT NULL")) {
+ Logger.error(new DatabaseManager(), "Error while
altering the indexComments table (2)");
+ return false;
+ }
+
+ if (!sendQuery(db, "ALTER TABLE indexComments ADD FOREIGN KEY
(authorId) REFERENCES signatures (id)")) {
+ Logger.error(new DatabaseManager(), "Error while
altering the indexComments table (3)");
+ return false;
+ }
+
+ if (!sendQuery(db, "ALTER TABLE indexComments ADD COLUMN r
VARBINARY(400) NOT NULL")) {
+ Logger.error(new DatabaseManager(), "Error while
altering the indexComments table (4)");
+ return false;
+ }
+
+ if (!sendQuery(db, "ALTER TABLE indexComments ADD COLUMN s
VARBINARY(400) NOT NULL")) {
+ Logger.error(new DatabaseManager(), "Error while
altering the indexComments table (5)");
+ return false;
+ }
+
+ return true;
+ }
}
Modified: trunk/apps/Thaw/src/thaw/plugins/index/DetailPanel.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/DetailPanel.java 2007-04-22
18:30:51 UTC (rev 12872)
+++ trunk/apps/Thaw/src/thaw/plugins/index/DetailPanel.java 2007-04-22
19:47:03 UTC (rev 12873)
@@ -18,7 +18,9 @@
import thaw.fcp.FCPQueueManager;
+import thaw.plugins.Hsqldb;
+
/**
* Initially, I wanted to use it to show details about the
* currently-viewed index, but in the end it will mostly
@@ -32,7 +34,7 @@
private Vector buttonActions;
- public DetailPanel(Config config, FCPQueueManager queueManager,
IndexBrowserPanel indexBrowser) {
+ public DetailPanel(FCPQueueManager queueManager, IndexBrowserPanel
indexBrowser) {
panel = new JPanel(new BorderLayout());
panel.add(new JLabel(""), BorderLayout.CENTER); /* because we
need something */
@@ -48,7 +50,7 @@
button = new
JButton(I18n.getMessage("thaw.plugin.index.comment.add"),
IconBox.minAddComment);
- buttonActions.add(new
IndexManagementHelper.IndexCommentAdder(config, queueManager, indexBrowser,
button));
+ buttonActions.add(new
IndexManagementHelper.IndexCommentAdder(queueManager, indexBrowser, button));
buttonPanel.add(button);
Modified: trunk/apps/Thaw/src/thaw/plugins/index/Index.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/Index.java 2007-04-22 18:30:51 UTC
(rev 12872)
+++ trunk/apps/Thaw/src/thaw/plugins/index/Index.java 2007-04-22 19:47:03 UTC
(rev 12873)
@@ -65,9 +65,9 @@
import thaw.plugins.Hsqldb;
import thaw.plugins.insertPlugin.DefaultMIMETypes;
+import thaw.plugins.signatures.Identity;
-
public class Index extends Observable implements MutableTreeNode,
FileAndLinkList, IndexTreeNode, Observer {
private final static long MAX_SIZE = 5242880; /* 5MB */
@@ -1836,7 +1836,7 @@
}
- public boolean postComment(FCPQueueManager queueManager, String author,
String msg) {
+ public boolean postComment(FCPQueueManager queueManager, Identity
author, String msg) {
String privKey;
if ((privKey = getCommentPrivateKey()) == null) {
@@ -1882,8 +1882,9 @@
PreparedStatement st;
- st =
db.getConnection().prepareStatement("SELECT author, text, rev FROM
indexComments WHERE indexId = ? ORDER BY rev"
- + (asc
? "" : " DESC"));
+ st =
db.getConnection().prepareStatement("SELECT authorId, text, rev "+
+ "FROM
indexComments WHERE indexId = ? ORDER BY rev" +
+ (asc ?
"" : " DESC"));
st.setInt(1, id);
@@ -1892,7 +1893,7 @@
while(set.next())
comments.add(new Comment(db, this,
set.getInt("rev"),
-
set.getString("author"),
+
Identity.getIdentity(db, set.getInt("authorId")),
set.getString("text")));
if (comments.size() == 0)
Modified: trunk/apps/Thaw/src/thaw/plugins/index/IndexBrowserPanel.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/IndexBrowserPanel.java
2007-04-22 18:30:51 UTC (rev 12872)
+++ trunk/apps/Thaw/src/thaw/plugins/index/IndexBrowserPanel.java
2007-04-22 19:47:03 UTC (rev 12873)
@@ -44,15 +44,14 @@
this.config = core.getConfig();
this.mainWindow = core.getMainWindow();
- blackList = new BlackList(core, db, this);
+ blackList = new BlackList(core, this);
unknownList = new UnknownIndexList(queueManager, this);
indexTree = new
IndexTree(I18n.getMessage("thaw.plugin.index.indexes"),
false, queueManager, this, config);
- commentTab = new CommentTab(core.getConfig(),
- core.getQueueManager(),
+ commentTab = new CommentTab(core.getQueueManager(),
this);
leftSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
@@ -63,7 +62,7 @@
listAndDetails.setLayout(new BorderLayout(10, 10));
tables = new Tables(false, queueManager, this, config);
- detailPanel = new DetailPanel(core.getConfig(), queueManager,
this);
+ detailPanel = new DetailPanel(queueManager, this);
listAndDetails.add(tables.getPanel(), BorderLayout.CENTER);
listAndDetails.add(detailPanel.getPanel(), BorderLayout.SOUTH);
Modified: trunk/apps/Thaw/src/thaw/plugins/index/IndexManagementHelper.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/IndexManagementHelper.java
2007-04-22 18:30:51 UTC (rev 12872)
+++ trunk/apps/Thaw/src/thaw/plugins/index/IndexManagementHelper.java
2007-04-22 19:47:03 UTC (rev 12873)
@@ -26,6 +26,8 @@
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
+import javax.swing.JList;
+import javax.swing.JComboBox;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
@@ -47,7 +49,9 @@
import thaw.fcp.FCPQueueManager;
import thaw.fcp.FCPTransferQuery;
import thaw.plugins.Hsqldb;
+import thaw.plugins.signatures.Identity;
+
/**
* Index.java, IndexFolder.java and IndexTree.java must NEVER use this helper
(to avoid loops).
*/
@@ -1518,25 +1522,19 @@
public static class IndexCommentAdder extends BasicIndexAction
implements Runnable, ActionListener {
- private Config config;
-
private JDialog dialog;
- private JTextField author;
+ private JComboBox author;
private JTextArea textArea;
private JButton okButton;
private JButton cancelButton;
- public IndexCommentAdder(Config config,
- FCPQueueManager queueManager,
+ public IndexCommentAdder(FCPQueueManager queueManager,
IndexBrowserPanel indexBrowser,
final AbstractButton actionSource) {
super(queueManager, indexBrowser, actionSource);
- this.config = config;
-
-
if (actionSource != null)
actionSource.setEnabled(false);
}
@@ -1555,12 +1553,7 @@
if (dialog != null)
return;
- String defaultAuthor = config.getValue("userNickname");
- if (defaultAuthor == null)
- defaultAuthor = "User who didn't configure its
Thaw";
-
-
dialog = new
JDialog(getIndexBrowserPanel().getMainWindow().getMainFrame(),
I18n.getMessage("thaw.plugin.index.comment.add"));
@@ -1572,7 +1565,7 @@
authorPanel.add(new
JLabel(I18n.getMessage("thaw.plugin.index.comment.author")),
BorderLayout.WEST);
- author = new JTextField(defaultAuthor);
+ author = new
JComboBox(Identity.getYourIdentities(getIndexBrowserPanel().getDb()));
authorPanel.add(author, BorderLayout.CENTER);
JPanel header = new JPanel(new GridLayout(2, 1));
@@ -1615,7 +1608,7 @@
if (e.getSource() == okButton) {
if (getTarget() instanceof Index) {
((Index)getTarget()).postComment(getQueueManager(),
-
author.getText().trim(),
+
((Identity)author.getSelectedItem()),
textArea.getText().trim());
}
Modified: trunk/apps/Thaw/src/thaw/plugins/index/IndexTree.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/IndexTree.java 2007-04-22
18:30:51 UTC (rev 12872)
+++ trunk/apps/Thaw/src/thaw/plugins/index/IndexTree.java 2007-04-22
19:47:03 UTC (rev 12873)
@@ -292,7 +292,7 @@
item = new
JMenuItem(I18n.getMessage("thaw.plugin.index.comment.add"),
IconBox.minAddComment);
commentMenu.add(item);
- indexAndFileActions.add(new
IndexManagementHelper.IndexCommentAdder(config, queueManager, indexBrowser,
item));
+ indexAndFileActions.add(new
IndexManagementHelper.IndexCommentAdder(queueManager, indexBrowser, item));
indexAndFileMenu.add(indexMenu);
Modified: trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java 2007-04-22
18:30:51 UTC (rev 12872)
+++ trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java 2007-04-22
19:47:03 UTC (rev 12873)
@@ -46,7 +46,7 @@
Color.BLUE,
Color.GREEN,
new java.awt.Color(0, 128, 0), /* light green */
- Color.ORANGE,
+ Color.BLACK,
new java.awt.Color(175, 0, 0), /* moderatly red */
Color.RED
};
@@ -77,6 +77,7 @@
/**
* If you don't have a value, let it to null and pray it won't be used
:P
+ * @param nick part *before* the @
*/
public Identity(Hsqldb db, int id, String nick,
byte[] y, byte[] x,
@@ -94,8 +95,114 @@
}
+ public int getId() {
+ return id;
+ }
+ public String getNick() {
+ return nick;
+ }
+
+ public byte[] getY() {
+ return y;
+ }
+
+
/**
+ * if the identity doesn't exists, it will be created
+ */
+ public static Identity getIdentity(Hsqldb db,
+ String nick,
+ byte[] y) {
+ try {
+ synchronized(db.dbLock) {
+ PreparedStatement st;
+
+ st =
db.getConnection().prepareStatement("SELECT id, nickName, y, x, isDup,
trustLevel "+
+ "FROM
signatures "+
+ "WHERE
y = ? LIMIT 1");
+ st.setBytes(1, y);
+
+ ResultSet set = st.executeQuery();
+
+ if (set.next()) {
+ Identity i = new Identity(db,
set.getInt("id"), set.getString("nickName"),
+
set.getBytes("y"), set.getBytes("x"),
+
set.getBoolean("isDup"), set.getInt("trustLevel"));
+ Logger.info(i, "Identity found");
+ return i;
+ }
+
+ /* else we must add it, but first we need to
know if it's a dup */
+
+ st =
db.getConnection().prepareStatement("SELECT id FROM signatures "+
+ "WHERE
nickName = ? LIMIT 1");
+ st.setString(1, nick);
+
+ set = st.executeQuery();
+
+ boolean isDup = set.next();
+
+ /* and we add */
+
+ st =
db.getConnection().prepareStatement("INSERT INTO signatures "+
+
"(nickName, y, x, isDup, trustLevel) "+
+
"VALUES (?, ?, ?, ?, 0)");
+
+ st.setString(1, nick);
+ st.setBytes(2, y);
+ st.setNull(3, Types.VARBINARY);
+ st.setBoolean(4, isDup);
+
+ st.execute();
+
+ Identity i = new Identity(db, -1, nick, y,
null, isDup, 0);
+ Logger.info(i, "New identity found");
+ return i;
+
+ }
+ } catch(SQLException e) {
+ Logger.error(new Identity(), "Error while getting
identity (2) : "+e.toString());
+ }
+
+ return null;
+ }
+
+
+ /**
+ * won't create
+ */
+ public static Identity getIdentity(Hsqldb db,
+ int id) {
+ Identity i = null;
+
+ try {
+ synchronized(db.dbLock) {
+ PreparedStatement st;
+
+ st =
db.getConnection().prepareStatement("SELECT id, nickName, y, x, isDup,
trustLevel "+
+ "FROM
signatures "+
+ "WHERE
id = ? LIMIT 1");
+ st.setInt(1, id);
+
+ ResultSet set = st.executeQuery();
+
+ if (!set.next())
+ return null;
+
+ i = new Identity(db, id,
set.getString("nickName"),
+ set.getBytes("y"),
set.getBytes("x"),
+ set.getBoolean("isDup"),
set.getInt("trustLevel"));
+ }
+ } catch(SQLException e) {
+ Logger.error(new Identity(), "Error while getting
identity (1) : "+e.toString());
+ }
+
+ return i;
+ }
+
+
+ /**
* Generate a new identity
* you have to insert() it
* @param db just here to fill in the class