Amigo
Amigo

Reputation: 109

How get ServletContext in ring init function

I'm using Clojure+Ring to build a web application running on Glassfish 3. How can I get access to ServletContext variable in the Ring init function?

Upvotes: 3

Views: 537

Answers (1)

schaueho
schaueho

Reputation: 3504

The ServletContext, if any, is available in the request map. I found it useful to look at the values of :context, :servlet-context and :servlet-context-path. Here's a small ring middleware I use to determine the path:

(def ^:dynamic *app-context* nil)

(defn wrap-context [handler]
 (fn [request]
  (when-let [context (:context request)]
    (logging/debug (str "Request with context " context)))
  (when-let [pathdebug (:path-debug request)]
    (logging/debug (str "Request with path-debug " pathdebug)))
  (when-let [servlet-context (:servlet-context request)]
    (logging/debug (str "Request with servlet-context " servlet-context)))
  (when-let [servlet-context-path (:servlet-context-path request)]
    (logging/debug (str "Request with servlet-context-path " servlet-context-path)))
  (binding [*app-context* (str (:context request) "/")]
     (logging/debug (str "Using appcontext " *app-context*))
     (-> request
         handler))))

(defn url-in-context [url]
    (str *app-context* url))

Upvotes: 1

Related Questions