Search in sources :

Example 36 with Jid

use of org.jxmpp.jid.Jid in project Smack by igniterealtime.

the class UnblockContactsIQTest method checkUnblockContactIQStanza.

@Test
public void checkUnblockContactIQStanza() throws Exception {
    List<Jid> jids = new ArrayList<>();
    jids.add(JidCreate.from("romeo@montague.net"));
    jids.add(JidCreate.from("pepe@montague.net"));
    UnblockContactsIQ unblockContactIQ = new UnblockContactsIQ(jids);
    unblockContactIQ.setStanzaId("unblock1");
    Assert.assertEquals(unblockContactIQExample, unblockContactIQ.toXML().toString());
}
Also used : Jid(org.jxmpp.jid.Jid) ArrayList(java.util.ArrayList) UnblockContactsIQ(org.jivesoftware.smackx.blocking.element.UnblockContactsIQ) Test(org.junit.Test)

Example 37 with Jid

use of org.jxmpp.jid.Jid in project Smack by igniterealtime.

the class PacketParserUtils method parseIQ.

/**
     * Parses an IQ packet.
     *
     * @param parser the XML parser, positioned at the start of an IQ packet.
     * @return an IQ object.
     * @throws Exception
     */
public static IQ parseIQ(XmlPullParser parser) throws Exception {
    ParserUtils.assertAtStartTag(parser);
    final int initialDepth = parser.getDepth();
    IQ iqPacket = null;
    XMPPError.Builder error = null;
    final String id = parser.getAttributeValue("", "id");
    final Jid to = ParserUtils.getJidAttribute(parser, "to");
    final Jid from = ParserUtils.getJidAttribute(parser, "from");
    final IQ.Type type = IQ.Type.fromString(parser.getAttributeValue("", "type"));
    outerloop: while (true) {
        int eventType = parser.next();
        switch(eventType) {
            case XmlPullParser.START_TAG:
                String elementName = parser.getName();
                String namespace = parser.getNamespace();
                switch(elementName) {
                    case "error":
                        error = PacketParserUtils.parseError(parser);
                        break;
                    // this element name and namespace.
                    default:
                        IQProvider<IQ> provider = ProviderManager.getIQProvider(elementName, namespace);
                        if (provider != null) {
                            iqPacket = provider.parse(parser);
                        } else // Note that if we reach this code, it is guranteed that the result IQ contained a child element
                        // (RFC 6120 ยง 8.2.3 6) because otherwhise we would have reached the END_TAG first.
                        {
                            // No Provider found for the IQ stanza, parse it to an UnparsedIQ instance
                            // so that the content of the IQ can be examined later on
                            iqPacket = new UnparsedIQ(elementName, namespace, parseElement(parser));
                        }
                        break;
                }
                break;
            case XmlPullParser.END_TAG:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
        }
    }
    // Decide what to do when an IQ packet was not understood
    if (iqPacket == null) {
        switch(type) {
            case error:
                // If an IQ packet wasn't created above, create an empty error IQ packet.
                iqPacket = new ErrorIQ(error);
                break;
            case result:
                iqPacket = new EmptyResultIQ();
                break;
            default:
                break;
        }
    }
    // Set basic values on the iq packet.
    iqPacket.setStanzaId(id);
    iqPacket.setTo(to);
    iqPacket.setFrom(from);
    iqPacket.setType(type);
    iqPacket.setError(error);
    return iqPacket;
}
Also used : ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) Jid(org.jxmpp.jid.Jid) UnparsedIQ(org.jivesoftware.smack.packet.UnparsedIQ) EmptyResultIQ(org.jivesoftware.smack.packet.EmptyResultIQ) ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) UnparsedIQ(org.jivesoftware.smack.packet.UnparsedIQ) IQ(org.jivesoftware.smack.packet.IQ) EmptyResultIQ(org.jivesoftware.smack.packet.EmptyResultIQ) XMPPError(org.jivesoftware.smack.packet.XMPPError) IQProvider(org.jivesoftware.smack.provider.IQProvider)

Example 38 with Jid

use of org.jxmpp.jid.Jid in project Smack by igniterealtime.

the class ParserUtils method getEntityJidAttribute.

public static EntityJid getEntityJidAttribute(XmlPullParser parser, String name) throws XmppStringprepException {
    final String jidString = parser.getAttributeValue("", name);
    if (jidString == null) {
        return null;
    }
    Jid jid = JidCreate.from(jidString);
    if (!jid.hasLocalpart())
        return null;
    EntityFullJid fullJid = jid.asEntityFullJidIfPossible();
    if (fullJid != null) {
        return fullJid;
    }
    EntityBareJid bareJid = jid.asEntityBareJidIfPossible();
    return bareJid;
}
Also used : Jid(org.jxmpp.jid.Jid) EntityJid(org.jxmpp.jid.EntityJid) EntityFullJid(org.jxmpp.jid.EntityFullJid) EntityBareJid(org.jxmpp.jid.EntityBareJid) EntityFullJid(org.jxmpp.jid.EntityFullJid) EntityBareJid(org.jxmpp.jid.EntityBareJid)

Example 39 with Jid

use of org.jxmpp.jid.Jid in project Smack by igniterealtime.

the class Stanza method setTo.

/**
     * Sets who the stanza(/packet) is being sent "to". The XMPP protocol often makes
     * the "to" attribute optional, so it does not always need to be set.
     *
     * @param to who the stanza(/packet) is being sent to.
     * @throws IllegalArgumentException if to is not a valid JID String.
     * @deprecated use {@link #setTo(Jid)} instead.
     */
@Deprecated
public void setTo(String to) {
    Jid jid;
    try {
        jid = JidCreate.from(to);
    } catch (XmppStringprepException e) {
        throw new IllegalArgumentException(e);
    }
    setTo(jid);
}
Also used : Jid(org.jxmpp.jid.Jid) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException)

Example 40 with Jid

use of org.jxmpp.jid.Jid in project Smack by igniterealtime.

the class MUCLightChangeAffiliationsIQTest method checkChangeAffiliationsMUCLightStanza.

@Test
public void checkChangeAffiliationsMUCLightStanza() throws Exception {
    HashMap<Jid, MUCLightAffiliation> affiliations = new HashMap<>();
    affiliations.put(JidCreate.from("sarasa2@shakespeare.lit"), MUCLightAffiliation.owner);
    affiliations.put(JidCreate.from("sarasa1@shakespeare.lit"), MUCLightAffiliation.member);
    affiliations.put(JidCreate.from("sarasa3@shakespeare.lit"), MUCLightAffiliation.none);
    MUCLightChangeAffiliationsIQ mucLightChangeAffiliationsIQ = new MUCLightChangeAffiliationsIQ(JidCreate.from("coven@muclight.shakespeare.lit"), affiliations);
    mucLightChangeAffiliationsIQ.setStanzaId("member1");
    Assert.assertEquals(mucLightChangeAffiliationsIQ.getTo(), "coven@muclight.shakespeare.lit");
    Assert.assertEquals(mucLightChangeAffiliationsIQ.getType(), IQ.Type.set);
    HashMap<Jid, MUCLightAffiliation> iqAffiliations = mucLightChangeAffiliationsIQ.getAffiliations();
    Assert.assertEquals(iqAffiliations.get(JidCreate.from("sarasa1@shakespeare.lit")), MUCLightAffiliation.member);
    Assert.assertEquals(iqAffiliations.get(JidCreate.from("sarasa2@shakespeare.lit")), MUCLightAffiliation.owner);
    Assert.assertEquals(iqAffiliations.get(JidCreate.from("sarasa3@shakespeare.lit")), MUCLightAffiliation.none);
}
Also used : Jid(org.jxmpp.jid.Jid) HashMap(java.util.HashMap) MUCLightChangeAffiliationsIQ(org.jivesoftware.smackx.muclight.element.MUCLightChangeAffiliationsIQ) Test(org.junit.Test)

Aggregations

Jid (org.jxmpp.jid.Jid)78 EntityBareJid (org.jxmpp.jid.EntityBareJid)18 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)14 Test (org.junit.Test)14 DomainBareJid (org.jxmpp.jid.DomainBareJid)14 EntityFullJid (org.jxmpp.jid.EntityFullJid)12 BareJid (org.jxmpp.jid.BareJid)11 IQ (org.jivesoftware.smack.packet.IQ)7 Presence (org.jivesoftware.smack.packet.Presence)7 MUCLightBlockingIQ (org.jivesoftware.smackx.muclight.element.MUCLightBlockingIQ)7 SmackException (org.jivesoftware.smack.SmackException)6 Message (org.jivesoftware.smack.packet.Message)6 WeakHashMap (java.util.WeakHashMap)4 XMPPConnection (org.jivesoftware.smack.XMPPConnection)4 Map (java.util.Map)3 SimpleResultSyncPoint (org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint)3 FeatureNotSupportedException (org.jivesoftware.smack.SmackException.FeatureNotSupportedException)3 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)3 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)3