com.henricook.tls.TLSConnectionWrapper.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cryptoutils_2.13 Show documentation
Show all versions of cryptoutils_2.13 Show documentation
Cryptoutils for Scala 2.12 and 2.13 - Forked from Karasiq
The newest version!
package com.henricook.tls
import java.nio.channels.SocketChannel
import org.bouncycastle.crypto.tls.{AlertDescription, TlsFatalAlert}
import scala.concurrent.Promise
import scala.util.control.Exception
trait TLSConnectionWrapper {
protected val handshake: Promise[Boolean] = Promise()
protected def onError(message: String, exc: Throwable): Unit = {}
protected def onInfo(message: String): Unit = {}
protected def wrapException[T](message: String)(f: ⇒ T): T = {
val catcher = Exception.allCatch.withApply { exc ⇒
handshake.tryFailure(exc)
if (exc.isInstanceOf[TlsFatalAlert]) throw exc
else {
onError(message, exc)
throw new TlsFatalAlert(
AlertDescription.internal_error,
new TLSException(message, exc)
)
}
}
catcher(f)
}
def apply(connection: SocketChannel): SocketChannel
}