Added OkHttp and used it to clean up the HTTP calls in the demo and then moved those new functions to a util lib.

This commit is contained in:
Clinton Alexander 2016-06-13 18:33:53 +01:00
parent bbc5c2e981
commit 6381b5e6a1
3 changed files with 42 additions and 71 deletions

View File

@ -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'

View File

@ -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 {

View File

@ -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
}