Search in sources :

Example 21 with XmlElement

use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.

the class PacketParserUtils method addExtensionElement.

public static void addExtensionElement(Collection<XmlElement> collection, XmlPullParser parser, String elementName, String namespace, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    XmlElement packetExtension = parseExtensionElement(elementName, namespace, parser, outerXmlEnvironment);
    collection.add(packetExtension);
}
Also used : XmlElement(org.jivesoftware.smack.packet.XmlElement)

Example 22 with XmlElement

use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.

the class XmppElementUtil method getElementsFrom.

public static <E extends ExtensionElement> List<E> getElementsFrom(MultiMap<QName, XmlElement> elementMap, Class<E> extensionElementClass) {
    QName qname = XmppElementUtil.getQNameFor(extensionElementClass);
    List<XmlElement> extensionElements = elementMap.getAll(qname);
    if (extensionElements.isEmpty()) {
        return Collections.emptyList();
    }
    List<E> res = new ArrayList<>(extensionElements.size());
    for (XmlElement extensionElement : extensionElements) {
        E e = castOrThrow(extensionElement, extensionElementClass);
        res.add(e);
    }
    return res;
}
Also used : QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) XmlElement(org.jivesoftware.smack.packet.XmlElement)

Example 23 with XmlElement

use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.

the class Workgroup method handlePacket.

// PacketListener Implementation.
private void handlePacket(Stanza packet) {
    if (packet instanceof Message) {
        Message msg = (Message) packet;
        // Check to see if the user left the queue.
        XmlElement pe = msg.getExtensionElement("depart-queue", "http://jabber.org/protocol/workgroup");
        XmlElement queueStatus = msg.getExtensionElement("queue-status", "http://jabber.org/protocol/workgroup");
        if (pe != null) {
            fireQueueDepartedEvent();
        } else if (queueStatus != null) {
            QueueUpdate queueUpdate = (QueueUpdate) queueStatus;
            if (queueUpdate.getPosition() != -1) {
                fireQueuePositionEvent(queueUpdate.getPosition());
            }
            if (queueUpdate.getRemaingTime() != -1) {
                fireQueueTimeEvent(queueUpdate.getRemaingTime());
            }
        } else {
            // Check if a room invitation was sent and if the sender is the workgroup
            MUCUser mucUser = MUCUser.from(msg);
            MUCUser.Invite invite = mucUser != null ? mucUser.getInvite() : null;
            if (invite != null && workgroupJID.equals(invite.getFrom())) {
                String sessionID = null;
                Map<String, List<String>> metaData = null;
                pe = msg.getExtensionElement(SessionID.ELEMENT_NAME, SessionID.NAMESPACE);
                if (pe != null) {
                    sessionID = ((SessionID) pe).getSessionID();
                }
                pe = msg.getExtensionElement(MetaData.ELEMENT_NAME, MetaData.NAMESPACE);
                if (pe != null) {
                    metaData = ((MetaData) pe).getMetaData();
                }
                WorkgroupInvitation inv = new WorkgroupInvitation(connection.getUser(), msg.getFrom(), workgroupJID, sessionID, msg.getBody(), msg.getFrom(), metaData);
                fireInvitationEvent(inv);
            }
        }
    }
}
Also used : MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) Message(org.jivesoftware.smack.packet.Message) MetaData(org.jivesoftware.smackx.workgroup.MetaData) WorkgroupInvitation(org.jivesoftware.smackx.workgroup.WorkgroupInvitation) XmlElement(org.jivesoftware.smack.packet.XmlElement) QueueUpdate(org.jivesoftware.smackx.workgroup.packet.QueueUpdate) Map(java.util.Map) SessionID(org.jivesoftware.smackx.workgroup.packet.SessionID)

Example 24 with XmlElement

use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.

the class EligibleForChatMarkerFilter method accept.

/**
 * From XEP-0333, Protocol Format: The Chat Marker MUST have an 'id' which is the 'id' of the
 * message being marked.<br>
 * In order to make Chat Markers works together with XEP-0085 as it said in
 * 8.5 Interaction with Chat States, only messages with <code>active</code> chat
 * state are accepted.
 *
 * @param message to be analyzed.
 * @return true if the message contains a stanza Id.
 * @see <a href="http://xmpp.org/extensions/xep-0333.html">XEP-0333: Chat Markers</a>
 */
@Override
public boolean accept(Stanza message) {
    if (!message.hasStanzaIdSet()) {
        return false;
    }
    if (super.accept(message)) {
        XmlElement extension = message.getExtension(ChatStateManager.NAMESPACE);
        String chatStateElementName = extension.getElementName();
        ChatState state;
        try {
            state = ChatState.valueOf(chatStateElementName);
            return state == ChatState.active;
        } catch (Exception ex) {
            return false;
        }
    }
    return true;
}
Also used : ChatState(org.jivesoftware.smackx.chatstates.ChatState) XmlElement(org.jivesoftware.smack.packet.XmlElement)

Example 25 with XmlElement

use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.

the class RegistrationProvider method parse.

@Override
public Registration parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    String instruction = null;
    Map<String, String> fields = new HashMap<>();
    List<XmlElement> packetExtensions = new LinkedList<>();
    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        if (eventType == XmlPullParser.Event.START_ELEMENT) {
            // attempt to parse it if it's in the form <name>value</name>.
            if (parser.getNamespace().equals(Registration.NAMESPACE)) {
                String name = parser.getName();
                String value = "";
                if (parser.next() == XmlPullParser.Event.TEXT_CHARACTERS) {
                    value = parser.getText();
                }
                // Ignore instructions, but anything else should be added to the map.
                if (!name.equals("instructions")) {
                    fields.put(name, value);
                } else {
                    instruction = value;
                }
            } else // Otherwise, it must be a packet extension.
            {
                PacketParserUtils.addExtensionElement(packetExtensions, parser, xmlEnvironment);
            }
        } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
            if (parser.getName().equals(IQ.QUERY_ELEMENT)) {
                break outerloop;
            }
        }
    }
    Registration registration = new Registration(instruction, fields);
    registration.addExtensions(packetExtensions);
    return registration;
}
Also used : HashMap(java.util.HashMap) Registration(org.jivesoftware.smackx.iqregister.packet.Registration) XmlElement(org.jivesoftware.smack.packet.XmlElement) LinkedList(java.util.LinkedList)

Aggregations

XmlElement (org.jivesoftware.smack.packet.XmlElement)28 XmlPullParser (org.jivesoftware.smack.xml.XmlPullParser)6 Test (org.junit.jupiter.api.Test)5 NamedElement (org.jivesoftware.smack.packet.NamedElement)4 Stanza (org.jivesoftware.smack.packet.Stanza)4 XmlEnvironment (org.jivesoftware.smack.packet.XmlEnvironment)4 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 QName (javax.xml.namespace.QName)2 Message (org.jivesoftware.smack.packet.Message)2 MessageBuilder (org.jivesoftware.smack.packet.MessageBuilder)2 JingleReason (org.jivesoftware.smackx.jingle.element.JingleReason)2 PubSub (org.jivesoftware.smackx.pubsub.packet.PubSub)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)1 Mechanisms (org.jivesoftware.smack.packet.Mechanisms)1