diff options
author | Stuart Sierra <mail@stuartsierra.com> | 2009-08-18 12:55:35 -0400 |
---|---|---|
committer | Stuart Sierra <mail@stuartsierra.com> | 2009-08-18 12:55:35 -0400 |
commit | cb32dc51ee01ee76f88e8cd61498e9bfd70e8c2f (patch) | |
tree | 7b2dccf257712447c7237fd3f12e5cd299684d81 /src/clojure/contrib | |
parent | 76a841f9e4f2e184beabe693856572dff265cab7 (diff) |
http/agent.clj: added type hints to avoid reflection
Diffstat (limited to 'src/clojure/contrib')
-rw-r--r-- | src/clojure/contrib/http/agent.clj | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/src/clojure/contrib/http/agent.clj b/src/clojure/contrib/http/agent.clj index d63cfd1e..2121b9db 100644 --- a/src/clojure/contrib/http/agent.clj +++ b/src/clojure/contrib/http/agent.clj @@ -63,7 +63,9 @@ clojure.contrib.http.agent (:require [clojure.contrib.http.connection :as c] [clojure.contrib.duck-streams :as duck]) - (:import (java.io ByteArrayOutputStream ByteArrayInputStream))) + (:import (java.io InputStream ByteArrayOutputStream + ByteArrayInputStream) + (java.net HttpURLConnection))) ;;; PRIVATE @@ -73,7 +75,7 @@ (defn- setup-http-connection "Sets the instance method, redirect behavior, and request headers of the HttpURLConnection." - [conn options] + [#^HttpURLConnection conn options] (.setRequestMethod conn (:method options)) (.setInstanceFollowRedirects conn (:follow-redirects options)) (doseq [[name value] (:headers options)] @@ -87,7 +89,7 @@ (c/start-http-connection conn (:body options)) (assoc state ::state ::started))) -(defn- connection-success? [conn] +(defn- connection-success? [#^HttpURLConnection conn] "Returns true if the HttpURLConnection response code is in the 2xx range." (= 2 (unchecked-divide (.getResponseCode conn) 100))) @@ -96,7 +98,7 @@ "Agent action that opens the response body stream on the HTTP request; this will block until the response stream is available." [state options] - (let [conn (::connection state)] + (let [#^HttpURLConnection conn (::connection state)] (assoc state ::response-stream (if (connection-success? conn) (.getInputStream conn) @@ -117,8 +119,8 @@ "Agent action that closes the response body stream and disconnects the HttpURLConnection." [state options] - (.close (::response-stream state)) - (.disconnect (::connection state)) + (.close #^InputStream (::response-stream state)) + (.disconnect #^HttpURLConnection (::connection state)) (assoc state ::response-stream nil ::state ::disconnected)) @@ -127,10 +129,11 @@ "Returns true if the response status of the HTTP agent begins with digit, an Integer." [digit http-agnt] - (= digit (unchecked-divide (.getResponseCode (::connection @http-agnt)) + (= digit (unchecked-divide (.getResponseCode + #^HttpURLConnection (::connection @http-agnt)) 100))) -(defn- get-byte-buffer [http-agnt] +(defn- #^ByteArrayOutputStream get-byte-buffer [http-agnt] (let [buffer (result http-agnt)] (if (instance? ByteArrayOutputStream buffer) buffer @@ -240,7 +243,8 @@ (let [a @http-agnt] (if (= (::state a) ::receiving) (::response-stream a) - (ByteArrayInputStream. (.toByteArray (result http-agnt)))))) + (ByteArrayInputStream. + (.toByteArray (get-byte-buffer http-agnt)))))) (defn bytes "Returns a Java byte array of the content returned by the server; @@ -256,9 +260,10 @@ headers, or clojure.contrib.duck-streams/*default-encoding* if it is not specified." ([http-agnt] - (string http-agnt (or (.getContentEncoding (::connection @http-agnt)) + (string http-agnt (or (.getContentEncoding + #^HttpURLConnection (::connection @http-agnt)) duck/*default-encoding*))) - ([http-agnt encoding] + ([http-agnt #^String encoding] (.toString (get-byte-buffer http-agnt) encoding))) @@ -302,14 +307,14 @@ received." [http-agnt] (when (done? http-agnt) - (.getResponseCode (::connection @http-agnt)))) + (.getResponseCode #^HttpURLConnection (::connection @http-agnt)))) (defn message "Returns the HTTP response message (e.g. 'Not Found'), for this request, or nil if the response has not yet been received." [http-agnt] (when (done? http-agnt) - (.getResponseMessage (::connection http-agnt)))) + (.getResponseMessage #^HttpURLConnection (::connection http-agnt)))) (defn headers "Returns a map of HTTP response headers. Header names are converted @@ -318,20 +323,21 @@ [http-agnt] (reduce (fn [m [#^String k v]] (assoc m (when k (keyword (.toLowerCase k))) (last v))) - {} (.getHeaderFields (::connection @http-agnt)))) + {} (.getHeaderFields + #^HttpURLConnection (::connection @http-agnt))) (defn headers-seq "Returns the HTTP response headers in order as a sequence of [String,String] pairs. The first 'header' name may be null for the HTTP status line." [http-agnt] - (let [conn (::connection @http-agnt) - f (fn thisfn [i] + (let [#^HttpURLConnection conn (::connection @http-agnt) + f (fn thisfn [#^Integer i] ;; Get value first because first key may be nil. (when-let [value (.getHeaderField conn i)] (cons [(.getHeaderFieldKey conn i) value] (thisfn (inc i)))))] - (lazy-seq (f 0)))) + (lazy-seq (f 0))))) ;;; RESPONSE STATUS CODE ACCESSORS |