diff options
author | steckbrief <steckbrief@chefmail.de> | 2017-08-06 01:15:13 +0200 |
---|---|---|
committer | steckbrief <steckbrief@chefmail.de> | 2017-08-06 01:15:13 +0200 |
commit | 867afe4c5b888ce3f4f9a867906cc8edb86e7aba (patch) | |
tree | 82d17b5fcb4471ae884a548fa815a653cb00e000 /src/main/java/de/thedevstack/conversationsplus/xmpp/avatar | |
parent | 6f588444a953a54543661f7603d45a9093b7196a (diff) |
avatar handling refactored
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/xmpp/avatar')
3 files changed, 98 insertions, 0 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarPacketParser.java b/src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarPacketParser.java index 0867524b..78ab9288 100644 --- a/src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarPacketParser.java +++ b/src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarPacketParser.java @@ -1,5 +1,6 @@ package de.thedevstack.conversationsplus.xmpp.avatar; +import de.thedevstack.conversationsplus.xmpp.pep.Avatar; import de.thedevstack.conversationsplus.xmpp.pubsub.PubSubPacketParser; import de.thedevstack.conversationsplus.xml.Element; import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; @@ -26,4 +27,56 @@ public class AvatarPacketParser { } return base64Avatar; } + + public static Avatar parseMetadata(Element items) { + if (null != items) { + Element item = items.findChild("item"); + if (item == null) { + return null; + } + Element metadata = item.findChild("metadata"); + if (metadata == null) { + return null; + } + String primaryId = item.getAttribute("id"); + if (primaryId == null) { + return null; + } + for (Element child : metadata.getChildren()) { + if (child.getName().equals("info") + && primaryId.equals(child.getAttribute("id"))) { + Avatar avatar = new Avatar(); + String height = child.getAttribute("height"); + String width = child.getAttribute("width"); + String size = child.getAttribute("bytes"); + try { + if (height != null) { + avatar.height = Integer.parseInt(height); + } + if (width != null) { + avatar.width = Integer.parseInt(width); + } + if (size != null) { + avatar.size = Long.parseLong(size); + } + } catch (NumberFormatException e) { + return null; + } + avatar.type = child.getAttribute("type"); + String hash = child.getAttribute("id"); + if (!Avatar.isValidSHA1(hash)) { + return null; + } + avatar.sha1sum = hash; + avatar.origin = Avatar.Origin.PEP; + return avatar; + } + } + } + return null; + } + + public static Avatar parseMetadata(IqPacket packet) { + return parseMetadata(PubSubPacketParser.findItems(packet)); + } } diff --git a/src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarVcardPacketGenerator.java b/src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarVcardPacketGenerator.java new file mode 100644 index 00000000..375702df --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarVcardPacketGenerator.java @@ -0,0 +1,16 @@ +package de.thedevstack.conversationsplus.xmpp.avatar; + +import de.thedevstack.conversationsplus.xmpp.pep.Avatar; +import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; + +/** + * + */ +public class AvatarVcardPacketGenerator { + public static IqPacket generateRetreivePacket(Avatar avatar) { + final IqPacket packet = new IqPacket(IqPacket.TYPE.GET); + packet.setTo(avatar.owner); + packet.addChild("vCard", "vcard-temp"); + return packet; + } +} diff --git a/src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarVcardParser.java b/src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarVcardParser.java new file mode 100644 index 00000000..32ad6f26 --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarVcardParser.java @@ -0,0 +1,29 @@ +package de.thedevstack.conversationsplus.xmpp.avatar; + +import de.thedevstack.conversationsplus.xml.Element; +import de.thedevstack.conversationsplus.xmpp.pep.Avatar; +import de.thedevstack.conversationsplus.xmpp.stanzas.PresencePacket; + +/** + */ +public final class AvatarVcardParser { + private AvatarVcardParser() {} + + public static Avatar parseVcardPresenceInformation(PresencePacket packet) { + return AvatarVcardParser.parsePresence(packet.findChild("x", "vcard-temp:x:update")); + } + + public static Avatar parsePresence(Element x) { + String hash = (x == null) ? null : x.findChildContent("photo"); + if (hash == null) { + return null; + } + if (!Avatar.isValidSHA1(hash)) { + return null; + } + Avatar avatar = new Avatar(); + avatar.sha1sum = hash; + avatar.origin = Avatar.Origin.VCARD; + return avatar; + } +} |