From 6381b5e6a1a3cc77fab8486c336497f265ed7560 Mon Sep 17 00:00:00 2001 From: Clinton Alexander Date: Mon, 13 Jun 2016 18:33:53 +0100 Subject: [PATCH] Added OkHttp and used it to clean up the HTTP calls in the demo and then moved those new functions to a util lib. --- build.gradle | 1 + src/main/kotlin/com/r3corda/demos/IRSDemo.kt | 73 +------------------ .../com/r3corda/demos/utilities/HttpUtils.kt | 39 ++++++++++ 3 files changed, 42 insertions(+), 71 deletions(-) create mode 100644 src/main/kotlin/com/r3corda/demos/utilities/HttpUtils.kt diff --git a/build.gradle b/build.gradle index 39d87a3993..e2a00d8296 100644 --- a/build.gradle +++ b/build.gradle @@ -85,6 +85,7 @@ dependencies { compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-test:$kotlin_version" compile "org.jetbrains.kotlinx:kotlinx-support-jdk8:0.2" + compile 'com.squareup.okhttp3:okhttp:3.3.1' // Unit testing helpers. testCompile 'junit:junit:4.12' diff --git a/src/main/kotlin/com/r3corda/demos/IRSDemo.kt b/src/main/kotlin/com/r3corda/demos/IRSDemo.kt index c2a60d4609..1a91e48dd9 100644 --- a/src/main/kotlin/com/r3corda/demos/IRSDemo.kt +++ b/src/main/kotlin/com/r3corda/demos/IRSDemo.kt @@ -37,6 +37,8 @@ import java.nio.file.Paths import java.util.* import kotlin.concurrent.fixedRateTimer import kotlin.system.exitProcess +import org.apache.commons.io.IOUtils +import com.r3corda.demos.utilities.* // IRS DEMO // @@ -421,77 +423,6 @@ private fun runUploadRates(host: HostAndPort) { }) } -// Todo: Use a simpler library function for this and handle timeout exceptions -private fun sendJson(url: URL, data: String, method: String) : Boolean { - val connection = url.openConnection() as HttpURLConnection - connection.doOutput = true - connection.useCaches = false - connection.requestMethod = method - connection.connectTimeout = 5000 - connection.readTimeout = 60000 - connection.setRequestProperty("Connection", "Keep-Alive") - connection.setRequestProperty("Cache-Control", "no-cache") - connection.setRequestProperty("Content-Type", "application/json") - connection.setRequestProperty("Content-Length", data.length.toString()) - - try { - val outStream = DataOutputStream(connection.outputStream) - outStream.writeBytes(data) - outStream.close() - - return when (connection.responseCode) { - 200 -> true - 201 -> true - else -> { - println("Failed to " + method + " data. Status Code: " + connection.responseCode + ". Message: " + connection.responseMessage) - false - } - } - } catch(e: SocketTimeoutException) { - println("Server took too long to respond") - return false - } -} - -private fun putJson(url: URL, data: String) : Boolean { - return sendJson(url, data, "PUT") -} - -private fun postJson(url: URL, data: String) : Boolean { - return sendJson(url, data, "POST") -} - -// Todo: Use a simpler library function for this and handle timeout exceptions -private fun uploadFile(url: URL, file: String) : Boolean { - val boundary = "===" + System.currentTimeMillis() + "===" - val hyphens = "--" - val clrf = "\r\n" - - val connection = url.openConnection() as HttpURLConnection - connection.doOutput = true - connection.doInput = true - connection.useCaches = false - connection.requestMethod = "POST" - connection.connectTimeout = 5000 - connection.readTimeout = 60000 - connection.setRequestProperty("Connection", "Keep-Alive") - connection.setRequestProperty("Cache-Control", "no-cache") - connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary) - - val request = DataOutputStream(connection.outputStream) - request.writeBytes(hyphens + boundary + clrf) - request.writeBytes("Content-Disposition: form-data; name=\"rates\" filename=\"example.rates.txt\"$clrf") - request.writeBytes(clrf) - request.writeBytes(file) - request.writeBytes(clrf) - request.writeBytes(hyphens + boundary + hyphens + clrf) - - if (connection.responseCode == 200) { - return true - } else { - println("Could not upload file. Status Code: " + connection + ". Message: " + connection.responseMessage) - return false - } } private fun getNodeConfig(cliParams: CliParams.RunNode): NodeConfiguration { diff --git a/src/main/kotlin/com/r3corda/demos/utilities/HttpUtils.kt b/src/main/kotlin/com/r3corda/demos/utilities/HttpUtils.kt new file mode 100644 index 0000000000..f26340303d --- /dev/null +++ b/src/main/kotlin/com/r3corda/demos/utilities/HttpUtils.kt @@ -0,0 +1,39 @@ +package com.r3corda.demos.utilities + +import okhttp3.* +import java.net.URL +import java.util.concurrent.TimeUnit + +/** + * A small set of utilities for making HttpCalls, aimed at demos. + */ +private val client = OkHttpClient.Builder() + .connectTimeout(5, TimeUnit.SECONDS) + .readTimeout(15, TimeUnit.SECONDS).build(); + +fun putJson(url: URL, data: String) : Boolean { + val body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), data) + return makeRequest(Request.Builder().url(url).put(body).build()) +} + +fun postJson(url: URL, data: String) : Boolean { + val body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), data) + return makeRequest(Request.Builder().url(url).post(body).build()) +} + +fun uploadFile(url: URL, file: String) : Boolean { + val body = MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("rates", "example.rates.txt", RequestBody.create(MediaType.parse("text/plain"), file)) + .build(); + return makeRequest(Request.Builder().url(url).post(body).build()) +} + +private fun makeRequest(request: Request): Boolean { + val response = client.newCall(request).execute(); + + if (!response.isSuccessful) { + println("Could not fulfill HTTP request. Status Code: ${response.code()}. Message: ${response.body()}") + } + return response.isSuccessful +} \ No newline at end of file