Permalink
Browse files

Replaced proxy for reify where possible and new reader syntax for met…

…adata
  • Loading branch information...
1 parent 0961cbd commit 4d4f933dd931677b11f4b670b7b8a7c4c346ccba @soyrochus soyrochus committed Aug 23, 2010
View
@@ -1,8 +1,8 @@
(defproject ring/ring-core "0.3.0-beta1"
:description "Ring core libraries."
:url "http://github.com/mmcgrana/ring"
- :dependencies [[org.clojure/clojure "1.2.0-RC1"]
- [org.clojure/clojure-contrib "1.2.0-RC1"]
+ :dependencies [[org.clojure/clojure "1.2.0"]
+ [org.clojure/clojure-contrib "1.2.0"]
[commons-codec "1.4"]
[commons-io "1.4"]
[commons-fileupload "1.2.1"]
@@ -25,11 +25,11 @@
"Create a RequestContext object from a request map."
{:tag RequestContext}
[request encoding]
- (proxy [RequestContext] []
- (getContentType [] (:content-type request))
- (getContentLength [] (:content-length request))
- (getCharacterEncoding [] encoding)
- (getInputStream [] (:body request))))
+ (reify RequestContext
+ (getContentType [this] (:content-type request))
+ (getContentLength [this] (:content-length request))
+ (getCharacterEncoding [this] encoding)
+ (getInputStream [this] (:body request))))
(defn- file-map
"Create a file map from a DiskFileItem."
@@ -8,8 +8,8 @@
(wrap-file (constantly :response) "not_here"))))
(def public-dir "test/ring/assets")
-(def index-html (File. #^String public-dir "index.html"))
-(def foo-html (File. #^String public-dir "foo.html"))
+(def index-html (File. ^String public-dir "index.html"))
+(def foo-html (File. ^String public-dir "foo.html"))
(def app (wrap-file (constantly :response) public-dir))
@@ -31,7 +31,7 @@
":server-name must be a String")
(lint (:remote-addr req) string?
":remote-addr must be a String")
- (lint (:uri req) #(and (string? %) (.startsWith #^String % "/"))
+ (lint (:uri req) #(and (string? %) (.startsWith ^String % "/"))
":uri must be a String starting with \"/\"")
(lint (:query-string req) #(or (nil? %) (string? %))
":query-string must be nil or a non-blank String")
@@ -52,7 +52,7 @@
(doseq [[hname hval] headers]
(lint hname string?
"header names must be Strings")
- (lint hname #(= % (.toLowerCase #^String %))
+ (lint hname #(= % (.toLowerCase ^String %))
"header names must be in lower case")
(lint hval string?
"header values must be strings")))
@@ -26,28 +26,28 @@
Executors Executor ThreadFactory))
(:use [clojure.contrib.except :only (throwf)]))
-(defmacro #^{:private true} -?>
+(defmacro ^{:private true} -?>
([form] form)
([form next-form & forms]
`(when-let [x# ~form] (-?> (-> x# ~next-form) ~@forms))))
-(defmacro #^{:private true} instance?-> [type x & forms]
+(defmacro ^{:private true} instance?-> [type x & forms]
`(when (instance? ~type ~x) (-> ~(vary-meta x assoc :tag type) ~@forms)))
-(defn- charset [#^BasicHeader content-type-header]
+(defn- charset [^BasicHeader content-type-header]
(-?> content-type-header .getElements
- #^BasicHeaderElement first (.getParameterByName "charset") .getValue))
+ ^BasicHeaderElement first (.getParameterByName "charset") .getValue))
-(defn- lower-case [#^String s]
+(defn- lower-case [^String s]
(.toLowerCase s java.util.Locale/ENGLISH))
(defn- build-req-map
"Augments the given request-prototype (a map) to represent the given HTTP
request, to be passed as the Ring request to a handler."
- [#^HttpRequest request request-prototype]
+ [^HttpRequest request request-prototype]
(let [request-line (.getRequestLine request)
headers (reduce
- (fn [header-map #^Header header]
+ (fn [header-map ^Header header]
(assoc header-map
(-> header .getName lower-case)
(.getValue header)))
@@ -74,7 +74,7 @@
(defn- apply-resp-map
"Apply the given response map to the servlet response, therby completing
the HTTP response."
- [#^HttpResponse response {:keys [status headers body]}]
+ [^HttpResponse response {:keys [status headers body]}]
; Apply the status.
(.setStatusCode response status)
; Apply the headers.
@@ -95,14 +95,14 @@
(StringEntity. body)
(seq? body)
(EntityTemplate.
- (proxy [ContentProducer] []
- (writeTo [#^OutputStream s]
- (let [w (if charset
- (OutputStreamWriter. s #^String charset)
- (OutputStreamWriter. s))]
- (doseq [#^String chunk body]
- (.write w chunk))
- (.flush w)))))
+ (reify ContentProducer
+ (writeTo [this ^OutputStream s]
+ (let [w (if charset
+ (OutputStreamWriter. s ^String charset)
+ (OutputStreamWriter. s))]
+ (doseq [^String chunk body]
+ (.write w chunk))
+ (.flush w)))))
(instance? InputStream body)
(InputStreamEntity. body
(let [l (or content-length -1)]
@@ -111,24 +111,24 @@
(FileEntity. body content-type)
:else
(throwf "Unrecognized body: %s" body))]
- (when-let [#^String type (headers "Content-Type")]
- (.setContentType #^AbstractHttpEntity entity type))
+ (when-let [^String type (headers "Content-Type")]
+ (.setContentType ^AbstractHttpEntity entity type))
(.setEntity response entity))))
(defn- ring-handler
"Returns an Handler implementation for the given Ring handler.
The HttpContext must contains a map associated to \"ring.request-prototype\"."
[handler]
- (proxy [HttpRequestHandler] []
- (handle [request response #^HttpContext context]
- (let [req (build-req-map request
- (.getAttribute context "ring.request-prototype"))
- resp (handler req)]
- (apply-resp-map response resp)))))
+ (reify HttpRequestHandler
+ (handle [this request response ^HttpContext context]
+ (let [req (build-req-map request
+ (.getAttribute context "ring.request-prototype"))
+ resp (handler req)]
+ (apply-resp-map response resp)))))
(defn- handle-request
"Handle the request, usually called from a worker thread."
- [#^HttpService httpservice #^HttpServerConnection conn request-prototype]
+ [^HttpService httpservice ^HttpServerConnection conn request-prototype]
(let [context (doto (BasicHttpContext. nil)
(.setAttribute "ring.request-prototype" request-prototype))]
(try
@@ -161,7 +161,7 @@
(defn executor-execute
"Executes (apply f args) using the specified Executor."
- [#^java.util.concurrent.Executor executor f & args]
+ [^java.util.concurrent.Executor executor f & args]
(.execute executor #(apply f args)))
(defn run-httpcore
@@ -176,10 +176,10 @@
[handler {:keys [port server-name server-port execute]}]
(let [execute (or execute (partial executor-execute
(Executors/newCachedThreadPool
- (proxy [ThreadFactory] []
- (newThread [r]
- (doto (Thread. #^Runnable r)
- (.setDaemon true)))))))
+ (reify ThreadFactory
+ (newThread [this r]
+ (doto (Thread. ^Runnable r)
+ (.setDaemon true)))))))
params (doto (BasicHttpParams.)
(.setIntParameter CoreConnectionPNames/SO_TIMEOUT 5000)
(.setIntParameter CoreConnectionPNames/SOCKET_BUFFER_SIZE (* 8 1024))
@@ -8,20 +8,20 @@
(:use (ring.util servlet)
(clojure.contrib except)))
-(defn- proxy-handler
+(defn- reify-handler
"Returns an Jetty Handler implementation for the given Ring handler."
[handler]
- (proxy [AbstractHandler] []
- (handle [target #^Request request response dispatch]
- (let [request-map (build-request-map request)
- response-map (handler request-map)]
- (when response-map
- (update-servlet-response response response-map)
- (.setHandled request true))))))
+ (reify AbstractHandler
+ (handle [this target ^Request request response dispatch]
+ (let [request-map (build-request-map request)
+ response-map (handler request-map)]
+ (when response-map
+ (update-servlet-response response response-map)
+ (.setHandled request true))))))
(defn- add-ssl-connector!
"Add an SslSocketConnector to a Jetty Server instance."
- [#^Server server options]
+ [^Server server options]
(let [ssl-connector (SslSocketConnector.)]
(doto ssl-connector
(.setPort (options :ssl-port 443))
@@ -46,7 +46,7 @@
(add-ssl-connector! server options))
server))
-(defn #^Server run-jetty
+(defn ^Server run-jetty
"Serve the given handler according to the options.
Options:
:configurator - A function called with the Server instance.
@@ -60,12 +60,13 @@
:truststore
:trust-password"
[handler options]
- (let [#^Server s (create-server (dissoc options :configurator))]
+ (let [^Server s (create-server (dissoc options :configurator))]
(when-let [configurator (:configurator options)]
(configurator s))
(doto s
- (.setHandler (proxy-handler handler))
+ (.setHandler (reify-handler handler))
(.start))
(when (:join? options true)
(.join s))
s))
+
@@ -9,9 +9,9 @@
(defn- get-headers
"Creates a name/value map of all the request headers."
- [#^HttpServletRequest request]
+ [^HttpServletRequest request]
(reduce
- (fn [headers, #^String name]
+ (fn [headers, ^String name]
(assoc headers
(.toLowerCase name)
(.getHeader request name)))
@@ -20,13 +20,13 @@
(defn- get-content-length
"Returns the content length, or nil if there is no content."
- [#^HttpServletRequest request]
+ [^HttpServletRequest request]
(let [length (.getContentLength request)]
(if (>= length 0) length)))
(defn build-request-map
"Create the request map from the HttpServletRequest object."
- [#^HttpServletRequest request]
+ [^HttpServletRequest request]
{:server-port (.getServerPort request)
:server-name (.getServerName request)
:remote-addr (.getRemoteAddr request)
@@ -44,9 +44,9 @@
"Associate servlet-specific keys with the request map for use with legacy
systems."
[request-map
- #^HttpServlet servlet
- #^HttpServletRequest request
- #^HttpServletResponse response]
+ ^HttpServlet servlet
+ ^HttpServletRequest request
+ ^HttpServletResponse response]
(merge request-map
{:servlet servlet
:servlet-request request
@@ -55,7 +55,7 @@
(defn- set-headers
"Update a HttpServletResponse with a map of headers."
- [#^HttpServletResponse response, headers]
+ [^HttpServletResponse response, headers]
(doseq [[key val-or-vals] headers]
(if (string? val-or-vals)
(.setHeader response key val-or-vals)
@@ -67,7 +67,7 @@
(defn- set-body
"Update a HttpServletResponse body with a String, ISeq, File or InputStream."
- [#^HttpServletResponse response, body]
+ [^HttpServletResponse response, body]
(cond
(string? body)
(with-open [writer (.getWriter response)]
@@ -78,13 +78,13 @@
(.print writer (str chunk))
(.flush writer)))
(instance? InputStream body)
- (let [#^InputStream b body]
+ (let [^InputStream b body]
(with-open [out (.getOutputStream response)]
(copy b out)
(.close b)
(.flush out)))
(instance? File body)
- (let [#^File f body]
+ (let [^File f body]
(with-open [stream (FileInputStream. f)]
(set-body response stream)))
(nil? body)
@@ -94,7 +94,7 @@
(defn update-servlet-response
"Update the HttpServletResponse using a response map."
- [#^HttpServletResponse response, {:keys [status headers body]}]
+ [^HttpServletResponse response, {:keys [status headers body]}]
(when-not response
(throw (Exception. "Null response given.")))
(when status
@@ -107,9 +107,9 @@
"Turns a handler into a function that takes the same arguments and has the
same return value as the service method in the HttpServlet class."
[handler]
- (fn [#^HttpServlet servlet
- #^HttpServletRequest request
- #^HttpServletResponse response]
+ (fn [^HttpServlet servlet
+ ^HttpServletRequest request
+ ^HttpServletResponse response]
(.setCharacterEncoding response "UTF-8")
(let [request-map (-> request
(build-request-map)

0 comments on commit 4d4f933

Please sign in to comment.