3 (:use clj-stacktrace.repl)
4 (:require clojure.contrib.string)
5 (:require clojure.contrib.http.agent)
6 (:require clojure-http.resourcefully)
7 (:require clojure.pprint)
9 (:import java.io.ByteArrayInputStream)
13 " Replacing these characters http encoded ones
23 (clojure.contrib.string/replace-str " " "%20"
24 (clojure.contrib.string/replace-str "'" "%27"
25 (clojure.contrib.string/replace-str ";" "%3B"
26 (clojure.contrib.string/replace-str "[" "%5B"
27 (clojure.contrib.string/replace-str "@" "%40"
28 (clojure.contrib.string/replace-str "=" "%3D"
29 (clojure.contrib.string/replace-str "]" "%5D" text
33 (defn url-encode-spaces
34 " Replacing just spaces
38 (clojure.contrib.string/replace-str " " "%20" text )
40 (defn url-encode-newlines
41 " Replacing just newlines"
44 (clojure.contrib.string/replace-str (str \newline) "" text )
46 (defn strip-xml-header [text]
47 (clojure.contrib.string/replace-str "<?xml version='1.0' encoding='UTF-8'?>" "" text )
51 (defn filterSpacesFromXML [text]
53 (clojure.contrib.string/replace-str "< " "<"
54 (clojure.contrib.string/replace-str " : " ":"
55 (clojure.contrib.string/replace-str " / >" " />"
56 (clojure.contrib.string/replace-str "< /" "</"
57 (clojure.contrib.string/replace-str "</ " "</"
58 (clojure.contrib.string/replace-str " / " "/"
59 (clojure.contrib.string/replace-re #"=\"\s" "=\""
60 (clojure.contrib.string/replace-re #"\s\"" "\""
61 (clojure.contrib.string/replace-str " = " "="
67 ;; set get base URL ...TODO - put in config
68 ;; TODO - replace URL with a config value
69 (def db-base-URL "http://localhost:8080/exist/rest/")
72 ;; set root/system dir fragment ...TODO - put in config
73 (def db-system-DIR "rootDir/system.main.system/")
75 ;; working directory lookup ...TODO - put these lookups into config
76 (defn working-dir-lookup [token]
78 ;;(println "DEBUG > 'working-dir-lookup' CALLED > ["(keyword token)"]" )
79 ( { :group "aauthentication.main.authentication/groups.aauth.groups"
80 :user "aauthentication.main.authentication/users.aauth.users"
81 :users "aauthentication.main.authentication/users.aauth.users"
82 :account "groups.main.groups"
83 :journal "groups.main.groups"
84 :entry "groups.main.groups"
85 :entries "groups.main.groups"
86 :debit "groups.main.groups"
87 :credit "groups.main.groups"
88 :bookkeeping "groups.main.groups"
94 (defn namespace-lookup
97 ;;(println "DEBUG > 'namespace-lookup' CALLED > ["token"]" )
98 ( { "group" "com/interrupt/bookkeeping/users"
99 "user" "com/interrupt/bookkeeping/users"
100 "users" "com/interrupt/bookkeeping/users"
101 "account" "com/interrupt/bookkeeping/account"
102 "journal" "com/interrupt/bookkeeping/journal"
103 "entry" "com/interrupt/bookkeeping/journal"
104 "entries" "com/interrupt/bookkeeping/journal"
105 "debit" "com/interrupt/bookkeeping/account"
106 "credit" "com/interrupt/bookkeeping/account"
112 (defn execute-http-call [ full-URL http-method header-hash xml-content ]
114 ;; from DB, get 'token' for 'option' args & value
115 (println "DEBUG > FINAL http query[" full-URL "] > http-method[" http-method "] > header-hash[" header-hash "] > xml-content[" xml-content "]")
118 (. "GET" equals http-method)
120 (clojure-http.resourcefully/get full-URL header-hash xml-content)
121 (catch Exception e { :msg "Error" :dmsg (. e getMessage ) } )
124 (. "PUT" equals http-method)
126 (clojure-http.resourcefully/put full-URL header-hash xml-content)
127 (catch Exception e { :msg "Error" :dmsg (. e getMessage ) } )
129 (. "POST" equals http-method)
131 (clojure-http.resourcefully/post full-URL header-hash xml-content)
132 (catch Exception e { :msg "Error" :dmsg (. e getMessage ) } )
134 (. "DELETE" equals http-method)
136 (clojure-http.resourcefully/delete full-URL header-hash nil)
137 (catch Exception e { :msg "Error" :dmsg (. e getMessage ) } )
144 (defn parse-xml-to-hash [x-input]
146 (clojure.xml/parse (ByteArrayInputStream. (.getBytes
147 (clojure.contrib.str-utils/str-join nil x-input) ;; get the XML string
151 (defn get-user [db-base-URL db-system-DIR working-USER]
155 (execute-http-call ;; TODO - put in 404 check
159 (working-dir-lookup (:tag working-USER)) ;; stringing together lookup URL leaf
162 (name (:tag working-USER))
164 (:id (:attrs working-USER)))
166 (str ;; repeating user name as leaf document
167 (name (:tag working-USER))
169 (:id (:attrs working-USER))))
171 {"Content-Type" "text/xml"}
175 (println "RESULT USER > result-hash... " result-hash)
176 (parse-xml-to-hash (:body-seq result-hash))