package de.thedevstack.conversationsplus.xmpp.avatar; import de.thedevstack.conversationsplus.xmpp.pubsub.PubSubPacketGenerator; import de.thedevstack.conversationsplus.xml.Element; import de.thedevstack.conversationsplus.xmpp.jid.Jid; import de.thedevstack.conversationsplus.dto.Avatar; import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; /** * Generates the IQ Packets for handling Avatars * as defined in XEP-0084. * @see http://xmpp.org/extensions/xep-0084.html */ public final class AvatarPacketGenerator { /** * Generates an IqPacket for publishing avatar data. * The attributes from and id are not set in here - this is added while sending the packet. *
     *     
     *          
     *              
     *                  
     *                      
     *                      qANQR1DBwU4DX7jmYZnncm...
     *                      
     *                  
     *              
     *          
     *      
     * 
* @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", AvatarPacket.NAMESPACE_AVATAR_DATA); data.setContent(avatar.image); return PubSubPacketGenerator.generatePubSubPublishPacket(AvatarPacket.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. *
     *     
     *       
     *           
     *              
     *           
     *       
     *      
     * 
* @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(AvatarPacket.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. *
     *     
     *          
     *              
     *                  
     *                      
     *                          
     *                      
     *                  
     *              
     *          
     *      
     * 
* @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", AvatarPacket.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(AvatarPacket.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(AvatarPacket.NAMESPACE_AVATAR_METADATA, null); if (to != null) { packet.setTo(to); } return packet; } /** * Helper class - private constructor to avoid instantiation */ private AvatarPacketGenerator() { // avoid instantiation } }