Author: batosai
Date: 2008-09-07 12:22:24 +0000 (Sun, 07 Sep 2008)
New Revision: 22522
Removed:
trunk/plugins/WoT/src/plugins/WoT/WoTplugin.java
Modified:
trunk/plugins/WoT/
trunk/plugins/WoT/build.xml
trunk/plugins/WoT/src/plugins/WoT/Identity.java
trunk/plugins/WoT/src/plugins/WoT/IdentityFetcher.java
trunk/plugins/WoT/src/plugins/WoT/IdentityInserter.java
trunk/plugins/WoT/src/plugins/WoT/IdentityParser.java
trunk/plugins/WoT/src/plugins/WoT/OwnIdentity.java
trunk/plugins/WoT/src/plugins/WoT/Score.java
trunk/plugins/WoT/src/plugins/WoT/WebInterface.java
trunk/plugins/WoT/src/plugins/WoT/WoT.java
trunk/plugins/WoT/src/plugins/WoT/WoTTest.java
Log:
Refactor in order to become an official plugin.
Made WoT the main plugin class.
Property changes on: trunk/plugins/WoT
___________________________________________________________________
Name: svn:ignore
+ build
build-test
dist
scoreTest.db4o
trustTest.db4o
Modified: trunk/plugins/WoT/build.xml
===================================================================
--- trunk/plugins/WoT/build.xml 2008-09-07 12:15:13 UTC (rev 22521)
+++ trunk/plugins/WoT/build.xml 2008-09-07 12:22:24 UTC (rev 22522)
@@ -82,7 +82,7 @@
description="generate the distribution" >
<jar jarfile="${dist}/wot.jar" duplicate="fail">
<manifest>
- <attribute name="Plugin-Main-Class"
value="plugins.WoT.WoTplugin"/>
+ <attribute name="Plugin-Main-Class"
value="plugins.WoT.WoT"/>
</manifest>
<fileset dir="." casesensitive="no">
<include name="*.txt"/>
Modified: trunk/plugins/WoT/src/plugins/WoT/Identity.java
===================================================================
--- trunk/plugins/WoT/src/plugins/WoT/Identity.java 2008-09-07 12:15:13 UTC
(rev 22521)
+++ trunk/plugins/WoT/src/plugins/WoT/Identity.java 2008-09-07 12:22:24 UTC
(rev 22522)
@@ -86,9 +86,17 @@
public static Identity getByURI (ObjectContainer db, FreenetURI uri)
throws UnknownIdentityException, DuplicateIdentityException {
return getById(db, new ByteArrayWrapper(uri.getRoutingKey()));
}
+
+ public static int getNbIdentities(ObjectContainer db) {
+ return db.queryByExample(Identity.class).size() -
OwnIdentity.getNbOwnIdentities(db);
+ }
+ public static ObjectSet<Identity> getAllIdentities(ObjectContainer db) {
+ return db.queryByExample(Identity.class);
+ }
+
@SuppressWarnings("unchecked")
public Score getScore(OwnIdentity treeOwner, ObjectContainer db) throws
NotInTrustTreeException, DuplicateScoreException {
Query query = db.query();
@@ -167,7 +175,7 @@
}
public void updateScore (ObjectContainer db) throws
DuplicateScoreException, DuplicateTrustException {
- ObjectSet<OwnIdentity> treeOwners = OwnIdentity.getAll(db);
+ ObjectSet<OwnIdentity> treeOwners =
OwnIdentity.getAllOwnIdentities(db);
while(treeOwners.hasNext())
updateScore (db, treeOwners.next());
}
Modified: trunk/plugins/WoT/src/plugins/WoT/IdentityFetcher.java
===================================================================
--- trunk/plugins/WoT/src/plugins/WoT/IdentityFetcher.java 2008-09-07
12:15:13 UTC (rev 22521)
+++ trunk/plugins/WoT/src/plugins/WoT/IdentityFetcher.java 2008-09-07
12:22:24 UTC (rev 22522)
@@ -30,14 +30,12 @@
public class IdentityFetcher implements ClientCallback {
private ObjectContainer db;
- private WoT wot;
private HighLevelSimpleClient client;
private ArrayList<ClientGetter> requests;
- public IdentityFetcher(ObjectContainer db, WoT wot,
HighLevelSimpleClient client) {
+ public IdentityFetcher(ObjectContainer db, HighLevelSimpleClient
client) {
this.db = db;
- this.wot = wot;
this.client = client;
requests = new ArrayList<ClientGetter>();
}
@@ -113,7 +111,7 @@
try {
Logger.debug(this, "Sucessfully fetched identity "+
state.getURI().toString());
- new IdentityParser(db, wot, client,
this).parse(result.asBucket().getInputStream(), state.getURI());
+ new IdentityParser(db, client,
this).parse(result.asBucket().getInputStream(), state.getURI());
db.commit();
state.restart(state.getURI().setSuggestedEdition(state.getURI().getSuggestedEdition()
+ 1));
} catch (Exception e) {
Modified: trunk/plugins/WoT/src/plugins/WoT/IdentityInserter.java
===================================================================
--- trunk/plugins/WoT/src/plugins/WoT/IdentityInserter.java 2008-09-07
12:15:13 UTC (rev 22521)
+++ trunk/plugins/WoT/src/plugins/WoT/IdentityInserter.java 2008-09-07
12:22:24 UTC (rev 22522)
@@ -34,15 +34,13 @@
*/
public class IdentityInserter implements Runnable {
- WoT wot;
ObjectContainer db;
HighLevelSimpleClient client;
final TempBucketFactory tBF;
boolean isRunning;
- public IdentityInserter(WoT wot, ObjectContainer db,
HighLevelSimpleClient client, TempBucketFactory tbf) {
- this.wot = wot;
+ public IdentityInserter(ObjectContainer db, HighLevelSimpleClient
client, TempBucketFactory tbf) {
this.db = db;
this.client = client;
isRunning = true;
@@ -54,7 +52,7 @@
Thread.sleep(30 * 1000); // Let the node start up (30
seconds)
} catch (InterruptedException e){}
while(isRunning) {
- ObjectSet<OwnIdentity> identities =
wot.getOwnIdentities();
+ ObjectSet<OwnIdentity> identities =
OwnIdentity.getAllOwnIdentities(db);
while(identities.hasNext()) {
OwnIdentity identity = identities.next();
if(identity.needsInsert()) {
Modified: trunk/plugins/WoT/src/plugins/WoT/IdentityParser.java
===================================================================
--- trunk/plugins/WoT/src/plugins/WoT/IdentityParser.java 2008-09-07
12:15:13 UTC (rev 22521)
+++ trunk/plugins/WoT/src/plugins/WoT/IdentityParser.java 2008-09-07
12:22:24 UTC (rev 22522)
@@ -30,16 +30,14 @@
public class IdentityParser {
ObjectContainer db;
- WoT wot;
HighLevelSimpleClient client;
IdentityFetcher fetcher;
SAXParser saxParser;
Identity identity;
- public IdentityParser(ObjectContainer db, WoT wot,
HighLevelSimpleClient client, IdentityFetcher fetcher) throws
ParserConfigurationException, SAXException {
+ public IdentityParser(ObjectContainer db, HighLevelSimpleClient client,
IdentityFetcher fetcher) throws ParserConfigurationException, SAXException {
this.db = db;
- this.wot = wot;
this.client = client;
this.fetcher = fetcher;
saxParser = SAXParserFactory.newInstance().newSAXParser();
Modified: trunk/plugins/WoT/src/plugins/WoT/OwnIdentity.java
===================================================================
--- trunk/plugins/WoT/src/plugins/WoT/OwnIdentity.java 2008-09-07 12:15:13 UTC
(rev 22521)
+++ trunk/plugins/WoT/src/plugins/WoT/OwnIdentity.java 2008-09-07 12:22:24 UTC
(rev 22522)
@@ -87,7 +87,11 @@
return getById(db, new ByteArrayWrapper(uri.getRoutingKey()));
}
- public static ObjectSet<OwnIdentity> getAll (ObjectContainer db) {
+ public static int getNbOwnIdentities(ObjectContainer db) {
+ return db.queryByExample(OwnIdentity.class).size();
+ }
+
+ public static ObjectSet<OwnIdentity> getAllOwnIdentities
(ObjectContainer db) {
return db.queryByExample(OwnIdentity.class);
}
Modified: trunk/plugins/WoT/src/plugins/WoT/Score.java
===================================================================
--- trunk/plugins/WoT/src/plugins/WoT/Score.java 2008-09-07 12:15:13 UTC
(rev 22521)
+++ trunk/plugins/WoT/src/plugins/WoT/Score.java 2008-09-07 12:22:24 UTC
(rev 22522)
@@ -5,8 +5,11 @@
*/
package plugins.WoT;
+import java.net.MalformedURLException;
+
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
+import com.db4o.query.Query;
/**
* @author Julien Cornuwel (batosai at freenetproject.org)
@@ -34,6 +37,29 @@
return scores.size();
}
+ @SuppressWarnings("unchecked")
+ public static ObjectSet<Score> getIdentitiesByScore (String owner,
String select, ObjectContainer db) throws InvalidParameterException,
MalformedURLException, UnknownIdentityException, DuplicateIdentityException {
+
+ OwnIdentity treeOwner = OwnIdentity.getByURI(db, owner);
+
+ Query query = db.query();
+ query.constrain(Score.class);
+ query.descend("treeOwner").constrain(treeOwner);
+
+ if(select.equals("+")) {
+ query.descend("score").constrain(new
Integer(0)).greater();
+ }
+ else if(select.equals("0")) {
+ query.descend("score").constrain(new Integer(0));
+ }
+ else if(select.equals("-")) {
+ query.descend("score").constrain(new
Integer(0)).smaller();
+ }
+ else throw new InvalidParameterException("Unhandled select
value ("+select+")");
+
+ return query.execute();
+ }
+
public String toString() {
return getTarget().getNickName() + " has " + getScore() + "
points in " + getTreeOwner().getNickName() + "'s trust tree (rank : " +
getRank() + ", capacity : " + getCapacity() + ")";
}
Modified: trunk/plugins/WoT/src/plugins/WoT/WebInterface.java
===================================================================
--- trunk/plugins/WoT/src/plugins/WoT/WebInterface.java 2008-09-07 12:15:13 UTC
(rev 22521)
+++ trunk/plugins/WoT/src/plugins/WoT/WebInterface.java 2008-09-07 12:22:24 UTC
(rev 22522)
@@ -30,14 +30,12 @@
private PageMaker pm;
private HighLevelSimpleClient client;
private String SELF_URI;
- private WoT wot;
private ObjectContainer db;
- public WebInterface(PluginRespirator pr, ObjectContainer db,
HighLevelSimpleClient client, String uri, WoT wot) {
+ public WebInterface(PluginRespirator pr, ObjectContainer db,
HighLevelSimpleClient client, String uri) {
this.pr = pr;
this.client = client;
this.SELF_URI = uri;
- this.wot = wot;
this.db = db;
pm = pr.getPageMaker();
@@ -51,8 +49,8 @@
HTMLNode list = new HTMLNode("ul");
- list.addChild(new HTMLNode("li", "Own Identities : " +
wot.getNbOwnIdentities()));
- list.addChild(new HTMLNode("li", "Known Identities : " +
wot.getNbIdentities()));
+ list.addChild(new HTMLNode("li", "Own Identities : " +
OwnIdentity.getNbOwnIdentities(db)));
+ list.addChild(new HTMLNode("li", "Known Identities : " +
Identity.getNbIdentities(db)));
list.addChild(new HTMLNode("li", "Trust relationships : " +
Trust.getNb(db)));
list.addChild(new HTMLNode("li", "Scores : " +
Score.getNb(db)));
@@ -75,7 +73,7 @@
HTMLNode boxContent = pm.getContentNode(box);
- ObjectSet<OwnIdentity> ownIdentities = OwnIdentity.getAll(db);
+ ObjectSet<OwnIdentity> ownIdentities =
OwnIdentity.getAllOwnIdentities(db);
if(ownIdentities.size() == 0) {
boxContent.addChild("p", "You have no own identity yet,
you should create one...");
}
@@ -170,7 +168,7 @@
HTMLNode listBox = pm.getInfobox("Known Identities");
HTMLNode listBoxContent = pm.getContentNode(listBox);
- int nbOwnIdentities = wot.getNbOwnIdentities();
+ int nbOwnIdentities = OwnIdentity.getNbOwnIdentities(db);
if (nbOwnIdentities == 0) {
listBoxContent.addChild("p", "You should create an
identity first...");
@@ -178,12 +176,12 @@
return pageNode.generate();
}
else if (nbOwnIdentities == 1) {
- treeOwner = OwnIdentity.getAll(db).next();
+ treeOwner = OwnIdentity.getAllOwnIdentities(db).next();
}
else {
// Get the identity the user asked for, or the first
one if he didn't
if(owner.equals("")) {
- treeOwner = OwnIdentity.getAll(db).next();
+ treeOwner =
OwnIdentity.getAllOwnIdentities(db).next();
}
else {
try {
@@ -197,7 +195,7 @@
HTMLNode selectForm = pr.addFormChild(listBoxContent,
SELF_URI + "/viewTree", "selectForm");
HTMLNode selectBox = selectForm.addChild("select",
"name", "ownerURI");
- ObjectSet<OwnIdentity> ownIdentities =
wot.getOwnIdentities();
+ ObjectSet<OwnIdentity> ownIdentities =
OwnIdentity.getAllOwnIdentities(db);
while(ownIdentities.hasNext()) {
OwnIdentity ownIdentity = ownIdentities.next();
if(ownIdentity == treeOwner) {
@@ -219,7 +217,7 @@
row.addChild("th", "Score (Rank)");
row.addChild("th", "Trust/Comment");
- ObjectSet<Identity> identities = wot.getAllIdentities();
+ ObjectSet<Identity> identities = Identity.getAllIdentities(db);
while(identities.hasNext()) {
Identity id = identities.next();
Modified: trunk/plugins/WoT/src/plugins/WoT/WoT.java
===================================================================
--- trunk/plugins/WoT/src/plugins/WoT/WoT.java 2008-09-07 12:15:13 UTC (rev
22521)
+++ trunk/plugins/WoT/src/plugins/WoT/WoT.java 2008-09-07 12:22:24 UTC (rev
22522)
@@ -3,72 +3,654 @@
* 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.FileNotFoundException;
+import java.io.IOException;
import java.net.MalformedURLException;
+import java.util.Iterator;
+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;
-import com.db4o.query.Query;
+import com.db4o.ext.DatabaseClosedException;
+import com.db4o.ext.Db4oIOException;
+import freenet.client.FetchException;
+import freenet.client.HighLevelSimpleClient;
+import freenet.client.InsertException;
+import freenet.keys.FreenetURI;
+import freenet.l10n.L10n.LANGUAGE;
+import freenet.pluginmanager.FredPlugin;
+import freenet.pluginmanager.FredPluginFCP;
+import freenet.pluginmanager.FredPluginHTTP;
+import freenet.pluginmanager.FredPluginL10n;
+import freenet.pluginmanager.FredPluginThreadless;
+import freenet.pluginmanager.FredPluginVersioned;
+import freenet.pluginmanager.PluginHTTPException;
+import freenet.pluginmanager.PluginReplySender;
+import freenet.pluginmanager.PluginRespirator;
+import freenet.support.Logger;
+import freenet.support.SimpleFieldSet;
+import freenet.support.api.Bucket;
+import freenet.support.api.HTTPRequest;
+
/**
- * The Web of Trust
- *
* @author Julien Cornuwel (batosai at freenetproject.org)
- *
*/
-public class WoT {
+public class WoT implements FredPlugin, FredPluginHTTP, FredPluginThreadless,
FredPluginFCP, FredPluginVersioned, FredPluginL10n {
+ private PluginRespirator pr;
+ private HighLevelSimpleClient client;
+
+ private ObjectContainer db;
+ private WebInterface web;
+ private IdentityInserter inserter;
+ private IdentityFetcher fetcher;
+ private String seedURI = "USK at
MF2Vc6FRgeFMZJ0s2l9hOop87EYWAydUZakJzL0OfV8,fQeN-RMQZsUrDha2LCJWOMFk1-EiXZxfTnBT8NEgY00,AQACAAE/WoT/0";
+ private Identity seed;
+ private Config config;
+ public static String SELF_URI = "/plugins/plugins.WoT.WoTplugin";
+
+ public void runPlugin(PluginRespirator pr) {
+
+ Logger.debug(this, "Start");
+
+ // Init
+ this.pr = pr;
+ db = Db4o.openFile("WoT.db4o");
+ client = pr.getHLSimpleClient();
+ web = new WebInterface(pr, db, client, SELF_URI);
+
+ ObjectSet<Config> result = db.queryByExample(Config.class);
+ if(result.size() == 0) {
+ Logger.debug(this, "Created new config");
+ config = new Config(db);
+ db.store(config);
+ }
+ else {
+ Logger.debug(this, "Loaded config");
+ config = result.next();
+ config.initDefault(false);
+ }
+
+ // Create the seed Identity if it doesn't exist
+ try {
+ seed = Identity.getByURI(db, seedURI);
+ } catch (UnknownIdentityException e) {
+ try {
+ seed = new Identity(seedURI, "Fetching seed
identity...", "true", "bootstrap");
+ } catch (Exception e1) {
+ Logger.error(this, "Seed identity error", e);
+ return;
+ }
+ db.store(seed);
+ db.commit();
+ } catch (Exception e) {
+ Logger.error(this, "Seed identity error", e);
+ return;
+ }
+
+ // Start the inserter thread
+ inserter = new IdentityInserter(db, client,
pr.getNode().clientCore.tempBucketFactory);
+ pr.getNode().executor.execute(inserter, "WoTinserter");
+
+ // Create the fetcher
+ fetcher = new IdentityFetcher(db, client);
+
+ // Try to fetch all known identities
+ ObjectSet<Identity> identities = Identity.getAllIdentities(db);
+ while (identities.hasNext()) {
+ fetcher.fetch(identities.next(), true);
+ }
+ }
- ObjectContainer db;
+ public void terminate() {
+ inserter.stop();
+ fetcher.stop();
+ db.commit();
+ db.close();
+ }
- /**
- * Implements a variation of the Advogato Trust Metric
- * The only difference is that we allow negative trust to avoid giving
- * capacity to people we dislike...
+ public String handleHTTPGet(HTTPRequest request) throws
PluginHTTPException {
+
+ String page = request.getPath().substring(SELF_URI.length());
+
+ try {
+ if(page.equals("/ownidentities")) {
+ return web.makeOwnIdentitiesPage();
+ }
+ if(page.equals("/knownidentities")) {
+ return web.makeKnownIdentitiesPage();
+ }
+ if(page.equals("/wotTestDrive")) {
+ return web.makeTestWarningPage();
+ }
+ else {
+ return web.makeHomePage();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ return e.getMessage();
+ }
+ }
+
+ public String handleHTTPPost(HTTPRequest request) throws
PluginHTTPException {
+
+ String pass = request.getPartAsString("formPassword", 32);
+ if ((pass.length() == 0) ||
!pass.equals(pr.getNode().clientCore.formPassword)) {
+ return "Buh! Invalid form password";
+ }
+
+ String page = request.getPath().substring(SELF_URI.length());
+
+ try {
+ if(page.equals("/createIdentity")) {
+ return web.makeCreateIdentityPage(request);
+ }
+ else if(page.equals("/createIdentity2")) {
+ createIdentity(request);
+ return web.makeOwnIdentitiesPage();
+ }
+ else if(page.equals("/addIdentity")) {
+ addIdentity(request);
+ return web.makeKnownIdentitiesPage();
+ }
+ else if(page.equals("/viewTree")) {
+ return web.makeKnownIdentitiesPage(request);
+ }
+ else if (page.equals("/setTrust")) {
+ setTrust(request);
+ return
web.makeKnownIdentitiesPage(request.getPartAsString("truster", 1024));
+ }
+ else if(page.equals("/editIdentity")) {
+ return
web.makeEditIdentityPage(request.getPartAsString("id", 1024));
+ }
+ else if(page.equals("/restoreIdentity")) {
+ //
restoreIdentity(request.getPartAsString("requestURI", 1024),
request.getPartAsString("insertURI", 1024));
+ return web.makeOwnIdentitiesPage();
+ }
+ else {
+ return web.makeHomePage();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ return e.getLocalizedMessage();
+ }
+ }
+
+ // TODO This is OwnIdentity's job, move it there
+
+ /*
+ private void restoreIdentity(String requestURI, String insertURI)
throws InvalidParameterException, MalformedURLException, Db4oIOException,
DatabaseClosedException, DuplicateScoreException, DuplicateIdentityException {
+
+ OwnIdentity id;
+
+ try {
+ Identity old = Identity.getByURI(db, requestURI);
+
+ // We already have fetched this identity as a
stranger's one. We need to update the database.
+ id = new OwnIdentity(insertURI, requestURI,
old.getLastChange(), new Date(), old.getNickName(), old.doesPublishTrustList()
? "true" : "false");
+ id.setContexts(old.getContexts(), db);
+ id.setProps(old.getProps(), db);
+
+ // Update all received trusts
+ ObjectSet<Trust> receivedTrusts = db.queryByExample(new
Trust(null, old, 0));
+ while(receivedTrusts.hasNext()) {
+ Trust receivedTrust = receivedTrusts.next();
+ receivedTrust.setTrustee(id);
+ db.store(receivedTrust);
+ }
+
+ // Update all received scores
+ ObjectSet<Score> scores = db.queryByExample(new
Score(null, old, 0, 0, 0));
+ while(scores.hasNext()) {
+ Score score = scores.next();
+ score.setTarget(id);
+ db.store(score);
+ }
+
+ // Initialize the trust tree
+ Score score = new Score(id, id, 100, 0, 100);
+ db.store(score);
+
+ // Store the new identity
+ db.store(id);
+
+ // Update all given trusts
+ ObjectSet<Trust> givenTrusts = db.queryByExample(new
Trust(old, null, 0));
+ while(givenTrusts.hasNext()) {
+ Trust givenTrust = givenTrusts.next();
+ givenTrust.setTruster(id);
+ wot.setTrust(givenTrust);
+ db.delete(givenTrust);
+ }
+
+ // Remove the old identity
+ db.delete(old);
+
+ Logger.debug(this, "Successfully restored an already
known identity from Freenet (" + id.getNickName() + ")");
+
+ } catch (UnknownIdentityException e) {
+ id = new OwnIdentity(insertURI, requestURI, new Date(),
new Date(0), "Restore in progress...", "false");
+
+ // Initialize the trust tree
+ Score score = new Score(id, id, 100, 0, 100);
+ db.store(score);
+
+ // Store the new identity
+ db.store(id);
+
+ Logger.debug(this, "Trying to restore a not-yet-known
identity from Freenet (" + id.getRequestURI() + ")");
+ }
+
+ db.commit();
+
+ fetcher.fetch(id);
+ }
*/
- public WoT(ObjectContainer db) {
- this.db = db;
+
+ private void setTrust(HTTPRequest request) throws
NumberFormatException, TransformerConfigurationException,
FileNotFoundException, InvalidParameterException, UnknownIdentityException,
ParserConfigurationException, TransformerException, IOException,
InsertException, Db4oIOException, DatabaseClosedException,
DuplicateScoreException, DuplicateIdentityException, NotTrustedException,
DuplicateTrustException {
+
+ setTrust( request.getPartAsString("truster", 1024),
+ request.getPartAsString("trustee",
1024),
+ request.getPartAsString("value", 1024),
+ request.getPartAsString("comment",
1024));
}
+
+ private void setTrust(String truster, String trustee, String value,
String comment) throws InvalidParameterException, UnknownIdentityException,
NumberFormatException, TransformerConfigurationException,
FileNotFoundException, ParserConfigurationException, TransformerException,
IOException, InsertException, Db4oIOException, DatabaseClosedException,
DuplicateScoreException, DuplicateIdentityException, NotTrustedException,
DuplicateTrustException {
- public int getNbOwnIdentities() {
- return db.queryByExample(OwnIdentity.class).size();
+ OwnIdentity trusterId = OwnIdentity.getByURI(db, truster);
+ Identity trusteeId = Identity.getByURI(db, trustee);
+
+ setTrust((OwnIdentity)trusterId, trusteeId,
Integer.parseInt(value), comment);
+}
+
+ private void setTrust(OwnIdentity truster, Identity trustee, int value,
String comment) throws TransformerConfigurationException,
FileNotFoundException, ParserConfigurationException, TransformerException,
IOException, InsertException, Db4oIOException, DatabaseClosedException,
InvalidParameterException, DuplicateScoreException, NotTrustedException,
DuplicateTrustException {
+
+ truster.setTrust(db, trustee, value, comment);
+ truster.updated();
+ db.store(truster);
+ db.commit();
}
- public int getNbIdentities() {
- return db.queryByExample(Identity.class).size() -
getNbOwnIdentities();
+ private void addIdentity(HTTPRequest request) throws
MalformedURLException, InvalidParameterException, FetchException,
DuplicateIdentityException {
+ addIdentity(request.getPartAsString("identityURI",
1024).trim());
}
- public ObjectSet<OwnIdentity> getOwnIdentities() {
- return db.queryByExample(OwnIdentity.class);
+ private Identity addIdentity(String requestURI) throws
MalformedURLException, InvalidParameterException, FetchException,
DuplicateIdentityException {
+ Identity identity = null;
+ try {
+ identity = Identity.getByURI(db, requestURI);
+ Logger.error(this, "Tried to manually add an identity
we already know, ignored.");
+ throw new InvalidParameterException("We already have
this identity");
+ }
+ catch (UnknownIdentityException e) {
+ // TODO Only add the identity after it is successfully
fetched
+ /*
+ identity = new Identity(requestURI, new
Date(0), "Not found yet...", "false");
+ db.store(identity);
+ db.commit();
+ Logger.debug(this, "Trying to fetch manually
added identity (" + identity.getRequestURI() + ")");
+ fetcher.fetch(identity);
+ */
+ }
+ return identity;
}
+
+ private OwnIdentity createIdentity(HTTPRequest request) throws
TransformerConfigurationException, FileNotFoundException,
InvalidParameterException, ParserConfigurationException, TransformerException,
IOException, InsertException, Db4oIOException, DatabaseClosedException,
DuplicateScoreException, NotTrustedException, DuplicateTrustException {
+
+ return createIdentity(
request.getPartAsString("insertURI",1024),
+
request.getPartAsString("requestURI",1024),
+
request.getPartAsString("nickName", 1024),
+
request.getPartAsString("publishTrustList", 1024),
+ "testing");
+ }
- public ObjectSet<Identity> getAllIdentities() {
- return db.queryByExample(Identity.class);
+ private OwnIdentity createIdentity(String nickName, String
publishTrustList, String context) throws TransformerConfigurationException,
FileNotFoundException, InvalidParameterException, ParserConfigurationException,
TransformerException, IOException, InsertException, Db4oIOException,
DatabaseClosedException, DuplicateScoreException, NotTrustedException,
DuplicateTrustException {
+
+ FreenetURI[] keypair = client.generateKeyPair("WoT");
+ return createIdentity(keypair[0].toString(),
keypair[1].toString(), nickName, publishTrustList, context);
}
+
+ private OwnIdentity createIdentity(String insertURI, String requestURI,
String nickName, String publishTrustList, String context) throws
InvalidParameterException, TransformerConfigurationException,
FileNotFoundException, ParserConfigurationException, TransformerException,
IOException, InsertException, Db4oIOException, DatabaseClosedException,
DuplicateScoreException, NotTrustedException, DuplicateTrustException {
+
+ // TODO Add context in the creation form
+ OwnIdentity identity = new OwnIdentity(insertURI, requestURI,
nickName, publishTrustList, "testing");
+ db.store(identity);
+ identity.initTrustTree(db);
+
+ // This identity has capacity on its own trust tree
+ Score score = new Score(identity, identity, 100, 0, 100);
+ db.store(score);
- @SuppressWarnings("unchecked")
- public ObjectSet<Score> getIdentitiesByScore (String owner, String
select) throws InvalidParameterException, MalformedURLException,
UnknownIdentityException, DuplicateIdentityException {
+ // This identity trusts the seed identity
+ identity.setTrust(db, seed, 100, "I trust the WoT plugin");
- OwnIdentity treeOwner = OwnIdentity.getByURI(db, owner);
+ Logger.debug(this, "Successfully created a new OwnIdentity (" +
identity.getNickName() + ")");
+
+ db.commit();
+
+ return identity;
+ }
+
+ private void addContext(String identity, String context) throws
InvalidParameterException, MalformedURLException, UnknownIdentityException,
DuplicateIdentityException {
+ Identity id = OwnIdentity.getByURI(db, identity);
+ id.addContext(context, db);
+ db.store(id);
- Query query = db.query();
- query.constrain(Score.class);
- query.descend("treeOwner").constrain(treeOwner);
+ Logger.debug(this, "Added context '" + context + "' to identity
'" + id.getNickName() + "'");
+ }
+
+ private void removeContext(String identity, String context) throws
InvalidParameterException, MalformedURLException, UnknownIdentityException,
DuplicateIdentityException {
+ Identity id = OwnIdentity.getByURI(db, identity);
+ id.removeContext(context, db);
+ db.store(id);
- if(select.equals("+")) {
- query.descend("score").constrain(new
Integer(0)).greater();
+ Logger.debug(this, "Removed context '" + context + "' from
identity '" + id.getNickName() + "'");
+ }
+
+ private void setProperty(String identity, String property, String
value) throws InvalidParameterException, MalformedURLException,
UnknownIdentityException, DuplicateIdentityException {
+ Identity id = OwnIdentity.getByURI(db, identity);
+ id.setProp(property, value, db);
+ db.store(id);
+
+ Logger.debug(this, "Added property '" + property + "=" + value
+ "' to identity '" + id.getNickName() + "'");
+ }
+
+ private String getProperty(String identity, String property) throws
InvalidParameterException, MalformedURLException, UnknownIdentityException,
DuplicateIdentityException {
+ return Identity.getByURI(db, identity).getProp(property);
+ }
+
+ private void removeProperty(String identity, String property) throws
InvalidParameterException, MalformedURLException, UnknownIdentityException,
DuplicateIdentityException {
+ Identity id = OwnIdentity.getByURI(db, identity);
+ id.removeProp(property, db);
+ db.store(id);
+
+ Logger.debug(this, "Removed property '" + property + "' from
identity '" + id.getNickName() + "'");
+ }
+
+ public String handleHTTPPut(HTTPRequest request) throws
PluginHTTPException {
+ return null;
+ }
+
+ public String getVersion() {
+ return "0.3.0 r"+Version.getSvnRevision();
+ }
+
+ public void handle(PluginReplySender replysender, SimpleFieldSet
params, Bucket data, int accesstype) {
+
+ try {
+ if(params.get("Message").equals("CreateIdentity")) {
+ replysender.send(handleCreateIdentity(params),
data);
+ }
+ else if(params.get("Message").equals("SetTrust")) {
+ replysender.send(handleSetTrust(params), data);
+ }
+ else if(params.get("Message").equals("AddIdentity")) {
+ replysender.send(handleAddIdentity(params),
data);
+ }
+ else if(params.get("Message").equals("GetIdentity")) {
+ replysender.send(handleGetIdentity(params),
data);
+ }
+ else
if(params.get("Message").equals("GetIdentitiesByScore")) {
+
replysender.send(handleGetIdentitiesByScore(params), data);
+ }
+ else if(params.get("Message").equals("GetTrusters")) {
+ replysender.send(handleGetTrusters(params),
data);
+ }
+ else if(params.get("Message").equals("GetTrustees")) {
+ replysender.send(handleGetTrustees(params),
data);
+ }
+ else if(params.get("Message").equals("AddContext")) {
+ replysender.send(handleAddContext(params),
data);
+ }
+ else if(params.get("Message").equals("RemoveContext")) {
+ replysender.send(handleRemoveContext(params),
data);
+ }
+ else if(params.get("Message").equals("SetProperty")) {
+ replysender.send(handleSetProperty(params),
data);
+ }
+ else if(params.get("Message").equals("GetProperty")) {
+ replysender.send(handleGetProperty(params),
data);
+ }
+ else if(params.get("Message").equals("RemoveProperty"))
{
+ replysender.send(handleRemoveProperty(params),
data);
+ }
+ else {
+ throw new Exception("Unknown message (" +
params.get("Message") + ")");
+ }
}
- else if(select.equals("0")) {
- query.descend("score").constrain(new Integer(0));
+ catch (Exception e) {
+ replysender.send(errorMessageFCP(e), data);
}
- else if(select.equals("-")) {
- query.descend("score").constrain(new
Integer(0)).smaller();
+ }
+
+ private SimpleFieldSet handleCreateIdentity(SimpleFieldSet params)
throws TransformerConfigurationException, FileNotFoundException,
InvalidParameterException, ParserConfigurationException, TransformerException,
IOException, InsertException, Db4oIOException, DatabaseClosedException,
DuplicateScoreException, NotTrustedException, DuplicateTrustException {
+
+ SimpleFieldSet sfs = new SimpleFieldSet(false);
+ OwnIdentity identity;
+
+ if(params.get("NickName")==null ||
params.get("PublishTrustList")==null || params.get("Context")==null) throw new
InvalidParameterException("Missing mandatory parameter");
+
+ if(params.get("RequestURI")==null ||
params.get("InsertURI")==null) {
+ identity = createIdentity(params.get("NickName"),
params.get("PublishTrustList"), params.get("Context"));
}
- else throw new InvalidParameterException("Unhandled select
value ("+select+")");
+ else {
+ identity = createIdentity( params.get("InsertURI"),
+
params.get("RequestURI"),
+
params.get("NickName"),
+
params.get("PublishTrustList"),
+
params.get("Context"));
+ }
+ sfs.putAppend("Message", "IdentityCreated");
+ sfs.putAppend("InsertURI", identity.getInsertURI().toString());
+ sfs.putAppend("RequestURI",
identity.getRequestURI().toString());
+ return sfs;
+ }
- return query.execute();
+ private SimpleFieldSet handleSetTrust(SimpleFieldSet params) throws
NumberFormatException, TransformerConfigurationException,
FileNotFoundException, InvalidParameterException, ParserConfigurationException,
TransformerException, IOException, InsertException, UnknownIdentityException,
Db4oIOException, DatabaseClosedException, DuplicateScoreException,
DuplicateIdentityException, NotTrustedException, DuplicateTrustException {
+
+ SimpleFieldSet sfs = new SimpleFieldSet(false);
+
+ if(params.get("Truster") == null || params.get("Trustee") ==
null || params.get("Value") == null || params.get("Comment") == null) throw new
InvalidParameterException("Missing mandatory parameter");
+
+ setTrust(params.get("Truster"), params.get("Trustee"),
params.get("Value"), params.get("Comment"));
+
+ sfs.putAppend("Message", "TrustSet");
+ return sfs;
}
+
+ private SimpleFieldSet handleAddIdentity(SimpleFieldSet params) throws
InvalidParameterException, MalformedURLException, FetchException,
DuplicateIdentityException {
+
+ SimpleFieldSet sfs = new SimpleFieldSet(false);
+
+ if(params.get("RequestURI") == null) throw new
InvalidParameterException("Missing mandatory parameter");
+
+ Identity identity =
addIdentity(params.get("RequestURI").trim());
+
+ sfs.putAppend("Message", "IdentityAdded");
+ sfs.putAppend("RequestURI",
identity.getRequestURI().toString());
+ return sfs;
+ }
+
+ private SimpleFieldSet handleGetIdentity(SimpleFieldSet params) throws
InvalidParameterException, MalformedURLException, FetchException,
UnknownIdentityException, DuplicateScoreException, DuplicateIdentityException,
DuplicateTrustException {
+
+ SimpleFieldSet sfs = new SimpleFieldSet(false);
+
+ if(params.get("TreeOwner") == null || params.get("Identity") ==
null) throw new InvalidParameterException("Missing mandatory parameter");
+
+ sfs.putAppend("Message", "Identity");
+
+ OwnIdentity treeOwner = OwnIdentity.getByURI(db,
params.get("TreeOwner"));
+ Identity identity = Identity.getByURI(db,
params.get("Identity"));
+
+ try {
+ Trust trust = identity.getReceivedTrust(treeOwner, db);
+ sfs.putAppend("Trust",
String.valueOf(trust.getValue()));
+ } catch (NotTrustedException e1) {
+ sfs.putAppend("Trust", "null");
+ }
+
+ Score score;
+ try {
+ score = identity.getScore(treeOwner, db);
+ sfs.putAppend("Score",
String.valueOf(score.getScore()));
+ sfs.putAppend("Rank", String.valueOf(score.getRank()));
+ } catch (NotInTrustTreeException e) {
+ sfs.putAppend("Score", "null");
+ sfs.putAppend("Rank", "null");
+ }
+
+ Iterator<String> contexts = identity.getContexts();
+ for(int i = 1 ; contexts.hasNext() ; i++)
sfs.putAppend("Context"+i, contexts.next());
+
+ return sfs;
+ }
+
+ private SimpleFieldSet handleGetIdentitiesByScore(SimpleFieldSet
params) throws InvalidParameterException, MalformedURLException,
UnknownIdentityException, DuplicateIdentityException {
+
+ SimpleFieldSet sfs = new SimpleFieldSet(false);
+
+ if(params.get("TreeOwner") == null || params.get("Select") ==
null || params.get("Context") == null) throw new
InvalidParameterException("Missing mandatory parameter");
+
+ sfs.putAppend("Message", "Identities");
+
+ ObjectSet<Score> result =
Score.getIdentitiesByScore(params.get("TreeOwner"),
params.get("Select").trim(), db);
+ for(int i = 1 ; result.hasNext() ; i++) {
+ Score score = result.next();
+ // Maybe there is a way to do this through SODA
+ if(score.getTarget().hasContext(params.get("Context"))
|| params.get("Context").equals("all"))
+ sfs.putAppend("Identity"+i,
score.getTarget().getRequestURI().toString());
+ }
+ return sfs;
+ }
+
+ private SimpleFieldSet handleGetTrusters(SimpleFieldSet params) throws
InvalidParameterException, MalformedURLException, UnknownIdentityException,
Db4oIOException, DatabaseClosedException, DuplicateIdentityException {
+
+ SimpleFieldSet sfs = new SimpleFieldSet(false);
+
+ if(params.get("Identity") == null || params.get("Context") ==
null) throw new InvalidParameterException("Missing mandatory parameter");
+
+ sfs.putAppend("Message", "Identities");
+
+ ObjectSet<Trust> result = Identity.getByURI(db,
params.get("Identity")).getReceivedTrusts(db);
+
+ for(int i = 1 ; result.hasNext() ; i++) {
+ Trust trust = result.next();
+ // Maybe there is a way to do this through SODA
+ if(trust.getTruster().hasContext(params.get("Context"))
|| params.get("Context").equals("all")) {
+ sfs.putAppend("Identity"+i,
trust.getTruster().getRequestURI().toString());
+ sfs.putAppend("Value"+i,
String.valueOf(trust.getValue()));
+ sfs.putAppend("Comment"+i, trust.getComment());
+ }
+ }
+ return sfs;
+ }
+
+ private SimpleFieldSet handleGetTrustees(SimpleFieldSet params) throws
InvalidParameterException, MalformedURLException, UnknownIdentityException,
Db4oIOException, DatabaseClosedException, DuplicateIdentityException {
+
+ SimpleFieldSet sfs = new SimpleFieldSet(false);
+
+ if(params.get("Identity") == null || params.get("Context") ==
null) throw new InvalidParameterException("Missing mandatory parameter");
+
+ sfs.putAppend("Message", "Identities");
+
+ ObjectSet<Trust> result = Identity.getByURI(db,
params.get("Identity")).getGivenTrusts(db);
+
+ for(int i = 1 ; result.hasNext() ; i++) {
+ Trust trust = result.next();
+ // Maybe there is a way to do this through SODA
+ if(trust.getTrustee().hasContext(params.get("Context"))
|| params.get("Context").equals("all")) {
+ sfs.putAppend("Identity"+i,
trust.getTrustee().getRequestURI().toString());
+ sfs.putAppend("Value"+i,
String.valueOf(trust.getValue()));
+ sfs.putAppend("Comment"+i, trust.getComment());
+ }
+ }
+ return sfs;
+ }
+
+ private SimpleFieldSet handleAddContext(SimpleFieldSet params) throws
InvalidParameterException, MalformedURLException, UnknownIdentityException,
DuplicateIdentityException {
+
+ SimpleFieldSet sfs = new SimpleFieldSet(false);
+
+ if(params.get("Identity") == null || params.get("Context") ==
null) throw new InvalidParameterException("Missing mandatory parameter");
+
+ addContext(params.get("Identity"), params.get("Context"));
+
+ sfs.putAppend("Message", "ContextAdded");
+ return sfs;
+ }
+
+ private SimpleFieldSet handleRemoveContext(SimpleFieldSet params)
throws InvalidParameterException, MalformedURLException,
UnknownIdentityException, DuplicateIdentityException {
+
+ SimpleFieldSet sfs = new SimpleFieldSet(false);
+
+ if(params.get("Identity") == null || params.get("Context") ==
null) throw new InvalidParameterException("Missing mandatory parameter");
+
+ removeContext(params.get("Identity"), params.get("Context"));
+
+ sfs.putAppend("Message", "ContextRemoved");
+ return sfs;
+ }
+
+ private SimpleFieldSet handleSetProperty(SimpleFieldSet params) throws
InvalidParameterException, MalformedURLException, UnknownIdentityException,
DuplicateIdentityException {
+
+ SimpleFieldSet sfs = new SimpleFieldSet(false);
+
+ if(params.get("Identity") == null || params.get("Property") ==
null || params.get("Value") == null) throw new
InvalidParameterException("Missing mandatory parameter");
+
+ setProperty(params.get("Identity"), params.get("Property"),
params.get("Value"));
+
+ sfs.putAppend("Message", "PropertyAdded");
+ return sfs;
+ }
+
+ private SimpleFieldSet handleGetProperty(SimpleFieldSet params) throws
InvalidParameterException, MalformedURLException, UnknownIdentityException,
DuplicateIdentityException {
+
+ SimpleFieldSet sfs = new SimpleFieldSet(false);
+
+ if(params.get("Identity") == null || params.get("Property") ==
null) throw new InvalidParameterException("Missing mandatory parameter");
+
+ sfs.putAppend("Message", "PropertyValue");
+ sfs.putAppend("Property", getProperty(params.get("Identity"),
params.get("Property")));
+
+ return sfs;
+ }
+
+ private SimpleFieldSet handleRemoveProperty(SimpleFieldSet params)
throws InvalidParameterException, MalformedURLException,
UnknownIdentityException, DuplicateIdentityException {
+
+ SimpleFieldSet sfs = new SimpleFieldSet(false);
+
+ if(params.get("Identity") == null || params.get("Property") ==
null) throw new InvalidParameterException("Missing mandatory parameter");
+
+ removeProperty(params.get("Identity"), params.get("Property"));
+
+ sfs.putAppend("Message", "PropertyRemoved");
+ return sfs;
+ }
+
+ private SimpleFieldSet errorMessageFCP (Exception e) {
+
+ SimpleFieldSet sfs = new SimpleFieldSet(false);
+ sfs.putAppend("Message", "Error");
+ sfs.putAppend("Description", (e.getLocalizedMessage() == null)
? "null" : e.getLocalizedMessage());
+ e.printStackTrace();
+ return sfs;
+ }
+
+ public String getString(String key) {
+ // TODO Auto-generated method stub
+ return key;
+ }
+
+ public void setLanguage(LANGUAGE newLanguage) {
+ // TODO Auto-generated method stub
+ }
}
-
Modified: trunk/plugins/WoT/src/plugins/WoT/WoTTest.java
===================================================================
--- trunk/plugins/WoT/src/plugins/WoT/WoTTest.java 2008-09-07 12:15:13 UTC
(rev 22521)
+++ trunk/plugins/WoT/src/plugins/WoT/WoTTest.java 2008-09-07 12:22:24 UTC
(rev 22522)
@@ -20,7 +20,6 @@
public class WoTTest extends TestCase {
private ObjectContainer db;
- private WoT wot;
private String uriA = "USK at
MF2Vc6FRgeFMZJ0s2l9hOop87EYWAydUZakJzL0OfV8,fQeN-RMQZsUrDha2LCJWOMFk1-EiXZxfTnBT8NEgY00,AQACAAE/WoT/0";
private String uriB = "USK at
R3Lp2s4jdX-3Q96c0A9530qg7JsvA9vi2K0hwY9wG-4,ipkgYftRpo0StBlYkJUawZhg~SO29NZIINseUtBhEfE,AQACAAE/WoT/0";
@@ -34,7 +33,6 @@
super.setUp();
db = Db4o.openFile("wotTest.db4o");
- wot = new WoT(db);
}
protected void tearDown() throws Exception {
@@ -48,8 +46,8 @@
db.store(a);
a.initTrustTree(db);
- assertTrue(wot.getNbIdentities() == 0);
- assertTrue(wot.getNbOwnIdentities() == 1);
+ assertTrue(Identity.getNbIdentities(db) == 0);
+ assertTrue(OwnIdentity.getNbOwnIdentities(db) == 1);
assertTrue(Trust.getNb(db) == 0);
assertTrue(Score.getNb(db) == 1);
@@ -74,8 +72,8 @@
// With A's trust tree not initialized, B shouldn't get a Score.
a.setTrust(db, b, 10, "Foo");
- assertTrue(wot.getNbIdentities() == 1);
- assertTrue(wot.getNbOwnIdentities() == 1);
+ assertTrue(Identity.getNbIdentities(db) == 1);
+ assertTrue(OwnIdentity.getNbOwnIdentities(db) == 1);
assertTrue(Trust.getNb(db) == 1);
assertTrue(Score.getNb(db) == 0);
@@ -84,8 +82,8 @@
a.setTrust(db, b, 100, "Foo");
// Check we have the correct number of objects
- assertTrue(wot.getNbIdentities() == 1);
- assertTrue(wot.getNbOwnIdentities() == 1);
+ assertTrue(Identity.getNbIdentities(db) == 1);
+ assertTrue(OwnIdentity.getNbOwnIdentities(db) == 1);
assertTrue(Trust.getNb(db) == 1);
assertTrue(Score.getNb(db) == 2);
@@ -109,8 +107,8 @@
a.setTrust(db, b, 50, "Bar");
// Check we have the correct number of objects
- assertTrue(wot.getNbIdentities() == 1);
- assertTrue(wot.getNbOwnIdentities() == 1);
+ assertTrue(Identity.getNbIdentities(db) == 1);
+ assertTrue(OwnIdentity.getNbOwnIdentities(db) == 1);
assertTrue(Trust.getNb(db) == 1);
assertTrue(Score.getNb(db) == 2);
@@ -147,8 +145,8 @@
b.setTrust(db, c, 50, "Bar");
// Check we have the correct number of objects
- assertTrue(wot.getNbOwnIdentities() == 1);
- assertTrue(wot.getNbIdentities() == 2);
+ assertTrue(OwnIdentity.getNbOwnIdentities(db) == 1);
+ assertTrue(Identity.getNbIdentities(db) == 2);
assertTrue(Trust.getNb(db) == 2);
assertTrue(Score.getNb(db) == 3);
@@ -170,8 +168,8 @@
a.setTrust(db, b, -1, "Bastard");
// Check we have the correct number of objects
- assertTrue(wot.getNbOwnIdentities() == 1);
- assertTrue(wot.getNbIdentities() == 2);
+ assertTrue(OwnIdentity.getNbOwnIdentities(db) == 1);
+ assertTrue(Identity.getNbIdentities(db) == 2);
assertTrue(Trust.getNb(db) == 2);
assertTrue(Score.getNb(db) == 2);
@@ -211,8 +209,8 @@
c.setTrust(db, b, 50, "Oops");
// Check we have the correct number of objects
- assertTrue(wot.getNbOwnIdentities() == 1);
- assertTrue(wot.getNbIdentities() == 2);
+ assertTrue(OwnIdentity.getNbOwnIdentities(db) == 1);
+ assertTrue(Identity.getNbIdentities(db) == 2);
assertTrue(Trust.getNb(db) == 4);
assertTrue(Score.getNb(db) == 3);
@@ -243,8 +241,8 @@
b.setTrust(db, a, 100, "Bar");
// Check we have the correct number of objects
- assertTrue(wot.getNbOwnIdentities() == 2);
- assertTrue(wot.getNbIdentities() == 0);
+ assertTrue(OwnIdentity.getNbOwnIdentities(db) == 2);
+ assertTrue(Identity.getNbIdentities(db) == 0);
assertTrue(Trust.getNb(db) == 2);
assertTrue(Score.getNb(db) == 4);
Deleted: trunk/plugins/WoT/src/plugins/WoT/WoTplugin.java
===================================================================
--- trunk/plugins/WoT/src/plugins/WoT/WoTplugin.java 2008-09-07 12:15:13 UTC
(rev 22521)
+++ trunk/plugins/WoT/src/plugins/WoT/WoTplugin.java 2008-09-07 12:22:24 UTC
(rev 22522)
@@ -1,658 +0,0 @@
-/**
- * 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.FileNotFoundException;
-import java.io.IOException;
-import java.net.MalformedURLException;
-import java.util.Iterator;
-
-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;
-import com.db4o.ext.DatabaseClosedException;
-import com.db4o.ext.Db4oIOException;
-
-import freenet.client.FetchException;
-import freenet.client.HighLevelSimpleClient;
-import freenet.client.InsertException;
-import freenet.keys.FreenetURI;
-import freenet.l10n.L10n.LANGUAGE;
-import freenet.pluginmanager.FredPlugin;
-import freenet.pluginmanager.FredPluginFCP;
-import freenet.pluginmanager.FredPluginHTTP;
-import freenet.pluginmanager.FredPluginL10n;
-import freenet.pluginmanager.FredPluginThreadless;
-import freenet.pluginmanager.FredPluginVersioned;
-import freenet.pluginmanager.PluginHTTPException;
-import freenet.pluginmanager.PluginReplySender;
-import freenet.pluginmanager.PluginRespirator;
-import freenet.support.Logger;
-import freenet.support.SimpleFieldSet;
-import freenet.support.api.Bucket;
-import freenet.support.api.HTTPRequest;
-
-/**
- * @author Julien Cornuwel (batosai at freenetproject.org)
- */
-public class WoTplugin implements FredPlugin, FredPluginHTTP,
FredPluginThreadless, FredPluginFCP, FredPluginVersioned, FredPluginL10n {
-
- private PluginRespirator pr;
- private HighLevelSimpleClient client;
-
- private ObjectContainer db;
- private WoT wot;
- private WebInterface web;
- private IdentityInserter inserter;
- private IdentityFetcher fetcher;
- private String seedURI = "USK at
MF2Vc6FRgeFMZJ0s2l9hOop87EYWAydUZakJzL0OfV8,fQeN-RMQZsUrDha2LCJWOMFk1-EiXZxfTnBT8NEgY00,AQACAAE/WoT/0";
- private Identity seed;
- private Config config;
-
- public static String SELF_URI = "/plugins/plugins.WoT.WoTplugin";
-
- public void runPlugin(PluginRespirator pr) {
-
- Logger.debug(this, "Start");
-
- // Init
- this.pr = pr;
- db = Db4o.openFile("WoT.db4o");
- wot = new WoT(db);
- client = pr.getHLSimpleClient();
- web = new WebInterface(pr, db, client, SELF_URI, wot);
-
- ObjectSet<Config> result = db.queryByExample(Config.class);
- if(result.size() == 0) {
- Logger.debug(this, "Created new config");
- config = new Config(db);
- db.store(config);
- }
- else {
- Logger.debug(this, "Loaded config");
- config = result.next();
- config.initDefault(false);
- }
-
- // Create the seed Identity if it doesn't exist
- try {
- seed = Identity.getByURI(db, seedURI);
- } catch (UnknownIdentityException e) {
- try {
- seed = new Identity(seedURI, "Fetching seed
identity...", "true", "bootstrap");
- } catch (Exception e1) {
- Logger.error(this, "Seed identity error", e);
- return;
- }
- db.store(seed);
- db.commit();
- } catch (Exception e) {
- Logger.error(this, "Seed identity error", e);
- return;
- }
-
- // Start the inserter thread
- inserter = new IdentityInserter(wot, db, client,
pr.getNode().clientCore.tempBucketFactory);
- pr.getNode().executor.execute(inserter, "WoTinserter");
-
- // Create the fetcher
- fetcher = new IdentityFetcher(db, wot, client);
-
- // Try to fetch all known identities
- ObjectSet<Identity> identities = wot.getAllIdentities();
- while (identities.hasNext()) {
- fetcher.fetch(identities.next(), true);
- }
- }
-
- public void terminate() {
- inserter.stop();
- fetcher.stop();
- db.commit();
- db.close();
- }
-
- public String handleHTTPGet(HTTPRequest request) throws
PluginHTTPException {
-
- String page = request.getPath().substring(SELF_URI.length());
-
- try {
- if(page.equals("/ownidentities")) {
- return web.makeOwnIdentitiesPage();
- }
- if(page.equals("/knownidentities")) {
- return web.makeKnownIdentitiesPage();
- }
- if(page.equals("/wotTestDrive")) {
- return web.makeTestWarningPage();
- }
- else {
- return web.makeHomePage();
- }
- } catch (Exception e) {
- e.printStackTrace();
- return e.getMessage();
- }
- }
-
- public String handleHTTPPost(HTTPRequest request) throws
PluginHTTPException {
-
- String pass = request.getPartAsString("formPassword", 32);
- if ((pass.length() == 0) ||
!pass.equals(pr.getNode().clientCore.formPassword)) {
- return "Buh! Invalid form password";
- }
-
- String page = request.getPath().substring(SELF_URI.length());
-
- try {
- if(page.equals("/createIdentity")) {
- return web.makeCreateIdentityPage(request);
- }
- else if(page.equals("/createIdentity2")) {
- createIdentity(request);
- return web.makeOwnIdentitiesPage();
- }
- else if(page.equals("/addIdentity")) {
- addIdentity(request);
- return web.makeKnownIdentitiesPage();
- }
- else if(page.equals("/viewTree")) {
- return web.makeKnownIdentitiesPage(request);
- }
- else if (page.equals("/setTrust")) {
- setTrust(request);
- return
web.makeKnownIdentitiesPage(request.getPartAsString("truster", 1024));
- }
- else if(page.equals("/editIdentity")) {
- return
web.makeEditIdentityPage(request.getPartAsString("id", 1024));
- }
- else if(page.equals("/restoreIdentity")) {
- //
restoreIdentity(request.getPartAsString("requestURI", 1024),
request.getPartAsString("insertURI", 1024));
- return web.makeOwnIdentitiesPage();
- }
- else {
- return web.makeHomePage();
- }
- } catch (Exception e) {
- e.printStackTrace();
- return e.getLocalizedMessage();
- }
- }
-
- // TODO This is OwnIdentity's job, move it there
-
- /*
- private void restoreIdentity(String requestURI, String insertURI)
throws InvalidParameterException, MalformedURLException, Db4oIOException,
DatabaseClosedException, DuplicateScoreException, DuplicateIdentityException {
-
- OwnIdentity id;
-
- try {
- Identity old = Identity.getByURI(db, requestURI);
-
- // We already have fetched this identity as a
stranger's one. We need to update the database.
- id = new OwnIdentity(insertURI, requestURI,
old.getLastChange(), new Date(), old.getNickName(), old.doesPublishTrustList()
? "true" : "false");
- id.setContexts(old.getContexts(), db);
- id.setProps(old.getProps(), db);
-
- // Update all received trusts
- ObjectSet<Trust> receivedTrusts = db.queryByExample(new
Trust(null, old, 0));
- while(receivedTrusts.hasNext()) {
- Trust receivedTrust = receivedTrusts.next();
- receivedTrust.setTrustee(id);
- db.store(receivedTrust);
- }
-
- // Update all received scores
- ObjectSet<Score> scores = db.queryByExample(new
Score(null, old, 0, 0, 0));
- while(scores.hasNext()) {
- Score score = scores.next();
- score.setTarget(id);
- db.store(score);
- }
-
- // Initialize the trust tree
- Score score = new Score(id, id, 100, 0, 100);
- db.store(score);
-
- // Store the new identity
- db.store(id);
-
- // Update all given trusts
- ObjectSet<Trust> givenTrusts = db.queryByExample(new
Trust(old, null, 0));
- while(givenTrusts.hasNext()) {
- Trust givenTrust = givenTrusts.next();
- givenTrust.setTruster(id);
- wot.setTrust(givenTrust);
- db.delete(givenTrust);
- }
-
- // Remove the old identity
- db.delete(old);
-
- Logger.debug(this, "Successfully restored an already
known identity from Freenet (" + id.getNickName() + ")");
-
- } catch (UnknownIdentityException e) {
- id = new OwnIdentity(insertURI, requestURI, new Date(),
new Date(0), "Restore in progress...", "false");
-
- // Initialize the trust tree
- Score score = new Score(id, id, 100, 0, 100);
- db.store(score);
-
- // Store the new identity
- db.store(id);
-
- Logger.debug(this, "Trying to restore a not-yet-known
identity from Freenet (" + id.getRequestURI() + ")");
- }
-
- db.commit();
-
- fetcher.fetch(id);
- }
- */
-
- private void setTrust(HTTPRequest request) throws
NumberFormatException, TransformerConfigurationException,
FileNotFoundException, InvalidParameterException, UnknownIdentityException,
ParserConfigurationException, TransformerException, IOException,
InsertException, Db4oIOException, DatabaseClosedException,
DuplicateScoreException, DuplicateIdentityException, NotTrustedException,
DuplicateTrustException {
-
- setTrust( request.getPartAsString("truster", 1024),
- request.getPartAsString("trustee",
1024),
- request.getPartAsString("value", 1024),
- request.getPartAsString("comment",
1024));
- }
-
- private void setTrust(String truster, String trustee, String value,
String comment) throws InvalidParameterException, UnknownIdentityException,
NumberFormatException, TransformerConfigurationException,
FileNotFoundException, ParserConfigurationException, TransformerException,
IOException, InsertException, Db4oIOException, DatabaseClosedException,
DuplicateScoreException, DuplicateIdentityException, NotTrustedException,
DuplicateTrustException {
-
- OwnIdentity trusterId = OwnIdentity.getByURI(db, truster);
- Identity trusteeId = Identity.getByURI(db, trustee);
-
- setTrust((OwnIdentity)trusterId, trusteeId,
Integer.parseInt(value), comment);
-}
-
- private void setTrust(OwnIdentity truster, Identity trustee, int value,
String comment) throws TransformerConfigurationException,
FileNotFoundException, ParserConfigurationException, TransformerException,
IOException, InsertException, Db4oIOException, DatabaseClosedException,
InvalidParameterException, DuplicateScoreException, NotTrustedException,
DuplicateTrustException {
-
- truster.setTrust(db, trustee, value, comment);
- truster.updated();
- db.store(truster);
- db.commit();
- }
-
- private void addIdentity(HTTPRequest request) throws
MalformedURLException, InvalidParameterException, FetchException,
DuplicateIdentityException {
- addIdentity(request.getPartAsString("identityURI",
1024).trim());
- }
-
- private Identity addIdentity(String requestURI) throws
MalformedURLException, InvalidParameterException, FetchException,
DuplicateIdentityException {
- Identity identity = null;
- try {
- identity = Identity.getByURI(db, requestURI);
- Logger.error(this, "Tried to manually add an identity
we already know, ignored.");
- throw new InvalidParameterException("We already have
this identity");
- }
- catch (UnknownIdentityException e) {
- // TODO Only add the identity after it is successfully
fetched
- /*
- identity = new Identity(requestURI, new
Date(0), "Not found yet...", "false");
- db.store(identity);
- db.commit();
- Logger.debug(this, "Trying to fetch manually
added identity (" + identity.getRequestURI() + ")");
- fetcher.fetch(identity);
- */
- }
- return identity;
- }
-
- private OwnIdentity createIdentity(HTTPRequest request) throws
TransformerConfigurationException, FileNotFoundException,
InvalidParameterException, ParserConfigurationException, TransformerException,
IOException, InsertException, Db4oIOException, DatabaseClosedException,
DuplicateScoreException, NotTrustedException, DuplicateTrustException {
-
- return createIdentity(
request.getPartAsString("insertURI",1024),
-
request.getPartAsString("requestURI",1024),
-
request.getPartAsString("nickName", 1024),
-
request.getPartAsString("publishTrustList", 1024),
- "testing");
- }
-
- private OwnIdentity createIdentity(String nickName, String
publishTrustList, String context) throws TransformerConfigurationException,
FileNotFoundException, InvalidParameterException, ParserConfigurationException,
TransformerException, IOException, InsertException, Db4oIOException,
DatabaseClosedException, DuplicateScoreException, NotTrustedException,
DuplicateTrustException {
-
- FreenetURI[] keypair = client.generateKeyPair("WoT");
- return createIdentity(keypair[0].toString(),
keypair[1].toString(), nickName, publishTrustList, context);
- }
-
- private OwnIdentity createIdentity(String insertURI, String requestURI,
String nickName, String publishTrustList, String context) throws
InvalidParameterException, TransformerConfigurationException,
FileNotFoundException, ParserConfigurationException, TransformerException,
IOException, InsertException, Db4oIOException, DatabaseClosedException,
DuplicateScoreException, NotTrustedException, DuplicateTrustException {
-
- // TODO Add context in the creation form
- OwnIdentity identity = new OwnIdentity(insertURI, requestURI,
nickName, publishTrustList, "testing");
- db.store(identity);
- identity.initTrustTree(db);
-
- // This identity has capacity on its own trust tree
- Score score = new Score(identity, identity, 100, 0, 100);
- db.store(score);
-
- // This identity trusts the seed identity
- identity.setTrust(db, seed, 100, "I trust the WoT plugin");
-
- Logger.debug(this, "Successfully created a new OwnIdentity (" +
identity.getNickName() + ")");
-
- db.commit();
-
- return identity;
- }
-
- private void addContext(String identity, String context) throws
InvalidParameterException, MalformedURLException, UnknownIdentityException,
DuplicateIdentityException {
- Identity id = OwnIdentity.getByURI(db, identity);
- id.addContext(context, db);
- db.store(id);
-
- Logger.debug(this, "Added context '" + context + "' to identity
'" + id.getNickName() + "'");
- }
-
- private void removeContext(String identity, String context) throws
InvalidParameterException, MalformedURLException, UnknownIdentityException,
DuplicateIdentityException {
- Identity id = OwnIdentity.getByURI(db, identity);
- id.removeContext(context, db);
- db.store(id);
-
- Logger.debug(this, "Removed context '" + context + "' from
identity '" + id.getNickName() + "'");
- }
-
- private void setProperty(String identity, String property, String
value) throws InvalidParameterException, MalformedURLException,
UnknownIdentityException, DuplicateIdentityException {
- Identity id = OwnIdentity.getByURI(db, identity);
- id.setProp(property, value, db);
- db.store(id);
-
- Logger.debug(this, "Added property '" + property + "=" + value
+ "' to identity '" + id.getNickName() + "'");
- }
-
- private String getProperty(String identity, String property) throws
InvalidParameterException, MalformedURLException, UnknownIdentityException,
DuplicateIdentityException {
- return Identity.getByURI(db, identity).getProp(property);
- }
-
- private void removeProperty(String identity, String property) throws
InvalidParameterException, MalformedURLException, UnknownIdentityException,
DuplicateIdentityException {
- Identity id = OwnIdentity.getByURI(db, identity);
- id.removeProp(property, db);
- db.store(id);
-
- Logger.debug(this, "Removed property '" + property + "' from
identity '" + id.getNickName() + "'");
- }
-
- public String handleHTTPPut(HTTPRequest request) throws
PluginHTTPException {
- return null;
- }
-
- public String getVersion() {
- return "0.3.0 r"+Version.getSvnRevision();
- }
-
- public void handle(PluginReplySender replysender, SimpleFieldSet
params, Bucket data, int accesstype) {
-
- try {
- if(params.get("Message").equals("CreateIdentity")) {
- replysender.send(handleCreateIdentity(params),
data);
- }
- else if(params.get("Message").equals("SetTrust")) {
- replysender.send(handleSetTrust(params), data);
- }
- else if(params.get("Message").equals("AddIdentity")) {
- replysender.send(handleAddIdentity(params),
data);
- }
- else if(params.get("Message").equals("GetIdentity")) {
- replysender.send(handleGetIdentity(params),
data);
- }
- else
if(params.get("Message").equals("GetIdentitiesByScore")) {
-
replysender.send(handleGetIdentitiesByScore(params), data);
- }
- else if(params.get("Message").equals("GetTrusters")) {
- replysender.send(handleGetTrusters(params),
data);
- }
- else if(params.get("Message").equals("GetTrustees")) {
- replysender.send(handleGetTrustees(params),
data);
- }
- else if(params.get("Message").equals("AddContext")) {
- replysender.send(handleAddContext(params),
data);
- }
- else if(params.get("Message").equals("RemoveContext")) {
- replysender.send(handleRemoveContext(params),
data);
- }
- else if(params.get("Message").equals("SetProperty")) {
- replysender.send(handleSetProperty(params),
data);
- }
- else if(params.get("Message").equals("GetProperty")) {
- replysender.send(handleGetProperty(params),
data);
- }
- else if(params.get("Message").equals("RemoveProperty"))
{
- replysender.send(handleRemoveProperty(params),
data);
- }
- else {
- throw new Exception("Unknown message (" +
params.get("Message") + ")");
- }
- }
- catch (Exception e) {
- replysender.send(errorMessageFCP(e), data);
- }
- }
-
- private SimpleFieldSet handleCreateIdentity(SimpleFieldSet params)
throws TransformerConfigurationException, FileNotFoundException,
InvalidParameterException, ParserConfigurationException, TransformerException,
IOException, InsertException, Db4oIOException, DatabaseClosedException,
DuplicateScoreException, NotTrustedException, DuplicateTrustException {
-
- SimpleFieldSet sfs = new SimpleFieldSet(false);
- OwnIdentity identity;
-
- if(params.get("NickName")==null ||
params.get("PublishTrustList")==null || params.get("Context")==null) throw new
InvalidParameterException("Missing mandatory parameter");
-
- if(params.get("RequestURI")==null ||
params.get("InsertURI")==null) {
- identity = createIdentity(params.get("NickName"),
params.get("PublishTrustList"), params.get("Context"));
- }
- else {
- identity = createIdentity( params.get("InsertURI"),
-
params.get("RequestURI"),
-
params.get("NickName"),
-
params.get("PublishTrustList"),
-
params.get("Context"));
- }
- sfs.putAppend("Message", "IdentityCreated");
- sfs.putAppend("InsertURI", identity.getInsertURI().toString());
- sfs.putAppend("RequestURI",
identity.getRequestURI().toString());
- return sfs;
- }
-
- private SimpleFieldSet handleSetTrust(SimpleFieldSet params) throws
NumberFormatException, TransformerConfigurationException,
FileNotFoundException, InvalidParameterException, ParserConfigurationException,
TransformerException, IOException, InsertException, UnknownIdentityException,
Db4oIOException, DatabaseClosedException, DuplicateScoreException,
DuplicateIdentityException, NotTrustedException, DuplicateTrustException {
-
- SimpleFieldSet sfs = new SimpleFieldSet(false);
-
- if(params.get("Truster") == null || params.get("Trustee") ==
null || params.get("Value") == null || params.get("Comment") == null) throw new
InvalidParameterException("Missing mandatory parameter");
-
- setTrust(params.get("Truster"), params.get("Trustee"),
params.get("Value"), params.get("Comment"));
-
- sfs.putAppend("Message", "TrustSet");
- return sfs;
- }
-
- private SimpleFieldSet handleAddIdentity(SimpleFieldSet params) throws
InvalidParameterException, MalformedURLException, FetchException,
DuplicateIdentityException {
-
- SimpleFieldSet sfs = new SimpleFieldSet(false);
-
- if(params.get("RequestURI") == null) throw new
InvalidParameterException("Missing mandatory parameter");
-
- Identity identity =
addIdentity(params.get("RequestURI").trim());
-
- sfs.putAppend("Message", "IdentityAdded");
- sfs.putAppend("RequestURI",
identity.getRequestURI().toString());
- return sfs;
- }
-
- private SimpleFieldSet handleGetIdentity(SimpleFieldSet params) throws
InvalidParameterException, MalformedURLException, FetchException,
UnknownIdentityException, DuplicateScoreException, DuplicateIdentityException,
DuplicateTrustException {
-
- SimpleFieldSet sfs = new SimpleFieldSet(false);
-
- if(params.get("TreeOwner") == null || params.get("Identity") ==
null) throw new InvalidParameterException("Missing mandatory parameter");
-
- sfs.putAppend("Message", "Identity");
-
- OwnIdentity treeOwner = OwnIdentity.getByURI(db,
params.get("TreeOwner"));
- Identity identity = Identity.getByURI(db,
params.get("Identity"));
-
- try {
- Trust trust = identity.getReceivedTrust(treeOwner, db);
- sfs.putAppend("Trust",
String.valueOf(trust.getValue()));
- } catch (NotTrustedException e1) {
- sfs.putAppend("Trust", "null");
- }
-
- Score score;
- try {
- score = identity.getScore(treeOwner, db);
- sfs.putAppend("Score",
String.valueOf(score.getScore()));
- sfs.putAppend("Rank", String.valueOf(score.getRank()));
- } catch (NotInTrustTreeException e) {
- sfs.putAppend("Score", "null");
- sfs.putAppend("Rank", "null");
- }
-
- Iterator<String> contexts = identity.getContexts();
- for(int i = 1 ; contexts.hasNext() ; i++)
sfs.putAppend("Context"+i, contexts.next());
-
- return sfs;
- }
-
- private SimpleFieldSet handleGetIdentitiesByScore(SimpleFieldSet
params) throws InvalidParameterException, MalformedURLException,
UnknownIdentityException, DuplicateIdentityException {
-
- SimpleFieldSet sfs = new SimpleFieldSet(false);
-
- if(params.get("TreeOwner") == null || params.get("Select") ==
null || params.get("Context") == null) throw new
InvalidParameterException("Missing mandatory parameter");
-
- sfs.putAppend("Message", "Identities");
-
- ObjectSet<Score> result =
wot.getIdentitiesByScore(params.get("TreeOwner"), params.get("Select").trim());
- for(int i = 1 ; result.hasNext() ; i++) {
- Score score = result.next();
- // Maybe there is a way to do this through SODA
- if(score.getTarget().hasContext(params.get("Context"))
|| params.get("Context").equals("all"))
- sfs.putAppend("Identity"+i,
score.getTarget().getRequestURI().toString());
- }
- return sfs;
- }
-
- private SimpleFieldSet handleGetTrusters(SimpleFieldSet params) throws
InvalidParameterException, MalformedURLException, UnknownIdentityException,
Db4oIOException, DatabaseClosedException, DuplicateIdentityException {
-
- SimpleFieldSet sfs = new SimpleFieldSet(false);
-
- if(params.get("Identity") == null || params.get("Context") ==
null) throw new InvalidParameterException("Missing mandatory parameter");
-
- sfs.putAppend("Message", "Identities");
-
- ObjectSet<Trust> result = Identity.getByURI(db,
params.get("Identity")).getReceivedTrusts(db);
-
- for(int i = 1 ; result.hasNext() ; i++) {
- Trust trust = result.next();
- // Maybe there is a way to do this through SODA
- if(trust.getTruster().hasContext(params.get("Context"))
|| params.get("Context").equals("all")) {
- sfs.putAppend("Identity"+i,
trust.getTruster().getRequestURI().toString());
- sfs.putAppend("Value"+i,
String.valueOf(trust.getValue()));
- sfs.putAppend("Comment"+i, trust.getComment());
- }
- }
- return sfs;
- }
-
- private SimpleFieldSet handleGetTrustees(SimpleFieldSet params) throws
InvalidParameterException, MalformedURLException, UnknownIdentityException,
Db4oIOException, DatabaseClosedException, DuplicateIdentityException {
-
- SimpleFieldSet sfs = new SimpleFieldSet(false);
-
- if(params.get("Identity") == null || params.get("Context") ==
null) throw new InvalidParameterException("Missing mandatory parameter");
-
- sfs.putAppend("Message", "Identities");
-
- ObjectSet<Trust> result = Identity.getByURI(db,
params.get("Identity")).getGivenTrusts(db);
-
- for(int i = 1 ; result.hasNext() ; i++) {
- Trust trust = result.next();
- // Maybe there is a way to do this through SODA
- if(trust.getTrustee().hasContext(params.get("Context"))
|| params.get("Context").equals("all")) {
- sfs.putAppend("Identity"+i,
trust.getTrustee().getRequestURI().toString());
- sfs.putAppend("Value"+i,
String.valueOf(trust.getValue()));
- sfs.putAppend("Comment"+i, trust.getComment());
- }
- }
- return sfs;
- }
-
- private SimpleFieldSet handleAddContext(SimpleFieldSet params) throws
InvalidParameterException, MalformedURLException, UnknownIdentityException,
DuplicateIdentityException {
-
- SimpleFieldSet sfs = new SimpleFieldSet(false);
-
- if(params.get("Identity") == null || params.get("Context") ==
null) throw new InvalidParameterException("Missing mandatory parameter");
-
- addContext(params.get("Identity"), params.get("Context"));
-
- sfs.putAppend("Message", "ContextAdded");
- return sfs;
- }
-
- private SimpleFieldSet handleRemoveContext(SimpleFieldSet params)
throws InvalidParameterException, MalformedURLException,
UnknownIdentityException, DuplicateIdentityException {
-
- SimpleFieldSet sfs = new SimpleFieldSet(false);
-
- if(params.get("Identity") == null || params.get("Context") ==
null) throw new InvalidParameterException("Missing mandatory parameter");
-
- removeContext(params.get("Identity"), params.get("Context"));
-
- sfs.putAppend("Message", "ContextRemoved");
- return sfs;
- }
-
- private SimpleFieldSet handleSetProperty(SimpleFieldSet params) throws
InvalidParameterException, MalformedURLException, UnknownIdentityException,
DuplicateIdentityException {
-
- SimpleFieldSet sfs = new SimpleFieldSet(false);
-
- if(params.get("Identity") == null || params.get("Property") ==
null || params.get("Value") == null) throw new
InvalidParameterException("Missing mandatory parameter");
-
- setProperty(params.get("Identity"), params.get("Property"),
params.get("Value"));
-
- sfs.putAppend("Message", "PropertyAdded");
- return sfs;
- }
-
- private SimpleFieldSet handleGetProperty(SimpleFieldSet params) throws
InvalidParameterException, MalformedURLException, UnknownIdentityException,
DuplicateIdentityException {
-
- SimpleFieldSet sfs = new SimpleFieldSet(false);
-
- if(params.get("Identity") == null || params.get("Property") ==
null) throw new InvalidParameterException("Missing mandatory parameter");
-
- sfs.putAppend("Message", "PropertyValue");
- sfs.putAppend("Property", getProperty(params.get("Identity"),
params.get("Property")));
-
- return sfs;
- }
-
- private SimpleFieldSet handleRemoveProperty(SimpleFieldSet params)
throws InvalidParameterException, MalformedURLException,
UnknownIdentityException, DuplicateIdentityException {
-
- SimpleFieldSet sfs = new SimpleFieldSet(false);
-
- if(params.get("Identity") == null || params.get("Property") ==
null) throw new InvalidParameterException("Missing mandatory parameter");
-
- removeProperty(params.get("Identity"), params.get("Property"));
-
- sfs.putAppend("Message", "PropertyRemoved");
- return sfs;
- }
-
- private SimpleFieldSet errorMessageFCP (Exception e) {
-
- SimpleFieldSet sfs = new SimpleFieldSet(false);
- sfs.putAppend("Message", "Error");
- sfs.putAppend("Description", (e.getLocalizedMessage() == null)
? "null" : e.getLocalizedMessage());
- e.printStackTrace();
- return sfs;
- }
-
- public String getString(String key) {
- // TODO Auto-generated method stub
- return key;
- }
-
- public void setLanguage(LANGUAGE newLanguage) {
- // TODO Auto-generated method stub
- }
-}