Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

version bump 0.0.5

  • Loading branch information...
commit 6d5aebb9601e4c8afb52b933993c70ff77eb0993 1 parent 9d196a8
@antoniogarrote authored
View
2  project.clj
@@ -1,4 +1,4 @@
-(defproject plaza "0.0.4-SNAPSHOT"
+(defproject plaza "0.0.5-SNAPSHOT"
:description "Plaza framework for semantic distributed applications"
:dependencies [[org.clojure/clojure "1.2.0-master-SNAPSHOT"]
[org.clojure/clojure-contrib "1.2.0-SNAPSHOT"]
View
37 src/plaza/examples/webapp.clj
@@ -3,41 +3,48 @@
;; @date 07.06.2010
(ns plaza.examples.webapp
- (:use [plaza.rest.core])
- (:use plaza.rdf.implementations.jena
- compojure.core
- compojure.response
- ring.adapter.jetty
- [plaza.rdf.core]
- [plaza.rdf.schemas]
+ (:use [compojure core]
+ [compojure response]
+ [ring.adapter jetty]
+ [plaza.rdf core schemas]
+ [plaza.rdf.implementations jena]
[plaza.triple-spaces.server.mulgara]
[plaza.triple-spaces.core]
- [plaza.rdf.implementations.jena]
[plaza.triple-spaces.distributed-server]
- [clojure.contrib.logging :only [log]])
+ [plaza.rest core])
(:require [compojure.route :as route]))
;; We will use jena
(init-jena-framework)
-(load-rdfs-schemas)
+;(load-rdfs-schemas)
;; We load the Friend Of A Friend vocabulary
;; and register the Agent schema in the TBox
(use 'plaza.rdf.vocabularies.foaf)
-(tbox-register-schema :foaf-agent foaf:Agent-schema)
+
+(def ComputationCelebrity-schema
+ (make-rdfs-schema foaf:Person
+ :name {:uri foaf:name :range :string}
+ :surname {:uri foaf:surname :range :string}
+ :nick {:nick foaf:nick :range :string}
+ :birthday {:uri foaf:birthday :range :date}
+ :interest {:uri foaf:topic_interest :range :string}
+ :wikipedia_entry {:uri foaf:holdsAccount :range rdfs:Resource}))
+
+(tbox-register-schema :celebrity ComputationCelebrity-schema)
;; We create a Triple Space for the resources
(defonce *mulgara* (build-model :mulgara :rmi "rmi://localhost/server1"))
-(def-ts :resource (make-distributed-triple-space "test" *mulgara* :redis-host "localhost" :redis-db "testdist" :redis-port 6379))
+(def-ts :celebrities (make-distributed-triple-space "test" *mulgara* :redis-host "localhost" :redis-db "testdist" :redis-port 6379))
;; Application routes
(defroutes example
(GET "/" [] "<h1>Testing plaza...</h1>")
- (spawn-rest-resource! :foaf-agent "/Agent/:id" :resource)
- (spawn-rest-collection-resource! :foaf-agent "/Agent" :resource)
+ (spawn-rest-resource! :celebrity "/Celebrity/:id" :celebrities)
+ (spawn-rest-collection-resource! :celebrity "/Celebrity" :celebrities)
(route/not-found "Page not found"))
;; Runnin the application
-(run-jetty (var example) {:port 8081})
+;(run-jetty (var example) {:port 8081})
View
34 src/plaza/rdf/vocabularies/foaf.clj
@@ -103,6 +103,40 @@
:tipjar {:uri foaf:tipjar :range :string}))
+(defonce foaf:Person-schema
+ (make-rdfs-schema foaf:Person
+ :knows {:uri foaf:knows :range foaf:knows}
+ :schoolHomepage {:uri foaf:schoolHomepage :range foaf:Document}
+ :firstName {:uri foaf:firstName :range :string}
+ :familyName {:uri foaf:familyName :range :string}
+ :topic_interest {:uri foaf:topic_interest :range "http://www.w3.org/2002/07/owl#Thing"}
+ :currentProject {:uri foaf:currentProject :range "http://www.w3.org/2002/07/owl#Thing"}
+ :geekcode {:uri foaf:geekcode :range :string}
+ :img {:uri foaf:img :range rdfs:Resource}
+ :workInfoHomepage {:uri foaf:workInfoHomepage :range foaf:Document}
+ :pastProject {:uri foaf:pastProject :range "http://www.w3.org/2002/07/owl#Thing"}
+ :surname {:uri foaf:surname :range :string}
+ ;; subclass of Agent
+ :weblog {:uri foaf:weblog :range foaf:Document }
+ :gender {:uri foaf:gender :range :string }
+ :holdsAccount {:uri foaf:holdsAccount :range foaf:OnlineAccount }
+ :birthday {:uri foaf:birthday :range :date }
+ :age {:uri foaf:age :range :integer }
+ :made {:uri foaf:made :range "http://www.w3.org/2002/07/owl#Thing"}
+ :skypeID {:uri foaf:skypeID :range :string}
+ :msnChatID {:uri foaf:msnChatID :range :string}
+ :icqChatID {:uri foaf:icqChatID :range :string}
+ :mbox {:uri foaf:mbox :range "http://www.w3.org/2002/07/owl#Thing"}
+ :status {:uri foaf:status :range :string}
+ :mbox_sha1sum {:uri foaf:mbox_sha1sum :range :string}
+ :account {:uri foaf:account :range foaf:OnlineAccount}
+ :yahooChatID {:uri foaf:yahooChatID :range :string}
+ :aimChatID {:uri foaf:aimChatID :range :string}
+ :jabberID {:uri foaf:jabberID :range :string}
+ :openid {:uri foaf:openid :range :string }
+ :tipjar {:uri foaf:tipjar :range :string}))
+
+
(defonce foaf:OnlineAccount-schema
(make-rdfs-schema foaf:OnlineAccount
:accountName {:uri foaf:accountName :range :string }
View
33 src/plaza/rest/core.clj
@@ -17,9 +17,8 @@
[clojure.contrib.str-utils2 :as str2]))
-(defn default-css-text
- []
- " * {
+(defn default-css-text []
+ " * {
margin: 0;
padding: 0;
border: 0;
@@ -192,7 +191,10 @@
([ts]
(let [tsp (map (fn [[s p o]]
(if (is-literal o)
- [(str s) (str p) {:value (literal-value o) :datatype (literal-datatype-uri o)}]
+ (let [value-pre (literal-value o)
+ value (if (or (instance? com.hp.hpl.jena.datatypes.xsd.XSDDateTime value-pre))
+ (str value-pre) value-pre)]
+ [(str s) (str p) {:value value :datatype (literal-datatype-uri o)}])
[(str s) (str p) (str o)]))
ts)]
(json/json-str tsp))))
@@ -206,7 +208,9 @@
(let [tsp (reduce (fn [acum [s p o]]
(if (is-literal o)
(let [pred (property-alias schema (str p))
- value (literal-value o)]
+ value-pre (literal-value o)
+ value (if (or (instance? com.hp.hpl.jena.datatypes.xsd.XSDDateTime value-pre))
+ (str value-pre) value-pre)]
(if (nil? pred) (assoc acum (str p) value)
(assoc acum pred value)))
(let [pred (property-alias schema (str p))]
@@ -389,13 +393,16 @@
"json:jsonp" "application/json"
"js:jsonp" "application/json"
"js3:jsonp" "application/json"
- "rdfa" "application/html+xml"
+ "rdfa" "text/html"
"html" "text/html"
- "xhtml" "application/html+xml"
+ "xhtml" "text/html"
"application/xml")))
-(defn default-uuid-gen [prefix local request]
- (random-resource-id (str prefix local)))
+(defn default-uuid-gen [request environment]
+ (let [prefix (:resource-qname-prefix environment)
+ local (:resource-qname-local environment)
+ port (if (= (:server-port request) 80) "" (str ":" (:server-port request)))]
+ (random-resource-id (str prefix port local))))
(defmacro wrap-request [kind prefix local request & body]
`(let [pre# (System/currentTimeMillis)]
@@ -436,10 +443,12 @@
(if (not (nil? (get @*tbox* alias-or-uri)))
(get @*tbox* alias-or-uri)
(first (filter #(= (str alias-or-uri) (str (type-uri %1))) (vals @*tbox*))))))
+
(defn default-id-match
"Matches a resource using the requested URI"
([request environment]
- (let [pattern (str (:resource-qname-prefix environment) (:resource-qname-local environment))]
+ (let [port (if (= (:server-port request) 80) "" (str ":" (:server-port request)))
+ pattern (str (:resource-qname-prefix environment) port (:resource-qname-local environment))]
(str2/replace pattern ":id" (get (:params request) "id")))))
(defn default-service-metadata-matcher-fn
@@ -549,7 +558,7 @@
query (build-single-resource-query-from-resource-map mapping id)
results (rd (ts (:resource-ts environment)) query)
triples (distinct (flatten-1 results))]
- (log :info (str "GET REQUEST -> mapping:" mapping " triples:" triples))
+ (log :info (str "GET REQUEST -> mapping:" mapping " triples:" triples " for query " query " and id " id))
{:body (render-triples triples (mime-to-format request) (:resource environment) request)
:headers {"Content-Type" (format-to-mime request)}
:status 200}))
@@ -593,7 +602,7 @@
(defn handle-post-collection [request environment]
(let [mapping (apply-resource-argument-map (:params request) (:resource-map environment))
- resource-id ((:id-gen-function environment) (:resource-qname-prefix environment) (:resource-qname-local environment) request)
+ resource-id ((:id-gen-function environment) request environment)
triples-pre (build-triples-from-resource-map resource-id mapping)
triples (conj triples-pre [resource-id rdf:type (:resource-type environment)])]
(log :info (str "POST REQUEST -> id:" resource-id " mapping:" mapping " triples:" triples))
View
120 test/plaza/rest/core_test.clj
@@ -11,7 +11,8 @@
(:use [plaza.rest.core] :reload-all)
(:use [clojure.test])
(:use [clojure.contrib.logging :only [log]])
- (:require [clojure-http.resourcefully]))
+ (:require [clojure-http.resourcefully]
+ [clojure.contrib.str-utils2 :as str2]))
(init-jena-framework)
(load-rdfs-schemas)
@@ -64,20 +65,37 @@
;; Application routes
(defroutes example
(spawn-rest-resource! :foaf-agent "/Agent/:id" :resource)
+
(spawn-rest-collection-resource! :foaf-agent "/Agent" :resource)
- ; testing of forbidden methods
+ ; testing of forbidden methods
+
(spawn-rest-collection-resource! :foaf-agent "/WriteOnlyResource" :resource
:allowed-methods [:post :delete])
+
(spawn-rest-collection-resource! :foaf-agent "/CustomGetCollection" :resource
:allowed-methods [:get]
:get-handle-fn (fn [request environment] {:body "custom get handler"
:headers {"Content-Type" "text-plain"}
:status 200}))
+
(spawn-rest-resource! :foaf-agent "/CustomGet/:id" :resource
:allowed-methods [:get]
:get-handle-fn (fn [id request environment] {:body "custom get handler"
:headers {"Content-Type" "text-plain"}
- :status 200})))
+ :status 200}))
+
+ (spawn-rest-resource! :foaf-agent "/CustomIds/:name" :resource
+ :allowed-methods [:get]
+ :id-match-fn (fn [req env]
+ (let [pattern (str (:resource-qname-prefix env) (:resource-qname-local env))]
+ (str2/replace pattern ":name" (get (:params req) "name")))))
+
+ (spawn-rest-collection-resource! :foaf-agent "/CustomIds" :resource
+ :allowed-methods [:post]
+ :id-gen-fn (fn [req env]
+ (let [prefix (:resource-qname-prefix env)
+ local (:resource-qname-local env)]
+ (str prefix local "/" (get (:params req) "name"))))))
;; Runnin the application
(future (run-jetty (var example) {:port 8082}))
@@ -99,8 +117,7 @@
m (build-model :jena)]
(with-model m (document-to-model (java.io.ByteArrayInputStream. (.getBytes (apply str (:body-seq res)))) :xml))
(is (= 3 (count (model-to-triples m))))
- (let [subj-pre (str (first (first (model-to-triples m))))
- subj (clojure.contrib.str-utils2/replace subj-pre "localhost" "localhost:8082")
+ (let [subj (str (first (first (model-to-triples m))))
res2 (clojure-http.resourcefully/get (str subj ".n3"))
m2 (build-model :jena)]
(with-model m2 (document-to-model (java.io.ByteArrayInputStream. (.getBytes (apply str (:body-seq res2)))) :n3))
@@ -115,8 +132,7 @@
m (build-model :jena)]
(with-model m (document-to-model (java.io.ByteArrayInputStream. (.getBytes (apply str (:body-seq res)))) :xml))
(is (= 3 (count (model-to-triples m))))
- (let [subj-pre (str (first (first (model-to-triples m))))
- subj (clojure.contrib.str-utils2/replace subj-pre "localhost" "localhost:8082")
+ (let [subj (str (first (first (model-to-triples m))))
m2 (build-model :jena)]
(load-stream m2 (str subj ".html") :html)
(is (= 3 (count (model-to-triples m2))))
@@ -130,8 +146,7 @@
m (build-model :jena)]
(with-model m (document-to-model (java.io.ByteArrayInputStream. (.getBytes (apply str (:body-seq res)))) :xml))
(is (= 3 (count (model-to-triples m))))
- (let [subj-pre (str (first (first (model-to-triples m))))
- subj (clojure.contrib.str-utils2/replace subj-pre "localhost" "localhost:8082")
+ (let [subj (str (first (first (model-to-triples m))))
res2 (clojure-http.resourcefully/get (str subj ".js3"))
ts (clojure.contrib.json/read-json (apply str (:body-seq res2)))]
(doseq [t ts] (log :info t))
@@ -144,8 +159,23 @@
m (build-model :jena)]
(with-model m (document-to-model (java.io.ByteArrayInputStream. (.getBytes (apply str (:body-seq res)))) :xml))
(is (= 3 (count (model-to-triples m))))
- (let [subj-pre (str (first (first (model-to-triples m))))
- subj (clojure.contrib.str-utils2/replace subj-pre "localhost" "localhost:8082")
+ (let [subj (str (first (first (model-to-triples m))))
+ res2 (clojure-http.resourcefully/get (str subj ".json"))
+ ts (clojure.contrib.json/read-json (apply str (:body-seq res2)))]
+ (log :info (str "triples: " ts))
+ (is (= 20 (:age ts)))
+ (is (= "male" (:gender ts)))
+ (is (= "http://xmlns.com/foaf/0.1/Agent" (:type ts)))
+ (is (= 4 (count (keys ts)))))))
+
+ (deftest test-del-post-get-form
+ (println "***************************************************\n DELETE - POST FORM \n******************************************************")
+ (clojure-http.resourcefully/delete "http://localhost:8082/Agent")
+ (let [res (clojure-http.resourcefully/post "http://localhost:8082/Agent" {} {"age" 20 "gender" "male"})
+ m (build-model :jena)]
+ (with-model m (document-to-model (java.io.ByteArrayInputStream. (.getBytes (apply str (:body-seq res)))) :xml))
+ (is (= 3 (count (model-to-triples m))))
+ (let [subj (str (first (first (model-to-triples m))))
res2 (clojure-http.resourcefully/get (str subj ".json"))
ts (clojure.contrib.json/read-json (apply str (:body-seq res2)))]
(log :info (str "triples: " ts))
@@ -154,38 +184,36 @@
(is (= "http://xmlns.com/foaf/0.1/Agent" (:type ts)))
(is (= 4 (count (keys ts)))))))
- (deftest test-del-post-get-form
- (println "***************************************************\n DELETE - POST FORM \n******************************************************")
- (clojure-http.resourcefully/delete "http://localhost:8082/Agent")
- (let [res (clojure-http.resourcefully/post "http://localhost:8082/Agent" {} {"age" 20 "gender" "male"})
- m (build-model :jena)]
- (with-model m (document-to-model (java.io.ByteArrayInputStream. (.getBytes (apply str (:body-seq res)))) :xml))
- (is (= 3 (count (model-to-triples m))))
- (let [subj-pre (str (first (first (model-to-triples m))))
- subj (clojure.contrib.str-utils2/replace subj-pre "localhost" "localhost:8082")
- res2 (clojure-http.resourcefully/get (str subj ".json"))
- ts (clojure.contrib.json/read-json (apply str (:body-seq res2)))]
- (log :info (str "triples: " ts))
- (is (= 20 (:age ts)))
- (is (= "male" (:gender ts)))
- (is (= "http://xmlns.com/foaf/0.1/Agent" (:type ts)))
- (is (= 4 (count (keys ts)))))))
-
-(deftest test-get-fobidden
- (println "***************************************************\n DELETE - GET FORBIDEN \n******************************************************")
- (clojure-http.resourcefully/delete "http://localhost:8082/WriteOnlyResource")
- (let [res-post (clojure-http.resourcefully/post "http://localhost:8082/WriteOnlyResource" {} {"age" 20 "gender" "male"})]
- (try (clojure-http.resourcefully/get "http://localhost:8082/WriteOnlyResource")
- (is false) ;; this method is not allowed, this should not be executed
- (catch java.io.IOException ex (is true)))))
-
-(deftest test-custom-get
- (println "***************************************************\n CUSTOM HANDLERS \n******************************************************")
- (let [res1 (clojure-http.resourcefully/get "http://localhost:8082/CustomGet/something")
- res2 (clojure-http.resourcefully/get "http://localhost:8082/CustomGetCollection")]
- (is (= "custom get handler" (apply str (:body-seq res1))))
- (is (= "custom get handler" (apply str (:body-seq res2))))))
-
- (catch Exception ex (throw ex))
- (finally (do (log :info "CLEANING...")
- (shutdown-agents)))))
+ (deftest test-get-forbiden
+ (println "***************************************************\n DELETE - GET FORBIDEN \n******************************************************")
+ (clojure-http.resourcefully/delete "http://localhost:8082/WriteOnlyResource")
+ (let [res-post (clojure-http.resourcefully/post "http://localhost:8082/WriteOnlyResource" {} {"age" 20 "gender" "male"})]
+ (try (clojure-http.resourcefully/get "http://localhost:8082/WriteOnlyResource")
+ (is false) ;; this method is not allowed, this should not be executed
+ (catch java.io.IOException ex (is true)))))
+
+ (deftest test-custom-get
+ (println "***************************************************\n CUSTOM HANDLERS \n******************************************************")
+ (let [res1 (clojure-http.resourcefully/get "http://localhost:8082/CustomGet/something")
+ res2 (clojure-http.resourcefully/get "http://localhost:8082/CustomGetCollection")]
+ (is (= "custom get handler" (apply str (:body-seq res1))))
+ (is (= "custom get handler" (apply str (:body-seq res2))))))
+
+ (deftest test-custom-id-fn
+ (println "***************************************************\n DELETE - POST - GET CUSTOM ID \n******************************************************")
+ (clojure-http.resourcefully/delete "http://localhost:8082/Agent")
+ (let [res (clojure-http.resourcefully/post "http://localhost:8082/CustomIds?age=20&gender=male&name=wadus")
+ m (build-model :jena)]
+ (with-model m (document-to-model (java.io.ByteArrayInputStream. (.getBytes (apply str (:body-seq res)))) :xml))
+ (is (= 3 (count (model-to-triples m))))
+ (let [res2 (clojure-http.resourcefully/get "http://localhost:8082/CustomIds/wadus.n3")
+ m2 (build-model :jena)]
+ (with-model m2 (document-to-model (java.io.ByteArrayInputStream. (.getBytes (apply str (:body-seq res2)))) :n3))
+ (is (= 3 (count (model-to-triples m2))))
+ (is (= (str (first (first (model-to-triples m2))))
+ (str (first (first (model-to-triples m)))))))))
+
+
+ (catch Exception ex (throw ex))
+ (finally (do (log :info "CLEANING...")
+ (shutdown-agents)))))
Please sign in to comment.
Something went wrong with that request. Please try again.