Author: xor
Date: 2008-11-11 13:54:45 +0000 (Tue, 11 Nov 2008)
New Revision: 23486

Modified:
   trunk/plugins/WoT/WoT.java
   trunk/plugins/WoT/introduction/IntroductionPuzzle.java
   trunk/plugins/WoT/introduction/IntroductionServer.java
Log:
Finish implementation of puzzle insertion. Things which are missing: Puzzle 
generation (= captcha library) and puzzle retrieval + solution insertion.

Modified: trunk/plugins/WoT/WoT.java
===================================================================
--- trunk/plugins/WoT/WoT.java  2008-11-11 13:22:10 UTC (rev 23485)
+++ trunk/plugins/WoT/WoT.java  2008-11-11 13:54:45 UTC (rev 23486)
@@ -23,6 +23,7 @@
 import plugins.WoT.exceptions.NotInTrustTreeException;
 import plugins.WoT.exceptions.NotTrustedException;
 import plugins.WoT.exceptions.UnknownIdentityException;
+import plugins.WoT.introduction.IntroductionPuzzle;
 import plugins.WoT.ui.web.HomePage;
 import plugins.WoT.ui.web.KnownIdentitiesPage;
 import plugins.WoT.ui.web.OwnIdentitiesPage;
@@ -761,6 +762,9 @@
                
cfg.objectClass(Trust.class).objectField("trustee").indexed(true);
                
cfg.objectClass(Score.class).objectField("treeOwner").indexed(true);
                
cfg.objectClass(Score.class).objectField("target").indexed(true);
+               for(String field : IntroductionPuzzle.getIndexedFields())
+                       cfg.objectClass(IntroductionPuzzle.class).indexed(true);
+               
cfg.objectClass(IntroductionPuzzle.class).cascadeOnUpdate(true); /* FIXME: 
verify if this does not break anything */
                
                // This will make db4o store any complex objects which are 
referenced by a Config object.
                cfg.objectClass(Config.class).cascadeOnUpdate(true);

Modified: trunk/plugins/WoT/introduction/IntroductionPuzzle.java
===================================================================
--- trunk/plugins/WoT/introduction/IntroductionPuzzle.java      2008-11-11 
13:22:10 UTC (rev 23485)
+++ trunk/plugins/WoT/introduction/IntroductionPuzzle.java      2008-11-11 
13:54:45 UTC (rev 23486)
@@ -5,41 +5,73 @@
  */
 package plugins.WoT.introduction;
 
+import java.io.OutputStream;
 import java.net.MalformedURLException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Map.Entry;
 
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
 import com.db4o.ObjectContainer;
 import com.db4o.ObjectSet;
 import com.db4o.query.Query;
+import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
 
 import freenet.crypt.SHA1;
 import freenet.keys.FreenetURI;
+import freenet.support.Logger;
 
 import plugins.WoT.Identity;
 import plugins.WoT.OwnIdentity;
+import plugins.WoT.Trustlist;
 import plugins.WoT.WoT;
+import plugins.WoT.exceptions.UnknownIdentityException;
 
 public class IntroductionPuzzle {
        
        public static final String INTRODUCTION_CONTEXT = "introduction";
        public static final int MINIMAL_SOLUTION_LENGTH = 5;
        
+       /* Included in XML: */
+       
        private final String mMimeType;
        
+       private final long mValidUntilTime;
+       
        private final byte[] mData;
        
-       private final String mSolution;
+       /* Not included in XML, decoded from URI: */
        
        private final Identity mInserter;
        
-       private final long mValidUntilTime;
-       
        private final Date mDateOfInsertion;
        
        private final int mIndex;
        
+       /* Supplied at creation time or by user: */
        
+       private final String mSolution;
+       
+       
        /* FIXME: wire this in */
        /**
         * Get a list of fields which the database should create an index on.
@@ -53,8 +85,8 @@
         * @param newType
         * @param newData
         */
-       public IntroductionPuzzle(Identity newInserter, String newMimeType, 
String newFilename, byte[] newData, long myValidUntilTime, Date 
myDateOfInsertion, int myIndex) {
-               assert( newInserter != null && newMimeType != null && 
!newMimeType.equals("") && newFilename!=null && !newFilename.equals("") &&
+       public IntroductionPuzzle(Identity newInserter, String newMimeType, 
byte[] newData, long myValidUntilTime, Date myDateOfInsertion, int myIndex) {
+               assert( newInserter != null && newMimeType != null && 
!newMimeType.equals("") &&
                                newData!=null && newData.length!=0 && 
myValidUntilTime > System.currentTimeMillis() && myDateOfInsertion != null &&
                                myDateOfInsertion.getTime() < 
System.currentTimeMillis() && myIndex >= 0);
                mInserter = newInserter;
@@ -162,4 +194,85 @@
                
                db.commit();
        }
+       
+       public void exportToXML(OutputStream os) throws TransformerException, 
ParserConfigurationException {
+               // Create the output file
+               StreamResult resultStream = new StreamResult(os);
+
+               // 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 puzzleTag = xmlDoc.createElement("IntroductionPuzzle");
+
+               Element mimeTypeTag = xmlDoc.createElement("MimeType");
+               mimeTypeTag.setAttribute("value", mMimeType);
+               puzzleTag.appendChild(mimeTypeTag);
+               
+               Element validUntilTag = xmlDoc.createElement("ValidUntilTime");
+               validUntilTag.setAttribute("value", 
Long.toString(mValidUntilTime));
+               puzzleTag.appendChild(validUntilTag);
+               
+               Element dataTag = xmlDoc.createElement("Data");
+               dataTag.setAttribute("value", Base64.encode(mData));
+               puzzleTag.appendChild(dataTag);
+               
+               rootElement.appendChild(puzzleTag);
+
+               DOMSource domSource = new DOMSource(xmlDoc);
+               TransformerFactory transformFactory = 
TransformerFactory.newInstance();
+               Transformer serializer = transformFactory.newTransformer();
+               
+               serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
+               serializer.setOutputProperty(OutputKeys.INDENT,"yes");
+               serializer.transform(domSource, resultStream);
+       }
+       
+       public class PuzzleHandler extends DefaultHandler {
+               private final Identity newInserter;
+               private String newMimeType;
+               private byte[] newData;
+               private long newValidUntilTime;
+               private Date newDateOfInsertion;
+               private int newIndex;
+
+               public PuzzleHandler(Identity myInserter, Date 
myDateOfInsertion, int myIndex) {
+                       super();
+                       newInserter = myInserter;
+                       newDateOfInsertion = myDateOfInsertion;
+                       newIndex = myIndex;
+               }
+
+               /**
+                * Called by SAXParser for each XML element.
+                */
+               public void startElement(String nameSpaceURI, String localName, 
String rawName, Attributes attrs) throws SAXException {
+                       String elt_name = rawName == null ? localName : rawName;
+
+                       try {
+                               if (elt_name.equals("MimeType")) {
+                                       newMimeType = attrs.getValue("value");
+                               }
+                               else if (elt_name.equals("ValidUntilTime")) {
+                                       newValidUntilTime = 
Long.parseLong(attrs.getValue("value"));
+                               }
+                               else if(elt_name.equals("Data")) {
+                                       newData = 
Base64.decode(attrs.getValue("value"));
+                               }                                       
+                               else
+                                       Logger.error(this, "Unknown element in 
puzzle: " + elt_name);
+                               
+                       } catch (Exception e1) {
+                               Logger.error(this, "Parsing error",e1);
+                       }
+               }
+
+               public IntroductionPuzzle getPuzzle() {
+                       return new IntroductionPuzzle(newInserter, newMimeType, 
newData, newValidUntilTime, newDateOfInsertion, newIndex);
+               }
+       }
 }

Modified: trunk/plugins/WoT/introduction/IntroductionServer.java
===================================================================
--- trunk/plugins/WoT/introduction/IntroductionServer.java      2008-11-11 
13:22:10 UTC (rev 23485)
+++ trunk/plugins/WoT/introduction/IntroductionServer.java      2008-11-11 
13:54:45 UTC (rev 23486)
@@ -132,33 +132,58 @@
                q.descend("mInserter").constrain(identity);
                ObjectSet<IntroductionPuzzle> puzzles = q.execute();
                
+               Logger.debug(this, "Identity " + identity.getNickName() + " has 
" + puzzles.size() + " puzzles stored. Deleting expired ones ...");
+               
+               for(IntroductionPuzzle p : puzzles) {
+                       if(p.getValidUntilTime() < System.currentTimeMillis()) {
+                               db.delete(p);
+                               puzzles.remove(p);
+                       }
+               }
+               
+               db.commit();
+               
+               int puzzlesToInsert = PUZZLES_COUNT - puzzles.size();
+               Logger.debug(this, "Trying to insert " + puzzlesToInsert + " 
puzzles from " + identity.getNickName());
+               while(puzzlesToInsert > 0) {
+                       try {
+                               insertNewPuzzle(identity);
+                       }
+                       catch(Exception e) {
+                               Logger.error(this, "Error while inserting 
puzzle", e);
+                       }
+                       --puzzlesToInsert;
+               }
+               Logger.debug(this, "Finished inserting puzzles from " + 
identity.getNickName());
        }
        
-       private void insertNewPuzzle(OwnIdentity identity) throws IOException, 
InsertException {
+       private void insertNewPuzzle(OwnIdentity identity) throws IOException, 
InsertException, TransformerException, ParserConfigurationException {
                Bucket tempB = mTBF.makeBucket(10 * 1024); /* TODO: set to a 
reasonable value */
                OutputStream os = tempB.getOutputStream();
                
                try {
                        IntroductionPuzzle p = 
mPuzzleFactories[(int)(Math.random() * 100) % 
mPuzzleFactories.length].generatePuzzle();
-                       os.write(p.getPuzzle());
-                       os.close();
+                       p.exportToXML(os);
+                       os.close(); os = null;
                        tempB.setReadOnly();
                
-                       ClientMetadata cmd = new 
ClientMetadata(p.getMimeType());
+                       ClientMetadata cmd = new ClientMetadata("text/xml");
                        InsertBlock ib = new InsertBlock(tempB, cmd, 
p.getURI());
 
-                       Logger.debug(this, "Started insert puzzle from '" + 
identity.getNickName() + "'");
+                       Logger.debug(this, "Started insert puzzle from " + 
identity.getNickName());
 
                        /* FIXME: use nonblocking insert */
-                       mClient.insert(ib, true, p.getURI().getMetaString());
+                       mClient.insert(ib, false, p.getURI().getMetaString());
 
                        db.store(p);
                        db.commit();
-               } finally {
+                       Logger.debug(this, "Successful insert of puzzle from " 
+ identity.getNickName());
+               }
+               finally {
                        tempB.free();
+                       if(os != null)
+                               os.close();
                }
-
-               Logger.debug(this, "Successful insert of puzzle for '" + 
identity.getNickName() + "'");
        }
        
        private void downloadSolutions(OwnIdentity identity) {

_______________________________________________
cvs mailing list
[email protected]
http://emu.freenetproject.org/cgi-bin/mailman/listinfo/cvs

Reply via email to