Java Code Examples for org.jivesoftware.smack.roster.packet.RosterPacket#setVersion()

The following examples show how to use org.jivesoftware.smack.roster.packet.RosterPacket#setVersion() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Roster.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * Reloads the entire roster from the server. This is an asynchronous operation,
 * which means the method will return immediately, and the roster will be
 * reloaded at a later point when the server responds to the reload request.
 * @throws NotLoggedInException If not logged in.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public void reload() throws NotLoggedInException, NotConnectedException, InterruptedException {
    final XMPPConnection connection = getAuthenticatedConnectionOrThrow();

    RosterPacket packet = new RosterPacket();
    if (rosterStore != null && isRosterVersioningSupported()) {
        packet.setVersion(rosterStore.getRosterVersion());
    }
    rosterState = RosterState.loading;

    SmackFuture<IQ, Exception> future = connection.sendIqRequestAsync(packet);

    future.onSuccess(new RosterResultListener()).onError(new ExceptionCallback<Exception>() {

        @Override
        public void processException(Exception exception) {
            rosterState = RosterState.uninitialized;
            Level logLevel;
            if (exception instanceof NotConnectedException) {
                logLevel = Level.FINE;
            } else {
                logLevel = Level.SEVERE;
            }
            LOGGER.log(logLevel, "Exception reloading roster", exception);
            for (RosterLoadedListener listener : rosterLoadedListeners) {
                listener.onRosterLoadingFailed(exception);
            }
        }

    });
}
 
Example 2
Source File: RosterPacketProvider.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Override
public RosterPacket parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
    RosterPacket roster = new RosterPacket();
    String version = parser.getAttributeValue("", "ver");
    roster.setVersion(version);

    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch (eventType) {
        case START_ELEMENT:
            String startTag = parser.getName();
            switch (startTag) {
            case "item":
                RosterPacket.Item item = parseItem(parser);
                roster.addRosterItem(item);
                break;
            }
            break;
        case END_ELEMENT:
            String endTag = parser.getName();
            switch (endTag) {
            case IQ.QUERY_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
            }
            break;
        default:
            // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
            break;
        }
    }
    return roster;
}
 
Example 3
Source File: RosterVersioningTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that a non-empty roster result empties the store.
 * @throws SmackException if Smack detected an exceptional situation.
 * @throws XMPPException if an XMPP protocol error was received.
 * @throws XmppStringprepException if the provided string is invalid.
 */
@Test(timeout = 5000)
public void testOtherVersionStored() throws XMPPException, SmackException, XmppStringprepException {
    Item vaglafItem = vaglafItem();

    // We expect that the roster request is the only packet sent. This is not part of the specification,
    // but a shortcut in the test implementation.
    Stanza sentPacket = connection.getSentPacket();
    if (sentPacket instanceof RosterPacket) {
        RosterPacket sentRP = (RosterPacket) sentPacket;
        RosterPacket answer = new RosterPacket();
        answer.setStanzaId(sentRP.getStanzaId());
        answer.setType(Type.result);
        answer.setTo(sentRP.getFrom());

        answer.setVersion("newVersion");
        answer.addRosterItem(vaglafItem);

        rosterListener.reset();
        connection.processStanza(answer);
        rosterListener.waitUntilInvocationOrTimeout();
    } else {
        assertTrue("Expected to get a RosterPacket ", false);
    }

    Roster roster = Roster.getInstanceFor(connection);
    assertEquals("Size of roster", 1, roster.getEntries().size());
    RosterEntry entry = roster.getEntry(vaglafItem.getJid());
    assertNotNull("Roster contains vaglaf entry", entry);
    assertEquals("vaglaf entry in roster equals the sent entry", vaglafItem, RosterEntry.toRosterItem(entry));

    RosterStore store = roster.getRosterStore();
    assertEquals("Size of store", 1, store.getEntries().size());
    Item item = store.getEntry(vaglafItem.getJid());
    assertNotNull("Store contains vaglaf entry", item);
    assertEquals("vaglaf entry in store equals the sent entry", vaglafItem, item);
}