Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /colibri/muc-client/list endpoint #1680

Merged
merged 5 commits into from Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion doc/rest-muc-client.md
Expand Up @@ -17,7 +17,7 @@ A new XMPP client connection (i.e. a MucClient) can be added by posting a JSON w

If a configuration with the specified ID already exists, the request will succeed (return 200), but the configuration will NOT be updated. If you need to update an existing configuration, you need to remove it first and then re-add it.

# Removing an XMPP client connection.
# Removing an XMPP client connection
An XMPP client connection (i.e. a MucClient) can be removed by posting a JSON which contains its ID to `/colibri/muc-client/remove`:
```
{
Expand All @@ -26,3 +26,7 @@ An XMPP client connection (i.e. a MucClient) can be removed by posting a JSON wh
```

The request will be successful (return 200) if an XMPP client connection was removed.


# Listing XMPP client connections
IDs of previously added XMPP client connections can be listed using a GET call to `/colibri/muc-client/list`.
Expand Up @@ -74,4 +74,12 @@ public Response removeMucClient(String requestBody) throws ParseException
}
return Response.status(HttpServletResponse.SC_BAD_REQUEST).build();
}

@Path("/list")
@GET
@Produces(MediaType.APPLICATION_JSON)
public String listMucClientIDs()
{
return xmppConnection.getMucClientIds();
}
}
Expand Up @@ -33,6 +33,7 @@ import org.jivesoftware.smack.packet.ExtensionElement
import org.jivesoftware.smack.packet.IQ
import org.jivesoftware.smack.packet.XMPPError
import org.jivesoftware.smackx.iqversion.packet.Version
import org.json.simple.JSONArray
import org.json.simple.JSONObject
import java.util.concurrent.atomic.AtomicBoolean

Expand Down Expand Up @@ -129,6 +130,14 @@ class XmppConnection : IQListener {
return true
}

/**
* Returns ids of [MucClient] that have been added.
* @return JSON string of the list of ids
*/
fun getMucClientIds(): String {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified to:

    val mucClientIds: String
        get() = JSONArray().apply { addAll(mucClientManager.mucClientIds) }.toJSONString()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh very nice.

Pardon my n00b questions; I've never actually touched Kotlin before.

How do I apply this? Is that a different form of function declaration, or a way to simplify the body? I attempted a few variations but generally ended up with compilation errors.

It did however work if I simplified the whole function to:

    fun getMucClientIds(): String {
        return JSONArray().apply { addAll(mucClientManager.mucClientIds) }.toJSONString()
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed an update with a variation of your simplification that is within my current ability. Happy to make further changes with your guidance.

return JSONArray().apply { addAll(mucClientManager.mucClientIds) }.toJSONString()
}

/**
* Removes a {@link MucClient} with an ID described in JSON.
* @param jsonObject the JSON which contains the ID of the client to remove.
Expand Down
Expand Up @@ -26,6 +26,7 @@ import org.glassfish.jersey.test.JerseyTest
import org.glassfish.jersey.test.TestProperties
import org.jitsi.videobridge.rest.MockBinder
import org.jitsi.videobridge.xmpp.XmppConnection
import org.json.simple.JSONArray
import org.json.simple.JSONObject
import org.junit.Test
import javax.ws.rs.client.Entity
Expand Down Expand Up @@ -91,25 +92,36 @@ class MucClientTest : JerseyTest() {
fun testRemoveMuc() {
val jsonConfigSlot = slot<JSONObject>()

every { xmppConnection.addMucClient(capture(jsonConfigSlot)) } returns true
every { xmppConnection.removeMucClient(capture(jsonConfigSlot)) } returns true

val json = JSONObject().apply {
put("id", "id")
}

val resp = target("$baseUrl/add").request().post(Entity.json(json.toJSONString()))
val resp = target("$baseUrl/remove").request().post(Entity.json(json.toJSONString()))
resp.status shouldBe HttpStatus.OK_200
jsonConfigSlot.captured shouldBe json
}

@Test
fun testRemoveMucFailure() {
every { xmppConnection.addMucClient(any()) } returns false
every { xmppConnection.removeMucClient(any()) } returns false

val json = JSONObject().apply {
put("id", "id")
}
val resp = target("$baseUrl/add").request().post(Entity.json(json.toJSONString()))
val resp = target("$baseUrl/remove").request().post(Entity.json(json.toJSONString()))
resp.status shouldBe HttpStatus.BAD_REQUEST_400
}

@Test
fun testGetIds() {
val fakeIds = JSONArray().apply {
add("id1")
}
every { xmppConnection.getMucClientIds() } returns fakeIds.toJSONString()

val resp = target("$baseUrl/list").request().get()
resp.readEntity(String::class.java) shouldBe "[\"id1\"]"
}
}
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -26,7 +26,7 @@
<jetty.version>9.4.40.v20210413</jetty.version>
<kotlin.version>1.3.72</kotlin.version>
<kotest.version>4.1.3</kotest.version>
<jicoco.version>1.1-84-g1d06d1f</jicoco.version>
<jicoco.version>1.1-89-g9ca691c</jicoco.version>
<jitsi.utils.version>1.0-93-ge6aba2e</jitsi.utils.version>
<maven-shade-plugin.version>3.2.2</maven-shade-plugin.version>
<spotbugs.version>4.1.4</spotbugs.version>
Expand Down