Search in sources :

Example 26 with PacketIDFilter

use of org.jivesoftware.smack.filter.PacketIDFilter in project ecf by eclipse.

the class AccountManager method getRegistrationInfo.

/**
 * Gets the account registration info from the server.
 *
 * @throws XMPPException if an error occurs.
 */
private synchronized void getRegistrationInfo() throws XMPPException {
    Registration reg = new Registration();
    reg.setTo(connection.getServiceName());
    PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class));
    PacketCollector collector = connection.createPacketCollector(filter);
    connection.sendPacket(reg);
    IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    collector.cancel();
    if (result == null) {
        throw new XMPPException("No response from server.");
    } else if (result.getType() == IQ.Type.ERROR) {
        throw new XMPPException(result.getError());
    } else {
        info = (Registration) result;
    }
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) Registration(org.jivesoftware.smack.packet.Registration) IQ(org.jivesoftware.smack.packet.IQ) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 27 with PacketIDFilter

use of org.jivesoftware.smack.filter.PacketIDFilter in project ecf by eclipse.

the class AccountManager method createAccount.

/**
 * Creates a new account using the specified username, password and account attributes.
 * The attributes Map must contain only String name/value pairs and must also have values
 * for all required attributes.
 *
 * @param username the username.
 * @param password the password.
 * @param attributes the account attributes.
 * @throws XMPPException if an error occurs creating the account.
 * @see #getAccountAttributes()
 */
public void createAccount(String username, String password, Map<String, String> attributes) throws XMPPException {
    if (!supportsAccountCreation()) {
        throw new XMPPException("Server does not support account creation.");
    }
    Registration reg = new Registration();
    reg.setType(IQ.Type.SET);
    reg.setTo(connection.getServiceName());
    attributes.put("username", username);
    attributes.put("password", password);
    reg.setAttributes(attributes);
    PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class));
    PacketCollector collector = connection.createPacketCollector(filter);
    connection.sendPacket(reg);
    IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    collector.cancel();
    if (result == null) {
        throw new XMPPException("No response from server.");
    } else if (result.getType() == IQ.Type.ERROR) {
        throw new XMPPException(result.getError());
    }
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) Registration(org.jivesoftware.smack.packet.Registration) IQ(org.jivesoftware.smack.packet.IQ) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 28 with PacketIDFilter

use of org.jivesoftware.smack.filter.PacketIDFilter in project ecf by eclipse.

the class NonSASLAuthentication method authenticate.

public String authenticate(String username, String password, String resource) throws XMPPException {
    // If we send an authentication packet in "get" mode with just the username,
    // the server will return the list of authentication protocols it supports.
    Authentication discoveryAuth = new Authentication();
    discoveryAuth.setType(IQ.Type.GET);
    discoveryAuth.setUsername(username);
    PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(discoveryAuth.getPacketID()));
    // Send the packet
    connection.sendPacket(discoveryAuth);
    // Wait up to a certain number of seconds for a response from the server.
    IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    if (response == null) {
        throw new XMPPException("No response from the server.");
    } else // If the server replied with an error, throw an exception.
    if (response.getType() == IQ.Type.ERROR) {
        throw new XMPPException(response.getError());
    }
    // Otherwise, no error so continue processing.
    Authentication authTypes = (Authentication) response;
    collector.cancel();
    // Now, create the authentication packet we'll send to the server.
    Authentication auth = new Authentication();
    auth.setUsername(username);
    // Figure out if we should use digest or plain text authentication.
    if (authTypes.getDigest() != null) {
        auth.setDigest(connection.getConnectionID(), password);
    } else if (authTypes.getPassword() != null) {
        auth.setPassword(password);
    } else {
        throw new XMPPException("Server does not support compatible authentication mechanism.");
    }
    auth.setResource(resource);
    collector = connection.createPacketCollector(new PacketIDFilter(auth.getPacketID()));
    // Send the packet.
    connection.sendPacket(auth);
    // Wait up to a certain number of seconds for a response from the server.
    response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    if (response == null) {
        throw new XMPPException("Authentication failed.");
    } else if (response.getType() == IQ.Type.ERROR) {
        throw new XMPPException(response.getError());
    }
    // We're done with the collector, so explicitly cancel it.
    collector.cancel();
    return response.getTo();
}
Also used : Authentication(org.jivesoftware.smack.packet.Authentication) IQ(org.jivesoftware.smack.packet.IQ) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 29 with PacketIDFilter

use of org.jivesoftware.smack.filter.PacketIDFilter in project ecf by eclipse.

the class NonSASLAuthentication method authenticateAnonymously.

public String authenticateAnonymously() throws XMPPException {
    // Create the authentication packet we'll send to the server.
    Authentication auth = new Authentication();
    PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(auth.getPacketID()));
    // Send the packet.
    connection.sendPacket(auth);
    // Wait up to a certain number of seconds for a response from the server.
    IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    if (response == null) {
        throw new XMPPException("Anonymous login failed.");
    } else if (response.getType() == IQ.Type.ERROR) {
        throw new XMPPException(response.getError());
    }
    // We're done with the collector, so explicitly cancel it.
    collector.cancel();
    if (response.getTo() != null) {
        return response.getTo();
    } else {
        return connection.getServiceName() + "/" + ((Authentication) response).getResource();
    }
}
Also used : Authentication(org.jivesoftware.smack.packet.Authentication) IQ(org.jivesoftware.smack.packet.IQ) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 30 with PacketIDFilter

use of org.jivesoftware.smack.filter.PacketIDFilter in project Smack by igniterealtime.

the class PacketReaderTest method testIQNotImplemented.

/**
 * Verify that when Smack receives a "not implemented IQ" answers with an IQ packet
 * with error code 501.
 */
public void testIQNotImplemented() {
    // Create a new type of IQ to send. The new IQ will include a
    // non-existant namespace to cause the "feature-not-implemented" answer
    IQ iqPacket = new IQ() {

        public String getChildElementXML() {
            return "<query xmlns=\"my:ns:test\"/>";
        }
    };
    iqPacket.setTo(getFullJID(1));
    iqPacket.setType(IQ.Type.get);
    // Send the IQ and wait for the answer
    StanzaCollector collector = getConnection(0).createStanzaCollector(new PacketIDFilter(iqPacket.getStanzaId()));
    getConnection(0).sendStanza(iqPacket);
    IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    if (response == null) {
        fail("No response from the other user.");
    }
    assertEquals("The received IQ is not of type ERROR", IQ.Type.error, response.getType());
    assertEquals("The error code is not 501", 501, response.getError().getCode());
    collector.cancel();
}
Also used : PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Aggregations

PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)66 IQ (org.jivesoftware.smack.packet.IQ)38 PacketCollector (org.jivesoftware.smack.PacketCollector)36 XMPPException (org.jivesoftware.smack.XMPPException)34 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)22 Packet (org.jivesoftware.smack.packet.Packet)10 AndFilter (org.jivesoftware.smack.filter.AndFilter)7 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)6 Registration (org.jivesoftware.smack.packet.Registration)6 MUCAdmin (org.jivesoftware.smackx.packet.MUCAdmin)6 MUCOwner (org.jivesoftware.smackx.packet.MUCOwner)6 RosterPacket (org.jivesoftware.smack.packet.RosterPacket)4 ArrayList (java.util.ArrayList)3 XMPPConnection (org.jivesoftware.smack.XMPPConnection)3 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 HashMap (java.util.HashMap)2 StanzaCollector (org.jivesoftware.smack.StanzaCollector)2 StanzaListener (org.jivesoftware.smack.StanzaListener)2 Authentication (org.jivesoftware.smack.packet.Authentication)2