aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarPacketParser.java
blob: e9f908989237cd6b96d0bc7dbc6cd3dde46d412d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package de.thedevstack.conversationsplus.xmpp.avatar;

import de.thedevstack.conversationsplus.dto.Avatar;
import de.thedevstack.conversationsplus.xmpp.pubsub.PubSubPacketParser;
import de.thedevstack.conversationsplus.xml.Element;
import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket;

/**
 * Parses the IQ Packets for handling Avatars
 * as defined in XEP-0084.
 * @see <a href="http://xmpp.org/extensions/xep-0084.html">http://xmpp.org/extensions/xep-0084.html</a>
 */
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", AvatarPacket.NAMESPACE_AVATAR_DATA);
            }
        }
        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));
    }
}