Author: batosai
Date: 2008-08-09 16:35:28 +0000 (Sat, 09 Aug 2008)
New Revision: 21687
Added:
trunk/apps/WoT/src/plugins/WoT/IdentityInserter.java
Modified:
trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java
trunk/apps/WoT/src/plugins/WoT/WoTplugin.java
Log:
Added necessary code to insert an identity.
Added: trunk/apps/WoT/src/plugins/WoT/IdentityInserter.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/IdentityInserter.java
(rev 0)
+++ trunk/apps/WoT/src/plugins/WoT/IdentityInserter.java 2008-08-09
16:35:28 UTC (rev 21687)
@@ -0,0 +1,100 @@
+/**
+ * This code is part of WoT, a plugin for Freenet. It is distributed
+ * under the GNU General Public License, version 2 (or at your option
+ * any later version). See http://www.gnu.org/ for details of the GPL.
+ */
+
+package plugins.WoT;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+
+import com.db4o.ObjectContainer;
+
+import freenet.client.ClientMetadata;
+import freenet.client.FetchException;
+import freenet.client.FetchResult;
+import freenet.client.HighLevelSimpleClient;
+import freenet.client.InsertBlock;
+import freenet.client.InsertException;
+import freenet.client.async.BaseClientPutter;
+import freenet.client.async.ClientCallback;
+import freenet.client.async.ClientGetter;
+import freenet.keys.FreenetURI;
+import freenet.support.io.FileBucket;
+
+/**
+ * @author Julien Cornuwel (batosai at freenetproject.org)
+ *
+ */
+public class IdentityInserter implements ClientCallback {
+
+ ObjectContainer db;
+ HighLevelSimpleClient client;
+ OwnIdentity identity;
+ String TEMP_DIR = ".";
+
+ public IdentityInserter(ObjectContainer db, HighLevelSimpleClient
client) {
+
+ this.db = db;
+ this.client = client;
+ }
+
+ public void insert(OwnIdentity identity) throws
TransformerConfigurationException, FileNotFoundException,
ParserConfigurationException, TransformerException, IOException,
InsertException {
+
+ this.identity = identity;
+
+ // Create XML file to insert
+ File outputFile= identity.exportToXML(db, new File(TEMP_DIR));
+
+ // Prepare the insert
+ FileBucket fb = new FileBucket(outputFile, false, false, true,
true, true); // We always delete the file after insert
+ ClientMetadata cmd = new ClientMetadata("text/xml");
+ InsertBlock ib = new
InsertBlock(fb,cmd,identity.getInsertURI());
+ client.getInsertContext(false);
+
+ // Insert
+ client.insert(ib, false, "identity.xml", false,
client.getInsertContext(false), this);
+ }
+
+ @Override
+ public void onFailure(FetchException e, ClientGetter state) {
+
+ }
+
+ @Override
+ public void onFailure(InsertException e, BaseClientPutter state) {
+
+ }
+
+ @Override
+ public void onFetchable(BaseClientPutter state) {
+
+ }
+
+ @Override
+ public void onGeneratedURI(FreenetURI uri, BaseClientPutter state) {
+
+ }
+
+ @Override
+ public void onMajorProgress() {
+
+ }
+
+ @Override
+ public void onSuccess(FetchResult result, ClientGetter state) {
+
+ }
+
+ @Override
+ public void onSuccess(BaseClientPutter state) {
+ System.out.println(identity.getRequestURI().toString());
+
+ }
+}
Modified: trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java 2008-08-09 15:26:33 UTC
(rev 21686)
+++ trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java 2008-08-09 16:35:28 UTC
(rev 21687)
@@ -73,7 +73,7 @@
public File exportToXML(ObjectContainer db, File dir ) throws
ParserConfigurationException, TransformerConfigurationException,
TransformerException, FileNotFoundException, IOException {
// Create the output file
- File outputFile = new File(dir, "identity.xml");
+ File outputFile = new File(dir, getInsertURI().getDocName() +
".xml");
BufferedOutputStream fos = new BufferedOutputStream(new
FileOutputStream(outputFile));
StreamResult resultStream = new StreamResult(fos);
Modified: trunk/apps/WoT/src/plugins/WoT/WoTplugin.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/WoTplugin.java 2008-08-09 15:26:33 UTC
(rev 21686)
+++ trunk/apps/WoT/src/plugins/WoT/WoTplugin.java 2008-08-09 16:35:28 UTC
(rev 21687)
@@ -6,10 +6,16 @@
package plugins.WoT;
+import java.io.FileNotFoundException;
+import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.Date;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+
import com.db4o.Db4o;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
@@ -47,11 +53,11 @@
private PluginRespirator pr;
private PageMaker pm;
private HighLevelSimpleClient client;
- private FCPServer fcp;
private ObjectContainer db;
private WoT wot;
private IdentityParser parser;
+ private IdentityInserter inserter;
public void runPlugin(PluginRespirator pr) {
@@ -60,12 +66,14 @@
this.pr = pr;
db = Db4o.openFile("WoT.db4o");
wot = new WoT(db);
+ client = pr.getHLSimpleClient();
try {
parser = new IdentityParser(db, wot);
} catch (Exception e) {
e.printStackTrace();
}
+ inserter = new IdentityInserter(db, client);
Config nc = pr.getNode().config;
SubConfig fc = nc.get("fproxy");
@@ -77,11 +85,6 @@
pm.addNavigationLink(SELF_URI + "/ownidentities", "Own
Identities", "Manage your own identities", false, null);
pm.addNavigationLink(SELF_URI + "/knownidentities", "Known
Identities", "Manage others identities", false, null);
pm.addNavigationLink("/plugins/", "Plugins page", "Back to
Plugins page", false, null);
-
- client = pr.getHLSimpleClient();
-
- fcp = pr.getNode().clientCore.getFCPServer();
-
}
public void terminate() {
@@ -122,6 +125,8 @@
new Date(0),
new Date(),
!request.getPartAsString("publishTrustList",1024).equals(""));
+ // TODO Set the nickName as an attribute
+
//TODO Make the new identity trust the seed
identity
db.store(identity);
db.commit();
@@ -150,6 +155,28 @@
return e.toString();
}
}
+ else if (page.equals("/insertIdentity")) {
+
+ FreenetURI uri;
+ try {
+ uri = new
FreenetURI(request.getPartAsString("identity", 1024));
+ } catch (MalformedURLException e) {
+ return e.getLocalizedMessage();
+ }
+
+ ObjectSet<OwnIdentity> ownIdentities =
db.queryByExample(new OwnIdentity(null, uri, null, null, true));
+ if (ownIdentities.size() == 0) return "Identity not
found";
+
+ OwnIdentity identity = ownIdentities.next();
+
+ try {
+ inserter.insert(identity);
+ } catch (Exception e) {
+ return e.getLocalizedMessage();
+ }
+
+ return "Insert in progress...";
+ }
else {
return makeHomePage();
}
@@ -192,11 +219,13 @@
else {
HTMLNode identitiesTable = boxContent.addChild("table",
"border", "0");
HTMLNode row=identitiesTable.addChild("tr");
- row.addChild("th", "");
+ row.addChild("th", "Name");
row.addChild("th", "Last change");
row.addChild("th", "Last insert");
row.addChild("th", "Publish TrustList ?");
+ row.addChild("th", "Insert");
+
while(ownIdentities.hasNext()) {
OwnIdentity id = ownIdentities.next();
row=identitiesTable.addChild("tr");
@@ -204,6 +233,12 @@
row.addChild("td",
id.getLastChange().toString());
row.addChild("td",
id.getLastInsert().equals(new Date(0)) ? "never" :
id.getLastInsert().toString());
row.addChild("td", id.PublishesTrustList() ?
"true" : "false");
+
+ // Insert button, will be automatic later
+ HTMLNode cell = row.addChild("td");
+ HTMLNode insertForm = pr.addFormChild(cell,
SELF_URI + "/insertIdentity", "insertForm");
+ insertForm.addChild("input", new String[] {
"type", "name", "value" }, new String[] { "hidden", "identity",
id.getRequestURI().toString() });
+ insertForm.addChild("input", new String[] {
"type", "name", "value" }, new String[] { "submit", "insert", "Insert now !" });
}
}
@@ -266,7 +301,7 @@
HTMLNode box = pm.getInfobox("Identity creation");
HTMLNode boxContent = pm.getContentNode(box);
- FreenetURI[] keypair = client.generateKeyPair(nickName);
+ FreenetURI[] keypair = client.generateKeyPair("WoT");
HTMLNode createForm = pr.addFormChild(boxContent, SELF_URI +
"/createIdentity2", "createForm");
createForm.addChild("#", "Request URI : ");
@@ -278,6 +313,7 @@
createForm.addChild("#", "Publish trust list ");
createForm.addChild("input", new String[] { "type", "name",
"checked" }, new String[] { "checkbox", "publishTrustList", "true"});
createForm.addChild("br");
+ createForm.addChild("input", new String[] { "type", "name",
"value" }, new String[] { "hidden", "nickName", nickName });
createForm.addChild("input", new String[] { "type", "name",
"value" }, new String[] { "submit", "create", "Create a new identity !" });
contentNode.addChild(box);