(defn ^String modify-link
"Takes a DOM tree `document` and a Ring request map `request` as arguments.
This function will modify the links in tags of elements to point
to our download proxy. Returns the modified document as a String."
[document request]
(let [dom (parse-xml document)
tfn (doto (make-transformer)
(.setOutputProperty OutputKeys/INDENT "yes")
(.setOutputProperty "{http://xml.apache.org/xslt}indent-amount" "2"))
src (DOMSource. dom)
dst (StreamResult. (StringWriter.))]
;; Modify all tag whose child is
(->> dom
(get-tags "link")
(filter in-?)
(run! (fn [tag]
(let [id (get-id (tag-text tag))
resource (absolute-url request id)]
(set-tag-text! tag resource)))))
;; Return the transformed feed
(.transform tfn src dst)
(.toString (.getWriter dst))))