diff options
32 files changed, 1694 insertions, 2 deletions
diff --git a/src/com/isode/stroke/base/DateTime.java b/src/com/isode/stroke/base/DateTime.java new file mode 100644 index 0000000..27c85d8 --- /dev/null +++ b/src/com/isode/stroke/base/DateTime.java @@ -0,0 +1,36 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.base; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +public class DateTime { + public static Date stringToDate(String date) { + SimpleDateFormat parser = new SimpleDateFormat(format); + parser.setTimeZone(TimeZone.getTimeZone("UTC")); + try { + return parser.parse(date); + } catch (ParseException e) { + return null; + } + } + + public static String dateToString(Date date) { + SimpleDateFormat parser = new SimpleDateFormat(format); + parser.setTimeZone(TimeZone.getTimeZone("UTC")); + return parser.format(date); + } + + static private String format = "yyyy-MM-dd'T'HH:mm:ss'Z'"; +} diff --git a/src/com/isode/stroke/elements/Delay.java b/src/com/isode/stroke/elements/Delay.java new file mode 100644 index 0000000..9827a82 --- /dev/null +++ b/src/com/isode/stroke/elements/Delay.java @@ -0,0 +1,43 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.elements; + +import java.util.Date; +import com.isode.stroke.jid.JID; + +public class Delay extends Payload { + public Delay() { + } + + public Delay(Date time, JID from) { + time_ = time; + from_ = from; + } + + public Date getStamp() { + return time_; + } + + public void setStamp(Date time) { + time_ = time; + } + + public JID getFrom() { + return from_; + } + + public void setFrom(JID from) { + from_ = from; + } + + private Date time_; + private JID from_; +} diff --git a/src/com/isode/stroke/elements/Forwarded.java b/src/com/isode/stroke/elements/Forwarded.java new file mode 100644 index 0000000..fbfabce --- /dev/null +++ b/src/com/isode/stroke/elements/Forwarded.java @@ -0,0 +1,32 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.elements; + +public class Forwarded extends Payload { + public void setDelay(Delay delay) { + delay_ = delay; + } + + public Delay getDelay() { + return delay_; + } + + public void setStanza(Stanza stanza) { + stanza_ = stanza; + } + + public Stanza getStanza() { + return stanza_; + } + + private Delay delay_; + private Stanza stanza_; +} diff --git a/src/com/isode/stroke/elements/MAMArchived.java b/src/com/isode/stroke/elements/MAMArchived.java new file mode 100644 index 0000000..c8ab2d9 --- /dev/null +++ b/src/com/isode/stroke/elements/MAMArchived.java @@ -0,0 +1,34 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.elements; + +import com.isode.stroke.jid.JID; + +public class MAMArchived extends Payload { + public void setBy(JID by) { + by_ = by; + } + + public JID getBy() { + return by_; + } + + public void setID(String id) { + id_ = id; + } + + public String getID() { + return id_; + } + + private JID by_; + private String id_; +} diff --git a/src/com/isode/stroke/elements/MAMQuery.java b/src/com/isode/stroke/elements/MAMQuery.java new file mode 100644 index 0000000..6da30b5 --- /dev/null +++ b/src/com/isode/stroke/elements/MAMQuery.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2014 Kevin Smith and Remko Tronçon + * All rights reserved. + */ + +/* + * Copyright (c) 2014, Isode Limited, London, England. + * All rights reserved. + */ + +package com.isode.stroke.elements; + +public class MAMQuery extends Payload { + public void setQueryID(String queryID) { + queryID_ = queryID; + } + + public String getQueryID() { + return queryID_; + } + + public void setForm(Form form) { + form_ = form; + } + + public Form getForm() { + return form_; + } + + public void setResultSet(ResultSet resultSet) { + resultSet_ = resultSet; + } + + public ResultSet getResultSet() { + return resultSet_; + } + + private String queryID_; + private Form form_; + private ResultSet resultSet_; +} diff --git a/src/com/isode/stroke/elements/MAMResult.java b/src/com/isode/stroke/elements/MAMResult.java new file mode 100644 index 0000000..aa83c88 --- /dev/null +++ b/src/com/isode/stroke/elements/MAMResult.java @@ -0,0 +1,32 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.elements; + +public class MAMResult extends ContainerPayload<Forwarded> { + public void setID(String id) { + id_ = id; + } + + public String getID() { + return id_; + } + + public void setQueryID(String queryID) { + queryID_ = queryID; + } + + public String getQueryID() { + return queryID_; + } + + private String id_; + private String queryID_; +} diff --git a/src/com/isode/stroke/elements/ResultSet.java b/src/com/isode/stroke/elements/ResultSet.java new file mode 100644 index 0000000..706d37c --- /dev/null +++ b/src/com/isode/stroke/elements/ResultSet.java @@ -0,0 +1,80 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.elements; + +/** + * ResultSet + */ +public class ResultSet extends Payload { + public void setMaxItems(Long maxItems) { + maxItems_ = maxItems; + } + + public Long getMaxItems() { + return maxItems_; + } + + public void setCount(Long count) { + count_ = count; + } + + public Long getCount() { + return count_; + } + + public void setFirstIDIndex(Long firstIndex) { + firstIndex_ = firstIndex; + } + + public Long getFirstIDIndex() { + return firstIndex_; + } + + public void setFirstID(String firstID) { + firstID_ = firstID; + } + + public String getFirstID() { + return firstID_; + } + + public void setLastID(String lastID) { + lastID_ = lastID; + } + + public String getLastID() { + return lastID_; + } + + public void setAfter(String after) { + after_ = after; + } + + public String getAfter() { + return after_; + } + + public void setBefore(String before) { + before_ = before; + } + + public String getBefore() { + return before_; + } + + private Long maxItems_; + private Long count_; + private Long firstIndex_; + private String firstID_; + private String lastID_; + private String after_; + private String before_; +} diff --git a/src/com/isode/stroke/parser/StanzaParser.java b/src/com/isode/stroke/parser/StanzaParser.java index cb7647d..6b0ccdf 100644 --- a/src/com/isode/stroke/parser/StanzaParser.java +++ b/src/com/isode/stroke/parser/StanzaParser.java @@ -25,7 +25,7 @@ public abstract class StanzaParser implements ElementParser { void handleStanzaAttributes(AttributeMap map) { } - Stanza getStanza() { + public Stanza getStanza() { return (Stanza) getElement(); } diff --git a/src/com/isode/stroke/parser/payloadparsers/DelayParser.java b/src/com/isode/stroke/parser/payloadparsers/DelayParser.java new file mode 100644 index 0000000..44d21ce --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/DelayParser.java @@ -0,0 +1,46 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import java.util.Date; +import com.isode.stroke.base.DateTime; +import com.isode.stroke.elements.Delay; +import com.isode.stroke.jid.JID; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.GenericPayloadParser; + +public class DelayParser extends GenericPayloadParser<Delay> { + + public DelayParser() { + super(new Delay()); + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + if (level_ == 0) { + Date stamp = DateTime.stringToDate(attributes.getAttribute("stamp")); + getPayloadInternal().setStamp(stamp); + if (!attributes.getAttribute("from").isEmpty()) { + String from = attributes.getAttribute("from"); + getPayloadInternal().setFrom(new JID(from)); + } + } + ++level_; + } + + public void handleEndElement(String element, String ns) { + --level_; + } + + public void handleCharacterData(String data) { + } + + private int level_; +} diff --git a/src/com/isode/stroke/parser/payloadparsers/ForwardedParser.java b/src/com/isode/stroke/parser/payloadparsers/ForwardedParser.java new file mode 100644 index 0000000..b7a023e --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/ForwardedParser.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2014 Kevin Smith and Remko Tronçon + * All rights reserved. + */ + +/* + * Copyright (c) 2014, Isode Limited, London, England. + * All rights reserved. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.Delay; +import com.isode.stroke.elements.Forwarded; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.GenericPayloadParser; +import com.isode.stroke.parser.IQParser; +import com.isode.stroke.parser.MessageParser; +import com.isode.stroke.parser.PayloadParserFactoryCollection; +import com.isode.stroke.parser.PresenceParser; +import com.isode.stroke.parser.StanzaParser; + +public class ForwardedParser extends GenericPayloadParser<Forwarded> { + public ForwardedParser(PayloadParserFactoryCollection factories) { + super(new Forwarded()); + factories_ = factories; + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + if (level_ == 1) { + if (element == "iq") { /* begin parsing a nested stanza? */ + childParser_ = new IQParser(factories_); + } else if (element == "message") { + childParser_ = new MessageParser(factories_); + } else if (element == "presence") { + childParser_ = new PresenceParser(factories_); + } else if (element == "delay" && ns == "urn:xmpp:delay") { + delayParser_ = new DelayParser(); + } + } + if (childParser_ != null) { /* parsing a nested stanza? */ + childParser_.handleStartElement(element, ns, attributes); + } + if (delayParser_ != null) { /* parsing a nested delay payload? */ + delayParser_.handleStartElement(element, ns, attributes); + } + ++level_; + } + + public void handleEndElement(String element, String ns) { + --level_; + if (childParser_ != null && level_ >= 1) { + childParser_.handleEndElement(element, ns); + } + if (childParser_ != null && level_ == 1) { /* done parsing nested stanza? */ + getPayloadInternal().setStanza(childParser_.getStanza()); + childParser_ = null; + } + if (delayParser_ != null && level_ >= 1) { + delayParser_.handleEndElement(element, ns); + } + if (delayParser_ != null && level_ == 1) { /* done parsing nested delay payload? */ + getPayloadInternal().setDelay((Delay) delayParser_.getPayload()); + delayParser_ = null; + } + } + + public void handleCharacterData(String data) { + if (childParser_ != null) { + childParser_.handleCharacterData(data); + } + if (delayParser_ != null) { + delayParser_.handleCharacterData(data); + } + } + + private PayloadParserFactoryCollection factories_; + private StanzaParser childParser_; + private DelayParser delayParser_; + private int level_; +} diff --git a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java index cf41338..41f44f5 100644 --- a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java +++ b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java @@ -64,6 +64,13 @@ public class FullPayloadParserFactoryCollection extends PayloadParserFactoryColl addFactory(new GenericPayloadParserFactory2<PubSubEventParser>("event", "http://jabber.org/protocol/pubsub#event", this, PubSubEventParser.class)); addFactory(new PubSubErrorParserFactory()); + addFactory(new GenericPayloadParserFactory<DelayParser>("delay", "urn:xmpp:delay", DelayParser.class)); + addFactory(new GenericPayloadParserFactory<ResultSetParser>("set", "http://jabber.org/protocol/rsm", ResultSetParser.class)); + addFactory(new GenericPayloadParserFactory2<ForwardedParser>("forwarded", "urn:xmpp:forward:0", this, ForwardedParser.class)); + addFactory(new GenericPayloadParserFactory2<MAMResultParser>("result", "urn:xmpp:mam:0", this, MAMResultParser.class)); + addFactory(new GenericPayloadParserFactory<MAMQueryParser>("query", "urn:xmpp:mam:0", MAMQueryParser.class)); + addFactory(new GenericPayloadParserFactory<MAMArchivedParser>("archived", "urn:xmpp:mam:0", MAMArchivedParser.class)); + //addFactory(new NicknameParserFactory()); PayloadParserFactory defaultFactory = new RawXMLPayloadParserFactory(); diff --git a/src/com/isode/stroke/parser/payloadparsers/MAMArchivedParser.java b/src/com/isode/stroke/parser/payloadparsers/MAMArchivedParser.java new file mode 100644 index 0000000..5547a87 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/MAMArchivedParser.java @@ -0,0 +1,46 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.MAMArchived; +import com.isode.stroke.jid.JID; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.GenericPayloadParser; + +public class MAMArchivedParser extends GenericPayloadParser<MAMArchived> { + public MAMArchivedParser() { + super(new MAMArchived()); + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + if (level_ == 0) { + String attributeValue = attributes.getAttributeValue("by"); + if (attributeValue != null) { + getPayloadInternal().setBy(JID.fromString(attributeValue)); + } + attributeValue = attributes.getAttributeValue("id"); + if (attributeValue != null) { + getPayloadInternal().setID(attributeValue); + } + } + + ++level_; + } + + public void handleEndElement(String element, String ns) { + --level_; + } + + public void handleCharacterData(String data) { + } + + private int level_; +} diff --git a/src/com/isode/stroke/parser/payloadparsers/MAMQueryParser.java b/src/com/isode/stroke/parser/payloadparsers/MAMQueryParser.java new file mode 100644 index 0000000..fcfd5e4 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/MAMQueryParser.java @@ -0,0 +1,82 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.Form; +import com.isode.stroke.elements.MAMQuery; +import com.isode.stroke.elements.ResultSet; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.GenericPayloadParser; + +public class MAMQueryParser extends GenericPayloadParser<MAMQuery> { + + public MAMQueryParser() { + super(new MAMQuery()); + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + if (level_ == 0) { + String attributeValue = attributes.getAttributeValue("queryid"); + if (attributeValue != null) { + getPayloadInternal().setQueryID(attributeValue); + } + } else if (level_ == 1) { + if (element == "x" && ns == "jabber:x:data") { + formParser_ = new FormParser(); + } else if (element == "set" && ns == "http://jabber.org/protocol/rsm") { + resultSetParser_ = new ResultSetParser(); + } + } + + if (formParser_ != null) { /* parsing a nested Form */ + formParser_.handleStartElement(element, ns, attributes); + } + + if (resultSetParser_ != null) { /* parsing a nested ResultSet */ + resultSetParser_.handleStartElement(element, ns, attributes); + } + + ++level_; + } + + public void handleEndElement(String element, String ns) { + --level_; + + if (formParser_ != null && level_>= 1) { + formParser_.handleEndElement(element, ns); + } + if (formParser_ != null && level_==1) { /* done parsing nested Form? */ + getPayloadInternal().setForm((Form)formParser_.getPayload()); + formParser_ = null; + } + + if (resultSetParser_ != null && level_>= 1) { + resultSetParser_.handleEndElement(element, ns); + } + if (resultSetParser_ != null && level_==1) { /* done parsing nested ResultSet? */ + getPayloadInternal().setResultSet((ResultSet)resultSetParser_.getPayload()); + resultSetParser_ = null; + } + } + + public void handleCharacterData(String data) { + if (formParser_ != null) { + formParser_.handleCharacterData(data); + } + if (resultSetParser_ != null) { + resultSetParser_.handleCharacterData(data); + } + } + + private FormParser formParser_; + private ResultSetParser resultSetParser_; + private int level_; +} diff --git a/src/com/isode/stroke/parser/payloadparsers/MAMResultParser.java b/src/com/isode/stroke/parser/payloadparsers/MAMResultParser.java new file mode 100644 index 0000000..cb3d7fd --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/MAMResultParser.java @@ -0,0 +1,68 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.Forwarded; +import com.isode.stroke.elements.MAMResult; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.GenericPayloadParser; +import com.isode.stroke.parser.PayloadParserFactoryCollection; + +public class MAMResultParser extends GenericPayloadParser<MAMResult> { + public MAMResultParser(PayloadParserFactoryCollection factories) { + super(new MAMResult()); + factories_ = factories; + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + if (level_ == 0) { + String attributeValue = attributes.getAttributeValue("id"); + if (attributeValue != null) { + getPayloadInternal().setID(attributeValue); + } + attributeValue = attributes.getAttributeValue("queryid"); + if (attributeValue != null) { + getPayloadInternal().setQueryID(attributeValue); + } + } else if (level_ == 1) { + if (element == "forwarded" && ns == "urn:xmpp:forward:0") { + payloadParser_ = new ForwardedParser(factories_); + } + } + + if (payloadParser_ != null) { /* parsing a nested payload? */ + payloadParser_.handleStartElement(element, ns, attributes); + } + + ++level_; + } + + public void handleEndElement(String element, String ns) { + --level_; + if (payloadParser_ != null && level_ >= 1) { + payloadParser_.handleEndElement(element, ns); + } + if (payloadParser_ != null && level_ == 1) { /* done parsing nested stanza? */ + getPayloadInternal().setPayload((Forwarded)payloadParser_.getPayload()); + payloadParser_ = null; + } + } + + public void handleCharacterData(String data) { + if (payloadParser_ != null) { + payloadParser_.handleCharacterData(data); + } + } + + private ForwardedParser payloadParser_; + private PayloadParserFactoryCollection factories_; + private int level_; +} diff --git a/src/com/isode/stroke/parser/payloadparsers/ResultSetParser.java b/src/com/isode/stroke/parser/payloadparsers/ResultSetParser.java new file mode 100644 index 0000000..26343a0 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/ResultSetParser.java @@ -0,0 +1,61 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.ResultSet; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.GenericPayloadParser; + +public class ResultSetParser extends GenericPayloadParser<ResultSet> { + + public ResultSetParser() { + super(new ResultSet()); + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + currentText_ = ""; + if (level_ == 1) { + if (element == "first" && ns == "http://jabber.org/protocol/rsm") { + String attributeValue = attributes.getAttributeValue("index"); + if (attributeValue != null) { + getPayloadInternal().setFirstIDIndex(Long.parseLong(attributeValue)); + } + } + } + ++level_; + } + + public void handleEndElement(String element, String ns) { + --level_; + if (level_ == 1) { + if (element == "max") { + getPayloadInternal().setMaxItems(Long.parseLong(currentText_)); + } else if (element == "count") { + getPayloadInternal().setCount(Long.parseLong(currentText_)); + } else if (element == "first") { + getPayloadInternal().setFirstID(currentText_); + } else if (element == "last") { + getPayloadInternal().setLastID(currentText_); + } else if (element == "after") { + getPayloadInternal().setAfter(currentText_); + } else if (element == "before") { + getPayloadInternal().setBefore(currentText_); + } + } + } + + public void handleCharacterData(String data) { + currentText_ += data; + } + + private String currentText_; + private int level_; +} diff --git a/src/com/isode/stroke/serializer/payloadserializers/DelaySerializer.java b/src/com/isode/stroke/serializer/payloadserializers/DelaySerializer.java new file mode 100644 index 0000000..f54f38f --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/DelaySerializer.java @@ -0,0 +1,31 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.serializer.payloadserializers; + +import com.isode.stroke.base.DateTime; +import com.isode.stroke.elements.Delay; +import com.isode.stroke.serializer.GenericPayloadSerializer; +import com.isode.stroke.serializer.xml.XMLElement; + +public class DelaySerializer extends GenericPayloadSerializer<Delay> { + public DelaySerializer() { + super(Delay.class); + } + + protected String serializePayload(Delay delay) { + XMLElement delayElement = new XMLElement("delay", "urn:xmpp:delay"); + if (delay.getFrom() != null && delay.getFrom().isValid()) { + delayElement.setAttribute("from", delay.getFrom().toString()); + } + delayElement.setAttribute("stamp", DateTime.dateToString(delay.getStamp())); + return delayElement.serialize(); + } +} diff --git a/src/com/isode/stroke/serializer/payloadserializers/ForwardedSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/ForwardedSerializer.java new file mode 100644 index 0000000..625205e --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/ForwardedSerializer.java @@ -0,0 +1,56 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.serializer.payloadserializers; + +import com.isode.stroke.elements.Forwarded; +import com.isode.stroke.elements.IQ; +import com.isode.stroke.elements.Message; +import com.isode.stroke.elements.Presence; +import com.isode.stroke.serializer.GenericPayloadSerializer; +import com.isode.stroke.serializer.IQSerializer; +import com.isode.stroke.serializer.MessageSerializer; +import com.isode.stroke.serializer.PayloadSerializerCollection; +import com.isode.stroke.serializer.PresenceSerializer; +import com.isode.stroke.serializer.xml.XMLElement; +import com.isode.stroke.serializer.xml.XMLRawTextNode; + +public class ForwardedSerializer extends GenericPayloadSerializer<Forwarded> { + public ForwardedSerializer(PayloadSerializerCollection serializers) { + super(Forwarded.class); + serializers_ = serializers; + } + + public String serializePayload(Forwarded payload) { + if (payload == null) { + return ""; + } + + XMLElement element = new XMLElement("forwarded", "urn:xmpp:forward:0"); + + if (payload.getDelay() != null) { + element.addNode(new XMLRawTextNode((new DelaySerializer()).serialize(payload.getDelay()))); + } + + if (payload.getStanza() != null) { /* find out what type of stanza we are dealing with and branch into the correct serializer */ + if (payload.getStanza() instanceof IQ) { + element.addNode(new XMLRawTextNode((new IQSerializer(serializers_)).serialize((IQ)payload.getStanza()))); + } else if (payload.getStanza() instanceof Message) { + element.addNode(new XMLRawTextNode((new MessageSerializer(serializers_)).serialize((Message)payload.getStanza()))); + } else if (payload.getStanza() instanceof Presence) { + element.addNode(new XMLRawTextNode((new PresenceSerializer(serializers_)).serialize((Presence)payload.getStanza()))); + } + } + + return element.serialize(); + } + + private PayloadSerializerCollection serializers_; +} diff --git a/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java b/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java index f846d85..9cb3646 100644 --- a/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java +++ b/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java @@ -49,7 +49,7 @@ public class FullPayloadSerializerCollection extends PayloadSerializerCollection //addSerializer(new VCardUpdateSerializer()); addSerializer(new RawXMLPayloadSerializer()); //addSerializer(new StorageSerializer()); - //addSerializer(new DelaySerializer()); + addSerializer(new DelaySerializer()); addSerializer(new FormSerializer()); //addSerializer(new PrivateStorageSerializer(this)); addSerializer(new CommandSerializer()); @@ -61,6 +61,12 @@ public class FullPayloadSerializerCollection extends PayloadSerializerCollection addSerializer(new PubSubEventSerializer(this)); addSerializer(new PubSubOwnerPubSubSerializer(this)); addSerializer(new PubSubErrorSerializer()); + + addSerializer(new ResultSetSerializer()); + addSerializer(new ForwardedSerializer(this)); + addSerializer(new MAMResultSerializer(this)); + addSerializer(new MAMQuerySerializer()); + addSerializer(new MAMArchivedSerializer()); } } diff --git a/src/com/isode/stroke/serializer/payloadserializers/MAMArchivedSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/MAMArchivedSerializer.java new file mode 100644 index 0000000..0a5f1f4 --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/MAMArchivedSerializer.java @@ -0,0 +1,33 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.serializer.payloadserializers; + +import com.isode.stroke.elements.MAMArchived; +import com.isode.stroke.serializer.GenericPayloadSerializer; +import com.isode.stroke.serializer.xml.XMLElement; + +public class MAMArchivedSerializer extends GenericPayloadSerializer<MAMArchived> { + public MAMArchivedSerializer() { + super(MAMArchived.class); + } + + protected String serializePayload(MAMArchived payload) { + if (payload == null) { + return ""; + } + + XMLElement element = new XMLElement("archived", "urn:xmpp:mam:0"); + element.setAttribute("by", payload.getBy().toString()); + element.setAttribute("id", payload.getID()); + + return element.serialize(); + } +} diff --git a/src/com/isode/stroke/serializer/payloadserializers/MAMQuerySerializer.java b/src/com/isode/stroke/serializer/payloadserializers/MAMQuerySerializer.java new file mode 100644 index 0000000..46cc42c --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/MAMQuerySerializer.java @@ -0,0 +1,35 @@ +package com.isode.stroke.serializer.payloadserializers; + +import com.isode.stroke.elements.MAMQuery; +import com.isode.stroke.serializer.GenericPayloadSerializer; +import com.isode.stroke.serializer.xml.XMLElement; +import com.isode.stroke.serializer.xml.XMLRawTextNode; + +public class MAMQuerySerializer extends GenericPayloadSerializer<MAMQuery> { + + public MAMQuerySerializer() { + super(MAMQuery.class); + } + + protected String serializePayload(MAMQuery payload) { + if (payload == null) { + return ""; + } + + XMLElement element = new XMLElement("query", "urn:xmpp:mam:0"); + + if (payload.getQueryID() != null) { + element.setAttribute("queryid", payload.getQueryID()); + } + + if (payload.getForm() != null) { + element.addNode(new XMLRawTextNode((new FormSerializer()).serialize(payload.getForm()))); + } + + if (payload.getResultSet() != null) { + element.addNode(new XMLRawTextNode((new ResultSetSerializer()).serialize(payload.getResultSet()))); + } + + return element.serialize(); + } +} diff --git a/src/com/isode/stroke/serializer/payloadserializers/MAMResultSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/MAMResultSerializer.java new file mode 100644 index 0000000..532e93f --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/MAMResultSerializer.java @@ -0,0 +1,44 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.serializer.payloadserializers; + +import com.isode.stroke.elements.MAMResult; +import com.isode.stroke.serializer.GenericPayloadSerializer; +import com.isode.stroke.serializer.PayloadSerializerCollection; +import com.isode.stroke.serializer.xml.XMLElement; +import com.isode.stroke.serializer.xml.XMLRawTextNode; + +public class MAMResultSerializer extends GenericPayloadSerializer<MAMResult> { + public MAMResultSerializer(PayloadSerializerCollection serializers) { + super(MAMResult.class); + serializers_ = serializers; + } + + public String serializePayload(MAMResult payload) { + if (payload == null) { + return ""; + } + + XMLElement element = new XMLElement("result", "urn:xmpp:mam:0"); + + element.setAttribute("id", payload.getID()); + + if (payload.getQueryID() != null) { + element.setAttribute("queryid", payload.getQueryID()); + } + + element.addNode(new XMLRawTextNode((new ForwardedSerializer(serializers_)).serialize(payload.getPayload()))); + + return element.serialize(); + } + + PayloadSerializerCollection serializers_; +} diff --git a/src/com/isode/stroke/serializer/payloadserializers/ResultSetSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/ResultSetSerializer.java new file mode 100644 index 0000000..06589d5 --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/ResultSetSerializer.java @@ -0,0 +1,60 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.serializer.payloadserializers; + +import com.isode.stroke.elements.ResultSet; +import com.isode.stroke.serializer.GenericPayloadSerializer; +import com.isode.stroke.serializer.xml.XMLElement; + +public class ResultSetSerializer extends GenericPayloadSerializer<ResultSet> { + + public ResultSetSerializer() { + super(ResultSet.class); + } + + protected String serializePayload(ResultSet payload) { + if (payload == null) { + return ""; + } + + XMLElement element = new XMLElement("set", "http://jabber.org/protocol/rsm"); + + if (payload.getMaxItems() != null) { + element.addNode(new XMLElement("max", "", payload.getMaxItems().toString())); + } + + if (payload.getCount() != null) { + element.addNode(new XMLElement("count", "", payload.getCount().toString())); + } + + if (payload.getFirstID() != null) { + XMLElement firstElement = new XMLElement("first", "", payload.getFirstID()); + if (payload.getFirstIDIndex() != null) { + firstElement.setAttribute("index", payload.getFirstIDIndex().toString()); + } + element.addNode(firstElement); + } + + if (payload.getLastID() != null) { + element.addNode(new XMLElement("last", "", payload.getLastID())); + } + + if (payload.getAfter() != null) { + element.addNode(new XMLElement("after", "", payload.getAfter())); + } + + if (payload.getBefore() != null) { + element.addNode(new XMLElement("before", "", payload.getBefore())); + } + + return element.serialize(); + } +} diff --git a/test/com/isode/stroke/parser/payloadparsers/ForwardedParserTest.java b/test/com/isode/stroke/parser/payloadparsers/ForwardedParserTest.java new file mode 100644 index 0000000..f2fee5b --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/ForwardedParserTest.java @@ -0,0 +1,127 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import com.isode.stroke.base.DateTime; +import com.isode.stroke.elements.Forwarded; +import com.isode.stroke.elements.IQ; +import com.isode.stroke.elements.Message; +import com.isode.stroke.elements.Presence; +import com.isode.stroke.eventloop.DummyEventLoop; + +public class ForwardedParserTest { + + @Test + public void testParseIQ() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<delay xmlns=\"urn:xmpp:delay\" stamp=\"2010-07-10T23:08:25Z\"/>" + + "<iq type=\"get\" from=\"kindanormal@example.com/IM\" to=\"stupidnewbie@example.com\" id=\"id0\"/>" + + "</forwarded>")); + + assertTrue(parser.getPayload() instanceof Forwarded); + Forwarded payload = (Forwarded)parser.getPayload(); + assertTrue(payload != null); + assertTrue(payload.getDelay() != null); + assertEquals("2010-07-10T23:08:25Z", DateTime.dateToString(payload.getDelay().getStamp())); + + assertTrue(payload.getStanza() instanceof IQ); + IQ iq = (IQ)payload.getStanza(); + assertTrue(iq != null); + assertEquals("stupidnewbie@example.com", iq.getTo().toString()); + assertEquals("kindanormal@example.com/IM", iq.getFrom().toString()); + assertEquals("id0", iq.getID()); + assertEquals(IQ.Type.Get, iq.getType()); + } + + @Test + public void testParseMessage() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<delay xmlns=\"urn:xmpp:delay\" stamp=\"2010-07-10T23:08:25Z\"/>" + + "<message xmlns=\"jabber:client\" to=\"juliet@capulet.lit/balcony\" from=\"romeo@montague.lit/orchard\" type=\"chat\">" + + "<body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body>" + + "</message>" + + "</forwarded>")); + + assertTrue(parser.getPayload() instanceof Forwarded); + Forwarded payload = (Forwarded)parser.getPayload(); + assertTrue(payload != null); + assertTrue(payload.getDelay() != null); + assertEquals("2010-07-10T23:08:25Z", DateTime.dateToString(payload.getDelay().getStamp())); + + assertTrue(payload.getStanza() instanceof Message); + Message message = (Message)payload.getStanza(); + assertTrue(message != null); + String expectedBody = "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo."; + assertEquals(expectedBody, message.getBody()); + assertEquals(Message.Type.Chat, message.getType()); + assertEquals("juliet@capulet.lit/balcony", message.getTo().toString()); + assertEquals("romeo@montague.lit/orchard", message.getFrom().toString()); + } + + @Test + public void testParseMessageNoDelay() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<message xmlns=\"jabber:client\" to=\"juliet@capulet.lit/balcony\" from=\"romeo@montague.lit/orchard\" type=\"chat\">" + + "<body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body>" + + "</message>" + + "</forwarded>")); + + assertTrue(parser.getPayload() instanceof Forwarded); + Forwarded payload = (Forwarded)parser.getPayload(); + assertTrue(payload != null); + assertTrue(payload.getDelay() == null); + + assertTrue(payload.getStanza() instanceof Message); + Message message = (Message)payload.getStanza(); + assertTrue(message != null); + String expectedBody = "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo."; + assertEquals(expectedBody, message.getBody()); + assertEquals(Message.Type.Chat, message.getType()); + assertEquals("juliet@capulet.lit/balcony", message.getTo().toString()); + assertEquals("romeo@montague.lit/orchard", message.getFrom().toString()); + } + + @Test + public void testParsePresence() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<delay xmlns=\"urn:xmpp:delay\" stamp=\"2010-07-10T23:08:25Z\"/>" + + "<presence from=\"alice@wonderland.lit/rabbithole\" to=\"madhatter@wonderland.lit\" type=\"unavailable\"/>" + + "</forwarded>")); + + assertTrue(parser.getPayload() instanceof Forwarded); + Forwarded payload = (Forwarded)parser.getPayload(); + assertTrue(payload != null); + assertTrue(payload.getDelay() != null); + assertEquals("2010-07-10T23:08:25Z", DateTime.dateToString(payload.getDelay().getStamp())); + + assertTrue(payload.getStanza() instanceof Presence); + Presence presence = (Presence)payload.getStanza(); + assertTrue(presence != null); + assertEquals("madhatter@wonderland.lit", presence.getTo().toString()); + assertEquals("alice@wonderland.lit/rabbithole", presence.getFrom().toString()); + assertEquals(Presence.Type.Unavailable, presence.getType()); + } +} diff --git a/test/com/isode/stroke/parser/payloadparsers/MAMArchivedParserTest.java b/test/com/isode/stroke/parser/payloadparsers/MAMArchivedParserTest.java new file mode 100644 index 0000000..9a657b7 --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/MAMArchivedParserTest.java @@ -0,0 +1,34 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import org.junit.Test; +import com.isode.stroke.elements.MAMArchived; +import com.isode.stroke.eventloop.DummyEventLoop; +import com.isode.stroke.jid.JID; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class MAMArchivedParserTest { + + @Test + public void testParse() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<archived by=\"juliet@capulet.lit\" id=\"28482-98726-73623\" xmlns=\"urn:xmpp:mam:0\"/>")); + + MAMArchived payload = (MAMArchived)parser.getPayload(); + assertTrue(payload != null); + assertEquals(JID.fromString("juliet@capulet.lit"), payload.getBy()); + assertEquals("28482-98726-73623", payload.getID()); + } +} diff --git a/test/com/isode/stroke/parser/payloadparsers/MAMQueryParserTest.java b/test/com/isode/stroke/parser/payloadparsers/MAMQueryParserTest.java new file mode 100644 index 0000000..c98562f --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/MAMQueryParserTest.java @@ -0,0 +1,76 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import org.junit.Test; +import com.isode.stroke.elements.FormField; +import com.isode.stroke.elements.MAMQuery; +import com.isode.stroke.elements.ResultSet; +import com.isode.stroke.eventloop.DummyEventLoop; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class MAMQueryParserTest { + + @Test + public void testParse() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<query queryid=\"id0\" xmlns=\"urn:xmpp:mam:0\">" + + "<x type=\"form\" xmlns=\"jabber:x:data\">" + + "<field type=\"text-single\" var=\"FORM_TYPE\">" + + "<value>urn:xmpp:mam:0</value>" + + "</field>" + + "<field type=\"text-single\" var=\"start\">" + + "<value>2010-08-07T00:00:00Z</value>" + + "</field>" + + "</x>" + + "<set xmlns=\"http://jabber.org/protocol/rsm\">" + + "<max>10</max>" + + "</set>" + + "</query>")); + + MAMQuery payload = (MAMQuery)parser.getPayload(); + assertTrue(payload != null); + assertTrue(payload.getQueryID() != null); + assertEquals("id0", payload.getQueryID()); + + assertTrue(payload.getForm() != null); + FormField.TextSingleFormField fieldType = (FormField.TextSingleFormField)payload.getForm().getField("FORM_TYPE"); + assertTrue(fieldType != null); + assertEquals("urn:xmpp:mam:0", fieldType.getValue()); + FormField.TextSingleFormField fieldStart = (FormField.TextSingleFormField)payload.getForm().getField("start"); + assertTrue(fieldStart != null); + assertEquals("2010-08-07T00:00:00Z", fieldStart.getValue()); + + assertTrue(payload.getResultSet() != null); + ResultSet resultSet = payload.getResultSet(); + assertTrue(resultSet.getMaxItems() != null); + assertEquals(resultSet.getMaxItems(), new Long(10)); + } + + @Test + public void testParseEmpty() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<query queryid=\"id0\" xmlns=\"urn:xmpp:mam:0\">" + + "</query>")); + + MAMQuery payload = (MAMQuery)parser.getPayload(); + assertTrue(payload != null); + assertTrue(payload.getQueryID() != null); + assertEquals("id0", payload.getQueryID()); + assertTrue(payload.getForm() == null); + assertTrue(payload.getResultSet() == null); + } +} diff --git a/test/com/isode/stroke/parser/payloadparsers/MAMResultParserTest.java b/test/com/isode/stroke/parser/payloadparsers/MAMResultParserTest.java new file mode 100644 index 0000000..9071158 --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/MAMResultParserTest.java @@ -0,0 +1,58 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import com.isode.stroke.base.DateTime; +import com.isode.stroke.elements.Forwarded; +import com.isode.stroke.elements.MAMResult; +import com.isode.stroke.elements.Message; +import com.isode.stroke.eventloop.DummyEventLoop; + +public class MAMResultParserTest { + + @Test + public void testParse() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<result id=\"28482-98726-73623\" queryid=\"f27\" xmlns=\"urn:xmpp:mam:0\">" + + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<delay stamp=\"2010-07-10T23:08:25Z\" xmlns=\"urn:xmpp:delay\"/>" + + "<message from=\"romeo@montague.lit/orchard\" to=\"juliet@capulet.lit/balcony\" type=\"chat\">" + + "<body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body>" + + "</message>" + + "</forwarded>" + + "</result>")); + + MAMResult payload = (MAMResult)parser.getPayload(); + assertTrue(payload != null); + assertTrue(payload.getID() != null); + assertEquals("28482-98726-73623", payload.getID()); + assertTrue(payload.getQueryID() != null); + assertEquals("f27", payload.getQueryID()); + + Forwarded forwarded = payload.getPayload(); + assertTrue(forwarded.getDelay() != null); + assertEquals("2010-07-10T23:08:25Z", DateTime.dateToString(forwarded.getDelay().getStamp())); + + assertTrue(forwarded.getStanza() instanceof Message); + Message message = (Message)forwarded.getStanza(); + assertTrue(message != null); + String expectedBody = "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo."; + assertEquals(expectedBody, message.getBody()); + assertEquals(Message.Type.Chat, message.getType()); + assertEquals("juliet@capulet.lit/balcony", message.getTo().toString()); + assertEquals("romeo@montague.lit/orchard", message.getFrom().toString()); + } +} diff --git a/test/com/isode/stroke/parser/payloadparsers/ResultSetParserTest.java b/test/com/isode/stroke/parser/payloadparsers/ResultSetParserTest.java new file mode 100644 index 0000000..d77ae95 --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/ResultSetParserTest.java @@ -0,0 +1,68 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import com.isode.stroke.elements.ResultSet; +import com.isode.stroke.eventloop.DummyEventLoop; + +public class ResultSetParserTest { + + @Test + public void testParse() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<set xmlns=\"http://jabber.org/protocol/rsm\">" + + "<max>100</max>" + + "<count>800</count>" + + "<first index=\"123\">stpeter@jabber.org</first>" + + "<last>peterpan@neverland.lit</last>" + + "<after>09af3-cc343-b409f</after>" + + "<before>decaf-badba-dbad1</before>" + + "</set>")); + + assertTrue(parser.getPayload() instanceof ResultSet); + ResultSet payload = (ResultSet)parser.getPayload(); + assertTrue(payload.getMaxItems() != null); + assertEquals(new Long(100), payload.getMaxItems()); + assertTrue(payload.getCount() != null); + assertEquals(new Long(800), payload.getCount()); + assertTrue(payload.getFirstID() != null); + assertEquals("stpeter@jabber.org", payload.getFirstID()); + assertTrue(payload.getFirstIDIndex() != null); + assertEquals(new Long(123), payload.getFirstIDIndex()); + assertTrue(payload.getLastID() != null); + assertEquals("peterpan@neverland.lit", payload.getLastID()); + assertTrue(payload.getAfter() != null); + assertEquals("09af3-cc343-b409f", payload.getAfter()); + assertTrue(payload.getBefore() != null); + assertEquals("decaf-badba-dbad1", payload.getBefore()); + } + + @Test + public void testParseFirstNoIndex() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<set xmlns=\"http://jabber.org/protocol/rsm\">" + + "<first>stpeter@jabber.org</first>" + + "</set>")); + + assertTrue(parser.getPayload() instanceof ResultSet); + ResultSet payload = (ResultSet)parser.getPayload(); + assertTrue(payload.getFirstID() != null); + assertEquals("stpeter@jabber.org", payload.getFirstID()); + assertTrue(payload.getFirstIDIndex() == null); + } +} diff --git a/test/com/isode/stroke/serializer/payloadserializers/ForwardedSerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/ForwardedSerializerTest.java new file mode 100644 index 0000000..281be2e --- /dev/null +++ b/test/com/isode/stroke/serializer/payloadserializers/ForwardedSerializerTest.java @@ -0,0 +1,114 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.serializer.payloadserializers; + +import org.junit.Test; +import com.isode.stroke.base.DateTime; +import com.isode.stroke.elements.Delay; +import com.isode.stroke.elements.Forwarded; +import com.isode.stroke.elements.IQ; +import com.isode.stroke.elements.Message; +import com.isode.stroke.elements.Payload; +import com.isode.stroke.elements.Presence; +import com.isode.stroke.jid.JID; +import static org.junit.Assert.assertEquals; + +public class ForwardedSerializerTest { + + @Test + public void testSerializeIQ() { + ForwardedSerializer serializer = new ForwardedSerializer(serializers_); + + IQ iq = IQ.createResult(JID.fromString("juliet@capulet.lit/balcony"), JID.fromString("romeo@montague.lit/orchard"), "id0", new Payload()); + + Forwarded forwarded = new Forwarded(); + forwarded.setStanza(iq); + forwarded.setDelay(new Delay(DateTime.stringToDate("2010-07-10T23:08:25Z"), null)); + + String expectedResult = + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<delay stamp=\"2010-07-10T23:08:25Z\" xmlns=\"urn:xmpp:delay\"/>" + + "<iq from=\"romeo@montague.lit/orchard\" id=\"id0\" to=\"juliet@capulet.lit/balcony\" type=\"result\"/>" + + "</forwarded>"; + + assertEquals(expectedResult, serializer.serialize(forwarded)); + } + + @Test + public void testSerializeMessage() { + ForwardedSerializer serializer = new ForwardedSerializer(serializers_); + + Message message = new Message(); + message.setType(Message.Type.Chat); + message.setTo(JID.fromString("juliet@capulet.lit/balcony")); + message.setFrom(JID.fromString("romeo@montague.lit/orchard")); + message.setBody("Call me but love, and I'll be new baptized; Henceforth I never will be Romeo."); + + Forwarded forwarded = new Forwarded(); + forwarded.setStanza(message); + forwarded.setDelay(new Delay(DateTime.stringToDate("2010-07-10T23:08:25Z"), null)); + + String expectedResult = + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<delay stamp=\"2010-07-10T23:08:25Z\" xmlns=\"urn:xmpp:delay\"/>" + + "<message from=\"romeo@montague.lit/orchard\" to=\"juliet@capulet.lit/balcony\" type=\"chat\">" + + "<body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body>" + + "</message>" + + "</forwarded>"; + + assertEquals(expectedResult, serializer.serialize(forwarded)); + } + + @Test + public void testSerializeMessageNoDelay() { + ForwardedSerializer serializer = new ForwardedSerializer(serializers_); + + Message message = new Message(); + message.setType(Message.Type.Chat); + message.setTo(JID.fromString("juliet@capulet.lit/balcony")); + message.setFrom(JID.fromString("romeo@montague.lit/orchard")); + message.setBody("Call me but love, and I'll be new baptized; Henceforth I never will be Romeo."); + + Forwarded forwarded = new Forwarded(); + forwarded.setStanza(message); + + String expectedResult = + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<message from=\"romeo@montague.lit/orchard\" to=\"juliet@capulet.lit/balcony\" type=\"chat\">" + + "<body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body>" + + "</message>" + + "</forwarded>"; + + assertEquals(expectedResult, serializer.serialize(forwarded)); + } + + @Test + public void testSerializePresence() { + ForwardedSerializer serializer = new ForwardedSerializer(serializers_); + + Presence presence = new Presence(); + presence.setType(Presence.Type.Subscribe); + + Forwarded forwarded = new Forwarded(); + forwarded.setStanza(presence); + forwarded.setDelay(new Delay(DateTime.stringToDate("2010-07-10T23:08:25Z"), null)); + + String expectedResult = + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<delay stamp=\"2010-07-10T23:08:25Z\" xmlns=\"urn:xmpp:delay\"/>" + + "<presence type=\"subscribe\"/>" + + "</forwarded>"; + + assertEquals(expectedResult, serializer.serialize(forwarded)); + } + + FullPayloadSerializerCollection serializers_ = new FullPayloadSerializerCollection(); +} diff --git a/test/com/isode/stroke/serializer/payloadserializers/MAMArchivedSerilaizerTest.java b/test/com/isode/stroke/serializer/payloadserializers/MAMArchivedSerilaizerTest.java new file mode 100644 index 0000000..865ad0c --- /dev/null +++ b/test/com/isode/stroke/serializer/payloadserializers/MAMArchivedSerilaizerTest.java @@ -0,0 +1,33 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.serializer.payloadserializers; + +import org.junit.Test; +import com.isode.stroke.elements.MAMArchived; +import com.isode.stroke.jid.JID; +import static org.junit.Assert.assertEquals; + +public class MAMArchivedSerilaizerTest { + + @Test + public void testSerialize() { + MAMArchivedSerializer serializer = new MAMArchivedSerializer(); + + MAMArchived archived = new MAMArchived(); + archived.setBy(JID.fromString("juliet@capulet.lit")); + archived.setID("28482-98726-73623"); + + String expectedResult = + "<archived by=\"juliet@capulet.lit\" id=\"28482-98726-73623\" xmlns=\"urn:xmpp:mam:0\"/>"; + + assertEquals(expectedResult, serializer.serialize(archived)); + } +} diff --git a/test/com/isode/stroke/serializer/payloadserializers/MAMQuerySerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/MAMQuerySerializerTest.java new file mode 100644 index 0000000..ab4a3fa --- /dev/null +++ b/test/com/isode/stroke/serializer/payloadserializers/MAMQuerySerializerTest.java @@ -0,0 +1,61 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.serializer.payloadserializers; + +import org.junit.Test; +import com.isode.stroke.elements.Form; +import com.isode.stroke.elements.FormField; +import com.isode.stroke.elements.MAMQuery; +import com.isode.stroke.elements.ResultSet; +import static org.junit.Assert.assertEquals; + +public class MAMQuerySerializerTest { + + @Test + public void testSerialize() { + MAMQuerySerializer serializer = new MAMQuerySerializer(); + + Form parameters = new Form(); + + FormField.TextSingleFormField fieldType = FormField.TextSingleFormField.create("urn:xmpp:mam:0"); + fieldType.setName("FORM_TYPE"); + parameters.addField(fieldType); + + FormField.TextSingleFormField fieldStart = FormField.TextSingleFormField.create("2010-08-07T00:00:00Z"); + fieldStart.setName("start"); + parameters.addField(fieldStart); + + ResultSet set = new ResultSet(); + set.setMaxItems(new Long(10)); + + MAMQuery query = new MAMQuery(); + query.setQueryID("id0"); + query.setForm(parameters); + query.setResultSet(set); + + String expectedResult = + "<query queryid=\"id0\" xmlns=\"urn:xmpp:mam:0\">" + + "<x type=\"form\" xmlns=\"jabber:x:data\">" + + "<field type=\"text-single\" var=\"FORM_TYPE\">" + + "<value>urn:xmpp:mam:0</value>" + + "</field>" + + "<field type=\"text-single\" var=\"start\">" + + "<value>2010-08-07T00:00:00Z</value>" + + "</field>" + + "</x>" + + "<set xmlns=\"http://jabber.org/protocol/rsm\">" + + "<max>10</max>" + + "</set>" + + "</query>"; + + assertEquals(expectedResult, serializer.serialize(query)); + } +} diff --git a/test/com/isode/stroke/serializer/payloadserializers/MAMResultSerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/MAMResultSerializerTest.java new file mode 100644 index 0000000..128fdc0 --- /dev/null +++ b/test/com/isode/stroke/serializer/payloadserializers/MAMResultSerializerTest.java @@ -0,0 +1,57 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.serializer.payloadserializers; + +import org.junit.Test; +import com.isode.stroke.base.DateTime; +import com.isode.stroke.elements.Delay; +import com.isode.stroke.elements.Forwarded; +import com.isode.stroke.elements.MAMResult; +import com.isode.stroke.elements.Message; +import com.isode.stroke.jid.JID; +import static org.junit.Assert.assertEquals; + +public class MAMResultSerializerTest { + + @Test + public void testSerialize() { + MAMResultSerializer serializer = new MAMResultSerializer(serializers_); + + Message message = new Message(); + message.setType(Message.Type.Chat); + message.setTo(JID.fromString("juliet@capulet.lit/balcony")); + message.setFrom(JID.fromString("romeo@montague.lit/orchard")); + message.setBody("Call me but love, and I'll be new baptized; Henceforth I never will be Romeo."); + + Forwarded forwarded = new Forwarded(); + forwarded.setStanza(message); + forwarded.setDelay(new Delay(DateTime.stringToDate("2010-07-10T23:08:25Z"), null)); + + MAMResult result = new MAMResult(); + result.setID("28482-98726-73623"); + result.setQueryID("f27"); + result.setPayload(forwarded); + + String expectedResult = + "<result id=\"28482-98726-73623\" queryid=\"f27\" xmlns=\"urn:xmpp:mam:0\">" + + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<delay stamp=\"2010-07-10T23:08:25Z\" xmlns=\"urn:xmpp:delay\"/>" + + "<message from=\"romeo@montague.lit/orchard\" to=\"juliet@capulet.lit/balcony\" type=\"chat\">" + + "<body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body>" + + "</message>" + + "</forwarded>" + + "</result>"; + + assertEquals(expectedResult, serializer.serialize(result)); + } + + FullPayloadSerializerCollection serializers_ = new FullPayloadSerializerCollection(); +} diff --git a/test/com/isode/stroke/serializer/payloadserializers/ResultSetSerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/ResultSetSerializerTest.java new file mode 100644 index 0000000..f4c92cf --- /dev/null +++ b/test/com/isode/stroke/serializer/payloadserializers/ResultSetSerializerTest.java @@ -0,0 +1,110 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.serializer.payloadserializers; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import com.isode.stroke.elements.ResultSet; + +public class ResultSetSerializerTest { + + @Test + public void testSerializeFull() { + ResultSetSerializer serializer = new ResultSetSerializer(); + + ResultSet resultSet = new ResultSet(); + + resultSet.setMaxItems(new Long(100)); + resultSet.setCount(new Long(800)); + resultSet.setFirstIDIndex(new Long(123)); + resultSet.setFirstID("stpeter@jabber.org"); + resultSet.setLastID("peterpan@neverland.lit"); + resultSet.setAfter("09af3-cc343-b409f"); + resultSet.setBefore("decaf-badba-dbad1"); + + String expectedResult = + "<set xmlns=\"http://jabber.org/protocol/rsm\">" + + "<max>100</max>" + + "<count>800</count>" + + "<first index=\"123\">stpeter@jabber.org</first>" + + "<last>peterpan@neverland.lit</last>" + + "<after>09af3-cc343-b409f</after>" + + "<before>decaf-badba-dbad1</before>" + + "</set>"; + + assertEquals(expectedResult, serializer.serialize(resultSet)); + } + + @Test + public void testSerializeMaxItems() { + ResultSetSerializer serializer = new ResultSetSerializer(); + + ResultSet resultSet = new ResultSet(); + + resultSet.setMaxItems(new Long(100)); + + String expectedResult = + "<set xmlns=\"http://jabber.org/protocol/rsm\">" + + "<max>100</max>" + + "</set>"; + + assertEquals(expectedResult, serializer.serialize(resultSet)); + } + + @Test + public void testSerializeEmptyBefore() { + ResultSetSerializer serializer = new ResultSetSerializer(); + + ResultSet resultSet = new ResultSet(); + + resultSet.setBefore(new String()); + + String expectedResult = + "<set xmlns=\"http://jabber.org/protocol/rsm\">" + + "<before/>" + + "</set>"; + + assertEquals(expectedResult, serializer.serialize(resultSet)); + } + + @Test + public void testSerializeFirst() { + ResultSetSerializer serializer = new ResultSetSerializer(); + + ResultSet resultSet = new ResultSet(); + + resultSet.setFirstID("stpeter@jabber.org"); + + String expectedResult = + "<set xmlns=\"http://jabber.org/protocol/rsm\">" + + "<first>stpeter@jabber.org</first>" + + "</set>"; + + assertEquals(expectedResult, serializer.serialize(resultSet)); + } + + @Test + public void testSerializeFirstWithIndex() { + ResultSetSerializer serializer = new ResultSetSerializer(); + + ResultSet resultSet = new ResultSet(); + + resultSet.setFirstID("stpeter@jabber.org"); + resultSet.setFirstIDIndex(new Long(123)); + + String expectedResult = + "<set xmlns=\"http://jabber.org/protocol/rsm\">" + + "<first index=\"123\">stpeter@jabber.org</first>" + + "</set>"; + + assertEquals(expectedResult, serializer.serialize(resultSet)); + } +} |