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;
}
}
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());
}
}
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();
}
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();
}
}
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();
}
Aggregations