aboutsummaryrefslogtreecommitdiff
path: root/modules/http-connection/src
diff options
context:
space:
mode:
authorStuart Sierra <mail@stuartsierra.com>2010-08-07 16:41:53 -0400
committerStuart Sierra <mail@stuartsierra.com>2010-08-07 16:41:53 -0400
commita6a92b9b3d2bfd9a56e1e5e9cfba706d1aeeaae5 (patch)
treef1f3da9887dc2dc557df3282b0bcbd4d701ec593 /modules/http-connection/src
parente7930c85290f77815cdb00a60604feedfa2d0194 (diff)
Split all namespaces into sub-modules.
* Examples and tests have not been copied over. * Clojure test/compile phases are commented out in parent POM. * May require installing parent POM before full build.
Diffstat (limited to 'modules/http-connection/src')
-rw-r--r--modules/http-connection/src/main/clojure/clojure/contrib/http/connection.clj62
1 files changed, 62 insertions, 0 deletions
diff --git a/modules/http-connection/src/main/clojure/clojure/contrib/http/connection.clj b/modules/http-connection/src/main/clojure/clojure/contrib/http/connection.clj
new file mode 100644
index 00000000..c6cf162a
--- /dev/null
+++ b/modules/http-connection/src/main/clojure/clojure/contrib/http/connection.clj
@@ -0,0 +1,62 @@
+;;; http/connection.clj: low-level HTTP client API around HttpURLConnection
+
+;; by Stuart Sierra, http://stuartsierra.com/
+;; June 8, 2009
+
+;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use
+;; and distribution terms for this software are covered by the Eclipse
+;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+;; which can be found in the file epl-v10.html at the root of this
+;; distribution. By using this software in any fashion, you are
+;; agreeing to be bound by the terms of this license. You must not
+;; remove this notice, or any other, from this software.
+
+;; DEPRECATED IN 1.2. Use direct Java bits, or take a look at
+;; http://github.com/technomancy/clojure-http-client
+
+(ns ^{:deprecated "1.2"
+ :doc "Low-level HTTP client API around HttpURLConnection"}
+ clojure.contrib.http.connection
+ (:require [clojure.contrib.io :as duck])
+ (:import (java.net URI URL HttpURLConnection)
+ (java.io File InputStream Reader)))
+
+(defn http-connection
+ "Opens an HttpURLConnection at the URL, handled by as-url."
+ [url]
+ (.openConnection (duck/as-url url)))
+
+(defmulti
+ ^{:doc "Transmits a request entity body."}
+ send-request-entity (fn [conn entity] (type entity)))
+
+(defmethod send-request-entity duck/*byte-array-type* [^HttpURLConnection conn entity]
+ (.setFixedLengthStreamingMode conn (count entity))
+ (.connect conn)
+ (duck/copy entity (.getOutputStream conn)))
+
+(defmethod send-request-entity String [conn ^String entity]
+ (send-request-entity conn (.getBytes entity duck/*default-encoding*)))
+
+(defmethod send-request-entity File [^HttpURLConnection conn ^File entity]
+ (.setFixedLengthStreamingMode conn (.length entity))
+ (.connect conn)
+ (duck/copy entity (.getOutputStream conn)))
+
+(defmethod send-request-entity InputStream [^HttpURLConnection conn entity]
+ (.setChunkedStreamingMode conn -1)
+ (.connect conn)
+ (duck/copy entity (.getOutputStream conn)))
+
+(defmethod send-request-entity Reader [^HttpURLConnection conn entity]
+ (.setChunkedStreamingMode conn -1)
+ (.connect conn)
+ (duck/copy entity (.getOutputStream conn)))
+
+(defn start-http-connection
+ ([^HttpURLConnection conn] (.connect conn))
+ ([^HttpURLConnection conn request-entity-body]
+ (if request-entity-body
+ (do (.setDoOutput conn true)
+ (send-request-entity conn request-entity-body))
+ (.connect conn))))