<commit_before>;
;  (C) Copyright 2016  <NAME>
;
;  All rights reserved. This program and the accompanying materials
;  are made available under the terms of the Eclipse Public License v1.0
;  which accompanies this distribution, and is available at
;  http://www.eclipse.org/legal/epl-v10.html
;
;  Contributors:
;      <NAME>
;

(ns emender-jenkins.server)

(require '[clojure.pprint           :as pprint])
(require '[ring.util.response       :as http-response])

(require '[emender-jenkins.config   :as config])
(require '[emender-jenkins.rest-api :as rest-api])

(defn return-file
    "Creates HTTP response containing content of specified file.
     Special value nil / HTTP response 404 is returned in case of any I/O error."
    [file-name content-type]
    (-> (http-response/file-response file-name {:root "www"})
        (http-response/content-type content-type)))

(defn render-front-page
    "Create front page."
    [request]
    (return-file "index.html" "text/html"))

(defn render-error-page
    "Create error page."
    [request]
    (return-file "error.html" "text/html"))

(defn get-hostname
    []
    (.. java.net.InetAddress getLocalHost getHostName))

(defn get-api-command
    "Retrieve the actual command from the API call."
    [uri prefix]
    (if uri
        (if (.startsWith uri prefix)
            (let [uri-without-prefix (subs uri (count prefix))]
                (if (empty? uri-without-prefix) ; special handler for a call with / only
                    ""
                 (let [api-part (re-find #"/[^/]*" (subs uri (count prefix)))]
                    (if (and api-part (.startsWith api-part "/"))
                        (subs api-part 1)
                        api-part)))))))

(defn api-call-handler
    [request uri method]
    (if (= uri "/api")
        (rest-api/info-handler request (get-hostname))
        (condp = [method (get-api-command uri (config/get-api-prefix request))]
            [:get  ""]                    (rest-api/info-handler request (get-hostname))
            [:get  "configuration"]       (rest-api/configuration-handler request)
            [:get  "system"]              (rest-api/system-banners        request uri)
            [:post "reload-all-results"]  (rest-api/reload-all-results    request)
            [:post "create_job"]          (rest-api/create-job            request)
            [:post "update_job"]          (rest-api/update-job            request)
            [:post "delete_job"]          (rest-api/delete-job            request)
            [:post "start_job"]           (rest-api/start-job             request)
            [:post "enable_job"]          (rest-api/enable-job            request)
            [:post "disable_job"]         (rest-api/disable-job           request)
            [:get  "get_job"]             (rest-api/get-job               request uri)
            [:get  "get_jobs"]            (rest-api/get-jobs              request)
            [:get  "get_job_results"]     (rest-api/get-job-results       request uri)
            [:post "job_started"]         (rest-api/job-started-handler   request)
            [:post "job_finished"]        (rest-api/job-finished-handler  request)
            [:post "job_results"]         (rest-api/job-results           request)
                                          (rest-api/unknown-call-handler uri method))))

(defn restcall-options-handler
    []
    (println "Method:  OPTIONS")
    (-> (http-response/response "")
        (http-response/content-type "application/json")))

(defn restcall-head-handler
    []
    (println "Method:  HEAD")
    (-> (http-response/response "")
        (http-response/content-type "application/json")))

(defn non-api-call-handler<commit_msg>Doc strings for all files in server.clj module<commit_after>;
;  (C) Copyright 2016  <NAME>
;
;  All rights reserved. This program and the accompanying materials
;  are made available under the terms of the Eclipse Public License v1.0
;  which accompanies this distribution, and is available at
;  http://www.eclipse.org/legal/epl-v10.html
;
;  Contributors:
;      <NAME>
;

(ns emender-jenkins.server
    "Server module that accepts all requests from users and generates proper responses.")

(require '[clojure.pprint           :as pprint])
(require '[ring.util.response       :as http-response])

(require '[emender-jenkins.config   :as config])
(require '[emender-jenkins.rest-api :as rest-api])

(defn return-file
    "Creates HTTP response containing content of specified file.
     Special value nil / HTTP response 404 is returned in case of any I/O error."
    [file-name content-type]
    (-> (http-response/file-response file-name {:root "www"})
        (http-response/content-type content-type)))

(defn render-front-page
    "Create front page."
    [request]
    (return-file "index.html" "text/html"))

(defn render-error-page
    "Create error page."
    [request]
    (return-file "error.html" "text/html"))

(defn get-hostname
    "Returns hostname of the server."
    []
    (.. java.net.InetAddress getLocalHost getHostName))

(defn get-api-command
    "Retrieve the actual command from the API call."
    [uri prefix]
    (if uri
        (if (.startsWith uri prefix)
            (let [uri-without-prefix (subs uri (count prefix))]
                (if (empty? uri-without-prefix) ; special handler for a call with / only
                    ""
                 (let [api-part (re-find #"/[^/]*" (subs uri (count prefix)))]
                    (if (and api-part (.startsWith api-part "/"))
                        (subs api-part 1)
                        api-part)))))))

(defn api-call-handler
    "This function is used to handle all API calls. Three parameters are expected:
     data structure containing HTTP request, string with URI, and the HTTP method."
    [request uri method]
    (if (= uri "/api")
        (rest-api/info-handler request (get-hostname))
        (condp = [method (get-api-command uri (config/get-api-prefix request))]
            [:get  ""]                    (rest-api/info-handler request (get-hostname))
            [:get  "configuration"]       (rest-api/configuration-handler request)
            [:get  "system"]              (rest-api/system-banners        request uri)
            [:post "reload-all-results"]  (rest-api/reload-all-results    request)
            [:post "create_job"]          (rest-api/create-job            request)
            [:post "update_job"]          (rest-api/update-job            request)
            [:post "delete_job"]          (rest-api/delete-job            request)
            [:post "start_job"]           (rest-api/start-job             request)
            [:post "enable_job"]          (rest-api/enable-job            request)
            [:post "disable_job"]         (rest-api/disable-job           request)
            [:get  "get_job"]             (rest-api/get-job               request uri)
            [:get  "get_jobs"]            (rest-api/get-jobs              request)
            [:get  "get_job_results"]     (rest-api/get-job-results       request uri)
            [:post "job_started"]         (rest-api/job-started-handler   request)
            [:post "job_finished"]        (rest-api/job-finished-handler  request)
            [:post "job_results"]         (rest-api/job-results           request)
                                          (rest-api/unknown-call-handler uri method))))

(defn restcall-options-handler
    "Empty handler for OPTIONS method."
    []
    (println "Method:  OPTIONS")
    (-> (http-response/response "")
        (http-response/content-type "application/json")))

(defn restcall-head-handler
    "Empty handler for HEAD method."
    []
    (println "Method:  HEAD")
    (-> (http-response/response "")
        (http-response/content-type "application/json")))

(defn non-api-call-handler
    "This function is used to handle all API calls. Two parameters are expected:
     data structure containing HTTP request and string with URI."
{'fraction_non_alphanumeric': 0.12785901762279714, 'fraction_numerical': 0.0029996250468691415, 'mean_word_length': 1.978042426497953, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1054128', 'n_tokens_mistral': 2358, 'n_tokens_neox': 2273, 'n_words': 610}