Paste number 73958: apache xml-rpc wrapper

Index of paste annotations: 1

Paste number 73958: apache xml-rpc wrapper
Pasted by: meredydd
When:3 years, 3 months ago
Share:Tweet this! | http://paste.lisp.org/+1L2E
Channel:#clojure
Paste contents:
Raw Source | XML | Display As
; Yes, this API is exactly as stupid as it looks. Design Patterns, oy vey.
(defn xmlrpc-servlet
  "Returns a servlet which calls the supplied function for every XML-RPC
   request.
   handler-fn => (fn [method-name & args] ...)"
  [handler-fn]
    (proxy [XmlRpcServlet] []
      (newXmlRpcHandlerMapping []
        (proxy [XmlRpcHandlerMapping] []
          (getHandler [handler-name]
            (proxy [XmlRpcHandler] []
              (execute [#^XmlRpcRequest request]
                (let [nargs  (.getParameterCount request)
                      args   (map #(.getParameter request %) (range 0 nargs))
                      method (.getMethodName request)]
                  (jmap (apply handler-fn method args))))))))))

Annotations for this paste:

Annotation number 1: jmap
Pasted by: meredydd
When:3 years, 3 months ago
Share:Tweet this! | http://paste.lisp.org/+1L2E/1
Paste contents:
Raw Source | Display As
; This is the (jmap) function referred to above. It turns any Clojure
; persistent maps you return into java.util.Map implementations, so the XML-RPC
; libs understand you.


; This is an eager transformation (this code predates the existence of Delays,
; and I couldn't be bothered to make it lazy by hand, when in this particular
; case I know the entire structure will be traversed anyway).

(defn to-map
  "Return a java.util.Map, backed by the supplied Clojure map.
  (Clojure maps can't be java.util.Maps themselves because that
  conflicts with java.util.Collection"
  [#^clojure.lang.IPersistentMap map]
    (proxy [Object java.util.Map] []
      (containsKey [key]
        (contains? map key))
      (containsValue [value]
        (some (fn [k,v] (= v value)) (seq map)))
      (entrySet []
        (set (seq map)))
      (get [key]
        (get map key))
      (isEmpty []
        (= {} map))
      (keySet []
        (set (keys map)))
      (size []
        (count map))
      (values []
        (vals map))))


(defn jmap
  "Takes any Clojure persistent collection, and returns a
   collection conforming to Java idioms (specifically, with
   all IPersistentMaps turned into java.util.HashMaps), and
   all java.util.Collections replaced with ArrayLists of
   their (transformed) contents."
  [#^clojure.lang.IPersistentCollection coll]
    (cond
      (instance? clojure.lang.IPersistentMap coll)
        (loop [ret (new java.util.HashMap) s (seq coll)]
          (if s
            (let [[k,v] (first s)]
              (.put ret (jmap k) (jmap v))
              (recur ret (rest s)))
            ret))

      (instance? java.util.Collection coll)
        (loop [ret (new java.util.ArrayList) s (seq coll)]
          (if s
            (do
              (.add ret (jmap (first s)))
              (recur ret (rest s)))
            ret))

      :else
        coll))

Colorize as:
Show Line Numbers
Index of paste annotations: 1

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.