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

JVB API IQ message #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/main/java/org/jitsi/xmpp/extensions/JsonPacketExtension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright @ 2018 - 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;

public class JsonPacketExtension extends AbstractPacketExtension
{
public static final String ELEMENT_NAME = "json";

/**
* Create a new {@link JsonPacketExtension}
*/
// Used by the ExtensionElementProvider when parsing
@SuppressWarnings("unused")
public JsonPacketExtension()
{
super(null, ELEMENT_NAME);
}

/**
* Create a new {@link JsonPacketExtension} with the given JSON content
* @param json the content, encoded as JSON
*/
public JsonPacketExtension(String json)
{
super(null, ELEMENT_NAME);
setText(json);
}

public String getJsonBody()
{
return getText();
}
}
75 changes: 75 additions & 0 deletions src/main/java/org/jitsi/xmpp/extensions/colibri/JvbApiIq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright @ 2018 - 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.colibri;

import org.jitsi.xmpp.extensions.*;
import org.jivesoftware.smack.packet.*;

import java.util.*;

/**
* A {@link SimpleIQ} which functions as a container for JVB API messages.
* Designed to be used with an added
* {@link org.jitsi.xmpp.extensions.JsonPacketExtension} child extension which
* contains the message payload
*/
public class JvbApiIq extends SimpleIQ
{
public static final String ELEMENT_NAME = "jvb-api";

public static final String NAMESPACE =
ColibriConferenceIQ.NAMESPACE + "/v2";

public JvbApiIq()
{
super(ELEMENT_NAME, NAMESPACE);
}


/**
* Helper to retrieve an extension by element name (but without an explicit
* namespace, which all the existing getExtension methods require)
*
* @param elementName the element name to search for
* @return the first {@link ExtensionElement} with an element name matchiing
* the given one, or null if none is found.
*/
private ExtensionElement getExtensionByElementName(String elementName)
{
return getExtensions().stream()
.filter(e -> e.getElementName().equals(elementName))
.findFirst()
.orElse(null);
}

/**
* Return the content of the first {@link JsonPacketExtension} child
* extension, or null if none is present.
* @return the content of the first {@link JsonPacketExtension}, or null
* if none is found
*/
String getJsonContent()
{
JsonPacketExtension jsonPE =
(JsonPacketExtension) getExtensionByElementName(JsonPacketExtension.ELEMENT_NAME);
if (jsonPE != null)
{
return jsonPE.getJsonBody();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright @ 2018 - 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.colibri;

import org.jitsi.xmpp.extensions.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.provider.*;
import org.xmlpull.v1.*;

public class JvbApiIqProvider extends IQProvider<JvbApiIq>
{

@Override
public JvbApiIq parse(XmlPullParser parser, int initialDepth) throws Exception
{
if (!JvbApiIq.NAMESPACE.equals(parser.getNamespace()))
{
return null;
}
if (!JvbApiIq.ELEMENT_NAME.equals(parser.getName()))
{
return null;
}
JvbApiIq iq = new JvbApiIq();

boolean done = false;
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG)
{
String name = parser.getName();
if (name.equals(JsonPacketExtension.ELEMENT_NAME))
{
ExtensionElementProvider<JsonPacketExtension> jsonProvider =
new DefaultPacketExtensionProvider<>(JsonPacketExtension.class);
JsonPacketExtension jsonPE = jsonProvider.parse(parser);
iq.addExtension(jsonPE);
}
}




return iq;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright @ 2018 - 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;

import junit.framework.*;
import org.jivesoftware.smack.provider.*;
import org.jivesoftware.smack.util.*;
import org.xmlpull.v1.*;

public class JsonPacketExtensionTest
extends TestCase
{

public void testSimple()
{
JsonPacketExtension ext = new JsonPacketExtension("{}");

assertEquals(JsonPacketExtension.ELEMENT_NAME, ext.getElementName());
assertEquals("<json>{}</json>", ext.toXML());
assertEquals("{}", ext.getJsonBody());
}

/**
* Verify that if the JSON includes special characters (i.e. '<' or '>'),
* it still works correctly
*/
public void testJsonWithSpecialCharacters()
{
String json = "" +
"{" +
"foo: a < b," +
"bar: [1, 2, 3]," +
"baz: '<=>'" +
"}";
JsonPacketExtension ext = new JsonPacketExtension(json);
assertEquals("<json>{foo: a &lt; b,bar: [1, 2, 3],baz: &apos;&lt;=&gt;&apos;}</json>", ext.toXML());
assertEquals("{foo: a < b,bar: [1, 2, 3],baz: '<=>'}", ext.getJsonBody());
}

public void testParse() throws Exception
{
String json = "" +
"{" +
"foo: a < b," +
"bar: [1, 2, 3]," +
"baz: '<=>'" +
"}";
JsonPacketExtension ext = new JsonPacketExtension(json);
String xml = ext.toXML();

ExtensionElementProvider<JsonPacketExtension> jsonProvider =
new DefaultPacketExtensionProvider<>(JsonPacketExtension.class);
XmlPullParser parser = PacketParserUtils.getParserFor(xml);
JsonPacketExtension parsed = jsonProvider.parse(parser);

assertEquals("{foo: a < b,bar: [1, 2, 3],baz: '<=>'}", parsed.getJsonBody());
System.out.println(parsed.getJsonBody());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright @ 2018 - 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.colibri;

import junit.framework.*;
import org.jivesoftware.smack.provider.*;
import org.jivesoftware.smack.util.*;
import org.xmlpull.v1.*;

public class JvbApiIqProviderTest extends TestCase
{
private final JvbApiIqProvider jvbApiIqProvider = new JvbApiIqProvider();

@Override
protected void setUp() throws Exception
{
super.setUp();
ProviderManager.addIQProvider(
JvbApiIq.ELEMENT_NAME,
JvbApiIq.NAMESPACE,
jvbApiIqProvider
);
}

public void testSimple() throws Exception
{
String xml = "" +
"<jvb-api xmlns='http://jitsi.org/protocol/colibri/v2'/>";

XmlPullParser parser = PacketParserUtils.getParserFor(xml);
JvbApiIq parsed = jvbApiIqProvider.parse(parser);
assertTrue(parsed.toXML().toString().contains(xml));
}

public void testWithJson() throws Exception
{
String xml = "" +
"<jvb-api xmlns='http://jitsi.org/protocol/colibri/v2'><json>{}</json></jvb-api>";
XmlPullParser parser = PacketParserUtils.getParserFor(xml);
JvbApiIq parsed = jvbApiIqProvider.parse(parser);
assertTrue(parsed.toXML().toString().contains(xml));
assertNotNull(parsed.getJsonContent());
assertEquals("{}", parsed.getJsonContent());
}
}
38 changes: 38 additions & 0 deletions src/test/java/org/jitsi/xmpp/extensions/colibri/JvbApiIqTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright @ 2018 - 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.colibri;

import junit.framework.*;
import org.jitsi.xmpp.extensions.*;

public class JvbApiIqTest extends TestCase
{

public void testSimple()
{
JvbApiIq iq = new JvbApiIq();
assertTrue(iq.toXML().toString().contains("<jvb-api xmlns='http://jitsi.org/protocol/colibri/v2'/>"));
}

public void testJsonChild()
{
JvbApiIq iq = new JvbApiIq();
JsonPacketExtension json = new JsonPacketExtension("{}");
iq.addExtension(json);
assertTrue(iq.toXML().toString().contains("<jvb-api xmlns='http://jitsi.org/protocol/colibri/v2'><json>{}</json></jvb-api>"));
}
}