Search in sources :

Example 11 with Jid

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

the class RosterExchangeProvider method parse.

/**
     * Parses a RosterExchange stanza(/packet) (extension sub-packet).
     *
     * @param parser the XML parser, positioned at the starting element of the extension.
     * @return a PacketExtension.
     * @throws IOException 
     * @throws XmlPullParserException 
     */
@Override
public RosterExchange parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
    // CHECKSTYLE:OFF
    RosterExchange rosterExchange = new RosterExchange();
    boolean done = false;
    RemoteRosterEntry remoteRosterEntry = null;
    Jid jid = null;
    String name = "";
    ArrayList<String> groupsName = new ArrayList<String>();
    while (!done) {
        int eventType = parser.next();
        if (eventType == XmlPullParser.START_TAG) {
            if (parser.getName().equals("item")) {
                // Reset this variable since they are optional for each item
                groupsName = new ArrayList<String>();
                // Initialize the variables from the parsed XML
                jid = ParserUtils.getJidAttribute(parser);
                name = parser.getAttributeValue("", "name");
            }
            if (parser.getName().equals("group")) {
                groupsName.add(parser.nextText());
            }
        } else if (eventType == XmlPullParser.END_TAG) {
            if (parser.getName().equals("item")) {
                // Create packet.
                remoteRosterEntry = new RemoteRosterEntry(jid, name, groupsName.toArray(new String[groupsName.size()]));
                rosterExchange.addRosterEntry(remoteRosterEntry);
            }
            if (parser.getName().equals("x")) {
                done = true;
            }
        }
    }
    // CHECKSTYLE:ON
    return rosterExchange;
}
Also used : RosterExchange(org.jivesoftware.smackx.xroster.packet.RosterExchange) Jid(org.jxmpp.jid.Jid) ArrayList(java.util.ArrayList) RemoteRosterEntry(org.jivesoftware.smackx.xroster.RemoteRosterEntry)

Example 12 with Jid

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

the class Chat method send.

public void send(Message message) throws NotConnectedException, InterruptedException {
    switch(message.getType()) {
        case normal:
        case chat:
            break;
        default:
            throw new IllegalArgumentException("Message must be of type 'normal' or 'chat'");
    }
    Jid to = lockedResource;
    if (to == null) {
        to = jid;
    }
    message.setTo(to);
    connection().sendStanza(message);
}
Also used : Jid(org.jxmpp.jid.Jid) EntityBareJid(org.jxmpp.jid.EntityBareJid) EntityFullJid(org.jxmpp.jid.EntityFullJid)

Example 13 with Jid

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

the class MultipleRecipientManager method reply.

/**
     * Sends a reply to a previously received stanza(/packet) that was sent to multiple recipients. Before
     * attempting to send the reply message some checkings are performed. If any of those checkings
     * fail then an XMPPException is going to be thrown with the specific error detail.
     *
     * @param connection the connection to use to send the reply.
     * @param original   the previously received stanza(/packet) that was sent to multiple recipients.
     * @param reply      the new message to send as a reply.
     * @throws SmackException 
     * @throws XMPPErrorException 
     * @throws InterruptedException 
     */
public static void reply(XMPPConnection connection, Message original, Message reply) throws SmackException, XMPPErrorException, InterruptedException {
    MultipleRecipientInfo info = getMultipleRecipientInfo(original);
    if (info == null) {
        throw new SmackException("Original message does not contain multiple recipient info");
    }
    if (info.shouldNotReply()) {
        throw new SmackException("Original message should not be replied");
    }
    if (info.getReplyRoom() != null) {
        throw new SmackException("Reply should be sent through a room");
    }
    // Any <thread/> element from the initial message MUST be copied into the reply.
    if (original.getThread() != null) {
        reply.setThread(original.getThread());
    }
    MultipleAddresses.Address replyAddress = info.getReplyAddress();
    if (replyAddress != null && replyAddress.getJid() != null) {
        // Send reply to the reply_to address
        reply.setTo(replyAddress.getJid());
        connection.sendStanza(reply);
    } else {
        // Send reply to multiple recipients
        List<Jid> to = new ArrayList<>(info.getTOAddresses().size());
        List<Jid> cc = new ArrayList<>(info.getCCAddresses().size());
        for (MultipleAddresses.Address jid : info.getTOAddresses()) {
            to.add(jid.getJid());
        }
        for (MultipleAddresses.Address jid : info.getCCAddresses()) {
            cc.add(jid.getJid());
        }
        // Add original sender as a 'to' address (if not already present)
        if (!to.contains(original.getFrom()) && !cc.contains(original.getFrom())) {
            to.add(original.getFrom());
        }
        // Remove the sender from the TO/CC list (try with bare JID too)
        EntityFullJid from = connection.getUser();
        if (!to.remove(from) && !cc.remove(from)) {
            EntityBareJid bareJID = from.asEntityBareJid();
            to.remove(bareJID);
            cc.remove(bareJID);
        }
        send(connection, reply, to, cc, null, null, null, false);
    }
}
Also used : MultipleAddresses(org.jivesoftware.smackx.address.packet.MultipleAddresses) Jid(org.jxmpp.jid.Jid) EntityFullJid(org.jxmpp.jid.EntityFullJid) DomainBareJid(org.jxmpp.jid.DomainBareJid) EntityBareJid(org.jxmpp.jid.EntityBareJid) EntityFullJid(org.jxmpp.jid.EntityFullJid) SmackException(org.jivesoftware.smack.SmackException) ArrayList(java.util.ArrayList) EntityBareJid(org.jxmpp.jid.EntityBareJid)

Example 14 with Jid

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

the class MultipleRecipientManager method send.

/**
     * Sends the specified stanza(/packet) to the collection of specified recipients using the specified
     * connection. If the server has support for XEP-33 then only one stanza(/packet) is going to be sent to
     * the server with the multiple recipient instructions. However, if XEP-33 is not supported by
     * the server then the client is going to send the stanza(/packet) to each recipient.
     * 
     * @param connection the connection to use to send the packet.
     * @param packet the stanza(/packet) to send to the list of recipients.
     * @param to the collection of JIDs to include in the TO list or <tt>null</tt> if no TO list exists.
     * @param cc the collection of JIDs to include in the CC list or <tt>null</tt> if no CC list exists.
     * @param bcc the collection of JIDs to include in the BCC list or <tt>null</tt> if no BCC list
     *        exists.
     * @param replyTo address to which all replies are requested to be sent or <tt>null</tt>
     *        indicating that they can reply to any address.
     * @param replyRoom JID of a MUC room to which responses should be sent or <tt>null</tt>
     *        indicating that they can reply to any address.
     * @param noReply true means that receivers should not reply to the message.
     * @throws XMPPErrorException if server does not support XEP-33: Extended Stanza Addressing and
     *         some XEP-33 specific features were requested.
     * @throws NoResponseException if there was no response from the server.
     * @throws FeatureNotSupportedException if special XEP-33 features where requested, but the
     *         server does not support them.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public static void send(XMPPConnection connection, Stanza packet, Collection<? extends Jid> to, Collection<? extends Jid> cc, Collection<? extends Jid> bcc, Jid replyTo, Jid replyRoom, boolean noReply) throws NoResponseException, XMPPErrorException, FeatureNotSupportedException, NotConnectedException, InterruptedException {
    // required at all and we can send it just as normal stanza without needing to add the extension element
    if (to != null && to.size() == 1 && (cc == null || cc.isEmpty()) && (bcc == null || bcc.isEmpty()) && !noReply && StringUtils.isNullOrEmpty(replyTo) && StringUtils.isNullOrEmpty(replyRoom)) {
        Jid toJid = to.iterator().next();
        packet.setTo(toJid);
        connection.sendStanza(packet);
        return;
    }
    DomainBareJid serviceAddress = getMultipleRecipienServiceAddress(connection);
    if (serviceAddress != null) {
        // Send packet to target users using multiple recipient service provided by the server
        sendThroughService(connection, packet, to, cc, bcc, replyTo, replyRoom, noReply, serviceAddress);
    } else {
        // Server does not support XEP-33 so try to send the packet to each recipient
        if (noReply || replyTo != null || replyRoom != null) {
            // the user that this features are not available
            throw new FeatureNotSupportedException("Extended Stanza Addressing");
        }
        // Send the packet to each individual recipient
        sendToIndividualRecipients(connection, packet, to, cc, bcc);
    }
}
Also used : Jid(org.jxmpp.jid.Jid) EntityFullJid(org.jxmpp.jid.EntityFullJid) DomainBareJid(org.jxmpp.jid.DomainBareJid) EntityBareJid(org.jxmpp.jid.EntityBareJid) FeatureNotSupportedException(org.jivesoftware.smack.SmackException.FeatureNotSupportedException) DomainBareJid(org.jxmpp.jid.DomainBareJid)

Example 15 with Jid

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

the class MamPrefIQProviderTest method checkMamPrefResult.

@Test
public void checkMamPrefResult() throws Exception {
    IQ iq = (IQ) PacketParserUtils.parseStanza(exampleMamPrefsResultIQ);
    MamPrefsIQ mamPrefsIQ = (MamPrefsIQ) iq;
    List<Jid> alwaysJids = mamPrefsIQ.getAlwaysJids();
    List<Jid> neverJids = mamPrefsIQ.getNeverJids();
    Assert.assertEquals(alwaysJids.size(), 1);
    Assert.assertEquals(neverJids.size(), 2);
    Assert.assertEquals(alwaysJids.get(0).toString(), "romeo@montague.lit");
    Assert.assertEquals(neverJids.get(1).toString(), "montague@montague.lit");
}
Also used : MamPrefsIQ(org.jivesoftware.smackx.mam.element.MamPrefsIQ) Jid(org.jxmpp.jid.Jid) MamPrefsIQ(org.jivesoftware.smackx.mam.element.MamPrefsIQ) IQ(org.jivesoftware.smack.packet.IQ) 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