Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a javax.servlet.Filter with Compojure

I'm trying to build a simple web site using Clojure / Compojure and want to feed apply a servlet filter to the request / response (i.e. a standard javax.servlet.Filter instance).

e.g. if the current source code is:

(defroutes my-app
  (GET "/*"
    (html [:h1 "Hello Foo!!"]))
)

I would like to add a filter like this:

(defroutes my-app
  (GET "/*"
    (FILTER my-filter-name
      (html [:h1 "Hello Foo!!"])))
)

Where my-filter-name is some arbitrary instance of javax.servlet.Filter.

Any idea how to do this effectively and elegantly?

like image 409
mikera Avatar asked May 10 '10 21:05

mikera


1 Answers

Ok I've now got this working! Thanks cgrand for the pointers in the right direction!

My solution involved creating proxies for a filtered servlet and a filter chain. Code below for anyone interested.....

(def pass-through-filter 
  (proxy [javax.servlet.Filter] []
    (doFilter
      [request response #^javax.servlet.FilterChain filterchain]
      (do
        (.doFilter filterchain request response)))))

(defn filter-chain 
  [#^javax.servlet.Servlet servlet]
  (proxy [javax.servlet.FilterChain] []
    (doFilter
      [request response]
      (.service servlet request response))))

(defn filtered-servlet 
  [#^javax.servlet.Filter servlet-filter handler]
  (let [#^javax.servlet.Servlet base-servlet (servlet handler)
        the-filter-chain (filter-chain base-servlet)]
    (proxy [javax.servlet.http.HttpServlet] []
      (service
        [request response] 
        (.doFilter servlet-filter request response the-filter-chain))
      (init
        [config] 
        (.init base-servlet config)))))

(defroutes my-app
  (GET "/*"
    (html 
      [:h1 "Hello Foo!!"]))
  (ANY "*"
    [404 "Page not found"]))

(run-server {:port 80}
  "/*" (filtered-servlet pass-through-filter my-app))
like image 100
mikera Avatar answered Sep 20 '22 12:09

mikera