|
12 | 12 | (:use hiccup.core)
|
13 | 13 | (:use hiccup.page-helpers))
|
14 | 14 |
|
15 |
| -(declare index-html example-html) |
| 15 | +(declare index-html example-html messages-html) |
16 | 16 |
|
17 | 17 | (defroutes main-routes
|
18 | 18 | (GET "/" req (index-html req))
|
19 | 19 | (GET "/admin" req (example-html req))
|
20 | 20 | (GET "/user" req (example-html req))
|
21 | 21 | (GET "/other" req (example-html req))
|
| 22 | + (GET "/messages" req (messages-html req)) |
22 | 23 | (route/resources "/")
|
23 | 24 | (route/not-found "Page not found"))
|
24 | 25 |
|
| 26 | +;Some general utilities and wrappers |
| 27 | + |
25 | 28 | (defn application-context
|
26 | 29 | ([req]
|
27 | 30 | (org.springframework.web.context.support.WebApplicationContextUtils/getWebApplicationContext
|
|
33 | 36 | (defn security-context []
|
34 | 37 | (org.springframework.security.core.context.SecurityContextHolder/getContext))
|
35 | 38 |
|
| 39 | +(defn message [req key & args] |
| 40 | + (let [context (application-context req)] |
| 41 | + (.getMessage |
| 42 | + context |
| 43 | + (if (keyword? key) (name key) key) |
| 44 | + (into-array java.lang.Object args) |
| 45 | + (.getLocale (:servlet-request req))))) |
| 46 | + |
36 | 47 | (defn wrap-reload-spring [app]
|
37 | 48 | "Reload the Spring application context on every HTTP request"
|
38 | 49 | (fn [req]
|
39 | 50 | (let [application-context (application-context req)]
|
40 | 51 | (.refresh application-context)
|
41 | 52 | (app req))))
|
42 | 53 |
|
| 54 | +(defn wrap-dump [app] |
| 55 | + (fn [req] |
| 56 | + (println req) |
| 57 | + (app req))) |
| 58 | + |
43 | 59 | (def app
|
44 | 60 | (-> (handler/site main-routes)
|
45 |
| -; (wrap-reload-spring) |
| 61 | + (wrap-reload-spring) |
| 62 | + (wrap-dump) |
46 | 63 | (wrap-reload '(ring-spring-security.core))
|
47 | 64 | (wrap-stacktrace)))
|
48 | 65 |
|
|
70 | 87 | [:li (link-to "/user" "/user")]
|
71 | 88 | [:li (link-to "/other" "/other")]]))
|
72 | 89 |
|
| 90 | +(defn messages-html [req] |
| 91 | + (layout |
| 92 | + [:p "Here's some messages in your locale:"] |
| 93 | + [:ul |
| 94 | + [:li (escape-html (message req :greeting "asdf"))]])) |
| 95 | + |
73 | 96 | (defn- boot-spring
|
74 | 97 | "Initialize a Jetty server for Spring and also Spring Security"
|
75 | 98 | ([server handler context-config-location]
|
|
94 | 117 | {:port 9090
|
95 | 118 | :join? join?
|
96 | 119 | :configurator (fn [server]
|
97 |
| - (boot-spring server #'app "classpath:security.xml"))})] |
| 120 | + (boot-spring server #'app "classpath:applicationContext.xml"))})] |
98 | 121 | ;this is a hack to get around the handler ring-adapter-jetty forces on us
|
99 | 122 | (doseq [handler (remove #(= (class %) org.mortbay.jetty.servlet.Context) (seq (.getHandlers server)))]
|
100 | 123 | (.removeHandler server handler))))
|
|
0 commit comments