aboutsummaryrefslogtreecommitdiff
path: root/plugins/sonatype-release/src/sonatype/HttpUtils.scala
diff options
context:
space:
mode:
authorNikolay Tatarinov <rockjam@actor.im>2016-10-03 20:19:27 +0300
committerJan Christopher Vogt <oss.nsp@cvogt.org>2016-10-03 13:19:27 -0400
commit669ef3dfc3201fffa451b47d2b629a856afc0b25 (patch)
tree7bcb64c58266a4fe2dd3965dda47dc6f8fa71b04 /plugins/sonatype-release/src/sonatype/HttpUtils.scala
parent174c52b2c24b8491eef687ee5eb3c3b77c34a61c (diff)
downloadcbt-669ef3dfc3201fffa451b47d2b629a856afc0b25.tar.gz
cbt-669ef3dfc3201fffa451b47d2b629a856afc0b25.tar.bz2
cbt-669ef3dfc3201fffa451b47d2b629a856afc0b25.zip
Sonatype release plugin (#247)
Diffstat (limited to 'plugins/sonatype-release/src/sonatype/HttpUtils.scala')
-rw-r--r--plugins/sonatype-release/src/sonatype/HttpUtils.scala65
1 files changed, 65 insertions, 0 deletions
diff --git a/plugins/sonatype-release/src/sonatype/HttpUtils.scala b/plugins/sonatype-release/src/sonatype/HttpUtils.scala
new file mode 100644
index 0000000..9d23744
--- /dev/null
+++ b/plugins/sonatype-release/src/sonatype/HttpUtils.scala
@@ -0,0 +1,65 @@
+package cbt.sonatype
+
+import java.net.URL
+
+import cbt.Stage0Lib
+
+import scala.annotation.tailrec
+import scala.util.{ Failure, Success, Try }
+
+private[sonatype] object HttpUtils {
+ // Make http GET. On failure request will be retried with exponential backoff.
+ def GET(uri: String, headers: Map[String, String]): (Int, String) =
+ withRetry(httpRequest("GET", uri, headers))
+
+ // Make http POST. On failure request will be retried with exponential backoff.
+ def POST(uri: String, body: Array[Byte], headers: Map[String, String]): (Int, String) =
+ withRetry(httpRequest("POST", uri, headers, body))
+
+ private def httpRequest(method: String, uri: String, headers: Map[String, String], body: Array[Byte] = Array.emptyByteArray): (Int, String) = {
+ val conn = Stage0Lib.openConnectionConsideringProxy(new URL(uri))
+ conn.setReadTimeout(60000) // 1 minute
+ conn.setConnectTimeout(30000) // 30 seconds
+
+ headers foreach { case (k,v) =>
+ conn.setRequestProperty(k, v)
+ }
+ conn.setRequestMethod(method)
+ if(method == "POST" || method == "PUT") { // PATCH too?
+ conn.setDoOutput(true)
+ conn.getOutputStream.write(body)
+ }
+
+ val arr = new Array[Byte](conn.getContentLength)
+ conn.getInputStream.read(arr)
+
+ conn.getResponseCode -> new String(arr)
+ }
+
+ // ============== General utilities
+
+ def withRetry[T](f: => T): T = withRetry(4000, 5)(f)
+
+ /**
+ * Retry execution of `f` `retriesLeft` times
+ * with `delay` doubled between attempts.
+ */
+ @tailrec
+ def withRetry[T](delay: Int, retriesLeft: Int)(f: ⇒ T): T = {
+ Try(f) match {
+ case Success(result) ⇒
+ result
+ case Failure(e) ⇒
+ if (retriesLeft == 0) {
+ throw new Exception(e)
+ } else {
+ val newDelay = delay * 2
+ val newRetries = retriesLeft - 1
+// log(s"Failed with exception: $e, will retry $newRetries times; waiting: $delay")
+ Thread.sleep(delay)
+
+ withRetry(newDelay, newRetries)(f)
+ }
+ }
+ }
+}