aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/xmpp/avatar
diff options
context:
space:
mode:
authorsteckbrief <steckbrief@chefmail.de>2016-01-15 22:15:00 +0100
committersteckbrief <steckbrief@chefmail.de>2016-01-15 22:15:00 +0100
commit69145c02f2ba357a95b38575bdc0eca40a837bae (patch)
treecc9ccda0e22a924818134048f2c0c87f679cc1e3 /src/main/java/de/thedevstack/conversationsplus/xmpp/avatar
parent424477dfbc84f12af8223b004692d84b0d3ea5aa (diff)
parentd44debab6726edbac07901a2b94a07f2ef4771ee (diff)
Merge remote-tracking branch 'remotes/origin/trz/rename' into trz/rebase
Cast problem fixed
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/xmpp/avatar')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarPacketGenerator.java125
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarPacketParser.java30
2 files changed, 155 insertions, 0 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarPacketGenerator.java b/src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarPacketGenerator.java
new file mode 100644
index 00000000..ed8a898d
--- /dev/null
+++ b/src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarPacketGenerator.java
@@ -0,0 +1,125 @@
+package de.thedevstack.conversationsplus.xmpp.avatar;
+
+import de.thedevstack.conversationsplus.xmpp.pubsub.PubSubPacketGenerator;
+
+import eu.siacs.conversations.xml.Element;
+import eu.siacs.conversations.xmpp.jid.Jid;
+import eu.siacs.conversations.xmpp.pep.Avatar;
+import eu.siacs.conversations.xmpp.stanzas.IqPacket;
+
+/**
+ * Generates the IQ Packets for handling Avatars
+ * as defined in XEP-0084.
+ * @see {@link http://xmpp.org/extensions/xep-0084.html}
+ */
+public final class AvatarPacketGenerator {
+ public static final String NAMESPACE_AVATAR_DATA = "urn:xmpp:avatar:data";
+ public static final String NAMESPACE_AVATAR_METADATA = "urn:xmpp:avatar:metadata";
+
+ /**
+ * Generates an IqPacket for publishing avatar data.
+ * The attributes from and id are not set in here - this is added while sending the packet.
+ * <pre>
+ * <iq type='set'>
+ * <pubsub xmlns='http://jabber.org/protocol/pubsub'>
+ * <publish node='urn:xmpp:avatar:data'>
+ * <item id='111f4b3c50d7b0df729d299bc6f8e9ef9066971f'>
+ * <data xmlns='urn:xmpp:avatar:data'>
+ * qANQR1DBwU4DX7jmYZnncm...
+ * </data>
+ * </item>
+ * </publish>
+ * </pubsub>
+ * </iq>
+ * </pre>
+ * @param avatar the avatar to publish
+ * @return the IqPacket
+ */
+ public static IqPacket generatePublishAvatarPacket(Avatar avatar) {
+ final Element item = new Element("item");
+ item.setAttribute("id", avatar.sha1sum);
+ final Element data = item.addChild("data", NAMESPACE_AVATAR_DATA);
+ data.setContent(avatar.image);
+ return PubSubPacketGenerator.generatePubSubPublishPacket(NAMESPACE_AVATAR_DATA, item);
+ }
+
+ /**
+ * Generates an IqPacket to retrieve avatar data.
+ * The attributes from and id are not set in here - this is added while sending the packet.
+ * <pre>
+ * <iq type='get'>
+ * <pubsub xmlns='http://jabber.org/protocol/pubsub'>
+ * <items node='urn:xmpp:avatar:data'>
+ * <item id='111f4b3c50d7b0df729d299bc6f8e9ef9066971f'/>
+ * </items>
+ * </pubsub>
+ * </iq>
+ * </pre>
+ * @param avatar the avatar to retrieve
+ * @return the IqPacket
+ */
+ public static IqPacket generateRetrieveAvatarPacket(Avatar avatar) {
+ final Element item = new Element("item");
+ item.setAttribute("id", avatar.sha1sum);
+ final IqPacket packet = PubSubPacketGenerator.generatePubSubRetrievePacket(NAMESPACE_AVATAR_DATA, item);
+ packet.setTo(avatar.owner);
+ return packet;
+ }
+
+ /**
+ * Generates an IqPacket to publish metadata for an avatar.
+ * The attributes from and id are not set in here - this is added while sending the packet.
+ * <pre>
+ * <iq type='set'>
+ * <pubsub xmlns='http://jabber.org/protocol/pubsub'>
+ * <publish node='urn:xmpp:avatar:metadata'>
+ * <item id='111f4b3c50d7b0df729d299bc6f8e9ef9066971f'>
+ * <metadata xmlns='urn:xmpp:avatar:metadata'>
+ * <info bytes='12345'
+ * id='111f4b3c50d7b0df729d299bc6f8e9ef9066971f'
+ * height='64'
+ * type='image/png'
+ * width='64'/>
+ * </metadata>
+ * </item>
+ * </publish>
+ * </pubsub>
+ * </iq>
+ * </pre>
+ * @param avatar the avatar to publish the metadata
+ * @return the IqPacket
+ */
+ public static IqPacket generatePublishAvatarMetadataPacket(Avatar avatar) {
+ final Element item = new Element("item");
+ item.setAttribute("id", avatar.sha1sum);
+ final Element metadata = item.addChild("metadata", NAMESPACE_AVATAR_METADATA);
+ final Element info = metadata.addChild("info");
+ info.setAttribute("bytes", avatar.size);
+ info.setAttribute("id", avatar.sha1sum);
+ info.setAttribute("height", avatar.height);
+ info.setAttribute("width", avatar.height);
+ info.setAttribute("type", avatar.type);
+ return PubSubPacketGenerator.generatePubSubPublishPacket(NAMESPACE_AVATAR_METADATA, item);
+ }
+
+ /**
+ * Generates an IqPacket to retrieve metadata of an avatar.
+ * The attributes from and id are not set in here - this is added while sending the packet.
+ * @param to the Jid to deliver the metadata to
+ * @return the IqPacket
+ */
+ public static IqPacket generateRetrieveAvatarMetadataPacket(Jid to) {
+ final IqPacket packet = PubSubPacketGenerator.generatePubSubRetrievePacket(NAMESPACE_AVATAR_METADATA, null);
+ if (to != null) {
+ packet.setTo(to);
+ }
+ return packet;
+ }
+
+ /**
+ * Helper class - private constructor to avoid instantiation
+ */
+ private AvatarPacketGenerator() {
+ // avoid instantiation
+ }
+}
diff --git a/src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarPacketParser.java b/src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarPacketParser.java
new file mode 100644
index 00000000..cf993462
--- /dev/null
+++ b/src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarPacketParser.java
@@ -0,0 +1,30 @@
+package de.thedevstack.conversationsplus.xmpp.avatar;
+
+import de.thedevstack.conversationsplus.xmpp.pubsub.PubSubPacketParser;
+
+import eu.siacs.conversations.xml.Element;
+import eu.siacs.conversations.xmpp.stanzas.IqPacket;
+
+/**
+ * Parses the IQ Packets for handling Avatars
+ * as defined in XEP-0084.
+ * @see {@link http://xmpp.org/extensions/xep-0084.html}
+ */
+public class AvatarPacketParser {
+ /**
+ * Extracts the base64 encoded avatar data from an IqPacket.
+ * @param packet the IqPacket to be parsed.
+ * @return base64 encoded avatar data
+ */
+ public static String parseAvatarData(IqPacket packet) {
+ Element items = PubSubPacketParser.findItems(packet);
+ String base64Avatar = null;
+ if (null != items) {
+ Element item = items.findChild("item");
+ if (null != item) {
+ base64Avatar = item.findChildContent("data", AvatarPacketGenerator.NAMESPACE_AVATAR_DATA);
+ }
+ }
+ return base64Avatar;
+ }
+}