From 5b3fac97c77c5c9945d08d98cb19251ca7736980 Mon Sep 17 00:00:00 2001 From: christoph Date: Fri, 20 Feb 2009 23:50:24 +0100 Subject: Made the login work. Added an elementary feed-list. --- cljssss-g.clj | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/cljssss-g.clj b/cljssss-g.clj index e1cee88..d9f2149 100644 --- a/cljssss-g.clj +++ b/cljssss-g.clj @@ -40,14 +40,25 @@ (with-db (sql/with-query-results [{id :id password :password}] ["SELECT id, password FROM user WHERE name = ?" - (@params :name)] - (if (= password (@params :password)) + (params :name)] + (if (= password (params :password)) (do (alter session assoc :id id) (redirect-to "/")) (redirect-to "/login?valuesofbetawillgiverisetodom=true")))))) + (GET "/bare_feedlist" + (if (not (session :id)) + (redirect-to "/login") + (with-dbt + (sql/with-query-results + [results] + ["SELECT feed.link FROM feed, user_feed_link WHERE user_feed_link.feed=feed.id AND user_feed_link.user=?" 1] + (loop [cstr "" r results] + (if (rest r) + (recur (str cstr (first r) "\n") (rest r)) + (str cstr (first r) "\n"))))))) (GET "/" - (if (@session :id) + (if (session :id) (.toString (doto (.getInstanceOf templates "index") (.setAttributes {"title" "Subscriptions", -- cgit v1.2.3 From 2c2e8de50c5b362891a40a1a727262d742b97c21 Mon Sep 17 00:00:00 2001 From: christoph Date: Sat, 21 Feb 2009 02:26:59 +0100 Subject: Adding an OPML-Export /feedlist.opml which is session-aware. Unfortunately I couldnt create a class fitting to StringTemplate's needs, unfortunately Clojure seems not to be able to create Interfaces on-the-fly and one has to compile them before using them. I couldnt manage to do so, so I decided to create an OPML-String directly. So far, this works. Anyway, added opml.st and opmlclass.clj - which somehow reflect my trial of doing this. --- cljssss-g.clj | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++-------- opml.st | 17 ++++++++++++++ opmlclass.clj | 18 +++++++++++++++ 3 files changed, 100 insertions(+), 9 deletions(-) create mode 100644 opml.st create mode 100644 opmlclass.clj diff --git a/cljssss-g.clj b/cljssss-g.clj index d9f2149..03856e1 100644 --- a/cljssss-g.clj +++ b/cljssss-g.clj @@ -26,6 +26,69 @@ (sql/transaction ~@body))) +(defn opml-init [text xmlurl htmlurl] + (ref {:text text :xmlurl xmlurl :htmlurl htmlurl})) + +(defn opml-getText [this] + (this :text)) +(defn opml-getXmlurl [this] + (this :xmlurl)) +(defn opml-getHtmlurl [this] + (this :htmlurl)) + +;(gen-interface +; :name opmliface +; :prefix "opml-" +; :init init +; :methods [[getXmlurl [] String] +; [getHtmlurl [] String] +; [getText [] String]]) + +;;; FIXME: clojure has no way of creating interfaces on-the-fly. So I +;;; cannot use stringtemplate here. +(defn opml-string [id] + (with-dbt + (sql/with-query-results + results + ["SELECT feed.uri, feed.link, user_feed_link.title FROM feed, user_feed_link WHERE user_feed_link.feed=feed.id AND user_feed_link.user=?" id] + (str "" + "blah" + "Gödel-Gentzen Clojure Syndication Services Super System Feed Export" + (loop [clstr "" r results] + (if (first r) + (recur + (str clstr + "") + (rest r)) + clstr)) + "")))) +; (let [template-object (.getInstanceOf templates "opml"), +; feeds +; (to-array +; (loop [clst '() r results] +; (if (first r) +; (recur +; (concat +; clst +; (list (proxy [Object] [] +; (getText [] +; ((first r) :title)) +; (getXmlurl [] +; ((first r) :uri)) +; (getHtmlurl [] +; ((first r) :link))))) +; (rest r)) + clst)))] +; (. template-object setAttribute "date" "heute") +; (. template-object setAttribute "feeds" feeds) +; (.toString template-object))))) + (defservlet cljssss-g (GET "/login" (if (= (params :valuesofbetawillgiverisetodom) "true") @@ -46,17 +109,10 @@ (alter session assoc :id id) (redirect-to "/")) (redirect-to "/login?valuesofbetawillgiverisetodom=true")))))) - (GET "/bare_feedlist" + (GET "/feedlist.opml" (if (not (session :id)) (redirect-to "/login") - (with-dbt - (sql/with-query-results - [results] - ["SELECT feed.link FROM feed, user_feed_link WHERE user_feed_link.feed=feed.id AND user_feed_link.user=?" 1] - (loop [cstr "" r results] - (if (rest r) - (recur (str cstr (first r) "\n") (rest r)) - (str cstr (first r) "\n"))))))) + (opml-string (session :id)))) (GET "/" (if (session :id) (.toString diff --git a/opml.st b/opml.st new file mode 100644 index 0000000..3fd6360 --- /dev/null +++ b/opml.st @@ -0,0 +1,17 @@ + + + + $date$ + Gödel-Gentzen Clojure Syndication Services Super System Feed Export + + + $feeds:{f | + }$ + + + + diff --git a/opmlclass.clj b/opmlclass.clj new file mode 100644 index 0000000..4486580 --- /dev/null +++ b/opmlclass.clj @@ -0,0 +1,18 @@ +(ns cljssss-g + (require [clojure.xml :as xml] + [clojure.contrib.sql :as sql] + compojure) + (import (org.antlr.stringtemplate StringTemplateGroup) + (com.sun.syndication.io SyndFeedInput XmlReader) + (com.sun.syndication.feed.synd SyndFeed SyndEntry) + (java.net URL)) + (use compojure)) + + +(gen-interface + :name opmliface + :prefix "opml-" + :init init + :methods [[getXmlurl [] String] + [getHtmlurl [] String] + [getText [] String]]) \ No newline at end of file -- cgit v1.2.3 From 253688a08924735061fa058b73af958e1057d52b Mon Sep 17 00:00:00 2001 From: christoph Date: Sun, 22 Feb 2009 02:36:31 +0100 Subject: Just an experiment. --- Geniface.java | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 Geniface.java diff --git a/Geniface.java b/Geniface.java new file mode 100644 index 0000000..371d4c7 --- /dev/null +++ b/Geniface.java @@ -0,0 +1,136 @@ +/* just an experiment - should be implemented in clojure later */ + +import java.io.*; +import javax.tools.*; + +import java.nio.charset.Charset; +import java.util.Locale; +import java.util.UUID; +import java.util.Properties; + +public class Geniface { + + class ByteClassLoader extends ClassLoader{ + String name; byte[] b; + ByteClassLoader (String name, byte[] b) { + this.b=b; + this.name=name; + } + Class getTheClass() { + return defineClass(name, b, 0, b.length); + } + } + + private String temp_prefix; + private String my_uuid; + + private String GeneratedText; + + public Geniface (String temp_prefix) { + this.temp_prefix = temp_prefix; + this.my_uuid = "myuid" + UUID.randomUUID().toString().replace('-', '_'); + this.GeneratedText = + "public interface " + my_uuid + " {"; + } + + public Geniface () { + this("/tmp"); + } + + public void addMethod (String name, int ObjectArgCount, boolean ret) { + /* Add a Method with ObjectArgCount arguments which either + * returns void if !ret, or returns an Object if ret */ + GeneratedText += + (ret ? "\n\tObject " : "\n\tvoid ") + name + "("; + for (int i = 0; i < ObjectArgCount; i++) { + GeneratedText += (i == 0 ? "" : ", ") + "Object arg" + i; + } + GeneratedText += ");"; + } + + public String getSourceString () { + return GeneratedText + "}"; + } + + public static byte[] getBytesFromFile(File file) throws IOException { + InputStream is = new FileInputStream(file); + + // Get the size of the file + long length = file.length(); + + if (length > Integer.MAX_VALUE) { + // File is too large + } + + // Create the byte array to hold the data + byte[] bytes = new byte[(int)length]; + + // Read in the bytes + int offset = 0; + int numRead = 0; + while (offset < bytes.length + && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { + offset += numRead; + } + + // Ensure all the bytes have been read in + if (offset < bytes.length) { + throw new IOException("Could not completely read file "+file.getName()); + } + + // Close the input stream and return bytes + is.close(); + return bytes; + } + + public Class compileToClass () { + try { + String javaname = temp_prefix + "/" + my_uuid + ".java"; + String classname = temp_prefix + "/" + my_uuid + ".class"; + + FileOutputStream out = new FileOutputStream(javaname); + out.write(getSourceString().getBytes()); + out.close(); + + File [] files1 = new File[] + { new File(javaname) }; + + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + DiagnosticCollector diagnostics = new + DiagnosticCollector(); + StandardJavaFileManager fileManager = + compiler.getStandardFileManager(diagnostics, Locale.GERMANY, + Charset.forName("UTF-8")); + + Iterable compilationUnits1 = + fileManager.getJavaFileObjectsFromFiles(java.util.Arrays.asList(files1)); + compiler.getTask(null, fileManager, diagnostics, null, null, +compilationUnits1).call(); + + for (Diagnostic diagnostic : diagnostics.getDiagnostics()) + { + System.out.println(diagnostic); + } + + fileManager.close(); + + return new ByteClassLoader(my_uuid, getBytesFromFile (new File(classname))).getTheClass(); + } + catch (IOException exn1) { + System.out.println("IOExn"); + } + + return null; + + } + + public static void main (String[] args) { + Geniface iface1 = new Geniface(); + iface1.addMethod("Hallo", 10, false); + iface1.addMethod("Fuck", 2, true); + iface1.addMethod("valuesofbetawillgiverisetodom", 1, true); + iface1.addMethod("nilpferd", 0, false); + System.out.println(iface1.getSourceString()); + System.out.println(iface1.compileToClass()); + } +} \ No newline at end of file -- cgit v1.2.3 From 64b7320d281382f325aa5e8b7df64564771c18ba Mon Sep 17 00:00:00 2001 From: christoph Date: Mon, 23 Feb 2009 00:14:41 +0100 Subject: Elementary Methods for showing Per-User Feed- and Entry-Lists (no control for read/unread entries yet) FIXME: Added a variable "sessions", because Clojure didnt compile otherwise. --- Geniface.java | 2 +- cljssss-g.clj | 121 +++++++++++++++++++++++++++++++--------------------------- 2 files changed, 66 insertions(+), 57 deletions(-) diff --git a/Geniface.java b/Geniface.java index 371d4c7..c307050 100644 --- a/Geniface.java +++ b/Geniface.java @@ -34,7 +34,7 @@ public class Geniface { } public Geniface () { - this("/tmp"); + this(System.getProperty("java.io.tmpdir")); } public void addMethod (String name, int ObjectArgCount, boolean ret) { diff --git a/cljssss-g.clj b/cljssss-g.clj index 03856e1..6dcefd7 100644 --- a/cljssss-g.clj +++ b/cljssss-g.clj @@ -17,6 +17,9 @@ :subname "cljssss-g.sqlite3" :create true}) +;; FIXME: WAAAAAAAAAH! +(def session {:id false}) + (defmacro with-db [& body] `(sql/with-connection db-connection-data ~@body)) @@ -26,24 +29,6 @@ (sql/transaction ~@body))) -(defn opml-init [text xmlurl htmlurl] - (ref {:text text :xmlurl xmlurl :htmlurl htmlurl})) - -(defn opml-getText [this] - (this :text)) -(defn opml-getXmlurl [this] - (this :xmlurl)) -(defn opml-getHtmlurl [this] - (this :htmlurl)) - -;(gen-interface -; :name opmliface -; :prefix "opml-" -; :init init -; :methods [[getXmlurl [] String] -; [getHtmlurl [] String] -; [getText [] String]]) - ;;; FIXME: clojure has no way of creating interfaces on-the-fly. So I ;;; cannot use stringtemplate here. (defn opml-string [id] @@ -68,26 +53,48 @@ (rest r)) clstr)) "")))) -; (let [template-object (.getInstanceOf templates "opml"), -; feeds -; (to-array -; (loop [clst '() r results] -; (if (first r) -; (recur -; (concat -; clst -; (list (proxy [Object] [] -; (getText [] -; ((first r) :title)) -; (getXmlurl [] -; ((first r) :uri)) -; (getHtmlurl [] -; ((first r) :link))))) -; (rest r)) - clst)))] -; (. template-object setAttribute "date" "heute") -; (. template-object setAttribute "feeds" feeds) -; (.toString template-object))))) + +(defn lynxy-feedlist [id] + (with-dbt + (sql/with-query-results + results + ["SELECT feed.id, feed.uri, feed.link, user_feed_link.title FROM feed, user_feed_link WHERE user_feed_link.feed=feed.id AND user_feed_link.user=?" id] + (html `[html [head [title "Gödel-Gentzen Clojure Syndication Services Super System"]] + [body [ul + ~@(loop [back '[] r results] + (if (first r) + (recur (concat back `[[li + [a {:href ~(str "lynxy-showfeed?feed=" (:id (first r)))} + ~(:title (first r)) " - " + ~(:link (first r)) + ]]]) + (rest r)) + back))]]])))) + +(defn lynxy-showfeed [id feednum] + (with-dbt + (sql/with-query-results + results + ["SELECT entry.link, entry.title FROM entry, feed_entry_link, user_feed_link WHERE entry.id = feed_entry_link.entry AND feed_entry_link.feed = user_feed_link.feed AND user_feed_link.user = ? AND user_feed_link.feed = ?" id feednum] + (html `[html [head [title "Gödel-Gentzen Clojure Syndication Services Super System"]] + [body [ul + ~@(loop [back '[] r results] + (if (first r) + (recur (concat back `[[li + [a {:href ~(:link (first r))} + ~(:title (first r)) " - " + ~(:link (first r)) + ]]]) + (rest r)) + back))] + [a {:href "lynxy-feedlist.html"} "« To Feedlist"] + ]])))) + +(defmacro with-session [& body] + `(if (not (session :id)) + (redirect-to "/login") + (do ~@body))) + (defservlet cljssss-g (GET "/login" @@ -100,26 +107,28 @@ (.setAttributes {"logintext" "Login"}))))) (POST "/login" (dosync - (with-db - (sql/with-query-results [{id :id password :password}] - ["SELECT id, password FROM user WHERE name = ?" - (params :name)] - (if (= password (params :password)) - (do - (alter session assoc :id id) - (redirect-to "/")) - (redirect-to "/login?valuesofbetawillgiverisetodom=true")))))) + (with-db + (sql/with-query-results [{id :id password :password}] + ["SELECT id, password FROM user WHERE name = ?" + (params :name)] + (if (= password (params :password)) + (do + (alter session assoc :id id) + (redirect-to "/")) + (redirect-to "/login?valuesofbetawillgiverisetodom=true")))))) (GET "/feedlist.opml" - (if (not (session :id)) - (redirect-to "/login") - (opml-string (session :id)))) + (with-session (opml-string (session :id)))) + (GET "/lynxy-feedlist.html" + (with-session (lynxy-feedlist (session :id)))) + (GET "/lynxy-showfeed" + (with-session + (lynxy-showfeed (session :id) (. Integer parseInt (params :feed))))) (GET "/" - (if (session :id) - (.toString - (doto (.getInstanceOf templates "index") - (.setAttributes {"title" "Subscriptions", - "mainParagraph" "Hi there!"}))) - (redirect-to "/login"))) + (with-session + (.toString + (doto (.getInstanceOf templates "index") + (.setAttributes {"title" "Subscriptions", + "mainParagraph" "Hi there!"}))))) (ANY "*" (page-not-found))) -- cgit v1.2.3