Search in sources :

Example 1 with PresenceBuilder

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

the class AgentSession method setStatus.

/**
 * Sets the agent's current status with the workgroup. The presence mode affects how offers
 * are routed to the agent. The possible presence modes with their meanings are as follows:<ul>
 *
 * <li>Presence.Mode.AVAILABLE -- (Default) the agent is available for more chats
 * (equivalent to Presence.Mode.CHAT).
 * <li>Presence.Mode.DO_NOT_DISTURB -- the agent is busy and should not be disturbed.
 * However, special case, or extreme urgency chats may still be offered to the agent.
 * <li>Presence.Mode.AWAY -- the agent is not available and should not
 * have a chat routed to them (equivalent to Presence.Mode.EXTENDED_AWAY).</ul>
 *
 * The max chats value is the maximum number of chats the agent is willing to have routed to
 * them at once. Some servers may be configured to only accept max chat values in a certain
 * range; for example, between two and five. In that case, the maxChats value the agent sends
 * may be adjusted by the server to a value within that range.
 *
 * @param presenceMode the presence mode of the agent.
 * @param maxChats     the maximum number of chats the agent is willing to accept.
 * @param status       sets the status message of the presence update.
 * @throws XMPPErrorException if there was an XMPP error returned.
 * @throws NoResponseException if there was no response from the remote entity.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws IllegalStateException if the agent is not online with the workgroup.
 */
public void setStatus(Presence.Mode presenceMode, int maxChats, String status) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    if (!online) {
        throw new IllegalStateException("Cannot set status when the agent is not online.");
    }
    if (presenceMode == null) {
        presenceMode = Presence.Mode.available;
    }
    this.presenceMode = presenceMode;
    this.maxChats = maxChats;
    PresenceBuilder presenceBuilder = connection.getStanzaFactory().buildPresenceStanza().ofType(Presence.Type.available).setMode(presenceMode).to(workgroupJID).setStatus(status);
    // Send information about max chats and current chats as a packet extension.
    StandardExtensionElement.Builder builder = StandardExtensionElement.builder(AgentStatus.ELEMENT_NAME, AgentStatus.NAMESPACE);
    builder.addElement("max_chats", Integer.toString(maxChats));
    presenceBuilder.addExtension(builder.build());
    presenceBuilder.addExtension(new MetaData(this.metaData));
    Presence presence = presenceBuilder.build();
    StanzaCollector collector = this.connection.createStanzaCollectorAndSend(new AndFilter(new StanzaTypeFilter(Presence.class), FromMatchesFilter.create(workgroupJID)), presence);
    collector.nextResultOrThrow();
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) MetaData(org.jivesoftware.smackx.workgroup.MetaData) PresenceBuilder(org.jivesoftware.smack.packet.PresenceBuilder) StandardExtensionElement(org.jivesoftware.smack.packet.StandardExtensionElement) Presence(org.jivesoftware.smack.packet.Presence) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 2 with PresenceBuilder

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

the class PacketParserUtils method parsePresence.

/**
 * Parses a presence packet.
 *
 * @param parser the XML parser, positioned at the start of a presence packet.
 * @param outerXmlEnvironment the outer XML environment (optional).
 * @return a Presence packet.
 * @throws IOException if an I/O error occurred.
 * @throws XmlPullParserException if an error in the XML parser occurred.
 * @throws SmackParsingException if the Smack parser (provider) encountered invalid input.
 */
public static Presence parsePresence(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    ParserUtils.assertAtStartTag(parser);
    final int initialDepth = parser.getDepth();
    XmlEnvironment presenceXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
    PresenceBuilder presence = parseCommonStanzaAttributes(stanzaId -> StanzaBuilder.buildPresence(stanzaId), parser, outerXmlEnvironment);
    Presence.Type type = Presence.Type.available;
    String typeString = parser.getAttributeValue("", "type");
    if (typeString != null && !typeString.equals("")) {
        type = Presence.Type.fromString(typeString);
    }
    presence.ofType(type);
    // Parse sub-elements
    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch(eventType) {
            case START_ELEMENT:
                String elementName = parser.getName();
                String namespace = parser.getNamespace();
                switch(elementName) {
                    case "status":
                        presence.setStatus(parser.nextText());
                        break;
                    case "priority":
                        Byte priority = ParserUtils.getByteAttributeFromNextText(parser);
                        presence.setPriority(priority);
                        break;
                    case "show":
                        String modeText = parser.nextText();
                        if (StringUtils.isNotEmpty(modeText)) {
                            presence.setMode(Presence.Mode.fromString(modeText));
                        } else {
                            // Some implementations send presence stanzas with a
                            // '<show />' element, which is a invalid XMPP presence
                            // stanza according to RFC 6121 4.7.2.1
                            LOGGER.warning("Empty or null mode text in presence show element form " + presence + "' which is invalid according to RFC6121 4.7.2.1");
                        }
                        break;
                    case "error":
                        presence.setError(parseError(parser, presenceXmlEnvironment));
                        break;
                    default:
                        // failing completely here. See SMACK-390 for more information.
                        try {
                            XmlElement extensionElement = parseExtensionElement(elementName, namespace, parser, presenceXmlEnvironment);
                            presence.addExtension(extensionElement);
                        } catch (Exception e) {
                            LOGGER.log(Level.WARNING, "Failed to parse extension element in Presence stanza: " + presence, e);
                        }
                        break;
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
    return presence.build();
}
Also used : PresenceBuilder(org.jivesoftware.smack.packet.PresenceBuilder) Presence(org.jivesoftware.smack.packet.Presence) XmlElement(org.jivesoftware.smack.packet.XmlElement) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException) SmackParsingException(org.jivesoftware.smack.parsing.SmackParsingException) IOException(java.io.IOException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) XmlEnvironment(org.jivesoftware.smack.packet.XmlEnvironment)

Example 3 with PresenceBuilder

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

the class AgentSession method setStatus.

/**
 * Sets the agent's current status with the workgroup. The presence mode affects how offers
 * are routed to the agent. The possible presence modes with their meanings are as follows:<ul>
 *
 * <li>Presence.Mode.AVAILABLE -- (Default) the agent is available for more chats
 * (equivalent to Presence.Mode.CHAT).
 * <li>Presence.Mode.DO_NOT_DISTURB -- the agent is busy and should not be disturbed.
 * However, special case, or extreme urgency chats may still be offered to the agent.
 * <li>Presence.Mode.AWAY -- the agent is not available and should not
 * have a chat routed to them (equivalent to Presence.Mode.EXTENDED_AWAY).</ul>
 *
 * @param presenceMode the presence mode of the agent.
 * @param status       sets the status message of the presence update.
 * @throws XMPPErrorException if there was an XMPP error returned.
 * @throws NoResponseException if there was no response from the remote entity.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws IllegalStateException if the agent is not online with the workgroup.
 */
public void setStatus(Presence.Mode presenceMode, String status) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    if (!online) {
        throw new IllegalStateException("Cannot set status when the agent is not online.");
    }
    if (presenceMode == null) {
        presenceMode = Presence.Mode.available;
    }
    this.presenceMode = presenceMode;
    PresenceBuilder presenceBuilder = connection.getStanzaFactory().buildPresenceStanza().ofType(Presence.Type.available).setMode(presenceMode).to(getWorkgroupJID());
    if (status != null) {
        presenceBuilder.setStatus(status);
    }
    Presence presence = presenceBuilder.build();
    presence.addExtension(new MetaData(this.metaData));
    StanzaCollector collector = this.connection.createStanzaCollectorAndSend(new AndFilter(new StanzaTypeFilter(Presence.class), FromMatchesFilter.create(workgroupJID)), presence);
    collector.nextResultOrThrow();
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) MetaData(org.jivesoftware.smackx.workgroup.MetaData) PresenceBuilder(org.jivesoftware.smack.packet.PresenceBuilder) Presence(org.jivesoftware.smack.packet.Presence) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Aggregations

Presence (org.jivesoftware.smack.packet.Presence)3 PresenceBuilder (org.jivesoftware.smack.packet.PresenceBuilder)3 StanzaCollector (org.jivesoftware.smack.StanzaCollector)2 AndFilter (org.jivesoftware.smack.filter.AndFilter)2 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)2 MetaData (org.jivesoftware.smackx.workgroup.MetaData)2 IOException (java.io.IOException)1 StandardExtensionElement (org.jivesoftware.smack.packet.StandardExtensionElement)1 XmlElement (org.jivesoftware.smack.packet.XmlElement)1 XmlEnvironment (org.jivesoftware.smack.packet.XmlEnvironment)1 SmackParsingException (org.jivesoftware.smack.parsing.SmackParsingException)1 XmlPullParserException (org.jivesoftware.smack.xml.XmlPullParserException)1 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)1