use of org.jivesoftware.smack.XMPPConnection in project Smack by igniterealtime.
the class PingManager method ping.
/**
* Pings the given jid. This method will return false if an error occurs. The exception
* to this, is a server ping, which will always return true if the server is reachable,
* event if there is an error on the ping itself (i.e. ping not supported).
* <p>
* Use {@link #isPingSupported(Jid)} to determine if XMPP Ping is supported
* by the entity.
*
* @param jid The id of the entity the ping is being sent to
* @param pingTimeout The time to wait for a reply in milliseconds
* @return true if a reply was received from the entity, false otherwise.
* @throws NoResponseException if there was no response from the jid.
* @throws NotConnectedException
* @throws InterruptedException
*/
public boolean ping(Jid jid, long pingTimeout) throws NotConnectedException, NoResponseException, InterruptedException {
final XMPPConnection connection = connection();
// otherwise the client JID will be null causing an NPE
if (!connection.isAuthenticated()) {
throw new NotConnectedException();
}
Ping ping = new Ping(jid);
try {
connection.createStanzaCollectorAndSend(ping).nextResultOrThrow(pingTimeout);
} catch (XMPPException exc) {
return jid.equals(connection.getXMPPServiceDomain());
}
return true;
}
use of org.jivesoftware.smack.XMPPConnection in project Smack by igniterealtime.
the class ConnectionUtils method createMockedConnection.
/**
* Creates a mocked XMPP connection that stores every stanza(/packet) that is send over this
* connection in the given protocol instance and returns the predefined answer packets
* form the protocol instance.
* <p>
* This mocked connection can used to collect packets that require a reply using a
* StanzaCollector.
*
* <pre>
* <code>
* StanzaCollector collector = connection.createStanzaCollector(new PacketFilter());
* connection.sendStanza(packet);
* Stanza(/Packet) reply = collector.nextResult();
* </code>
* </pre>
*
* @param protocol protocol helper containing answer packets
* @param initiatorJID the user associated to the XMPP connection
* @param xmppServer the XMPP server associated to the XMPP connection
* @return a mocked XMPP connection
* @throws SmackException
* @throws XMPPErrorException
* @throws InterruptedException
*/
public static XMPPConnection createMockedConnection(final Protocol protocol, EntityFullJid initiatorJID, DomainBareJid xmppServer) throws SmackException, XMPPErrorException, InterruptedException {
// mock XMPP connection
XMPPConnection connection = mock(XMPPConnection.class);
when(connection.getUser()).thenReturn(initiatorJID);
when(connection.getXMPPServiceDomain()).thenReturn(xmppServer);
// mock packet collector
final StanzaCollector collector = mock(StanzaCollector.class);
when(connection.createStanzaCollector(isA(StanzaFilter.class))).thenReturn(collector);
Answer<StanzaCollector> collectorAndSend = new Answer<StanzaCollector>() {
@Override
public StanzaCollector answer(InvocationOnMock invocation) throws Throwable {
Stanza packet = (Stanza) invocation.getArguments()[0];
protocol.getRequests().add(packet);
return collector;
}
};
when(connection.createStanzaCollectorAndSend(isA(IQ.class))).thenAnswer(collectorAndSend);
// mock send method
Answer<Object> addIncoming = new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
protocol.getRequests().add((Stanza) invocation.getArguments()[0]);
return null;
}
};
doAnswer(addIncoming).when(connection).sendStanza(isA(Stanza.class));
// mock receive methods
Answer<Stanza> answer = new Answer<Stanza>() {
@Override
public Stanza answer(InvocationOnMock invocation) throws Throwable {
return protocol.getResponses().poll();
}
};
when(collector.nextResult(anyInt())).thenAnswer(answer);
when(collector.nextResult()).thenAnswer(answer);
Answer<Stanza> answerOrThrow = new Answer<Stanza>() {
@Override
public Stanza answer(InvocationOnMock invocation) throws Throwable {
Stanza packet = protocol.getResponses().poll();
if (packet == null)
return packet;
XMPPErrorException.ifHasErrorThenThrow(packet);
return packet;
}
};
when(collector.nextResultOrThrow()).thenAnswer(answerOrThrow);
when(collector.nextResultOrThrow(anyLong())).thenAnswer(answerOrThrow);
// initialize service discovery manager for this connection
ServiceDiscoveryManager.getInstanceFor(connection);
return connection;
}
use of org.jivesoftware.smack.XMPPConnection in project Smack by igniterealtime.
the class Roster method sendSubscriptionRequest.
public void sendSubscriptionRequest(BareJid jid) throws NotLoggedInException, NotConnectedException, InterruptedException {
final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
// Create a presence subscription packet and send.
Presence presencePacket = new Presence(Presence.Type.subscribe);
presencePacket.setTo(jid);
connection.sendStanza(presencePacket);
}
use of org.jivesoftware.smack.XMPPConnection in project Smack by igniterealtime.
the class Roster method preApprove.
/**
* Pre-approve user presence subscription.
*
* @param user the user. (e.g. johndoe@jabber.org)
* @throws NotLoggedInException if not logged in.
* @throws NotConnectedException
* @throws InterruptedException
* @throws FeatureNotSupportedException if pre-approving is not supported.
* @since 4.2
*/
public void preApprove(BareJid user) throws NotLoggedInException, NotConnectedException, InterruptedException, FeatureNotSupportedException {
final XMPPConnection connection = connection();
if (!isSubscriptionPreApprovalSupported()) {
throw new FeatureNotSupportedException("Pre-approving");
}
Presence presencePacket = new Presence(Presence.Type.subscribed);
presencePacket.setTo(user);
connection.sendStanza(presencePacket);
}
use of org.jivesoftware.smack.XMPPConnection in project Smack by igniterealtime.
the class Roster method createGroup.
/**
* Creates a new group.
* <p>
* Note: you must add at least one entry to the group for the group to be kept
* after a logout/login. This is due to the way that XMPP stores group information.
* </p>
*
* @param name the name of the group.
* @return a new group, or null if the group already exists
*/
public RosterGroup createGroup(String name) {
final XMPPConnection connection = connection();
if (groups.containsKey(name)) {
return groups.get(name);
}
RosterGroup group = new RosterGroup(name, connection);
groups.put(name, group);
return group;
}
Aggregations