Author: xor
Date: 2008-11-11 21:46:27 +0000 (Tue, 11 Nov 2008)
New Revision: 23494

Modified:
   trunk/plugins/WoT/introduction/IntroductionPuzzle.java
   trunk/plugins/WoT/introduction/IntroductionServer.java
Log:
Finish implementation of the IntroductionServer. All we need is the client, a 
puzzle factory and UI.

Modified: trunk/plugins/WoT/introduction/IntroductionPuzzle.java
===================================================================
--- trunk/plugins/WoT/introduction/IntroductionPuzzle.java      2008-11-11 
21:43:23 UTC (rev 23493)
+++ trunk/plugins/WoT/introduction/IntroductionPuzzle.java      2008-11-11 
21:46:27 UTC (rev 23494)
@@ -5,6 +5,7 @@
  */
 package plugins.WoT.introduction;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.MalformedURLException;
@@ -18,6 +19,8 @@
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerConfigurationException;
@@ -43,9 +46,12 @@
 import freenet.support.Logger;
 
 import plugins.WoT.Identity;
+import plugins.WoT.IdentityFetcher;
 import plugins.WoT.OwnIdentity;
 import plugins.WoT.Trustlist;
 import plugins.WoT.WoT;
+import plugins.WoT.IdentityParser.IdentityHandler;
+import plugins.WoT.exceptions.InvalidParameterException;
 import plugins.WoT.exceptions.UnknownIdentityException;
 
 public class IntroductionPuzzle {
@@ -175,7 +181,7 @@
         * Get the URI at which to insert the solution of this puzzle.
         */
        public FreenetURI getSolutionURI(String guessOfSolution) {
-               String dayOfInsertion = new 
SimpleDateFormat("yyyy-MM-dd").format(mDateOfInsertion);
+               String dayOfInsertion = mDateFormat.format(mDateOfInsertion);
                return new FreenetURI("KSK",    INTRODUCTION_CONTEXT + "|" +
                                                                
mInserter.getId() + "|" +
                                                                dayOfInsertion 
+ "|" +
@@ -262,15 +268,15 @@
                serializer.transform(domSource, resultStream);
        }
        
-       public static IntroductionPuzzle importFromXML(InputStream is) {
-               return null;
+       public static void importFromXML(ObjectContainer db, InputStream is, 
FreenetURI puzzleURI ) throws SAXException, IOException, 
ParserConfigurationException, UnknownIdentityException, ParseException {
+               PuzzleHandler puzzleHandler = new PuzzleHandler(db, puzzleURI);
+               SAXParserFactory.newInstance().newSAXParser().parse(is, 
puzzleHandler);
+               
+               db.store(puzzleHandler.getPuzzle());
+               db.commit();
        }
        
-       public static Identity importSolutionFromXML(ObjectContainer db, 
InputStream is) {
-               return null;
-       }
-       
-       public class PuzzleHandler extends DefaultHandler {
+       public static class PuzzleHandler extends DefaultHandler {
                private final Identity newInserter;
                private String newMimeType;
                private byte[] newData;
@@ -278,11 +284,14 @@
                private Date newDateOfInsertion;
                private int newIndex;
 
-               public PuzzleHandler(Identity myInserter, Date 
myDateOfInsertion, int myIndex) {
+
+               public PuzzleHandler(ObjectContainer db, FreenetURI puzzleURI) 
throws UnknownIdentityException, ParseException {
                        super();
-                       newInserter = myInserter;
-                       newDateOfInsertion = myDateOfInsertion;
-                       newIndex = myIndex;
+                       newInserter = Identity.getByURI(db, puzzleURI);
+                       String filename = 
puzzleURI.getDocName().replaceAll(".xml", "");
+                       String tokens[] = filename.split("|");
+                       newDateOfInsertion = mDateFormat.parse(tokens[0]);
+                       newIndex = Integer.parseInt(tokens[1]);
                }
 
                /**

Modified: trunk/plugins/WoT/introduction/IntroductionServer.java
===================================================================
--- trunk/plugins/WoT/introduction/IntroductionServer.java      2008-11-11 
21:43:23 UTC (rev 23493)
+++ trunk/plugins/WoT/introduction/IntroductionServer.java      2008-11-11 
21:46:27 UTC (rev 23494)
@@ -38,6 +38,7 @@
 import freenet.support.api.Bucket;
 import freenet.support.io.TempBucketFactory;
 import plugins.WoT.Identity;
+import plugins.WoT.IdentityFetcher;
 import plugins.WoT.IdentityParser;
 import plugins.WoT.OwnIdentity;
 import plugins.WoT.exceptions.InvalidParameterException;
@@ -66,6 +67,8 @@
        
        /** The TempBucketFactory used to create buckets from puzzles before 
insert */
        private final TempBucketFactory mTBF;
+       
+       private final IdentityFetcher mIdentityFetcher;
 
        private final IntroductionPuzzleFactory[] mPuzzleFactories = new 
IntroductionPuzzleFactory[] { new CaptchaFactory1() };
        
@@ -82,11 +85,12 @@
         * @param tbf
         *            Needed to create buckets from Identities before insert
         */
-       public IntroductionServer(ObjectContainer myDB, HighLevelSimpleClient 
myClient, TempBucketFactory myTBF) {
+       public IntroductionServer(ObjectContainer myDB, HighLevelSimpleClient 
myClient, TempBucketFactory myTBF, IdentityFetcher myFetcher) {
                isRunning = true;
                db = myDB;
                mClient = myClient;
                mTBF = myTBF;
+               mIdentityFetcher = myFetcher;
        }
 
        public void run() {
@@ -233,15 +237,18 @@
                Logger.debug(this, "Fetched puzzle solution: " + 
state.getURI());
 
                try {
+                       db.commit();
+                       Identity newIdentity = 
Identity.importIntroductionFromXML(db, mIdentityFetcher, 
result.asBucket().getInputStream());
                        IntroductionPuzzle p = 
IntroductionPuzzle.getBySolutionURI(db, state.getURI());
-                       Identity newIdentity = p.importSolutionFromXML(db, 
result.asBucket().getInputStream());
                        OwnIdentity puzzleOwner = (OwnIdentity)p.getInserter();
-                       
                        puzzleOwner.setTrust(db, newIdentity, (byte)0, null); 
/* FIXME: is 0 the proper trust for newly imported identities? */
+                       db.delete(p);
+                       db.commit();
                
                        state.cancel(); /* FIXME: is this necessary */ 
                        mRequests.remove(state);
                } catch (Exception e) {
+                       db.rollback(); /* FIXME: toad: is this safe or might 
some other thread get active and start a transaction which will be interrupted 
by this? */ 
                        Logger.error(this, "Parsing failed for "+ 
state.getURI(), e);
                }
        }

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

Reply via email to