Clojure, Servlet and JWT
質問
EDIT: For a working sample, take a look at this demo project.
Disclaimer: I'm a total noob in building java web applications.
I'm trying to use JWT with Clojure but I simply don't know how deal with this "servlet" thing. So far, my idea was:
Create a
WApplication
with a "Hello World" form.(defn make-hello-app [env] (let [wapp (WApplication. env) root (.getRoot wapp)] (.setTitle wapp "Hello world") (.addWidget root (WText. "Hello!!!!")) wapp))
Create a servlet, inherited from
WtServlet
.(def servlet (proxy [WtServlet] [] (createApplication [env] (make-hello-app env))))
Start jetty and use the servlet. This is what I don't know how to do. So far, this was my best shot:
(ns jwttest.core (:use compojure.core) (:use ring.adapter.jetty) (:import (org.eclipse.jetty.server Server)) (:import (eu.webtoolkit.jwt WApplication WEnvironment WtServlet WText WPushButton WLineEdit WBreak))) ;; (the funcions above were defined here) ;; create a jetty server (defn create-a-jetty-server [] (let [connector (doto (SelectChannelConnector.) (.setPort 8080) (.setHost "localhost")) server (doto (Server.) (.addConnector connector) (.setSendDateHeader true))] server)) ;; start the application (defn start-the-app [] (let [server (create-a-jetty-server)] ;; ???? .addServlet ? How? (.start server)))
In my project.clj
I have:
[org.clojure/clojure "1.4.0"]
[eu.webtoolkit/jwt "3.2.0"]
[compojure "1.1.1"]
[ring "1.1.2"]
I know ring can create a servlet from a handler, but in this case I already have a servlet so... what should I do to run this?
Note: I'm basing my code on this very old post made in 2009.
解決
I dug through some of the Jetty API and the Ring/Noir jetty handling code and here's a summary of what I found (and haven't had a chance to test)
- Jetty Server has a "setHandler" method which takes a handler (thanks Ring)
- There is a ServletHandler class which looks like it fits into the above and which has a number of addServlet like methods which look like they do what you need them to.
You should be able to set the handler to a Servlet handler and go from there.