Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support for validations and filters.

Presence of properties validation.
  • Loading branch information...
commit 599023fe2143411631bf297f5e3549e32672f82f 1 parent d2ec6bc
@antoniogarrote authored
View
9 src/plaza/rdf/vocabularies/plaza.clj
@@ -10,4 +10,13 @@
(defonce plz "http://plaza.org/vocabularies")
+(defonce plz:RestResource "http://plaza.org/vocabularies/restResource")
(defonce plz:restResourceId "http://plaza.org/vocabularies/restResourceId")
+(defonce plz:validationError "http://plaza.org/vocabularies/validationError")
+
+(register-rdf-ns :plz plz)
+
+(declare-schemas-to-load
+ (defonce plz:RestResource-schema
+ (make-rdfs-schema plz:RestResource
+ :validationError {:uri plz:validationError :range :string})))
View
8 src/plaza/rest/core.clj
@@ -24,6 +24,7 @@
(let [resource (if (keyword? resource-or-symbol) (tbox-find-schema resource-or-symbol) resource-or-symbol)
resource-type (type-uri resource)
resource-map (model-to-argument-map resource)
+ validations (if (nil? (:validations opts)) [] (:validations opts))
id-gen (if (nil? (:id-gen-fn opts)) default-uuid-gen (:id-gen-fn opts))
id-property-alias (if (nil? (:id-property-alias opts)) :id (:id-property-alias opts))
id-property-uri (if (and (nil? (:id-property-uri opts))
@@ -82,7 +83,12 @@
:kind :collection
:id-property-alias id-property-alias
:id-property-uri id-property-uri
- :filters filters}))
+ ;; list with the validations to apply
+ :filters filters
+ ;; array of validations
+ :validations validations
+ ;; initial valid status
+ :status 200 }))
(defn make-single-resource-environment-map [resource-or-symbol path ts opts]
(let [coll-env (make-environment-map resource-or-symbol path ts opts)
View
25 src/plaza/rest/handlers.clj
@@ -30,20 +30,29 @@
build-graph-query-handler [(:build-graph-query-handler environment)]
build-triples-handler [(:build-triples-handler environment)]
render-triples-handler [(:render-triples-handler environment)]
+ validation-handlers (let [req-method (:request-method request)]
+ (if (or (= :post req-method) (= :put req-method))
+ (:validations environment) []))
handlers (concat pre-graph-filters build-graph-query-handler post-graph-filters
+ validation-handlers
pre-build-filters build-triples-handler post-build-filters
pre-render-filters render-triples-handler post-render-filters)]
(loop [request request
environment environment
handlers handlers]
(if (empty? handlers)
- {:body (:body environment)
- :headers (:headers environment)
- :status (:status environment)}
- (let [handler (first handlers)]
- (recur request
- (handler request environment)
- (rest handlers))))))))
+ ;; all handlers already processed
+ {:body (:body environment)
+ :headers (:headers environment)
+ :status (:status environment)}
+ (let [handler (first handlers)
+ environmentp (handler request environment)]
+ (if (or (< 200 (:status environmentp))
+ (> 200 (:status environmentp)))
+ ;; error code returned, halting processing
+ (recur request environmentp [])
+ ;; no error code returned, continue processing
+ (recur request environmentp (rest handlers)))))))))
(defn default-render-triples-handler
([request environment]
@@ -223,7 +232,7 @@
(defn delete-collection-build-graph
([request environment]
(let [mapping (apply-resource-argument-map (:params request) (:resource-map environment))
- query (build-query-from-resource-map mapping (:resource-type environment))]
+ query (build-query-from-resource-map mapping (:resource-type environment))]
(-> environment
(assoc :mapping mapping)
(assoc :query query)))))
View
6 src/plaza/rest/utils.clj
@@ -681,14 +681,14 @@
(defn default-service-metadata-matcher-fn
([request environment]
- (if (nil? (re-find #"collection_resource_service(\..*)?$" (:uri request)))
- (if (nil? (re-find #"single_resource_service(\..*)?$" (:uri request)))
+ (if (nil? (re-find #"_collection_resource_service(\..*)?$" (:uri request)))
+ (if (nil? (re-find #"_single_resource_service(\..*)?$" (:uri request)))
false :individual)
:collection)))
(defn default-schema-metadata-matcher-fn
([request environment]
- (if (nil? (re-find #"schema(\..*)?$" (:uri request))) false true)))
+ (if (nil? (re-find #"_schema(\..*)?$" (:uri request))) false true)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Resource functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
View
34 src/plaza/rest/validations.clj
@@ -0,0 +1,34 @@
+;; @author Antonio Garrote
+;; @email antoniogarrote@gmail.com
+;; @date 12.07.2010
+
+(ns plaza.rest.validations
+ (:use
+ [clojure.contrib.logging :only [log]]
+ [clojure.contrib.seq-utils :only [includes?]]
+ [plaza utils]
+ [plaza.rdf core]
+ [plaza.rdf.vocabularies plaza]
+ [plaza.rest utils])
+ (:require
+ [clojure.contrib.json :as json]))
+
+
+(defn validate-presence-property-urls
+ "Checks that a list of RDF properties have received values after processing the HTTP request parameters"
+ ([property-urls]
+ (fn [request environment]
+ (let [graph (:mapping environment)
+ predicates-pre (map (fn [[p o]] (let [op (if (keyword? o) (name o) (str o))]
+ (if (.startsWith op "?") nil (str p)))) graph)
+ predicates (reduce (fn [acum p] (if (nil? p) acum (conj acum p))) [] predicates-pre)
+ errors (reduce (fn [acum prop]
+ (if (includes? predicates (str prop))
+ acum
+ (conj acum [(rdf-resource prop) (rdf-property plz:validationError) (l "property not found")])))
+ [] property-urls)]
+ (if (empty? errors)
+ environment
+ (-> environment
+ (assoc :status 400)
+ (assoc :body (render-triples errors (mime-to-format request) plz:RestResource request))))))))
View
1  test/plaza/rdf/schemas_test.clj
@@ -5,6 +5,7 @@
(init-jena-framework)
(use 'plaza.rdf.vocabularies.foaf)
+(init-vocabularies)
(defonce *test-model* (make-rdfs-schema ["http://something/" "Good"]
:name {:uri "http://test.com/name" :range :string}
View
36 test/plaza/rest/core_test.clj
@@ -10,7 +10,7 @@
(:use [plaza.rdf.vocabularies.foaf] :reload-all)
(:use [plaza.triple-spaces.core] :reload-all)
(:use [plaza.rdf.implementations.stores.mulgara])
- (:use [plaza.rest core utils handlers] :reload-all)
+ (:use [plaza.rest core utils handlers validations] :reload-all)
(:use [clojure.test])
(:use [clojure.contrib.logging :only [log]])
(:require [clojure-http.resourcefully]
@@ -29,7 +29,7 @@
(defn- build-mulgara
([] (build-model :mulgara :rmi "rmi://localhost/server1")))
-(defonce *should-test* true)
+(defonce *should-test* false)
(when *should-test*
@@ -65,6 +65,10 @@
;; Application routes
(defroutes example
+
+ (spawn-rest-collection-resource! :foaf-agent "/AgentTestValidatePresence" :resource
+ :validations [(validate-presence-property-urls [foaf:age foaf:status])])
+
(spawn-rest-resource! :foaf-agent "/Agent/:id" :resource)
(spawn-rest-collection-resource! :foaf-agent "/Agent" :resource)
@@ -273,12 +277,38 @@
(deftest test-service_descriptions
(println "***********************************************\n SERVICE DESCRIPTIONS \n************************************************")
(clojure-http.resourcefully/delete "http://localhost:8082/Agent")
- (let [res1 (clojure-http.resourcefully/get "http://localhost:8082/Agent/collection_resource_service.n3")
+ (let [res1 (clojure-http.resourcefully/get "http://localhost:8082/Agent/_collection_resource_service.n3")
m1 (build-model :jena)]
(with-model m1 (document-to-model (java.io.ByteArrayInputStream. (.getBytes (apply str (:body-seq res1)))) :n3))
(is (= 238 (count (model-to-triples m1))))))
+ (deftest test-del-post-get-validation-pesence-n3-ok
+ (println "**********************************************\n DELETE - POST - GET VALIDATES PRESENCE OK N3 \n*************************************************")
+ (clojure-http.resourcefully/delete "http://localhost:8082/AgentTestValidatePresence")
+ (let [res (clojure-http.resourcefully/post "http://localhost:8082/AgentTestValidatePresence?age=20&status=ok")
+ m (build-model :jena)]
+ (with-model m (document-to-model (java.io.ByteArrayInputStream. (.getBytes (apply str (:body-seq res)))) :xml))
+ (is (= 4 (count (model-to-triples m))))
+ (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))
+ (doseq [t (model-to-triples m2)] (log :error (str "*** " t)))
+ (is (= 4 (count (model-to-triples m2))))
+ (is (= (str (first (first (model-to-triples m2))))
+ (str (first (first (model-to-triples m)))))))))
+
+ (deftest test-del-post-get-validation-pesence-n3-nok
+ (println "**********************************************\n DELETE - POST - GET VALIDATES PRESENCE NOK N3 \n*************************************************")
+ (clojure-http.resourcefully/delete "http://localhost:8082/AgentTestValidatePresence")
+ (try
+ (let [res (clojure-http.resourcefully/post "http://localhost:8082/AgentTestValidatePresence?gender=male")]
+ (is false))
+ (catch Exception ex
+ (is true))))
+
+
(catch Exception ex (throw ex))
(finally (do (log :info "CLEANING...")
(shutdown-agents)))))
View
41 test/plaza/rest/validations_test.clj
@@ -0,0 +1,41 @@
+;; @author Antonio Garrote
+;; @email antoniogarrote@gmail.com
+;; @date 12.07.2010
+
+(ns plaza.rest.validations-test
+ (:use
+ [clojure.contrib.logging :only [log]]
+ [plaza.rdf core]
+ [plaza.rdf.implementations jena]
+ [plaza.rest validations]
+ [clojure.test])
+ (:require
+ [clojure.contrib.json :as json]))
+
+(init-jena-framework)
+
+(defonce test1 "http://test.com/test1")
+(defonce test2 "http://test.com/test2")
+
+(deftest test-validate-presence-property-urls
+ (let [validation (validate-presence-property-urls [test1 test2])]
+ (let [fake-request {:params {"format" "js3"}}
+ fake-environment {:mapping [[test1 "b"] [test2 "b"]] :status 200 }
+ fake-environmentp (validation fake-request fake-environment)]
+ (is (= (:status fake-environmentp) 200))
+ (is (nil? (:body fake-environmentp))))
+
+ (let [fake-request {:params {"format" "js3"}}
+ fake-environment {:status 200 :mapping []}
+ fake-environmentp (validation fake-request fake-environment)
+ body (json/read-json (:body fake-environmentp))]
+ (is (= (:status fake-environmentp 403)))
+ (is (= 2 (count body))))
+
+ (let [fake-request {:params {"format" "js3"}}
+ fake-environment {:status 200 :mapping [[test1 "b"]]}
+ fake-environmentp (validation fake-request fake-environment)
+ body (json/read-json (:body fake-environmentp))]
+ (is (= (:status fake-environmentp 403)))
+ (is (= 1 (count body)))
+ (is (= test2 (str (first (first body))))))))
Please sign in to comment.
Something went wrong with that request. Please try again.