<commit_before>(ns cmr.common.mime-types
  "Provides functions for handling mime types."
  (:require [pantomime.media :as mt]
            [clojure.string :as str]
            [cmr.common.services.errors :as errors]))

(def base-mime-type-to-format
  "A map of base mime types to the format symbols supported"
  {"application/json" :json
   "application/xml" :xml
   "application/echo10+xml" :echo10
   "application/iso+xml" :iso
   "application/iso-smap+xml" :iso-smap
   "application/iso-mends+xml" :iso-mends
   "application/iso19115+xml" :iso19115
   "application/dif+xml" :dif
   "text/csv" :csv
   "application/atom+xml" :atom})

(def format->mime-type
  {:json "application/json"
   :xml "application/xml"
   :echo10 "application/echo10+xml"
   :iso "application/iso+xml"
   :iso-smap "application/iso-smap+xml"
   :iso-mends "application/iso-mends+xml"
   :iso19115 "application/iso19115+xml"
   :dif "application/dif+xml"
   :csv "text/csv"
   :atom "application/atom+xml"})

(defn- base-mime-type
  [mime-type]
  (str (mt/base-type (mt/parse mime-type))))

(defn mime-type->format
  "Converts a mime-type into the format requested."
  ([mime-type]
   (mime-type->format mime-type "application/json"))
  ([mime-type default-mime-type]
   (if mime-type
     (or (get base-mime-type-to-format (base-mime-type mime-type))
         (get base-mime-type-to-format (base-mime-type default-mime-type)))
     (get base-mime-type-to-format (base-mime-type default-mime-type)))))

(defn mime-type-from-headers
  "Try to get a supported mime-type from the 'accept' header."
  [headers supported-mime-types]
  (let [orig-mime-type-str (get headers "accept")
        ;; Strip out any semicolon clauses
        mime-type-str (when orig-mime-type-str
                        (str/replace orig-mime-type-str  #";.*?(,|$)" "$1"))
        ;; Split mime-type string on commas
        mime-types (when mime-type-str (str/split (str/lower-case mime-type-str), #"[,]"))
        first-supported-mime-type (some (set mime-types) supported-mime-types)]
    (or first-supported-mime-type orig-mime-type-str)))

(defn validate-request-mime-type
  "Validates the requested mime type is supported."
  [mime-type supported-types]
  (when-not (get supported-types mime-type)
    (errors/throw-service-error
      :bad-request (format "The mime type [%s] is not supported." mime-type))))

(defn get-request-format
  "Returns the requested format parsed from headers"
  [headers supported-types]
  (let [mime-type (mime-type-from-headers headers supported-types)]
    (validate-request-mime-type mime-type supported-types)
    (mime-type->format mime-type)))<commit_msg>Added function for checking aliases of :iso-mends format.<commit_after>(ns cmr.common.mime-types
  "Provides functions for handling mime types."
  (:require [pantomime.media :as mt]
            [clojure.string :as str]
            [cmr.common.services.errors :as errors]))

(def base-mime-type-to-format
  "A map of base mime types to the format symbols supported"
  {"application/json" :json
   "application/xml" :xml
   "application/echo10+xml" :echo10
   "application/iso+xml" :iso
   "application/iso-smap+xml" :iso-smap
   "application/iso-mends+xml" :iso-mends
   "application/iso19115+xml" :iso19115
   "application/dif+xml" :dif
   "text/csv" :csv
   "application/atom+xml" :atom})

(def format->mime-type
  {:json "application/json"
   :xml "application/xml"
   :echo10 "application/echo10+xml"
   :iso "application/iso+xml"
   :iso-smap "application/iso-smap+xml"
   :iso-mends "application/iso-mends+xml"
   :iso19115 "application/iso19115+xml"
   :dif "application/dif+xml"
   :csv "text/csv"
   :atom "application/atom+xml"})

(defn- base-mime-type
  [mime-type]
  (str (mt/base-type (mt/parse mime-type))))

(defn mime-type->format
  "Converts a mime-type into the format requested."
  ([mime-type]
   (mime-type->format mime-type "application/json"))
  ([mime-type default-mime-type]
   (if mime-type
     (or (get base-mime-type-to-format (base-mime-type mime-type))
         (get base-mime-type-to-format (base-mime-type default-mime-type)))
     (get base-mime-type-to-format (base-mime-type default-mime-type)))))

(defn mime-type-from-headers
  "Try to get a supported mime-type from the 'accept' header."
  [headers supported-mime-types]
  (let [orig-mime-type-str (get headers "accept")
        ;; Strip out any semicolon clauses
        mime-type-str (when orig-mime-type-str
                        (str/replace orig-mime-type-str  #";.*?(,|$)" "$1"))
        ;; Split mime-type string on commas
        mime-types (when mime-type-str (str/split (str/lower-case mime-type-str), #"[,]"))
        first-supported-mime-type (some (set mime-types) supported-mime-types)]
    (or first-supported-mime-type orig-mime-type-str)))

(defn validate-request-mime-type
  "Validates the requested mime type is supported."
  [mime-type supported-types]
  (when-not (get supported-types mime-type)
    (errors/throw-service-error
      :bad-request (format "The mime type [%s] is not supported." mime-type))))

(defn get-request-format
  "Returns the requested format parsed from headers"
  [headers supported-types]
  (let [mime-type (mime-type-from-headers headers supported-types)]
    (validate-request-mime-type mime-type supported-types)
    (mime-type->format mime-type)))

(defn alias-of-iso-mends?
  "Returns true if the given result format is an alias of :iso-mends"
  [result-format]
  (some #{result-format} [:iso :iso19115]))
{'fraction_non_alphanumeric': 0.15079220542706245, 'fraction_numerical': 0.011473319978146056, 'mean_word_length': 3.4006410256410255, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 36}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3077059', 'n_tokens_mistral': 2018, 'n_tokens_neox': 1890, 'n_words': 437}