Search in sources :

Example 1 with StandardExtensionElement

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

the class StandardExtensionElementParserTest method buildAndParse.

@Test
public void buildAndParse() throws Exception {
    Builder builder = StandardExtensionElement.builder("foo", "ns1");
    builder.addAttribute("attr1", "attr1-value");
    builder.addElement(StandardExtensionElement.builder("bar", "ns2").addAttribute("attr2", "attr2-value").build());
    builder.addElement("another-element", "another-element-text");
    final String elementString = builder.build().toXML().toString();
    StandardExtensionElement parsedElement = StandardExtensionElementProvider.INSTANCE.parse(getParserFor(elementString));
    assertEquals("foo", parsedElement.getElementName());
    assertEquals("ns1", parsedElement.getNamespace());
    assertEquals("attr1-value", parsedElement.getAttributeValue("attr1"));
    StandardExtensionElement barNs2Element = parsedElement.getFirstElement("bar", "ns2");
    assertEquals("bar", barNs2Element.getElementName());
    assertEquals("ns2", barNs2Element.getNamespace());
    assertEquals("attr2-value", barNs2Element.getAttributeValue("attr2"));
    assertEquals("another-element-text", parsedElement.getFirstElement("another-element").getText());
}
Also used : Builder(org.jivesoftware.smack.packet.StandardExtensionElement.Builder) StandardExtensionElement(org.jivesoftware.smack.packet.StandardExtensionElement) Test(org.junit.Test)

Example 2 with StandardExtensionElement

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

the class StandardExtensionElementParserTest method buildWithAttrNamespacesAndParse.

@Test
public void buildWithAttrNamespacesAndParse() throws Exception {
    Builder builder = StandardExtensionElement.builder("foo", "ns1-value");
    builder.addAttribute("xmlns:ns2", "ns2-value");
    builder.addAttribute("ns2:bar", "bar-ns2-value");
    final String elementString = builder.build().toXML().toString();
    StandardExtensionElement parsedElement = StandardExtensionElementProvider.INSTANCE.parse(getParserFor(elementString));
    assertEquals("foo", parsedElement.getElementName());
    assertEquals("ns1-value", parsedElement.getNamespace());
    String barNs2Value = parsedElement.getAttributeValue("ns2:bar");
    assertEquals("bar-ns2-value", barNs2Value);
    String ns2Value = parsedElement.getAttributeValue("xmlns:ns2");
    assertEquals("ns2-value", ns2Value);
}
Also used : Builder(org.jivesoftware.smack.packet.StandardExtensionElement.Builder) StandardExtensionElement(org.jivesoftware.smack.packet.StandardExtensionElement) Test(org.junit.Test)

Example 3 with StandardExtensionElement

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

the class AgentSession method setOnline.

/**
     * Sets whether the agent is online with the workgroup. If the user tries to go online with
     * the workgroup but is not allowed to be an agent, an XMPPError with error code 401 will
     * be thrown.
     *
     * @param online true to set the agent as online with the workgroup.
     * @throws XMPPException if an error occurs setting the online status.
     * @throws SmackException             assertEquals(SmackException.Type.NO_RESPONSE_FROM_SERVER, e.getType());
            return;
     * @throws InterruptedException 
     */
public void setOnline(boolean online) throws XMPPException, SmackException, InterruptedException {
    // If the online status hasn't changed, do nothing.
    if (this.online == online) {
        return;
    }
    Presence presence;
    // If the user is going online...
    if (online) {
        presence = new Presence(Presence.Type.available);
        presence.setTo(workgroupJID);
        presence.addExtension(new StandardExtensionElement(AgentStatus.ELEMENT_NAME, AgentStatus.NAMESPACE));
        StanzaCollector collector = this.connection.createStanzaCollectorAndSend(new AndFilter(new StanzaTypeFilter(Presence.class), FromMatchesFilter.create(workgroupJID)), presence);
        presence = (Presence) collector.nextResultOrThrow();
        // We can safely update this iv since we didn't get any error
        this.online = online;
    } else // Otherwise the user is going offline...
    {
        // Update this iv now since we don't care at this point of any error
        this.online = online;
        presence = new Presence(Presence.Type.unavailable);
        presence.setTo(workgroupJID);
        presence.addExtension(new StandardExtensionElement(AgentStatus.ELEMENT_NAME, AgentStatus.NAMESPACE));
        connection.sendStanza(presence);
    }
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) StandardExtensionElement(org.jivesoftware.smack.packet.StandardExtensionElement) Presence(org.jivesoftware.smack.packet.Presence) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 4 with StandardExtensionElement

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

the class AgentSession method handlePacket.

// PacketListener Implementation.
private void handlePacket(Stanza packet) {
    if (packet instanceof Presence) {
        Presence presence = (Presence) packet;
        // The workgroup can send us a number of different presence packets. We
        // check for different packet extensions to see what type of presence
        // packet it is.
        Resourcepart queueName = presence.getFrom().getResourceOrNull();
        WorkgroupQueue queue = queues.get(queueName);
        // If there isn't already an entry for the queue, create a new one.
        if (queue == null) {
            queue = new WorkgroupQueue(queueName);
            queues.put(queueName, queue);
        }
        // QueueOverview packet extensions contain basic information about a queue.
        QueueOverview queueOverview = (QueueOverview) presence.getExtension(QueueOverview.ELEMENT_NAME, QueueOverview.NAMESPACE);
        if (queueOverview != null) {
            if (queueOverview.getStatus() == null) {
                queue.setStatus(WorkgroupQueue.Status.CLOSED);
            } else {
                queue.setStatus(queueOverview.getStatus());
            }
            queue.setAverageWaitTime(queueOverview.getAverageWaitTime());
            queue.setOldestEntry(queueOverview.getOldestEntry());
            // Fire event.
            fireQueueUsersEvent(queue, queueOverview.getStatus(), queueOverview.getAverageWaitTime(), queueOverview.getOldestEntry(), null);
            return;
        }
        // QueueDetails packet extensions contain information about the users in
        // a queue.
        QueueDetails queueDetails = (QueueDetails) packet.getExtension(QueueDetails.ELEMENT_NAME, QueueDetails.NAMESPACE);
        if (queueDetails != null) {
            queue.setUsers(queueDetails.getUsers());
            // Fire event.
            fireQueueUsersEvent(queue, null, -1, null, queueDetails.getUsers());
            return;
        }
        // Notify agent packets gives an overview of agent activity in a queue.
        StandardExtensionElement notifyAgents = presence.getExtension("notify-agents", "http://jabber.org/protocol/workgroup");
        if (notifyAgents != null) {
            int currentChats = Integer.parseInt(notifyAgents.getFirstElement("current-chats", "http://jabber.org/protocol/workgroup").getText());
            int maxChats = Integer.parseInt(notifyAgents.getFirstElement("max-chats", "http://jabber.org/protocol/workgroup").getText());
            queue.setCurrentChats(currentChats);
            queue.setMaxChats(maxChats);
            // TODO: might need another event for current chats and max chats of queue
            return;
        }
    } else if (packet instanceof Message) {
        Message message = (Message) packet;
        // Check if a room invitation was sent and if the sender is the workgroup
        MUCUser mucUser = (MUCUser) message.getExtension("x", "http://jabber.org/protocol/muc#user");
        MUCUser.Invite invite = mucUser != null ? mucUser.getInvite() : null;
        if (invite != null && workgroupJID.equals(invite.getFrom())) {
            String sessionID = null;
            Map<String, List<String>> metaData = null;
            SessionID sessionIDExt = (SessionID) message.getExtension(SessionID.ELEMENT_NAME, SessionID.NAMESPACE);
            if (sessionIDExt != null) {
                sessionID = sessionIDExt.getSessionID();
            }
            MetaData metaDataExt = (MetaData) message.getExtension(MetaData.ELEMENT_NAME, MetaData.NAMESPACE);
            if (metaDataExt != null) {
                metaData = metaDataExt.getMetaData();
            }
            this.fireInvitationEvent(message.getFrom(), sessionID, message.getBody(), message.getFrom(), metaData);
        }
    }
}
Also used : MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) Message(org.jivesoftware.smack.packet.Message) QueueOverview(org.jivesoftware.smackx.workgroup.packet.QueueOverview) Resourcepart(org.jxmpp.jid.parts.Resourcepart) QueueDetails(org.jivesoftware.smackx.workgroup.packet.QueueDetails) MetaData(org.jivesoftware.smackx.workgroup.MetaData) StandardExtensionElement(org.jivesoftware.smack.packet.StandardExtensionElement) Presence(org.jivesoftware.smack.packet.Presence) Map(java.util.Map) HashMap(java.util.HashMap) SessionID(org.jivesoftware.smackx.workgroup.packet.SessionID)

Aggregations

StandardExtensionElement (org.jivesoftware.smack.packet.StandardExtensionElement)4 Presence (org.jivesoftware.smack.packet.Presence)2 Builder (org.jivesoftware.smack.packet.StandardExtensionElement.Builder)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 StanzaCollector (org.jivesoftware.smack.StanzaCollector)1 AndFilter (org.jivesoftware.smack.filter.AndFilter)1 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)1 Message (org.jivesoftware.smack.packet.Message)1 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)1 MetaData (org.jivesoftware.smackx.workgroup.MetaData)1 QueueDetails (org.jivesoftware.smackx.workgroup.packet.QueueDetails)1 QueueOverview (org.jivesoftware.smackx.workgroup.packet.QueueOverview)1 SessionID (org.jivesoftware.smackx.workgroup.packet.SessionID)1 Resourcepart (org.jxmpp.jid.parts.Resourcepart)1