From 9518f8b9d6924e95e4ad839f40866560bd7a7878 Mon Sep 17 00:00:00 2001 From: Tarun Gupta Date: Tue, 9 Jun 2015 00:46:57 +0530 Subject: Add the Bytestreams Element. Adds Bytestreams Element, Parser and Serializer. License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details. Test-Information: Tests added for both Parser and Serializer, which passes. Change-Id: Ia623f8fcd628d7b34f7f8f935602c6c4c0ffaac1 diff --git a/src/com/isode/stroke/elements/Bytestreams.java b/src/com/isode/stroke/elements/Bytestreams.java new file mode 100644 index 0000000..9c351b5 --- /dev/null +++ b/src/com/isode/stroke/elements/Bytestreams.java @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.elements; + +import com.isode.stroke.base.NotNull; +import com.isode.stroke.jid.JID; +import com.isode.stroke.elements.Payload; +import java.util.Vector; + +public class Bytestreams extends Payload { + + public class StreamHost { + + public String host; + public JID jid; + public int port; + + /** + * Default Constructor. + */ + public StreamHost() { + this("", new JID(), -1); + } + + /** + * StreamHost with single parameter, host. + */ + public StreamHost(String host) { + this(host, new JID(), -1); + } + + /** + * StreamHost with two parameter, host and jid. + */ + public StreamHost(String host, JID jid) { + this(host, jid, -1); + } + + /** + * StreamHost with three parameter, host, jid and port. + */ + public StreamHost(String host, JID jid, int port) { + NotNull.exceptIfNull(host, "host"); + NotNull.exceptIfNull(jid, "jid"); + this.host = host; + this.jid = jid; + this.port = port; + } + + /** + * @return host, NotNull. + */ + public String getHost() { + return host; + } + + /** + * @param host, NotNull. + */ + public void setHost(String host) { + NotNull.exceptIfNull(host, "host"); + this.host = host; + } + + /** + * @return jid, NotNull. + */ + public JID getJID() { + return jid; + } + + /** + * @param jid, NotNull. + */ + public void setJID(JID jid) { + NotNull.exceptIfNull(jid, "jid"); + this.jid = jid; + } + + /** + * @return port. + */ + public int getPort() { + return port; + } + + /** + * @param port. + */ + public void setPort(int port) { + this.port = port; + } + + } + + private String id = ""; + JID usedStreamHost; + Vector streamHosts = new Vector(); + + /** + * Default Constructor. + */ + public Bytestreams() { + + } + + /** + * @return id, notnull. + */ + public String getStreamID() { + return id; + } + + /** + * @param id, notnull. + */ + public void setStreamID(String id) { + NotNull.exceptIfNull(id, "id"); + this.id = id; + } + + /** + * @return usedStreamHost + */ + public JID getUsedStreamHost() { + return usedStreamHost; + } + + /** + * @param host + */ + public void setUsedStreamHost(JID host) { + usedStreamHost = host; + } + + /** + * @return streamHosts, notnull. + */ + public Vector getStreamHosts() { + return streamHosts; + } + + /** + * @param streamHost, notnull. + */ + public void addStreamHost(StreamHost streamHost) { + NotNull.exceptIfNull(streamHost, "streamHost"); + streamHosts.add(streamHost); + } +} \ No newline at end of file diff --git a/src/com/isode/stroke/parser/payloadparsers/BytestreamsParser.java b/src/com/isode/stroke/parser/payloadparsers/BytestreamsParser.java new file mode 100644 index 0000000..879a97d --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/BytestreamsParser.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.parser.GenericPayloadParser; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.elements.Bytestreams; +import com.isode.stroke.jid.JID; +import com.isode.stroke.base.NotNull; + +public class BytestreamsParser extends GenericPayloadParser { + + private final int TopLevel = 0; + private final int PayloadLevel = 1; + private int level = TopLevel; + + public BytestreamsParser() { + super(new Bytestreams()); + } + + /** + * @param element, NotNull. + * @param ns. + * @param attributes, notnull. + */ + public void handleStartElement(String element, String ns, AttributeMap attributes) { + NotNull.exceptIfNull(element, "element"); + NotNull.exceptIfNull(attributes, "attributes"); + if (level == TopLevel) { + getPayloadInternal().setStreamID(attributes.getAttribute("sid")); + } + else if (level == PayloadLevel) { + if (element.equals("streamhost")) { + Bytestreams bytestreams = new Bytestreams(); + try { + getPayloadInternal().addStreamHost(bytestreams.new StreamHost(attributes.getAttribute("host"), new JID(attributes.getAttribute("jid")), Integer.parseInt(attributes.getAttribute("port")))); + } + catch(NumberFormatException e) { + + } + } + else if (element.equals("streamhost-used")) { + getPayloadInternal().setUsedStreamHost(new JID(attributes.getAttribute("jid"))); + } + } + ++level; + } + + /** + * @param element. + * @param ns. + */ + public void handleEndElement(String element, String ns) { + --level; + } + + public void handleCharacterData(String data) { + + } +} \ No newline at end of file diff --git a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java index 80d5a5b..7c9679b 100644 --- a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java +++ b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java @@ -39,7 +39,7 @@ public class FullPayloadParserFactoryCollection extends PayloadParserFactoryColl //addFactery(new InBandRegistrationPayloadParserFactory()); addFactory(new SearchPayloadParserFactory()); //addFactory(new StreamInitiationParserFactory()); - //addFactory(new BytestreamsParserFactory()); + addFactory(new GenericPayloadParserFactory("query", "http://jabber.org/protocol/bytestreams", BytestreamsParser.class)); addFactory(new GenericPayloadParserFactory("x", "vcard-temp:x:update", VCardUpdateParser.class)); addFactory(new GenericPayloadParserFactory("vCard", "vcard-temp", VCardParser.class)); addFactory(new PrivateStorageParserFactory(this)); diff --git a/src/com/isode/stroke/serializer/payloadserializers/BytestreamsSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/BytestreamsSerializer.java new file mode 100644 index 0000000..67efc52 --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/BytestreamsSerializer.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.serializer.payloadserializers; + +import com.isode.stroke.serializer.GenericPayloadSerializer; +import com.isode.stroke.serializer.xml.XMLTextNode; +import com.isode.stroke.serializer.xml.XMLElement; +import com.isode.stroke.elements.Bytestreams; +import com.isode.stroke.base.NotNull; + +public class BytestreamsSerializer extends GenericPayloadSerializer { + + /** + * Constructor. + */ + public BytestreamsSerializer() { + super(Bytestreams.class); + } + + /** + * @param bytestreams, notnull + */ + @Override + public String serializePayload(Bytestreams bytestreams) { + NotNull.exceptIfNull(bytestreams, "bytestreams"); + XMLElement queryElement = new XMLElement("query", "http://jabber.org/protocol/bytestreams"); + queryElement.setAttribute("sid", bytestreams.getStreamID()); + for(Bytestreams.StreamHost streamHost: bytestreams.getStreamHosts()) { + XMLElement streamHostElement = new XMLElement("streamhost"); + streamHostElement.setAttribute("host", streamHost.host); + streamHostElement.setAttribute("jid", streamHost.jid.toString()); + streamHostElement.setAttribute("port", Integer.toString(streamHost.port)); + queryElement.addNode(streamHostElement); + } + + if (bytestreams.getUsedStreamHost() != null) { + XMLElement streamHostElement = new XMLElement("streamhost-used"); + streamHostElement.setAttribute("jid", bytestreams.getUsedStreamHost().toString()); + queryElement.addNode(streamHostElement); + } + return queryElement.serialize(); + } +} \ No newline at end of file diff --git a/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java b/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java index 5232d2c..644b7e0 100644 --- a/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java +++ b/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java @@ -40,7 +40,7 @@ public class FullPayloadSerializerCollection extends PayloadSerializerCollection addSerializer(new SecurityLabelSerializer()); addSerializer(new SecurityLabelsCatalogSerializer()); //addSerializer(new StreamInitiationSerializer()); - //addSerializer(new BytestreamsSerializer()); + addSerializer(new BytestreamsSerializer()); addSerializer(new VCardSerializer()); addSerializer(new VCardUpdateSerializer()); addSerializer(new RawXMLPayloadSerializer()); diff --git a/test/com/isode/stroke/parser/payloadparsers/BytestreamsParserTest.java b/test/com/isode/stroke/parser/payloadparsers/BytestreamsParserTest.java new file mode 100644 index 0000000..ead1de2 --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/BytestreamsParserTest.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser.payloadparsers; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import org.junit.Test; +import com.isode.stroke.elements.Bytestreams; +import com.isode.stroke.jid.JID; +import com.isode.stroke.parser.payloadparsers.BytestreamsParser; +import com.isode.stroke.parser.payloadparsers.PayloadsParserTester; +import com.isode.stroke.eventloop.DummyEventLoop; + +public class BytestreamsParserTest { + + public BytestreamsParserTest() { + + } + + @Test + public void testParse() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertNotNull(parser.parse("" + + "" + + "" + + "")); + + Bytestreams payload = (Bytestreams)parser.getPayload(); + assertEquals("hello", payload.getStreamID()); + + assertEquals(2, payload.getStreamHosts().size()); + + assertEquals("blah.xyz.edu", payload.getStreamHosts().get(0).getHost()); + assertEquals(new JID("user1@bar.com/bla"), payload.getStreamHosts().get(0).getJID()); + assertEquals(445, payload.getStreamHosts().get(0).getPort()); + + assertEquals("bal.zyx.ude", payload.getStreamHosts().get(1).getHost()); + assertEquals(new JID("user1@baz.com/bal"), payload.getStreamHosts().get(1).getJID()); + assertEquals(449, payload.getStreamHosts().get(1).getPort()); + } +} \ No newline at end of file diff --git a/test/com/isode/stroke/serializer/payloadserializers/BytestreamsSerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/BytestreamsSerializerTest.java new file mode 100644 index 0000000..331a6a5 --- /dev/null +++ b/test/com/isode/stroke/serializer/payloadserializers/BytestreamsSerializerTest.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.serializer.payloadserializers; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; +import com.isode.stroke.serializer.payloadserializers.BytestreamsSerializer; +import com.isode.stroke.elements.Bytestreams; +import com.isode.stroke.jid.JID; + +public class BytestreamsSerializerTest { + + public BytestreamsSerializerTest() { + + } + + @Test + public void testSerialize_withoutUsedStreamHost_singleStreamHost() { + BytestreamsSerializer testling = new BytestreamsSerializer(); + Bytestreams byteStreams = new Bytestreams(); + byteStreams.addStreamHost(byteStreams.new StreamHost("blah.xyz.edu", new JID("user1@bar.com/bla"), 445)); + byteStreams.setStreamID("hello"); + String expectedResult = "" + + ""; + assertEquals(expectedResult, testling.serialize(byteStreams)); + } + + @Test + public void testSerialize_withoutUsedStreamHost_doubleStreamHost() { + BytestreamsSerializer testling = new BytestreamsSerializer(); + Bytestreams byteStreams = new Bytestreams(); + byteStreams.addStreamHost(byteStreams.new StreamHost("blah.xyz.edu", new JID("user1@bar.com/bla"), 445)); + byteStreams.addStreamHost(byteStreams.new StreamHost("bal.zyx.ude", new JID("user1@baz.com/bal"), 449)); + byteStreams.setStreamID("hello"); + String expectedResult = "" + + "" + + "" + + ""; + assertEquals(expectedResult, testling.serialize(byteStreams)); + } + + @Test + public void testSerialize_withUsedStreamHost() { + BytestreamsSerializer testling = new BytestreamsSerializer(); + Bytestreams byteStreams = new Bytestreams(); + byteStreams.addStreamHost(byteStreams.new StreamHost("blah.xyz.edu", new JID("user1@bar.com/bla"), 445)); + byteStreams.setUsedStreamHost(new JID("user1@baz.com/bal")); + byteStreams.setStreamID("hello"); + String expectedResult = "" + + "" + + "" + + ""; + assertEquals(expectedResult, testling.serialize(byteStreams)); + } +} \ No newline at end of file -- cgit v0.10.2-6-g49f6