-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add a "connect" extension to colibri2.
- Loading branch information
Showing
8 changed files
with
318 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/main/kotlin/org/jitsi/xmpp/extensions/colibri2/Connect.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright @ 2024 - present 8x8, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jitsi.xmpp.extensions.colibri2 | ||
|
||
import org.jitsi.xmpp.extensions.AbstractPacketExtension | ||
import org.jitsi.xmpp.extensions.DefaultPacketExtensionProvider | ||
import org.jivesoftware.smack.packet.XmlEnvironment | ||
import org.jivesoftware.smack.parsing.SmackParsingException | ||
import org.jivesoftware.smack.xml.XmlPullParser | ||
import org.jivesoftware.smack.xml.XmlPullParserException | ||
import java.io.IOException | ||
import java.net.URI | ||
|
||
class Connect( | ||
val url: URI, | ||
val protocol: Protocols, | ||
val type: Types, | ||
audio: Boolean = false, | ||
video: Boolean = false | ||
) : AbstractPacketExtension(NAMESPACE, ELEMENT) { | ||
init { | ||
setAttribute(URL_ATTR_NAME, url) | ||
setAttribute(PROTOCOL_ATTR_NAME, protocol.toString().lowercase()) | ||
setAttribute(TYPE_ATTR_NAME, type.toString().lowercase()) | ||
if (audio) { | ||
setAttribute(AUDIO_ATTR_NAME, true) | ||
} | ||
if (video) { | ||
setAttribute(VIDEO_ATTR_NAME, true) | ||
} | ||
} | ||
|
||
val audio: Boolean | ||
get() = getAttributeAsString(AUDIO_ATTR_NAME)?.toBoolean() ?: false | ||
val video: Boolean | ||
get() = getAttributeAsString(VIDEO_ATTR_NAME)?.toBoolean() ?: false | ||
|
||
enum class Protocols(val value: String) { | ||
MEDIAJSON("mediajson") | ||
} | ||
|
||
enum class Types(val value: String) { | ||
RECORDER("recorder"), | ||
TRANSCRIBER("transcriber") | ||
} | ||
|
||
companion object { | ||
const val ELEMENT = "connect" | ||
const val NAMESPACE = ConferenceModifyIQ.NAMESPACE | ||
const val URL_ATTR_NAME = "url" | ||
const val PROTOCOL_ATTR_NAME = "protocol" | ||
const val TYPE_ATTR_NAME = "type" | ||
const val AUDIO_ATTR_NAME = "audio" | ||
const val VIDEO_ATTR_NAME = "video" | ||
} | ||
} | ||
|
||
class ConnectProvider : DefaultPacketExtensionProvider<Connect>(Connect::class.java) { | ||
@Throws(XmlPullParserException::class, IOException::class, SmackParsingException::class) | ||
override fun parse(parser: XmlPullParser, depth: Int, xml: XmlEnvironment?): Connect { | ||
val url = parser.getAttributeValue("", Connect.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("", Connect.AUDIO_ATTR_NAME)?.toBoolean() ?: false | ||
val video = parser.getAttributeValue("", Connect.VIDEO_ATTR_NAME)?.toBoolean() ?: false | ||
val protocolStr = parser.getAttributeValue("", Connect.PROTOCOL_ATTR_NAME) | ||
?: throw SmackParsingException.RequiredAttributeMissingException("Missing 'protocol' attribute") | ||
val protocol = try { | ||
Connect.Protocols.valueOf(protocolStr.uppercase()) | ||
} catch (e: Exception) { | ||
throw SmackParsingException("Invalid 'protocol': $protocolStr") | ||
} | ||
val typeStr = parser.getAttributeValue("", Connect.TYPE_ATTR_NAME) | ||
?: throw SmackParsingException.RequiredAttributeMissingException("Missing 'type' attribute") | ||
val type = try { | ||
Connect.Types.valueOf(typeStr.uppercase()) | ||
} catch (e: Exception) { | ||
throw SmackParsingException("Invalid 'type': $typeStr") | ||
} | ||
|
||
return Connect(url = uri, protocol = protocol, type = type, audio = audio, video = video) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/org/jitsi/xmpp/extensions/colibri2/Connects.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright @ 2024 - present 8x8, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jitsi.xmpp.extensions.colibri2 | ||
|
||
import org.jitsi.xmpp.extensions.AbstractPacketExtension | ||
import org.jitsi.xmpp.extensions.DefaultPacketExtensionProvider | ||
|
||
class Connects : AbstractPacketExtension(NAMESPACE, ELEMENT) { | ||
|
||
fun getConnects(): List<Connect> = getChildExtensionsOfType(Connect::class.java) | ||
fun addConnect(connect: Connect) = addChildExtension(connect) | ||
|
||
companion object { | ||
const val ELEMENT = "connects" | ||
const val NAMESPACE = ConferenceModifyIQ.NAMESPACE | ||
} | ||
} | ||
|
||
class ConnectsProvider : DefaultPacketExtensionProvider<Connects>(Connects::class.java) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/test/kotlin/org/jitsi/xmpp/extensions/colibri2/ConnectTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright @ 2024 - present 8x8, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jitsi.xmpp.extensions.colibri2 | ||
|
||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.core.spec.style.ShouldSpec | ||
import io.kotest.matchers.shouldBe | ||
import org.jivesoftware.smack.parsing.SmackParsingException | ||
import org.jivesoftware.smack.util.PacketParserUtils | ||
import java.net.URI | ||
|
||
class ConnectTest : ShouldSpec() { | ||
init { | ||
IqProviderUtils.registerProviders() | ||
val provider = ConnectProvider() | ||
val url = "ws://example.com" | ||
|
||
context("Parsing a valid extension") { | ||
context("Without audio/video") { | ||
val connect = provider.parse( | ||
PacketParserUtils.getParserFor("<connect url='$url' protocol='mediajson' type='recorder'/>") | ||
) | ||
connect.url shouldBe URI(url) | ||
connect.protocol shouldBe Connect.Protocols.MEDIAJSON | ||
connect.type shouldBe Connect.Types.RECORDER | ||
connect.audio shouldBe false | ||
connect.video shouldBe false | ||
} | ||
context("With audio") { | ||
val connect = provider.parse( | ||
PacketParserUtils.getParserFor( | ||
"<connect url='$url' protocol='mediajson' type='recorder' audio='true'/>" | ||
) | ||
) | ||
connect.url shouldBe URI(url) | ||
connect.protocol shouldBe Connect.Protocols.MEDIAJSON | ||
connect.type shouldBe Connect.Types.RECORDER | ||
connect.audio shouldBe true | ||
connect.video shouldBe false | ||
} | ||
context("With video") { | ||
val connect = provider.parse( | ||
PacketParserUtils.getParserFor( | ||
"<connect url='$url' protocol='mediajson' type='transcriber' audio='false' video='true'/>" | ||
) | ||
) | ||
connect.url shouldBe URI(url) | ||
connect.protocol shouldBe Connect.Protocols.MEDIAJSON | ||
connect.type shouldBe Connect.Types.TRANSCRIBER | ||
connect.audio shouldBe false | ||
connect.video shouldBe true | ||
} | ||
} | ||
context("Parsing with missing url") { | ||
shouldThrow<SmackParsingException> { | ||
provider.parse( | ||
PacketParserUtils.getParserFor("<connect protocol='mediajson' type='recorder '></connect>") | ||
) | ||
} | ||
} | ||
context("Parsing with invalid url") { | ||
shouldThrow<SmackParsingException> { | ||
provider.parse( | ||
PacketParserUtils.getParserFor("<connect url='in val id' protocol='mediajson' type='recorder'/>") | ||
) | ||
} | ||
} | ||
context("Parsing with missing protocol") { | ||
shouldThrow<SmackParsingException> { | ||
provider.parse(PacketParserUtils.getParserFor("<connect url='$url' type='recorder'/>")) | ||
} | ||
} | ||
context("Parsing with invalid protocol") { | ||
shouldThrow<SmackParsingException> { | ||
provider.parse(PacketParserUtils.getParserFor("<connect url='$url' protocol='abc' type='recorder'/>")) | ||
} | ||
} | ||
context("Parsing with missing type") { | ||
shouldThrow<SmackParsingException> { | ||
provider.parse(PacketParserUtils.getParserFor("<connect url='$url' protocol='mediajson'/>")) | ||
} | ||
} | ||
context("Parsing with invalid type") { | ||
shouldThrow<SmackParsingException> { | ||
provider.parse(PacketParserUtils.getParserFor("<connect url='$url' protocol='mediajson' type='inv'/>")) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.