Improved error handling in IRS demo

This commit is contained in:
Clinton Alexander 2016-06-07 17:17:19 +01:00 committed by Andras Slemmer
parent 60daf8059f
commit de27b1e8de

View File

@ -40,11 +40,7 @@ import java.util.*
import kotlin.concurrent.fixedRateTimer import kotlin.concurrent.fixedRateTimer
import kotlin.system.exitProcess import kotlin.system.exitProcess
import org.apache.commons.io.IOUtils import org.apache.commons.io.IOUtils
import org.glassfish.jersey.client.JerseyClientBuilder import java.net.SocketTimeoutException
import org.glassfish.jersey.client.JerseyWebTarget
import java.io.FileNotFoundException
import javax.ws.rs.client.Entity
import javax.ws.rs.client.Invocation
// IRS DEMO // IRS DEMO
// //
@ -306,22 +302,28 @@ private fun sendJson(url: URL, data: String, method: String) : Boolean {
connection.useCaches = false connection.useCaches = false
connection.requestMethod = method connection.requestMethod = method
connection.connectTimeout = 5000 connection.connectTimeout = 5000
connection.readTimeout = 5000 connection.readTimeout = 10000
connection.setRequestProperty("Connection", "Keep-Alive") connection.setRequestProperty("Connection", "Keep-Alive")
connection.setRequestProperty("Cache-Control", "no-cache") connection.setRequestProperty("Cache-Control", "no-cache")
connection.setRequestProperty("Content-Type", "application/json") connection.setRequestProperty("Content-Type", "application/json")
connection.setRequestProperty("Content-Length", data.length.toString()) connection.setRequestProperty("Content-Length", data.length.toString())
val outStream = DataOutputStream(connection.outputStream)
outStream.writeBytes(data)
outStream.close()
return when (connection.responseCode) { try {
200 -> true val outStream = DataOutputStream(connection.outputStream)
201 -> true outStream.writeBytes(data)
else -> { outStream.close()
println("Failed to " + method + " data. Status Code: " + connection.responseCode + ". Mesage: " + connection.responseMessage)
false return when (connection.responseCode) {
200 -> true
201 -> true
else -> {
println("Failed to " + method + " data. Status Code: " + connection.responseCode + ". Mesage: " + connection.responseMessage)
false
}
} }
} catch(e: SocketTimeoutException) {
println("Server took too long to respond")
return false
} }
} }