 
 (ns compojure.http.response
   (:use clojure.contrib.def)
+  (:import clojure.lang.IFn)
   (:import clojure.lang.IPersistentVector)
   (:import java.util.Map)
   (:import clojure.lang.ISeq)

  (if (not= kw :next)
    (response-from (str kw))))

(defmethod response-from nil
  [_]
  {})

  (:import java.net.URL)
  (:import clojure.lang.Keyword))

(defn merge-bodies
  "Merge the bodies of two responses together."
  [to from]
  (if-let [from-body (:body from)]
    (assoc to :body
      (if (and (string? (:body to)) (string? from-body))
        (str (:body to) from-body)
        from-body))
    to))

(defn- merge-headers
  "Merge headers from two responses together."
  [to from]
  (merge to (select-keys from [:headers])))

(defn merge-response
  "Intelligently merge two response maps together."
  [to from]
  (cond
    (map? from)
      (-> to (merge-headers from)
             (merge-bodies from)
             (merge (dissoc from :headers :body)))
    (ifn? from)
      (from to)))

(defmulti response-from class)

