   (merge (basic-response-map response)
          (extended-response-map response)))
 
+(declare handle-download-request)
+
+(defn- handle-redirect [result]
+  (let [code (:response_status_code result)
+        headers (into {} (:response_headers result))
+        location (get headers "Location")
+        original-request (select-keys result [:request_form_fields
+                                              :request_headers])
+        new-request (assoc original-request :request_uri location)]
+    (log/warn "HTTP response " code " to " (pr-str original-request))
+    (assoc (handle-download-request new-request) :redirect_from result)))
+
 (defn- execute-request
   "Calls the client to handle the request, logs the URL in the
   download log, and returns the download record as a keyword=>value

  [request]
  (let [response (.handle (crawler-http-client) request)
        result (merge (request-map request)
                      (response-map response))]
    (dl/log-download (:request_uri result))
    result))

(defn crawl
  "Given a URL, downloads it and returns a download record as a

(ns org.altlaw.util.crawler
  (:require [org.altlaw.db.download-log :as dl]
            [org.altlaw.util.log :as log]
            [clojure.contrib.singleton :as sing]
            [clojure.contrib.java-utils :as j])
  (:import (org.restlet Client Context)
