mirror of
https://github.com/corda/corda.git
synced 2025-06-17 06:38:21 +00:00
CORDA-499: Remove further Kt
classes (#1489)
* Change how NetworkHostAndPort is parsed Change from using a global extension function to parse NetworkHostAndPort strings, into using a function on the companion object. This is a lot easier for Java interop and matches the common style used elsewhere both in Corda and in Java libraries. * Move JAR extraction into new utils file * Move path verification function to ArtemisUtils Move path verification function "requireOnDefaultFileSystem()" to new ArtemisUtils.kt file, as this makes more sense from a Java interop perspective. * Add JvmName to AMQPSchemaExtensions * Add JvmName to AMQPSerializationScheme * Revert "Move JAR extraction into new utils file" This reverts commit 1f0f41909b68ff21cc24b5efd6a1a360393a0a14. * Reformat code * Run formatter on ArtemisUtils
This commit is contained in:
@ -2,38 +2,43 @@ package net.corda.core.utilities
|
||||
|
||||
import net.corda.core.serialization.CordaSerializable
|
||||
import java.net.URI
|
||||
import java.net.URISyntaxException
|
||||
|
||||
/**
|
||||
* Tuple of host and port. Use [parseNetworkHostAndPort] on untrusted data.
|
||||
* Tuple of host and port. Use [NetworkHostAndPort.parse] on untrusted data.
|
||||
* @param host a hostname or IP address. IPv6 addresses must not be enclosed in square brackets.
|
||||
* @param port a valid port number.
|
||||
*/
|
||||
@CordaSerializable
|
||||
data class NetworkHostAndPort(val host: String, val port: Int) {
|
||||
companion object {
|
||||
internal val invalidPortFormat = "Invalid port: %s"
|
||||
internal val unparseableAddressFormat = "Unparseable address: %s"
|
||||
internal val missingPortFormat = "Missing port: %s"
|
||||
internal const val INVALID_PORT_FORMAT = "Invalid port: %s"
|
||||
internal const val UNPARSEABLE_ADDRESS_FORMAT = "Unparseable address: %s"
|
||||
internal const val MISSING_PORT_FORMAT = "Missing port: %s"
|
||||
private val bracketedHost = "\\[(.*)]".toRegex()
|
||||
|
||||
/**
|
||||
* Parses a string of the form host:port into a [NetworkHostAndPort].
|
||||
* The host part may be a hostname or IP address. If it's an IPv6 address, it must be enclosed in square brackets.
|
||||
* Note this does not parse the toString of a resolved [java.net.InetSocketAddress], which is of a host/IP:port form.
|
||||
* @throws IllegalArgumentException if the port is missing, the string is garbage, or the NetworkHostAndPort constructor rejected the parsed parts.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun parse(str: String): NetworkHostAndPort {
|
||||
val uri = try {
|
||||
URI(null, str, null, null, null)
|
||||
} catch(ex: URISyntaxException) {
|
||||
throw IllegalArgumentException("Host and port syntax is invalid, expected host:port")
|
||||
}
|
||||
require(uri.host != null) { NetworkHostAndPort.UNPARSEABLE_ADDRESS_FORMAT.format(str) }
|
||||
require(uri.port != -1) { NetworkHostAndPort.MISSING_PORT_FORMAT.format(str) }
|
||||
return NetworkHostAndPort(bracketedHost.matchEntire(uri.host)?.groupValues?.get(1) ?: uri.host, uri.port)
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
require(port in (0..0xffff)) { invalidPortFormat.format(port) }
|
||||
require(port in (0..0xffff)) { INVALID_PORT_FORMAT.format(port) }
|
||||
}
|
||||
|
||||
override fun toString() = if (':' in host) "[$host]:$port" else "$host:$port"
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a string of the form host:port into a [NetworkHostAndPort].
|
||||
* The host part may be a hostname or IP address. If it's an IPv6 address, it must be enclosed in square brackets.
|
||||
* Note this does not parse the toString of a resolved [java.net.InetSocketAddress], which is of a host/IP:port form.
|
||||
* @throws IllegalArgumentException if the port is missing, the string is garbage, or the NetworkHostAndPort constructor rejected the parsed parts.
|
||||
*/
|
||||
fun String.parseNetworkHostAndPort() = run {
|
||||
val uri = URI(null, this, null, null, null)
|
||||
require(uri.host != null) { NetworkHostAndPort.unparseableAddressFormat.format(this) }
|
||||
require(uri.port != -1) { NetworkHostAndPort.missingPortFormat.format(this) }
|
||||
NetworkHostAndPort(bracketedHost.matchEntire(uri.host)?.groupValues?.get(1) ?: uri.host, uri.port)
|
||||
}
|
||||
|
||||
private val bracketedHost = "\\[(.*)]".toRegex()
|
||||
|
@ -24,7 +24,7 @@ class NetworkHostAndPortTest {
|
||||
listOf(65536, -1).forEach {
|
||||
assertThatThrownBy {
|
||||
NetworkHostAndPort("example.com", it)
|
||||
}.isInstanceOf(IllegalArgumentException::class.java).hasMessage(NetworkHostAndPort.invalidPortFormat.format(it))
|
||||
}.isInstanceOf(IllegalArgumentException::class.java).hasMessage(NetworkHostAndPort.INVALID_PORT_FORMAT.format(it))
|
||||
}
|
||||
}
|
||||
|
||||
@ -41,20 +41,20 @@ class NetworkHostAndPortTest {
|
||||
|
||||
@Test
|
||||
fun `parseNetworkHostAndPort works`() {
|
||||
assertEquals(NetworkHostAndPort("example.com", 1234), "example.com:1234".parseNetworkHostAndPort())
|
||||
assertEquals(NetworkHostAndPort("example.com", 65535), "example.com:65535".parseNetworkHostAndPort())
|
||||
assertEquals(NetworkHostAndPort("1.2.3.4", 1234), "1.2.3.4:1234".parseNetworkHostAndPort())
|
||||
assertEquals(NetworkHostAndPort("::1", 1234), "[::1]:1234".parseNetworkHostAndPort())
|
||||
assertEquals(NetworkHostAndPort("0:0:0:0:0:0:0:1", 1234), "[0:0:0:0:0:0:0:1]:1234".parseNetworkHostAndPort())
|
||||
assertEquals(NetworkHostAndPort("example.com", 1234), NetworkHostAndPort.parse("example.com:1234"))
|
||||
assertEquals(NetworkHostAndPort("example.com", 65535), NetworkHostAndPort.parse("example.com:65535"))
|
||||
assertEquals(NetworkHostAndPort("1.2.3.4", 1234), NetworkHostAndPort.parse("1.2.3.4:1234"))
|
||||
assertEquals(NetworkHostAndPort("::1", 1234), NetworkHostAndPort.parse("[::1]:1234"))
|
||||
assertEquals(NetworkHostAndPort("0:0:0:0:0:0:0:1", 1234), NetworkHostAndPort.parse("[0:0:0:0:0:0:0:1]:1234"))
|
||||
listOf("0:0:0:0:0:0:0:1:1234", ":1234", "example.com:-1").forEach {
|
||||
assertThatThrownBy {
|
||||
it.parseNetworkHostAndPort()
|
||||
}.isInstanceOf(IllegalArgumentException::class.java).hasMessage(NetworkHostAndPort.unparseableAddressFormat.format(it))
|
||||
NetworkHostAndPort.parse(it)
|
||||
}.isInstanceOf(IllegalArgumentException::class.java).hasMessage(NetworkHostAndPort.UNPARSEABLE_ADDRESS_FORMAT.format(it))
|
||||
}
|
||||
listOf("example.com:", "example.com").forEach {
|
||||
assertThatThrownBy {
|
||||
it.parseNetworkHostAndPort()
|
||||
}.isInstanceOf(IllegalArgumentException::class.java).hasMessage(NetworkHostAndPort.missingPortFormat.format(it))
|
||||
NetworkHostAndPort.parse(it)
|
||||
}.isInstanceOf(IllegalArgumentException::class.java).hasMessage(NetworkHostAndPort.MISSING_PORT_FORMAT.format(it))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user