Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

openid - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...