Skip to content

Commit

Permalink
feat: Add a protocol field to export.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrozev committed Oct 21, 2024
1 parent adc8b79 commit 362deeb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 deletions.
26 changes: 21 additions & 5 deletions src/main/kotlin/org/jitsi/xmpp/extensions/colibri2/Export.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ import java.net.URISyntaxException

class Export(
val url: URI,
val protocol: Protocols?,
audio: Boolean = false,
video: Boolean = false
) : AbstractPacketExtension(NAMESPACE, ELEMENT) {
init {
setAttribute(URL_ATTR_NAME, url)
protocol?.let { setAttribute(PROTOCOL_ATTR_NAME, it.toString().lowercase()) }
if (audio) {
setAttribute(AUDIO_ATTR_NAME, true)
}
Expand All @@ -45,10 +47,15 @@ class Export(
val video: Boolean
get() = getAttributeAsString(VIDEO_ATTR_NAME)?.toBoolean() ?: false

enum class Protocols(val value: String) {
MEDIAJSON("mediajson")
}

companion object {
const val ELEMENT = "export"
const val NAMESPACE = ConferenceModifyIQ.NAMESPACE
const val URL_ATTR_NAME = "url"
const val PROTOCOL_ATTR_NAME = "protocol"
const val AUDIO_ATTR_NAME = "audio"
const val VIDEO_ATTR_NAME = "video"
}
Expand All @@ -59,13 +66,22 @@ class ExportProvider : DefaultPacketExtensionProvider<Export>(Export::class.java
override fun parse(parser: XmlPullParser, depth: Int, xml: XmlEnvironment?): Export {
val url = parser.getAttributeValue("", Export.URL_ATTR_NAME)
?: throw SmackParsingException.RequiredAttributeMissingException("Missing 'url' attribute")
val uri = try {
URI(url)
} catch (e: Exception) {
throw SmackParsingException("Invalid 'url': ${e.message}")
}
val audio = parser.getAttributeValue("", Export.AUDIO_ATTR_NAME)?.toBoolean() ?: false
val video = parser.getAttributeValue("", Export.VIDEO_ATTR_NAME)?.toBoolean() ?: false

try {
return Export(URI(url), audio, video)
} catch (e: URISyntaxException) {
throw SmackParsingException("Invalid 'url': ${e.message}")
val protocolStr = parser.getAttributeValue("", Export.PROTOCOL_ATTR_NAME)
?: throw SmackParsingException.RequiredAttributeMissingException("Missing 'protocol' attribute")
val protocol = try {
Export.Protocols.valueOf(protocolStr.uppercase())
} catch (e: Exception) {
throw SmackParsingException("Invalid 'protocol': $protocolStr")
}


return Export(url = uri, protocol = protocol, audio = audio, video = video)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ object Colibri2JSONDeserializer {
addExport(
Export(
URI(export[Export.URL_ATTR_NAME] as String),
protocol = Export.Protocols.valueOf(
(export[Export.PROTOCOL_ATTR_NAME] as String).uppercase()
),
audio = export[Export.AUDIO_ATTR_NAME]?.toString()?.toBoolean() ?: false,
video = export[Export.VIDEO_ATTR_NAME]?.toString()?.toBoolean() ?: false
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ object Colibri2JSONSerializer {

private fun serializeExport(export: Export) = JSONObject().apply {
put("url", export.url.toString())
put("protocol", export.protocol.toString().lowercase())
if (export.audio) put("audio", true)
if (export.video) put("video", true)
}
Expand Down
26 changes: 21 additions & 5 deletions src/test/kotlin/org/jitsi/xmpp/extensions/colibri2/ExportTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,26 @@ class ExportTest : ShouldSpec() {

context("Parsing a valid extension") {
context("Without audio/video") {
val export = provider.parse(PacketParserUtils.getParserFor("<export url='$url'/>"))
val export = provider.parse(
PacketParserUtils.getParserFor("<export url='$url' protocol='mediajson'/>")
)
export.url shouldBe URI(url)
export.audio shouldBe false
export.video shouldBe false
}
context("With audio") {
val export = provider.parse(PacketParserUtils.getParserFor("<export url='$url' audio='true'/>"))
val export = provider.parse(
PacketParserUtils.getParserFor("<export url='$url' protocol='mediajson' audio='true'/>")
)
export.url shouldBe URI(url)
export.audio shouldBe true
export.video shouldBe false
}
context("With video") {
val export = provider.parse(
PacketParserUtils.getParserFor("<export url='$url' audio='false' video='true'/>")
PacketParserUtils.getParserFor(
"<export url='$url' protocol='mediajson' audio='false' video='true'/>"
)
)
export.url shouldBe URI(url)
export.audio shouldBe false
Expand All @@ -52,12 +58,22 @@ class ExportTest : ShouldSpec() {
}
context("Parsing with missing url") {
shouldThrow<SmackParsingException> {
provider.parse(PacketParserUtils.getParserFor("<export/>"))
provider.parse(PacketParserUtils.getParserFor("<export protocol='mediajson'></export>"))
}
}
context("Parsing with invalid url") {
shouldThrow<SmackParsingException> {
provider.parse(PacketParserUtils.getParserFor("<export url='in val id'/>"))
provider.parse(PacketParserUtils.getParserFor("<export url='in val id' protocol='mediajson'/>"))
}
}
context("Parsing with missing protocol") {
shouldThrow<SmackParsingException> {
provider.parse(PacketParserUtils.getParserFor("<export url='$url'/>"))
}
}
context("Parsing with invalid protocol") {
shouldThrow<SmackParsingException> {
provider.parse(PacketParserUtils.getParserFor("<export url='$url' protocol='abc'/>"))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ private val expectedMappings = listOf(
<capability name="source-names"/>
</endpoint>
<exports>
<export url='wss://example.com/audio' audio='true'/>
<export url='wss://example.com/video' video='true'/>
<export url='wss://example.com/audio' protocol='mediajson' audio='true'/>
<export url='wss://example.com/video' protocol='mediajson' video='true'/>
</exports>
</conference-modify>
</iq>
Expand Down Expand Up @@ -284,8 +284,8 @@ private val expectedMappings = listOf(
}
],
"exports": [
{ "url": "wss://example.com/audio", "audio": true },
{ "url": "wss://example.com/video", "video": true }
{ "url": "wss://example.com/audio", "protocol": "mediajson", "audio": true },
{ "url": "wss://example.com/video", "protocol":"mediajson", "video": true }
]
}
""",
Expand Down

0 comments on commit 362deeb

Please sign in to comment.