diff options
author | Tarun Gupta <tarun1995gupta@gmail.com> | 2015-06-17 20:29:48 (GMT) |
---|---|---|
committer | Tarun Gupta <tarun1995gupta@gmail.com> | 2015-06-23 14:50:06 (GMT) |
commit | 45e302d354d663eed61de58325ac162182497046 (patch) | |
tree | b5115b957fc2d0d7ffecc43d6cf3c3cf64c94493 /src/com/isode | |
parent | bb52f4e377fac262fe5b4f0ca2e1f6ed3669ee7d (diff) | |
download | stroke-45e302d354d663eed61de58325ac162182497046.zip stroke-45e302d354d663eed61de58325ac162182497046.tar.bz2 |
Add Roster Elements.
Adds RosterItemExchangePayload Element, its Parser and Serializer.
Updates RosterItemPayload, its Parser and Serializer.
License:
This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.
Test-Information:
Tests added for:
RosterItemPayload Parser and Serializer.
RosterItemExchangePayload Parser and Serializer.
All tests passes.
Change-Id: I8d16a18290d9820cea6839af1f075da00a25db09
Diffstat (limited to 'src/com/isode')
8 files changed, 357 insertions, 6 deletions
diff --git a/src/com/isode/stroke/elements/RosterItemExchangePayload.java b/src/com/isode/stroke/elements/RosterItemExchangePayload.java new file mode 100644 index 0000000..a94040f --- /dev/null +++ b/src/com/isode/stroke/elements/RosterItemExchangePayload.java @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2011 Jan Kaluza + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt 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.elements.Payload; +import com.isode.stroke.jid.JID; +import com.isode.stroke.base.NotNull; +import java.util.Vector; + +public class RosterItemExchangePayload extends Payload { + + public static class Item { + + public enum Action { + Add, Modify, Delete + }; + + private Action action; + private JID jid = new JID(); + private String name = ""; + private Vector<String> groups = new Vector<String>(); + + /** + * Default Constructor. + */ + public Item() { + this(Item.Action.Add); + } + + /** + * Parameterized Constructor. + * @param action, Not Null. + */ + public Item(Action action) { + NotNull.exceptIfNull(action, "action"); + this.action = action; + } + + /** + * @return action, Not Null. + */ + public Action getAction() { + return action; + } + + /** + * @param action, Not Null. + */ + public void setAction(Action action) { + NotNull.exceptIfNull(action, "action"); + this.action = action; + } + + /** + * @return jid, Not Null. + */ + public JID getJID() { + return jid; + } + + /** + * @param jid, Not Null. + */ + public void setJID(JID jid) { + NotNull.exceptIfNull(jid, "jid"); + this.jid = jid; + } + + /** + * @return name, Not Null. + */ + public String getName() { + return name; + } + + /** + * @param name, Not Null. + */ + public void setName(String name) { + NotNull.exceptIfNull(name, "name"); + this.name = name; + } + + /** + * @return groups, Not Null. + */ + public Vector<String> getGroups() { + return groups; + } + + /** + * @param groups, Not Null. + */ + public void setGroups(Vector<String> groups) { + NotNull.exceptIfNull(groups, "groups"); + this.groups = groups; + } + + /** + * @param group, Not Null. + */ + public void addGroup(String group) { + NotNull.exceptIfNull(group, "group"); + groups.add(group); + } + } + + private Vector<RosterItemExchangePayload.Item> items_ = new Vector<RosterItemExchangePayload.Item>(); + + public RosterItemExchangePayload() { + + } + + /** + * @param item, Not Null. + */ + public void addItem(RosterItemExchangePayload.Item item) { + NotNull.exceptIfNull(item, "item"); + items_.add(item); + } + + /** + * @return items, Not Null. + */ + public Vector<RosterItemExchangePayload.Item> getItems() { + return items_; + } +} diff --git a/src/com/isode/stroke/elements/RosterItemPayload.java b/src/com/isode/stroke/elements/RosterItemPayload.java index c080915..1ede267 100644 --- a/src/com/isode/stroke/elements/RosterItemPayload.java +++ b/src/com/isode/stroke/elements/RosterItemPayload.java @@ -86,9 +86,19 @@ public class RosterItemPayload { public boolean getSubscriptionRequested() { return ask_; } + + public String getUnknownContent() { + return unknownContent_; + } + + public void addUnknownContent(String c) { + unknownContent_ += c; + } + private JID jid_; private String name_; private Subscription subscription_; private Collection<String> groups_; private boolean ask_; + private String unknownContent_ = ""; } diff --git a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java index b591047..f5ae485 100644 --- a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java +++ b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java @@ -35,6 +35,7 @@ public class FullPayloadParserFactoryCollection extends PayloadParserFactoryColl addFactory(new GenericPayloadParserFactory<IsodeIQDelegationParser>("delegate", "http://isode.com/iq_delegation", IsodeIQDelegationParser.class)); addFactory(new GenericPayloadParserFactory<StorageParser>("storage", "storage:bookmarks", StorageParser.class)); addFactory(new RosterParserFactory()); + addFactory(new GenericPayloadParserFactory<RosterItemExchangeParser>("x", "http://jabber.org/protocol/rosterx", RosterItemExchangeParser.class)); addFactory(new GenericPayloadParserFactory<IBBParser>("data", "http://jabber.org/protocol/ibb", IBBParser.class)); addFactory(new GenericPayloadParserFactory<DiscoInfoParser>("query", "http://jabber.org/protocol/disco#info", DiscoInfoParser.class)); addFactory(new GenericPayloadParserFactory<DiscoItemsParser>("query", "http://jabber.org/protocol/disco#items", DiscoItemsParser.class)); diff --git a/src/com/isode/stroke/parser/payloadparsers/RosterItemExchangeParser.java b/src/com/isode/stroke/parser/payloadparsers/RosterItemExchangeParser.java new file mode 100644 index 0000000..7228fc3 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/RosterItemExchangeParser.java @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2011 Jan Kaluza + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ +/* + * Copyright (c) 2015 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.RosterItemExchangePayload; +import com.isode.stroke.base.NotNull; +import com.isode.stroke.jid.JID; + +public class RosterItemExchangeParser extends GenericPayloadParser<RosterItemExchangePayload> { + + private final int TopLevel = 0; + private final int PayloadLevel = 1; + private final int ItemLevel = 2; + private int level_ = 0; + private boolean inItem_; + private RosterItemExchangePayload.Item currentItem_; + private String currentText_ = ""; + + public RosterItemExchangeParser() { + super(new RosterItemExchangePayload()); + this.level_ = TopLevel; + this.inItem_ = false; + } + + /** + * @param element, NotNull. + * @param ns. + * @param attributes, NotNull. + */ + @Override + public void handleStartElement(String element, String ns, AttributeMap attributes) { + NotNull.exceptIfNull(element, "element"); + NotNull.exceptIfNull(attributes, "attributes"); + if (level_ == PayloadLevel) { + if (element.equals("item")) { + inItem_ = true; + + currentItem_ = new RosterItemExchangePayload.Item(); + + currentItem_.setJID(new JID(attributes.getAttribute("jid"))); + currentItem_.setName(attributes.getAttribute("name")); + + String action = attributes.getAttribute("action"); + if (action.equals("add")) { + currentItem_.setAction(RosterItemExchangePayload.Item.Action.Add); + } + else if (action.equals("modify")) { + currentItem_.setAction(RosterItemExchangePayload.Item.Action.Modify); + } + else if (action.equals("delete")) { + currentItem_.setAction(RosterItemExchangePayload.Item.Action.Delete); + } + else { + // Add is default action according to XEP + currentItem_.setAction(RosterItemExchangePayload.Item.Action.Add); + } + } + } + else if (level_ == ItemLevel) { + if (element.equals("group")) { + currentText_ = ""; + } + } + ++level_; + } + + /** + * @param element, NotNull. + * @param ns. + */ + @Override + public void handleEndElement(String element, String ns) { + NotNull.exceptIfNull(element, "element"); + --level_; + if (level_ == PayloadLevel) { + if (inItem_) { + getPayloadInternal().addItem(currentItem_); + inItem_ = false; + } + } + else if (level_ == ItemLevel) { + if (element.equals("group")) { + currentItem_.addGroup(currentText_); + } + } + } + + /** + * @param data, NotNull. + */ + @Override + public void handleCharacterData(String data) { + NotNull.exceptIfNull(data, "data"); + currentText_ += data; + } +}
\ No newline at end of file diff --git a/src/com/isode/stroke/parser/payloadparsers/RosterParser.java b/src/com/isode/stroke/parser/payloadparsers/RosterParser.java index bb57c55..3d410d5 100644 --- a/src/com/isode/stroke/parser/payloadparsers/RosterParser.java +++ b/src/com/isode/stroke/parser/payloadparsers/RosterParser.java @@ -9,6 +9,7 @@ import com.isode.stroke.elements.RosterPayload; import com.isode.stroke.jid.JID; import com.isode.stroke.parser.AttributeMap; import com.isode.stroke.parser.GenericPayloadParser; +import com.isode.stroke.parser.SerializingParser; public class RosterParser extends GenericPayloadParser<RosterPayload> { @@ -17,7 +18,13 @@ public class RosterParser extends GenericPayloadParser<RosterPayload> { } public void handleStartElement(String element, String ns, AttributeMap attributes) { - if (level_ == PayloadLevel) { + if (level_ == TopLevel) { + String ver = attributes.getAttributeValue("ver"); + if (ver != null) { + getPayloadInternal().setVersion(ver); + } + } + else if (level_ == PayloadLevel) { if (element.equals("item")) { inItem_ = true; currentItem_ = new RosterItemPayload(); @@ -30,7 +37,7 @@ public class RosterParser extends GenericPayloadParser<RosterPayload> { currentItem_.setSubscription(RosterItemPayload.Subscription.Both); } else if ("to".equals(subscription)) { currentItem_.setSubscription(RosterItemPayload.Subscription.To); - } else if ("frome".equals(subscription)) { + } else if ("from".equals(subscription)) { currentItem_.setSubscription(RosterItemPayload.Subscription.From); } else if ("remove".equals(subscription)) { currentItem_.setSubscription(RosterItemPayload.Subscription.Remove); @@ -42,10 +49,19 @@ public class RosterParser extends GenericPayloadParser<RosterPayload> { currentItem_.setSubscriptionRequested(); } } - } else if (level_ == ItemLevel) { + } + else if (level_ == ItemLevel) { if (element.equals("group")) { currentText_ = ""; } + else { + assert(unknownContentParser_ == null); + unknownContentParser_ = new SerializingParser(); + unknownContentParser_.handleStartElement(element, ns, attributes); + } + } + else if (unknownContentParser_ != null) { + unknownContentParser_.handleStartElement(element, ns, attributes); } ++level_; } @@ -57,15 +73,29 @@ public class RosterParser extends GenericPayloadParser<RosterPayload> { getPayloadInternal().addItem(currentItem_); inItem_ = false; } - } else if (level_ == ItemLevel) { - if (element.equals("group")) { + } + else if (level_ == ItemLevel) { + if (unknownContentParser_ != null) { + unknownContentParser_.handleEndElement(element, ns); + currentItem_.addUnknownContent(unknownContentParser_.getResult()); + unknownContentParser_ = null; + } + else if (element.equals("group")) { currentItem_.addGroup(currentText_); } } + else if (unknownContentParser_ != null) { + unknownContentParser_.handleEndElement(element, ns); + } } public void handleCharacterData(String data) { - currentText_ += data; + if (unknownContentParser_ != null) { + unknownContentParser_.handleCharacterData(data); + } + else { + currentText_ += data; + } } private final int TopLevel = 0; private final int PayloadLevel = 1; @@ -74,4 +104,5 @@ public class RosterParser extends GenericPayloadParser<RosterPayload> { private boolean inItem_ = false; private RosterItemPayload currentItem_; private String currentText_; + private SerializingParser unknownContentParser_; } diff --git a/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java b/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java index 9b88d0c..9ad402c 100644 --- a/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java +++ b/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java @@ -31,6 +31,7 @@ public class FullPayloadSerializerCollection extends PayloadSerializerCollection addSerializer(new JingleFileTransferHashSerializer()); addSerializer(new JingleContentPayloadSerializer()); addSerializer(new RosterSerializer()); + addSerializer(new RosterItemExchangeSerializer()); addSerializer(new MUCPayloadSerializer()); addSerializer(new MUCDestroyPayloadSerializer()); addSerializer(new MUCAdminPayloadSerializer()); diff --git a/src/com/isode/stroke/serializer/payloadserializers/RosterItemExchangeSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/RosterItemExchangeSerializer.java new file mode 100644 index 0000000..3aaab00 --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/RosterItemExchangeSerializer.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2011 Jan Kaluza + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt 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.RosterItemExchangePayload; +import com.isode.stroke.base.NotNull; + +public class RosterItemExchangeSerializer extends GenericPayloadSerializer<RosterItemExchangePayload> { + + public RosterItemExchangeSerializer() { + super(RosterItemExchangePayload.class); + } + + public String serializePayload(RosterItemExchangePayload roster) { + XMLElement queryElement = new XMLElement("x", "http://jabber.org/protocol/rosterx"); + for(RosterItemExchangePayload.Item item : roster.getItems()) { + XMLElement itemElement = new XMLElement("item"); + itemElement.setAttribute("jid", item.getJID().toString()); + itemElement.setAttribute("name", item.getName()); + + switch (item.getAction()) { + case Add: itemElement.setAttribute("action", "add"); break; + case Modify: itemElement.setAttribute("action", "modify"); break; + case Delete: itemElement.setAttribute("action", "delete"); break; + } + + for(String group : item.getGroups()) { + XMLElement groupElement = new XMLElement("group"); + groupElement.addNode(new XMLTextNode(group)); + itemElement.addNode(groupElement); + } + + queryElement.addNode(itemElement); + } + + return queryElement.serialize(); + } +}
\ No newline at end of file diff --git a/src/com/isode/stroke/serializer/payloadserializers/RosterSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/RosterSerializer.java index b75d217..6ec1e58 100644 --- a/src/com/isode/stroke/serializer/payloadserializers/RosterSerializer.java +++ b/src/com/isode/stroke/serializer/payloadserializers/RosterSerializer.java @@ -13,6 +13,7 @@ import com.isode.stroke.elements.RosterPayload; import com.isode.stroke.serializer.GenericPayloadSerializer; import com.isode.stroke.serializer.xml.XMLElement; import com.isode.stroke.serializer.xml.XMLTextNode; +import com.isode.stroke.serializer.xml.XMLRawTextNode; /** * Roster to string. @@ -26,6 +27,9 @@ public class RosterSerializer extends GenericPayloadSerializer<RosterPayload> { @Override protected String serializePayload(RosterPayload roster) { XMLElement queryElement = new XMLElement("query", "jabber:iq:roster"); + if (roster.getVersion() != null) { + queryElement.setAttribute("ver", roster.getVersion()); + } for (RosterItemPayload item : roster.getItems()) { XMLElement itemElement = new XMLElement("item"); itemElement.setAttribute("jid", item.getJID().toString()); @@ -53,6 +57,11 @@ public class RosterSerializer extends GenericPayloadSerializer<RosterPayload> { itemElement.addNode(groupElement); } + + if (item.getUnknownContent().length() != 0) { + itemElement.addNode(new XMLRawTextNode(item.getUnknownContent())); + } + queryElement.addNode(itemElement); } |