Author: batosai
Date: 2008-08-13 09:57:52 +0000 (Wed, 13 Aug 2008)
New Revision: 21783

Added:
   trunk/apps/WoT/src/plugins/WoT/InvalidParameterException.java
   trunk/apps/WoT/src/plugins/WoT/UnknownIdentityException.java
Modified:
   trunk/apps/WoT/src/plugins/WoT/Identity.java
   trunk/apps/WoT/src/plugins/WoT/IdentityFetcher.java
   trunk/apps/WoT/src/plugins/WoT/IdentityInserter.java
   trunk/apps/WoT/src/plugins/WoT/IdentityParser.java
   trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java
   trunk/apps/WoT/src/plugins/WoT/Trust.java
   trunk/apps/WoT/src/plugins/WoT/WoT.java
   trunk/apps/WoT/src/plugins/WoT/WoTplugin.java
Log:
Big refactoring. Most objects have been modified !
Basically, it consists in switching from FreenetURIs to Strings in Identities. 
This makes database searches more efficient and the code cleaner.
I also added controls on user's entries.

Modified: trunk/apps/WoT/src/plugins/WoT/Identity.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/Identity.java        2008-08-13 08:02:19 UTC 
(rev 21782)
+++ trunk/apps/WoT/src/plugins/WoT/Identity.java        2008-08-13 09:57:52 UTC 
(rev 21783)
@@ -5,6 +5,7 @@
  */
 package plugins.WoT;

+import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
@@ -22,42 +23,24 @@
  */
 public class Identity {

-       FreenetURI requestURI;
+       String requestURI;
+       long edition;
        Date lastChange;
+       String nickName;
+       boolean publishTrustList;
        HashMap<String, String> props;
        ArrayList<String> contexts;

-       /**
-        * Create an identity with the given parameters
-        * 
-        * @param requestURI EequestURI where the identity can be fetched
-        * @param lastChange Last time the Identity has been updated
-        * @param publishTrustList Whether the identity publishes its trustList 
or not
-        */
-       public Identity (FreenetURI requestURI, Date lastChange) {
+       public Identity (String requestURI, Date lastChange, String nickName, 
String publishTrustList) throws InvalidParameterException {

                setRequestURI(requestURI);
                setLastChange(lastChange);
+               setNickName(nickName);
+               setPublishTrustList(publishTrustList);
                props = new HashMap<String, String>();
                contexts = new ArrayList<String>();
        }

-       /**
-        * Create a new trust relationship for this identity
-        * 
-        * @param db Connection to the database
-        * @param trustee Identity that receives the trust
-        * @param value Numeric value of the trust granted [-100;+100]
-        */
-       public void addTrust (ObjectContainer db, Identity trustee, int value) {
-               Trust trust = new Trust(this,trustee,value);
-               db.store(trust);
-       }
-       
-       public String toString() {
-               return getRequestURI().toString();
-       }
-       
        public Score getScore(OwnIdentity treeOwner, ObjectContainer db) {

                ObjectSet<Score> score = db.queryByExample(new Score(treeOwner, 
this, 0, 0, 0));
@@ -66,20 +49,57 @@
        }       

        /**
-        * @return requestURI
+        * @return A FreenetURI
+        * @throws MalformedURLException 
         */
        public FreenetURI getRequestURI() {
-               return requestURI;
+               
+               FreenetURI key;
+               try {
+                       key = new FreenetURI(requestURI+"/WoT/"+edition);
+               } catch (MalformedURLException e) {
+                       // TODO Remove this as soon as the creation is checked
+                       e.printStackTrace();
+                       return null;
+                       // Can't happen, the key is tested on creation
+               }
+               return key.setMetaString(new String [] {"identity.xml"});
        }

        /**
         * @param requestURI The RequestURI of the Identity
+        * @throws MalformedURLException 
+        * @throws InvalidParameterException 
         */
-       public void setRequestURI(FreenetURI requestURI) {
+       public void setRequestURI(String requestURI) throws 
InvalidParameterException {
+
+               FreenetURI key;
+               try {
+                       key = new FreenetURI(requestURI).setDocName("WoT");
+               } catch (MalformedURLException e) {
+                       throw new InvalidParameterException("Invalid key : 
"+e.getMessage());
+               }
+               setRequestURI(key);
+       }

-               this.requestURI = 
requestURI.setDocName("WoT").setKeyType("USK");
+       public void setRequestURI(FreenetURI key) throws 
InvalidParameterException {
+               
+               if(key.getKeyType().equals("SSK")) key = key.setKeyType("USK");
+               if(!key.getKeyType().equals("USK")) throw new 
InvalidParameterException("Key type not supported");
+               
+               setEdition(key.getSuggestedEdition());
+
+               this.requestURI = key.toString().substring(0, 
key.toString().indexOf("/"));
        }

+       public long getEdition() {
+               return edition;
+       }
+
+       public void setEdition(long edition) {
+               this.edition = edition;
+       }
+
        /**
         * @return lastChange
         */
@@ -92,9 +112,32 @@
         */
        public void setLastChange(Date lastChange) {
                this.lastChange = lastChange;
+               
        }
+       public String getNickName() {
+               return nickName;
+       }
+
+       public void setNickName(String nickName) throws 
InvalidParameterException {
+               if(nickName.length() == 0) throw new 
InvalidParameterException("Blank nickName");
+               if(nickName.length() > 50) throw new 
InvalidParameterException("NickName is too long (50 chars max)");
+               this.nickName = nickName;
+       }
+
+       public boolean doesPublishTrustList() {
+               return publishTrustList;
+       }
+
+       public void setPublishTrustList(String publishTrustList) {
+               setPublishTrustList(publishTrustList.equals("true"));
+       }

-       public void setProp(String key, String value, ObjectContainer db) {
+       public void setPublishTrustList(boolean publishTrustList) {
+               this.publishTrustList = publishTrustList;
+       }
+
+       public void setProp(String key, String value, ObjectContainer db) 
throws InvalidParameterException {
+               if(key.length() == 0 || value.length() == 0) throw new 
InvalidParameterException("Blank key or value in this property");
                props.put(key, value);
                db.store(props);
        }
@@ -108,7 +151,8 @@
                db.store(props);
        }

-       public void addContext(String context, ObjectContainer db) {
+       public void addContext(String context, ObjectContainer db) throws 
InvalidParameterException {
+               if(context.length() == 0) throw new 
InvalidParameterException("Blank context");
                if(!contexts.contains(context)) contexts.add(context);
                db.store(contexts);
        }

Modified: trunk/apps/WoT/src/plugins/WoT/IdentityFetcher.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/IdentityFetcher.java 2008-08-13 08:02:19 UTC 
(rev 21782)
+++ trunk/apps/WoT/src/plugins/WoT/IdentityFetcher.java 2008-08-13 09:57:52 UTC 
(rev 21783)
@@ -6,10 +6,7 @@

 package plugins.WoT;

-import java.io.InputStream;
-
 import com.db4o.ObjectContainer;
-import com.db4o.ObjectSet;

 import freenet.client.FetchContext;
 import freenet.client.FetchException;
@@ -39,19 +36,12 @@
        }

        public void fetch(Identity identity) throws FetchException {
-               fetch(identity, false);
-       }
-       
-       public void fetch(Identity identity, boolean nextEdition) throws 
FetchException {

-               FreenetURI uri = identity.getRequestURI().setMetaString(new 
String [] {"identity.xml"}); 
-               if(nextEdition) uri = 
uri.setSuggestedEdition(uri.getSuggestedEdition() + 1);
-               
-               fetch(uri);
+               fetch(identity.getRequestURI());
        }

-       private void fetch(FreenetURI uri) throws FetchException {
-               
+       public void fetch(FreenetURI uri) throws FetchException {
+
                FetchContext fetchContext = client.getFetchContext();
                fetchContext.maxSplitfileBlockRetries = -1; // retry forever
                fetchContext.maxNonSplitfileRetries = -1; // retry forever
@@ -63,80 +53,50 @@

                if(e.newURI != null) { // Handle redirection to a new edition
                        try {
-                               Identity identity = 
wot.getIdentityByURI(state.getURI());
-                               // We just take the redirection number to avoid 
a nasty redirect loop by an attacker 
-                               
identity.setRequestURI(identity.getRequestURI().setSuggestedEdition(e.newURI.getSuggestedEdition()));
-                               db.store(identity);
-
-                               fetch(identity);
-                       } catch (Exception e2) {
-                               System.out.print(e2.getLocalizedMessage());
+                               fetch(e.newURI);
+                       } catch (FetchException e1) {
+                               // TODO Log this properly
+                               e1.printStackTrace();
                        }
                }
        }

        @Override
        public void onFailure(InsertException e, BaseClientPutter state) {
-               System.out.println("CALL: onFailure(InsertException e, 
BaseClientPutter state)");

        }

        @Override
        public void onFetchable(BaseClientPutter state) {
-               System.out.println("CALL: onFetchable(BaseClientPutter state)");

        }

        @Override
        public void onGeneratedURI(FreenetURI uri, BaseClientPutter state) {
-               System.out.println("CALL: onGeneratedURI(FreenetURI uri, 
BaseClientPutter state)");

        }

        @Override
        public void onMajorProgress() {
-               System.out.println("CALL: onMajorProgress()");

        }

        @Override
        public void onSuccess(FetchResult result, ClientGetter state) {
-               
-               // Find the identity we just fetched
-               Identity identity;
+
                try {
-                       identity = wot.getIdentityByURI(state.getURI());
+                       new IdentityParser(db, wot, 
client).parse(result.asBucket().getInputStream(), state.getURI());
+                       db.commit();            
+                       
fetch(state.getURI().setSuggestedEdition(state.getURI().getSuggestedEdition() + 
1));
                } catch (Exception e) {
+                       // TODO Log this properly
                        e.printStackTrace();
-                       return;
                }
-               
-               // Parse it
-               try {
-                       IdentityParser parser = new IdentityParser(db, wot, 
this);
-                       parser.parse(result.asBucket().getInputStream(), 
identity);
-               } catch (Exception e) {
-                       System.out.println(e.getLocalizedMessage());
-               }

-               // Update the edition number and commit the changes
-               
identity.setRequestURI(identity.getRequestURI().setSuggestedEdition(state.getURI().getSuggestedEdition()));
-               db.store(identity);
-               db.commit();            
-               
-               // Try to fetch next edition
-               try {
-                       fetch(identity, true);
-               } catch (FetchException e) {
-                       System.out.println(e.getLocalizedMessage());
-               }
-               
-               
        }

        @Override
        public void onSuccess(BaseClientPutter state) {
-               System.out.println("CALL: onSuccess(BaseClientPutter state)");

        }
 }

Modified: trunk/apps/WoT/src/plugins/WoT/IdentityInserter.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/IdentityInserter.java        2008-08-13 
08:02:19 UTC (rev 21782)
+++ trunk/apps/WoT/src/plugins/WoT/IdentityInserter.java        2008-08-13 
09:57:52 UTC (rev 21783)
@@ -47,7 +47,7 @@
        }

        public void insert(OwnIdentity identity) throws 
TransformerConfigurationException, FileNotFoundException, 
ParserConfigurationException, TransformerException, IOException, 
InsertException {
-               
+
                this.identity = identity;

                // Create XML file to insert
@@ -97,11 +97,8 @@

        @Override
        public void onSuccess(BaseClientPutter state) {
-               
-               long edition = state.getURI().getSuggestedEdition();
-               
-               
identity.setInsertURI(identity.getInsertURI().setSuggestedEdition(edition + 1));
-               
identity.setRequestURI(identity.getRequestURI().setSuggestedEdition(edition));
+
+               identity.setEdition(state.getURI().getSuggestedEdition());
                identity.setLastInsert(new Date());

                db.store(identity);

Modified: trunk/apps/WoT/src/plugins/WoT/IdentityParser.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/IdentityParser.java  2008-08-13 08:02:19 UTC 
(rev 21782)
+++ trunk/apps/WoT/src/plugins/WoT/IdentityParser.java  2008-08-13 09:57:52 UTC 
(rev 21783)
@@ -6,8 +6,8 @@

 package plugins.WoT;

+import java.io.IOException;
 import java.io.InputStream;
-import java.net.MalformedURLException;
 import java.util.Date;

 import javax.xml.parsers.ParserConfigurationException;
@@ -19,9 +19,8 @@
 import org.xml.sax.helpers.DefaultHandler;

 import com.db4o.ObjectContainer;
-import com.db4o.ObjectSet;

-import freenet.client.FetchException;
+import freenet.client.HighLevelSimpleClient;
 import freenet.keys.FreenetURI;

 /**
@@ -32,31 +31,24 @@

        ObjectContainer db;
        WoT wot;
-       IdentityFetcher fetcher;
+       HighLevelSimpleClient client;
        SAXParser saxParser;
        Identity identity;
+       
+       public IdentityParser(ObjectContainer db, WoT wot, 
HighLevelSimpleClient client) throws ParserConfigurationException, SAXException 
{

-       public IdentityParser(ObjectContainer db, WoT wot, IdentityFetcher 
fetcher) throws ParserConfigurationException, SAXException {
-
                this.db = db;
                this.wot = wot;
-               this.fetcher = fetcher;
-               SAXParserFactory factory = SAXParserFactory.newInstance();
-               saxParser = factory.newSAXParser();
+               this.client = client;
+               saxParser = SAXParserFactory.newInstance().newSAXParser();
        }

-       public void parse (InputStream is, Identity id) throws Exception {
+       public void parse (InputStream is, FreenetURI uri) throws 
InvalidParameterException, SAXException, IOException, UnknownIdentityException {

-               this.identity = id;
+               identity = wot.getIdentityByURI(uri.toString());        
                identity.setLastChange(new Date());
-               
-               try {
-                       saxParser.parse(is, new IdentityHandler() );
-                       is.close();
-               } catch (Throwable err) {
-                       err.printStackTrace ();
-                       throw new Exception("Could not parse XML: 
"+err.toString());
-               }
+               saxParser.parse(is, new IdentityHandler() );
+               db.store(identity);
        }

        public class IdentityHandler extends DefaultHandler {
@@ -70,28 +62,34 @@
                        String elt_name;
                        if (rawName == null) elt_name = localName;
                        else elt_name = rawName;
-                       
-                       if (elt_name.equals("prop")) {
-                               identity.setProp(attrs.getValue("key"), 
attrs.getValue("value"), db);
-                       }
-                       else if(elt_name.equals("context")) {
-                               identity.addContext(attrs.getValue("value"), 
db);
-                       }
-                       else if (elt_name.equals("trust")) {
-                               try {
-                                       Identity trustee = 
wot.getIdentityByURI(new FreenetURI(attrs.getValue("uri")));
-                                       if (trustee == null) {
-                                               trustee = new Identity(new 
FreenetURI(attrs.getValue("uri")), new Date(0));
-                                               db.store(trustee);
-                                               fetcher.fetch(trustee);
-                                       }
+
+                       try {
+                               if (elt_name.equals("nickName")) {
+                                       
identity.setNickName(attrs.getValue("value"));
+                               }
+                               if (elt_name.equals("publishTrustList")) {
+                                       
identity.setPublishTrustList(attrs.getValue("value"));
+                               }
+                               else if (elt_name.equals("prop")) {
+                                       identity.setProp(attrs.getValue("key"), 
attrs.getValue("value"), db);
+                               }
+                               else if(elt_name.equals("context")) {
+                                       
identity.addContext(attrs.getValue("value"), db);
+                               }
+                               else if (elt_name.equals("trust")) {
+       
+                                       Identity trustee = new 
Identity(attrs.getValue("uri"), new Date(0), "Not found yet...", "false");
+                                       db.store(trustee);
+                                       new IdentityFetcher(db, wot, 
client).fetch(trustee);
+                                       
                                        int value = 
Integer.parseInt(attrs.getValue("value"));
                                        String comment = 
attrs.getValue("comment");

                                        wot.setTrust(new Trust(identity, 
trustee, value, comment));                                     
-                               } catch (Exception e) {
-                                       
System.out.println(e.getLocalizedMessage());
-                               }
+                               }       
+                       } catch (Exception e1) {
+                               // TODO Log this properly
+                               e1.printStackTrace();
                        }
                }
        }

Added: trunk/apps/WoT/src/plugins/WoT/InvalidParameterException.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/InvalidParameterException.java               
                (rev 0)
+++ trunk/apps/WoT/src/plugins/WoT/InvalidParameterException.java       
2008-08-13 09:57:52 UTC (rev 21783)
@@ -0,0 +1,19 @@
+/**
+ * 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;
+
+/**
+ * @author Julien Cornuwel (batosai at freenetproject.org)
+ *
+ */
+public class InvalidParameterException extends Exception {
+       
+       private static final long serialVersionUID = -1;
+
+       public InvalidParameterException(String message) {
+               super(message);
+       }
+}

Modified: trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java     2008-08-13 08:02:19 UTC 
(rev 21782)
+++ trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java     2008-08-13 09:57:52 UTC 
(rev 21783)
@@ -10,6 +10,7 @@
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.net.MalformedURLException;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.Map;
@@ -42,22 +43,14 @@
  */
 public class OwnIdentity extends Identity {

-       private FreenetURI insertURI;
+       private String insertURI;
        private Date lastInsert;


-       /**
-        * Creates a local identity with the given parameters
-        * 
-        * @param insertURI Insert URI of the Identity
-        * @param requestURI Request URI of the Identity
-        * @param lastInsert Last time the Identity has been inserted in Freenet
-        * @param lastChange Last time the identity (or its trustList) has been 
updated
-        * @param publishTrustList Whether the Identity publishes its trustList 
or not
-        */
-       public OwnIdentity(FreenetURI insertURI, FreenetURI requestURI, Date 
lastInsert, Date lastChange) {
+
+       public OwnIdentity(String insertURI, String requestURI, Date 
lastInsert, Date lastChange, String nickName, String publishTrustList) throws 
InvalidParameterException {

-               super(requestURI, lastChange);
+               super(requestURI, lastChange, nickName, publishTrustList);
                setInsertURI(insertURI);
                setLastInsert(lastInsert);              
        }
@@ -73,22 +66,32 @@
         * @throws IOException
         */
        public File exportToXML(ObjectContainer db, File dir ) throws 
ParserConfigurationException, TransformerConfigurationException, 
TransformerException, FileNotFoundException, IOException {
-               
+
                // Create the output file
-               File outputFile = new File(dir, getInsertURI().getDocName() + 
".xml");
+               File outputFile = new File(dir, getNickName());
                BufferedOutputStream fos = new BufferedOutputStream(new 
FileOutputStream(outputFile));
                StreamResult resultStream = new StreamResult(fos);
-               
+
                // Create the XML document
                DocumentBuilderFactory xmlFactory = 
DocumentBuilderFactory.newInstance();
                DocumentBuilder xmlBuilder = xmlFactory.newDocumentBuilder();
                DOMImplementation impl = xmlBuilder.getDOMImplementation();
                Document xmlDoc = impl.createDocument(null, "WoT", null);
                Element rootElement = xmlDoc.getDocumentElement();
-               
+
                // Create the content
                Element identity = xmlDoc.createElement("identity");
-               
+
+               // NickName
+               Element nickNameTag = xmlDoc.createElement("nickName");
+               nickNameTag.setAttribute("value", nickName);
+               identity.appendChild(nickNameTag);
+
+               // PublishTrustList
+               Element publishTrustListTag = 
xmlDoc.createElement("publishTrustList");
+               publishTrustListTag.setAttribute("value", publishTrustList ? 
"true" : "false");
+               identity.appendChild(publishTrustListTag);
+
                // Properties
                Set set = props.entrySet();
                Iterator i = set.iterator();
@@ -99,7 +102,7 @@
                        propTag.setAttribute("value", prop.getValue());
                        identity.appendChild(propTag);
                }
-               
+
                // Contexts
                Iterator i2 = contexts.iterator();
                while(i2.hasNext()) {
@@ -108,10 +111,10 @@
                        contextTag.setAttribute("value", context);
                        identity.appendChild(contextTag);                       
                }
-               
+
                rootElement.appendChild(identity);

-               if(getProp("publishTrustList").equals("true")) {
+               if(doesPublishTrustList()) {
                        Trustlist trustList = new Trustlist(db, this);
                        rootElement.appendChild(trustList.toXML(xmlDoc));
                }
@@ -142,17 +145,41 @@
         * @return insertURI
         */
        public FreenetURI getInsertURI() {
-               return insertURI;
+               
+               FreenetURI key;
+               try {
+                       key = new FreenetURI(insertURI+"/WoT/"+edition);
+               } catch (MalformedURLException e) {
+                       // TODO Remove this as soon as the creation is checked
+                       e.printStackTrace();
+                       return null;
+                       // Can't happen, the key is tested on creation
+               }
+               return key;
        }

        /**
         * @param insertURI InsertURI of the Identity
+        * @throws InvalidParameterException 
         */
-       public void setInsertURI(FreenetURI insertURI) {
-               if(insertURI == null) this.insertURI = null;
-               else this.insertURI = 
insertURI.setDocName("WoT").setKeyType("USK");
+       public void setInsertURI(String insertURI) throws 
InvalidParameterException {
+               
+               FreenetURI key;
+               try {
+                       key = new FreenetURI(insertURI).setDocName("WoT");
+               } catch (MalformedURLException e) {
+                       throw new InvalidParameterException("Invalid key : 
"+e.getMessage());
+               }
+               setInsertURI(key);
        }
+       
+       public void setInsertURI(FreenetURI key) throws 
InvalidParameterException {
+               if(key.getKeyType().equals("SSK")) key = key.setKeyType("USK");
+               if(!key.getKeyType().equals("USK")) throw new 
InvalidParameterException("Key type not supported");

+               this.insertURI = key.toString().substring(0, 
key.toString().indexOf("/"));
+       }
+
        /**
         * @return lastInsert
         */

Modified: trunk/apps/WoT/src/plugins/WoT/Trust.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/Trust.java   2008-08-13 08:02:19 UTC (rev 
21782)
+++ trunk/apps/WoT/src/plugins/WoT/Trust.java   2008-08-13 09:57:52 UTC (rev 
21783)
@@ -53,9 +53,9 @@
         */
        public Element toXML(Document xmlDoc) {
                Element elem = xmlDoc.createElement("trust");
+               elem.setAttribute("uri", trustee.getRequestURI().toString());
                elem.setAttribute("value", String.valueOf(value));
                elem.setAttribute("comment", comment);
-               elem.setAttribute("uri", trustee.getRequestURI().toString());

                return elem;
        }

Added: trunk/apps/WoT/src/plugins/WoT/UnknownIdentityException.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/UnknownIdentityException.java                
                (rev 0)
+++ trunk/apps/WoT/src/plugins/WoT/UnknownIdentityException.java        
2008-08-13 09:57:52 UTC (rev 21783)
@@ -0,0 +1,20 @@
+/**
+ * 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;
+
+/**
+ * @author Julien Cornuwel (batosai at freenetproject.org)
+ *
+ */
+public class UnknownIdentityException extends Exception {
+       
+       private static final long serialVersionUID = -1;
+
+       public UnknownIdentityException(String message) {
+               super(message);
+       }
+
+}

Modified: trunk/apps/WoT/src/plugins/WoT/WoT.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/WoT.java     2008-08-13 08:02:19 UTC (rev 
21782)
+++ trunk/apps/WoT/src/plugins/WoT/WoT.java     2008-08-13 09:57:52 UTC (rev 
21783)
@@ -5,8 +5,11 @@
  */
 package plugins.WoT;

+import java.net.MalformedURLException;
+
 import com.db4o.ObjectContainer;
 import com.db4o.ObjectSet;
+import com.db4o.query.Query;

 import freenet.keys.FreenetURI;

@@ -169,24 +172,23 @@
                return identities.size() - getNbOwnIdentities();
        }

-       public Identity getIdentityByURI(FreenetURI uri) throws Exception {
+       public Identity getIdentityByURI(String uri) throws 
InvalidParameterException, MalformedURLException, UnknownIdentityException {

-               String searched = uri.toString().substring(0, 
uri.toString().indexOf("/"));
-               Identity identity = null;
+               FreenetURI key = new FreenetURI(uri);
+               if(!key.getKeyType().equals("USK")) throw new 
InvalidParameterException("Not an USK");

-               // This is ugly, I could not find a request able to find the 
good identity 
-               ObjectSet<Identity> search = db.queryByExample(Identity.class);
-               while (search.hasNext()) {
-                       Identity id = search.next();
-                       String found = 
id.getRequestURI().toString().substring(0, 
id.getRequestURI().toString().indexOf("/"));
-                       if(found.equals(searched)) {
-                               identity = id;
-                               break;
-                       }
-               }
-               if(identity==null) throw new Exception("Identity doesn't exist 
: "+uri.toString());
+               return getIdentityByURI(key);
+       }
+       
+       private Identity getIdentityByURI(FreenetURI uri) throws 
InvalidParameterException, UnknownIdentityException {

-               return identity;
+               Query query = db.query();
+               query.constrain(Identity.class);
+               
query.descend("requestURI").constrain(uri.toString().substring(0, 
uri.toString().indexOf("/")));
+               ObjectSet<Identity> result = query.execute();
+               
+               if(result.size() == 0) throw new UnknownIdentityException("No 
identity has this request URI ("+uri.toString()+")");
+               return result.next();
        }
 }


Modified: trunk/apps/WoT/src/plugins/WoT/WoTplugin.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/WoTplugin.java       2008-08-13 08:02:19 UTC 
(rev 21782)
+++ trunk/apps/WoT/src/plugins/WoT/WoTplugin.java       2008-08-13 09:57:52 UTC 
(rev 21783)
@@ -125,7 +125,7 @@

                ObjectSet<OwnIdentity> ownIdentities = 
db.queryByExample(OwnIdentity.class);
                if(ownIdentities.size() == 0) {
-                       boxContent.addChild("p", "You have no own identities 
yet, you should create one...");
+                       boxContent.addChild("p", "You have no own identity yet, 
you should create one...");
                }
                else {
                        HTMLNode identitiesTable = boxContent.addChild("table", 
"border", "0");
@@ -138,16 +138,16 @@
                        while(ownIdentities.hasNext()) {
                                OwnIdentity id = ownIdentities.next();
                                row=identitiesTable.addChild("tr");
-                               row.addChild("td", new String[] {"title", 
"style"}, new String[] {id.getRequestURI().toString(), "cursor: help;"}, 
id.getProp("nickName"));
+                               row.addChild("td", new String[] {"title", 
"style"}, new String[] {id.getRequestURI().toString(), "cursor: help;"}, 
id.getNickName());
                                
row.addChild("td",id.getLastChange().toString());
                                HTMLNode cell = row.addChild("td");
-                               if(id.getLastInsert() == null) {
-                                       cell.addChild("p", "In progress...");
+                               if(id.getLastInsert().equals(new Date(0))) {
+                                       cell.addChild("p", "Never");
                                }
                                else {
-                                       cell.addChild(new HTMLNode("a", "href", 
"http://127.0.0.1:8888/"; + id.getRequestURI() + "/identity.xml", 
id.getLastInsert().toString()));
+                                       cell.addChild(new HTMLNode("a", "href", 
"/"+id.getRequestURI().toString(), id.getLastInsert().toString()));
                                }
-                               row.addChild("td", 
id.getProp("publishTrustList").equals("true") ? "Yes" : "No");
+                               row.addChild("td", id.doesPublishTrustList() ? 
"Yes" : "No");
                        }
                }

@@ -202,11 +202,10 @@
                        }
                        else {
                                try {
-                                       treeOwner = wot.getIdentityByURI(new 
FreenetURI(owner));
-                                       if(treeOwner == null) return "Unknown 
URI";
+                                       treeOwner = wot.getIdentityByURI(owner);
                                } catch (Exception e) {
-                                       return e.getLocalizedMessage();
-                               }       
+                                       return e.getMessage();
+                               } 
                        }

                        // Display a form to select the tree owner 
@@ -245,15 +244,14 @@
                        row=identitiesTable.addChild("tr");

                        // NickName
-                       row.addChild("td", new String[] {"title", "style"}, new 
String[] {id.getRequestURI().toString(), "cursor: help;"}, 
id.getProp("nickName"));
+                       row.addChild("td", new String[] {"title", "style"}, new 
String[] {id.getRequestURI().toString(), "cursor: help;"}, id.getNickName());

                        // Last Change
                        if (id.getLastChange().equals(new Date(0))) 
row.addChild("td", "Fetching...");
                        else row.addChild("td", id.getLastChange().toString());

                        // Publish TrustList
-                       boolean publishTrustList = 
id.getProp("publishTrustList") != null && 
id.getProp("publishTrustList").equals("true");
-                       row.addChild("td", publishTrustList ? "Yes" : "No");
+                       row.addChild("td", id.doesPublishTrustList() ? "Yes" : 
"No");

                        //Score
                        row.addChild("td", 
String.valueOf(id.getScore((OwnIdentity)treeOwner, db).getScore())+" 
("+id.getScore((OwnIdentity)treeOwner, db).getRank()+")");
@@ -343,7 +341,7 @@
                }
        }

-       private void setTrust(HTTPRequest request) throws Exception {
+       private void setTrust(HTTPRequest request) throws 
NumberFormatException, TransformerConfigurationException, 
FileNotFoundException, InvalidParameterException, UnknownIdentityException, 
ParserConfigurationException, TransformerException, IOException, 
InsertException  {

                setTrust(       request.getPartAsString("truster", 1024),
                                        request.getPartAsString("trustee", 
1024),
@@ -351,19 +349,17 @@
                                        request.getPartAsString("comment", 
1024));
        }

-       private void setTrust(String truster, String trustee, String value, 
String comment) throws Exception {
+       private void setTrust(String truster, String trustee, String value, 
String comment) throws InvalidParameterException, UnknownIdentityException, 
NumberFormatException, TransformerConfigurationException, 
FileNotFoundException, ParserConfigurationException, TransformerException, 
IOException, InsertException  {

-               Identity trusterId = wot.getIdentityByURI(new 
FreenetURI(truster));
-               Identity trusteeId = wot.getIdentityByURI(new 
FreenetURI(trustee));
+               Identity trusterId = wot.getIdentityByURI(truster);
+               Identity trusteeId = wot.getIdentityByURI(trustee);

-               if(!(trusterId instanceof OwnIdentity)) throw new Exception 
("Truster isn't an OwnIdentity");
-               if(trusterId == null) throw new Exception ("Truster doesn't 
exist");
-               if(trusteeId == null) throw new Exception ("Trustee doesn't 
exist");
+               if(!(trusterId instanceof OwnIdentity)) throw new 
InvalidParameterException ("Truster isn't an OwnIdentity");

                setTrust((OwnIdentity)trusterId, trusteeId, 
Integer.parseInt(value), comment);
 }      

-       private void setTrust(OwnIdentity truster, Identity trustee, int value, 
String comment) throws Exception {
+       private void setTrust(OwnIdentity truster, Identity trustee, int value, 
String comment) throws TransformerConfigurationException, 
FileNotFoundException, ParserConfigurationException, TransformerException, 
IOException, InsertException {

                wot.setTrust(new Trust(truster, trustee, value, comment));

@@ -378,65 +374,58 @@

        private String addIdentity(HTTPRequest request) {

+               Identity identity;
                try {
-                       FreenetURI uri = new 
FreenetURI(request.getPartAsString("identityURI", 1024));
-                       Identity identity = wot.getIdentityByURI(uri);
-                       
-                       if(identity == null) {
-                               identity = new Identity(uri, new Date(0));
+                       identity = 
wot.getIdentityByURI(request.getPartAsString("identityURI", 1024));
+                       return "We already know that identity !";
+               }
+               catch (UnknownIdentityException e) {
+                       try {
+                               identity = new 
Identity(request.getPartAsString("identityURI", 1024), new Date(0), "Not found 
yet...", "false");
                                db.store(identity);
                                db.commit();
                                fetcher.fetch(identity);
                        }
-                       else return "We already know that identity !";
-                       
-               } catch (Exception e) {
-                       return e.getLocalizedMessage();
+                       catch(Exception e2) {
+                               return e2.getMessage();
+                       }
+               } catch (MalformedURLException e) {
+                       return e.getMessage();
+               } catch (InvalidParameterException e) {
+                       return e.getMessage();
                }

                return makeKnownIdentitiesPage();
        }

-       private OwnIdentity createIdentity(HTTPRequest request) throws 
Exception {
+       private OwnIdentity createIdentity(HTTPRequest request) throws 
TransformerConfigurationException, FileNotFoundException, 
InvalidParameterException, ParserConfigurationException, TransformerException, 
IOException, InsertException {
+
                return createIdentity(  
request.getPartAsString("insertURI",1024),
                                                                
request.getPartAsString("requestURI",1024),
                                                                
request.getPartAsString("nickName", 1024),
-                                                               
request.getPartAsString("publishTrustList", 1024));                     
+                                                               
request.getPartAsString("publishTrustList", 1024));     
        }

-       private OwnIdentity createIdentity(String nickName, String 
publishTrustList) throws Exception {
+       private OwnIdentity createIdentity(String nickName, String 
publishTrustList) throws TransformerConfigurationException, 
FileNotFoundException, InvalidParameterException, ParserConfigurationException, 
TransformerException, IOException, InsertException {

+
                FreenetURI[] keypair = client.generateKeyPair("WoT");
-               return createIdentity(  keypair[0], keypair[1], nickName, 
publishTrustList);
+               return createIdentity(keypair[0].toString(), 
keypair[1].toString(), nickName, publishTrustList);
        }
-       
-       private OwnIdentity createIdentity(String insertURI, String requestURI, 
String nickName, String publishTrustList) throws Exception {
-                       
-               return createIdentity(new FreenetURI(insertURI), new 
FreenetURI(requestURI), nickName, publishTrustList);
-       }

-               
-       private OwnIdentity createIdentity(FreenetURI insertURI, FreenetURI 
requestURI, String nickName, String publishTrustList) throws Exception {
-               
-               if(nickName==null || nickName.equals("")) throw new 
Exception("Missing parameter (NickName)");
+       private OwnIdentity createIdentity(String insertURI, String requestURI, 
String nickName, String publishTrustList) throws InvalidParameterException, 
TransformerConfigurationException, FileNotFoundException, 
ParserConfigurationException, TransformerException, IOException, 
InsertException {

-               OwnIdentity identity = new OwnIdentity( 
insertURI.setKeyType("USK"), requestURI.setKeyType("USK"), null, new Date());
-               identity.setProp("nickName", nickName, db);
-               
-               if(!publishTrustList.equals("true")) publishTrustList = "false";
-               identity.setProp("publishTrustList", publishTrustList, db);
-               
+               OwnIdentity identity = new OwnIdentity(insertURI, requestURI, 
new Date(0), new Date(), nickName, publishTrustList);
                db.store(identity);

                Score score = new Score(identity, identity, 100, 0, 100); // We 
need to initialize the trust tree 
                db.store(score);
-
+               
                //TODO Make the new identity trust the seed identity

                db.commit();    

-               new IdentityInserter(db, client).insert(identity);
-               
+               new IdentityInserter(db, client).insert(identity);      
                return identity;
        }

@@ -472,11 +461,13 @@
                }
        }

-       private SimpleFieldSet handleCreateIdentity(SimpleFieldSet params) 
throws Exception {
+       private SimpleFieldSet handleCreateIdentity(SimpleFieldSet params) 
throws TransformerConfigurationException, FileNotFoundException, 
InvalidParameterException, ParserConfigurationException, TransformerException, 
IOException, InsertException  {

                SimpleFieldSet sfs = new SimpleFieldSet(false);
                OwnIdentity identity;

+               if(params.get("NickName")==null || 
params.get("PublishTrustList")==null) throw new 
InvalidParameterException("Missing mandatory parameter");
+               
                if(params.get("RequestURI")==null || 
params.get("InsertURI")==null) {
                        identity = createIdentity(params.get("NickName"), 
params.get("PublishTrustList"));
                        sfs.putAppend("Message", "IdentityCreated");
@@ -484,8 +475,8 @@
                        sfs.putAppend("RequestURI", 
identity.getRequestURI().toString());                       
                }
                else {
-                       identity = createIdentity(      new 
FreenetURI(params.get("InsertURI")),
-                                                                               
new FreenetURI(params.get("RequestURI")),
+                       identity = createIdentity(      params.get("InsertURI"),
+                                                                               
params.get("RequestURI"),
                                                                                
params.get("NickName"), 
                                                                                
params.get("PublishTrustList"));
                        sfs.putAppend("Message", "IdentityCreated");
@@ -496,11 +487,11 @@
                return sfs;
        }

-       private SimpleFieldSet handleSetTrust(SimpleFieldSet params) throws 
Exception {
+       private SimpleFieldSet handleSetTrust(SimpleFieldSet params) throws 
NumberFormatException, TransformerConfigurationException, 
FileNotFoundException, InvalidParameterException, ParserConfigurationException, 
TransformerException, IOException, InsertException, UnknownIdentityException  {

                SimpleFieldSet sfs = new SimpleFieldSet(false);

-               if(params.get("truster") == null || params.get("trustee") == 
null || params.get("value") == null || params.get("comment") == null) throw new 
Exception("Missing parameter");
+               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"));



Reply via email to