From b54bc5406f95902ce2438ac64a19390ae0f2f409 Mon Sep 17 00:00:00 2001 From: Gurmeen Bindra Date: Tue, 17 Apr 2012 10:18:20 +0100 Subject: Port MUC Payload Serializers from Swiften to Stroke All the serializers for different kind of MUC payloads have been ported from swiften to stroke. Test-information: There is a junit test that's ported which tests the admin payload serialiser. Also executed the other MUC Junits. diff --git a/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java b/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java index 4051d9b..d8a7a7d 100644 --- a/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java +++ b/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java @@ -8,6 +8,12 @@ */ package com.isode.stroke.serializer.payloadserializers; +import com.isode.stroke.serializer.payloadserializers.MUCAdminPayloadSerializer; +import com.isode.stroke.serializer.payloadserializers.MUCDestroyPayloadSerializer; +import com.isode.stroke.serializer.payloadserializers.MUCInvitationPayloadSerializer; +import com.isode.stroke.serializer.payloadserializers.MUCOwnerPayloadSerializer; +import com.isode.stroke.serializer.payloadserializers.MUCPayloadSerializer; +import com.isode.stroke.serializer.payloadserializers.MUCUserPayloadSerializer; import com.isode.stroke.serializer.PayloadSerializerCollection; public class FullPayloadSerializerCollection extends PayloadSerializerCollection { @@ -21,9 +27,12 @@ public class FullPayloadSerializerCollection extends PayloadSerializerCollection //addSerializer(new PrioritySerializer()); addSerializer(new ErrorSerializer()); addSerializer(new RosterSerializer()); - //addSerializer(new MUCPayloadSerializer()); - //addSerializer(new MUCUserPayloadSerializer()); - //addSerializer(new MUCOwnerPayloadSerializer(this)); + addSerializer(new MUCPayloadSerializer()); + addSerializer(new MUCDestroyPayloadSerializer()); + addSerializer(new MUCAdminPayloadSerializer()); + addSerializer(new MUCInvitationPayloadSerializer()); + addSerializer(new MUCOwnerPayloadSerializer(this)); + addSerializer(new MUCUserPayloadSerializer(this)); addSerializer(new SoftwareVersionSerializer()); //addSerializer(new StatusSerializer()); //addSerializer(new StatusShowSerializer()); diff --git a/src/com/isode/stroke/serializer/payloadserializers/MUCAdminPayloadSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/MUCAdminPayloadSerializer.java new file mode 100644 index 0000000..618add2 --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/MUCAdminPayloadSerializer.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2011, Kevin Smith + * All rights reserved. + */ +package com.isode.stroke.serializer.payloadserializers; + +import com.isode.stroke.elements.MUCAdminPayload; +import com.isode.stroke.elements.MUCItem; +import com.isode.stroke.serializer.GenericPayloadSerializer; +import com.isode.stroke.serializer.xml.XMLElement; + +/** + * Serializer for {@link MUCAdminPayload} element. + */ +public final class MUCAdminPayloadSerializer extends GenericPayloadSerializer { + + /** + * Constructor + */ + public MUCAdminPayloadSerializer() { + super(MUCAdminPayload.class); + } + + @Override + public String serializePayload(MUCAdminPayload payload) { + XMLElement mucElement = new XMLElement("query", "http://jabber.org/protocol/muc#admin"); + for(MUCItem item : payload.getItems()) { + mucElement.addNode(MUCItemSerializer.itemToElement(item)); + } + return mucElement.serialize(); + } +} diff --git a/src/com/isode/stroke/serializer/payloadserializers/MUCDestroyPayloadSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/MUCDestroyPayloadSerializer.java new file mode 100644 index 0000000..7152a5d --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/MUCDestroyPayloadSerializer.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2011, Kevin Smith + * All rights reserved. + */ +package com.isode.stroke.serializer.payloadserializers; + +import com.isode.stroke.elements.MUCDestroyPayload; +import com.isode.stroke.serializer.GenericPayloadSerializer; +import com.isode.stroke.serializer.xml.XMLElement; +import com.isode.stroke.serializer.xml.XMLTextNode; + +/** + * Serializer for {@link MUCDestroyPayload} element. + */ +public class MUCDestroyPayloadSerializer extends GenericPayloadSerializer { + /** + * Create the serializer + */ + public MUCDestroyPayloadSerializer() { + super(MUCDestroyPayload.class); + } + + @Override + public String serializePayload(MUCDestroyPayload payload) { + XMLElement mucElement = new XMLElement("destroy", ""); + if (payload.getReason() != null && !payload.getReason().isEmpty()) { + XMLElement reason = new XMLElement("reason", ""); + reason.addNode(new XMLTextNode(payload.getReason())); + mucElement.addNode(reason); + } + if (payload.getNewVenue() != null && payload.getNewVenue().isValid()) { + mucElement.setAttribute("jid", payload.getNewVenue().toString()); + } + return mucElement.serialize(); + } +} diff --git a/src/com/isode/stroke/serializer/payloadserializers/MUCInvitationPayloadSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/MUCInvitationPayloadSerializer.java new file mode 100644 index 0000000..e149aa7 --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/MUCInvitationPayloadSerializer.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2011, Kevin Smith + * All rights reserved. + */ +package com.isode.stroke.serializer.payloadserializers; + +import com.isode.stroke.elements.MUCInvitationPayload; +import com.isode.stroke.serializer.GenericPayloadSerializer; +import com.isode.stroke.serializer.xml.XMLElement; + +/** + * Serializer for {@link MUCInvitationPayload} element. + */ +public class MUCInvitationPayloadSerializer extends GenericPayloadSerializer { + /** + * Constructor + */ + public MUCInvitationPayloadSerializer() { + super(MUCInvitationPayload.class); + } + + @Override + public String serializePayload(MUCInvitationPayload payload) { + XMLElement mucElement = new XMLElement("x", "jabber:x:conference"); + if (payload.getIsContinuation()) { + mucElement.setAttribute("continue", "true"); + } + if (payload.getJID() != null && payload.getJID().isValid()) { + mucElement.setAttribute("jid", payload.getJID().toString()); + } + if (payload.getPassword() != null && !payload.getPassword().isEmpty()) { + mucElement.setAttribute("password", payload.getPassword()); + } + if (payload.getReason() != null && !payload.getReason().isEmpty()) { + mucElement.setAttribute("reason", payload.getReason()); + } + if (payload.getThread() != null && !payload.getThread().isEmpty()) { + mucElement.setAttribute("thread", payload.getThread()); + } + return mucElement.serialize(); + } +} diff --git a/src/com/isode/stroke/serializer/payloadserializers/MUCItemSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/MUCItemSerializer.java new file mode 100644 index 0000000..a3bf80c --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/MUCItemSerializer.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2011, Kevin Smith + * All rights reserved. + */ +package com.isode.stroke.serializer.payloadserializers; + +import com.isode.stroke.elements.MUCItem; +import com.isode.stroke.serializer.xml.XMLElement; +import com.isode.stroke.serializer.xml.XMLTextNode; + +/** + * Class representing a serializer for {@link MUCItem} + * + */ +public class MUCItemSerializer { + + /** + * Create an XMLElement from the given MUC Item + * @param item MUC Item, not null + * @return XML Element, not null + */ + public static XMLElement itemToElement(MUCItem item) { + XMLElement itemElement = new XMLElement("item"); + if (item.affiliation != null) { + itemElement.setAttribute("affiliation", item.affiliation.nodeName); + } + if (item.role != null) { + itemElement.setAttribute("role", item.role.nodeName); + } + if (item.realJID != null) { + itemElement.setAttribute("jid", item.realJID.toString()); + } + if (item.nick != null) { + itemElement.setAttribute("nick", item.nick); + } + if (item.actor != null) { + XMLElement actorElement = new XMLElement("actor"); + actorElement.setAttribute("jid", item.actor.toString()); + itemElement.addNode(actorElement); + } + if (item.reason != null) { + XMLElement reasonElement = new XMLElement("reason"); + reasonElement.addNode(new XMLTextNode(item.reason)); + itemElement.addNode(reasonElement); + } + return itemElement; + } +} \ No newline at end of file diff --git a/src/com/isode/stroke/serializer/payloadserializers/MUCOwnerPayloadSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/MUCOwnerPayloadSerializer.java new file mode 100644 index 0000000..ed7b5f5 --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/MUCOwnerPayloadSerializer.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2011, Kevin Smith + * All rights reserved. + */ +package com.isode.stroke.serializer.payloadserializers; + +import com.isode.stroke.elements.MUCOwnerPayload; +import com.isode.stroke.elements.Payload; +import com.isode.stroke.serializer.GenericPayloadSerializer; +import com.isode.stroke.serializer.PayloadSerializer; +import com.isode.stroke.serializer.PayloadSerializerCollection; +import com.isode.stroke.serializer.xml.XMLElement; +import com.isode.stroke.serializer.xml.XMLRawTextNode; + +/** + * Serializer for {@link MUCOwnerPayload} element. + */ +public class MUCOwnerPayloadSerializer extends GenericPayloadSerializer { + public MUCOwnerPayloadSerializer(PayloadSerializerCollection serializers) { + super(MUCOwnerPayload.class); + this.serializers_ = serializers; + } + + @Override + public String serializePayload(MUCOwnerPayload mucOwner) { + XMLElement mucElement = new XMLElement("query", "http://jabber.org/protocol/muc#owner"); + Payload payload = mucOwner.getPayload(); + if (payload != null) { + PayloadSerializer serializer = serializers_.getPayloadSerializer(payload); + if (serializer != null) { + mucElement.addNode(new XMLRawTextNode(serializer.serialize(payload))); + } + } + return mucElement.serialize(); + } + public PayloadSerializerCollection serializers_; +} diff --git a/src/com/isode/stroke/serializer/payloadserializers/MUCPayloadSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/MUCPayloadSerializer.java new file mode 100644 index 0000000..d6e7a10 --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/MUCPayloadSerializer.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010, Kevin Smith + * All rights reserved. + */ +package com.isode.stroke.serializer.payloadserializers; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import com.isode.stroke.elements.MUCPayload; +import com.isode.stroke.serializer.GenericPayloadSerializer; +import com.isode.stroke.serializer.xml.XMLElement; +import com.isode.stroke.serializer.xml.XMLTextNode; + +/** + * Serializer for {@link MUCPayload} element. + */ +public class MUCPayloadSerializer extends GenericPayloadSerializer { + + /** + * Constructor + */ + public MUCPayloadSerializer() { + super(MUCPayload.class); + } + + @Override + public String serializePayload(MUCPayload muc) { + XMLElement mucElement = new XMLElement("x", "http://jabber.org/protocol/muc"); + XMLElement historyElement = new XMLElement("history"); + boolean history = false; + if (muc.getMaxChars() >= 0) { + historyElement.setAttribute("maxchars", String.valueOf(muc.getMaxChars())); + history = true; + } + if (muc.getMaxStanzas() >= 0) { + historyElement.setAttribute("maxstanzas", String.valueOf(muc.getMaxStanzas())); + history = true; + } + if (muc.getSeconds() >= 0) { + historyElement.setAttribute("seconds", String.valueOf(muc.getSeconds())); + history = true; + } + if (muc.getSince() != null) { + SimpleDateFormat dfm = new SimpleDateFormat("YYYY-MM-dd"); + SimpleDateFormat tfm = new SimpleDateFormat("hh:mm:ss"); + Date date = muc.getSince(); + + dfm.setTimeZone(TimeZone.getTimeZone("UTC")); + tfm.setTimeZone(TimeZone.getTimeZone("UTC")); + String sinceDateString = dfm.format(date) + "T" + tfm.format(date) + "Z"; + historyElement.setAttribute("since", sinceDateString); + history = true; + } + if (muc.getPassword() != null) { + String password = muc.getPassword(); + XMLElement passwordElement = new XMLElement("password"); + passwordElement.addNode(new XMLTextNode(password)); + mucElement.addNode(passwordElement); + } + if (history) { + mucElement.addNode(historyElement); + } + return mucElement.serialize(); + } +} diff --git a/src/com/isode/stroke/serializer/payloadserializers/MUCUserPayloadSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/MUCUserPayloadSerializer.java new file mode 100644 index 0000000..1fa7ce8 --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/MUCUserPayloadSerializer.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010, Kevin Smith + * All rights reserved. + */ +package com.isode.stroke.serializer.payloadserializers; + +import com.isode.stroke.elements.MUCItem; +import com.isode.stroke.elements.MUCUserPayload; +import com.isode.stroke.elements.Payload; +import com.isode.stroke.serializer.GenericPayloadSerializer; +import com.isode.stroke.serializer.PayloadSerializer; +import com.isode.stroke.serializer.PayloadSerializerCollection; +import com.isode.stroke.serializer.xml.XMLElement; +import com.isode.stroke.serializer.xml.XMLRawTextNode; +import com.isode.stroke.serializer.xml.XMLTextNode; + +/** + * Serializer for {@link MUCUserPayload} element. + */ +public class MUCUserPayloadSerializer extends GenericPayloadSerializer { + + private PayloadSerializerCollection serializers_; + + /** + * Constructor + * @param serializers Payload Serializer Collection, not null + */ + public MUCUserPayloadSerializer(PayloadSerializerCollection serializers) { + super(MUCUserPayload.class); + this.serializers_ = serializers; + } + + @Override + public String serializePayload(MUCUserPayload payload) { + XMLElement mucElement = new XMLElement("x", "http://jabber.org/protocol/muc#user"); + for (MUCUserPayload.StatusCode statusCode : payload.getStatusCodes()) { + XMLElement statusElement = new XMLElement("status"); + statusElement.setAttribute("code", String.valueOf(statusCode.code)); + mucElement.addNode(statusElement); + } + for (MUCItem item : payload.getItems()) { + mucElement.addNode(MUCItemSerializer.itemToElement(item)); + } + + if (payload.getPassword() != null) { + XMLElement passwordElement = new XMLElement("password"); + passwordElement.addNode(new XMLTextNode(payload.getPassword())); + } + + if (payload.getInvite() != null) { + MUCUserPayload.Invite invite = payload.getInvite(); + XMLElement inviteElement = new XMLElement("invite"); + if (invite.to != null && invite.to.isValid()) { + inviteElement.setAttribute("to", invite.to.toString()); + } + if (invite.from != null && invite.from.isValid()) { + inviteElement.setAttribute("from", invite.from.toString()); + } + if (invite.reason != null && !invite.reason.isEmpty()) { + XMLElement reasonElement = new XMLElement("reason"); + reasonElement.addNode(new XMLTextNode(invite.reason)); + } + mucElement.addNode(inviteElement); + } + + Payload childPayload = payload.getPayload(); + if (childPayload != null) { + PayloadSerializer serializer = serializers_.getPayloadSerializer(childPayload); + if (serializers_ != null) { + mucElement.addNode(new XMLRawTextNode(serializer.serialize(childPayload))); + } + } + return mucElement.serialize(); + } +} diff --git a/test/com/isode/stroke/serializer/payloadserializers/MUCAdminPayloadSerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/MUCAdminPayloadSerializerTest.java new file mode 100644 index 0000000..9d55ad5 --- /dev/null +++ b/test/com/isode/stroke/serializer/payloadserializers/MUCAdminPayloadSerializerTest.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2011, Kevin Smith + * All rights reserved. + */ +package com.isode.stroke.serializer.payloadserializers; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.isode.stroke.elements.MUCAdminPayload; +import com.isode.stroke.elements.MUCItem; +import com.isode.stroke.elements.MUCOccupant; +import com.isode.stroke.jid.JID; +import com.isode.stroke.serializer.payloadserializers.MUCAdminPayloadSerializer; + +public class MUCAdminPayloadSerializerTest { + + @Test + public void testSerialize() { + MUCAdminPayloadSerializer testling = new MUCAdminPayloadSerializer(); + MUCAdminPayload admin = new MUCAdminPayload(); + MUCItem item = new MUCItem(); + item.affiliation = MUCOccupant.Affiliation.Owner; + item.role = MUCOccupant.Role.Visitor; + item.reason = "malice"; + item.actor = new JID("kev@tester.lit"); + admin.addItem(item); + + assertEquals("malice", + testling.serialize(admin)); + } + +} -- cgit v0.10.2-6-g49f6