             [ring.middleware.json :as ring-json]
             [cmr.common.log :refer (debug info warn error)]
             [cmr.common.api.errors :as errors]
+            [cmr.common.services.errors :as svc-errors]
             [cmr.common.mime-types :as mt]
             [cmr.search.services.query-service :as query-svc]
             [cmr.system-trace.http :as http-trace]
             [cmr.search.api.search-results :as sr]
             [cmr.search.services.parameters.legacy-parameters :as lp]))
 
-(defn- get-search-results-format
-  "Returns the requested search results format parsed from headers"
-  [headers]
-  (let [mime-type (get headers "accept")]
-    (mt/validate-request-mime-type mime-type sr/supported-mime-types)
-    (mt/mime-type->format mime-type)))
 
 (defn- measure-query-time
   "Executes the query function measuring how long it takes. Adds a :took key to the results map

        took (- stop start)]
    (assoc result :took took)))

(defn- find-references
  "Invokes query service to find references and returns the response"
  [context concept-type params headers query-string]
  (let [result-format (get-search-results-format headers)
        params (assoc params :result-format result-format)
        pretty? (= (get params :pretty) "true")
        _ (info (format "Searching for %ss in format %s with params %s." (name concept-type) result-format (pr-str params)))

     :headers {"Content-Type" "application/xml; charset=utf-8"}
     :body (:metadata concept)}))

(def concept-type-w-extension-regex
  "A regular expression that matches URLs including the concept type (pluralized) along with a file
  extension."
  #"(?:granules)|(?:collections)(?:\..+)?")

(defn- build-routes [system]
  (routes
    (context (get-in system [:search-public-conf :relative-root-url]) []

      ;; Retrieve by cmr concept id
      (context "/concepts/:cmr-concept-id" [cmr-concept-id]
        (GET "/" {headers :headers context :request-context}
          (find-concept-by-cmr-concept-id context cmr-concept-id headers)))

      ;; Find concepts
      (context ["/:concept-type-w-extension" :concept-type-w-extension concept-type-w-extension-regex] [concept-type-w-extension]
        (GET "/" {params :params headers :headers context :request-context query-string :query-string}
          (find-concepts context concept-type-w-extension params headers query-string))
        (POST "/" {params :params headers :headers context :request-context body :body-copy}
          (find-concepts context concept-type-w-extension params headers body)))

      ;; reset operation available just for development purposes
      ;; clear the cache for search app
