Skip to content

Instantly share code, notes, and snippets.

@9876691
Created October 24, 2017 08:17
Show Gist options
  • Save 9876691/8bc3c2cf6494bd67b759f878fcb3851a to your computer and use it in GitHub Desktop.
Save 9876691/8bc3c2cf6494bd67b759f878fcb3851a to your computer and use it in GitHub Desktop.
Create a DSConnection from Kotlin.
import com.calypso.tk.util.ConnectionUtil
import java.io.FileOutputStream
import java.io.IOException
import java.io.File
import java.net.URL
fun main(args : Array<String>) {
addSSLSettings()
val dsCon = ConnectionUtil.connect("username", "password", "Testing", "env")
val t = dsCon.remoteTrade.getTrade(1234)
println("Trade {$t.getId()}")
dsCon.disconnect()
}
fun addSSLSettings() {
val url = File::class.java!!.getResource("/client.truststore")
try {
if (url != null && url!!.getProtocol() != null) {
val trustFile = createTrustFile(url)
System.setProperty("javax.net.ssl.trustStore", trustFile!!.toString())
System.setProperty("javax.net.ssl.trustStorePassword", "calypso")
}
} catch (e: Exception) {
println(e.message)
}
}
@Throws(IOException::class)
fun createTrustFile(url: URL): File? {
val trustFile = File.createTempFile("client", "truststore")
val out = FileOutputStream(trustFile)
val input = url.openStream()
println("Writting client truststore to temp file : " + trustFile!!)
val buf = ByteArray(8192)
var br = input.read(buf)
while (br != -1) {
out.write(buf, 0, br)
br = input.read(buf)
}
input.close()
out.close()
return trustFile
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment