use of org.jivesoftware.smack.StanzaListener in project Spark by igniterealtime.
the class ChatContainer method cleanupChatRoom.
/**
* Removes the ChatRoom resources.
*
* @param room the room to remove.
*/
private void cleanupChatRoom(ChatRoom room) {
if (room.isActive()) {
room.leaveChatRoom();
room.closeChatRoom();
}
final StanzaListener listener = presenceMap.get(room.getRoomname());
if (listener != null) {
SparkManager.getConnection().removeAsyncStanzaListener(listener);
}
fireChatRoomClosed(room);
room.removeMessageListener(this);
// Remove mappings
presenceMap.remove(room.getRoomname());
chatRoomList.remove(room);
room.getChatInputEditor().removeKeyListener(this);
// Clear all Text :)
room.getTranscriptWindow().cleanup();
}
use of org.jivesoftware.smack.StanzaListener in project Spark by igniterealtime.
the class BattleshipPlugin method initialize.
@Override
public void initialize() {
ProviderManager.addIQProvider(GameOfferPacket.ELEMENT_NAME, GameOfferPacket.NAMESPACE, GameOfferPacket.class);
ProviderManager.addExtensionProvider(MovePacket.ELEMENT_NAME, MovePacket.NAMESPACE, MovePacket.class);
ProviderManager.addExtensionProvider(MoveAnswerPacket.ELEMENT_NAME, MoveAnswerPacket.NAMESPACE, MoveAnswerPacket.class);
_gameofferListener = new StanzaListener() {
@Override
public void processPacket(Stanza stanza) {
GameOfferPacket invitation = (GameOfferPacket) stanza;
if (invitation.getType() == IQ.Type.get) {
showInvitationInChat(invitation);
}
}
};
SparkManager.getConnection().addAsyncStanzaListener(_gameofferListener, new StanzaTypeFilter(GameOfferPacket.class));
_chatRoomListener = new ChatRoomOpeningListener();
SparkManager.getChatManager().addChatRoomListener(_chatRoomListener);
}
use of org.jivesoftware.smack.StanzaListener in project spring-integration by spring-projects.
the class ChatMessageListeningEndpointTests method testWithErrorChannel.
@Test
public void testWithErrorChannel() throws Exception {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
XMPPConnection connection = mock(XMPPConnection.class);
bf.registerSingleton(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, connection);
ChatMessageListeningEndpoint endpoint = new ChatMessageListeningEndpoint();
DirectChannel outChannel = new DirectChannel();
outChannel.subscribe(message -> {
throw new RuntimeException("ooops");
});
PollableChannel errorChannel = new QueueChannel();
endpoint.setBeanFactory(bf);
endpoint.setOutputChannel(outChannel);
endpoint.setErrorChannel(errorChannel);
endpoint.afterPropertiesSet();
StanzaListener listener = (StanzaListener) TestUtils.getPropertyValue(endpoint, "stanzaListener");
Message smackMessage = new Message(JidCreate.from("kermit@frog.com"));
smackMessage.setBody("hello");
smackMessage.setThread("1234");
listener.processStanza(smackMessage);
ErrorMessage msg = (ErrorMessage) errorChannel.receive();
assertEquals("hello", ((MessagingException) msg.getPayload()).getFailedMessage().getPayload());
}
use of org.jivesoftware.smack.StanzaListener in project xabber-android by redsolution.
the class AbstractChat method sendMessage.
@SuppressWarnings("WeakerAccess")
boolean sendMessage(MessageItem messageItem) {
String text = prepareText(messageItem.getText());
messageItem.setEncrypted(OTRManager.getInstance().isEncrypted(text));
Long timestamp = messageItem.getTimestamp();
Date currentTime = new Date(System.currentTimeMillis());
Date delayTimestamp = null;
if (timestamp != null) {
if (currentTime.getTime() - timestamp > 60000) {
delayTimestamp = currentTime;
}
}
Message message = null;
if (messageItem.haveAttachments()) {
message = createFileMessagePacket(messageItem.getStanzaId(), messageItem.getAttachments(), text);
} else if (messageItem.haveForwardedMessages()) {
Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
RealmResults<MessageItem> items = realm.where(MessageItem.class).in(MessageItem.Fields.UNIQUE_ID, messageItem.getForwardedIdsAsArray()).findAll();
List<ReferenceElement> references = new ArrayList<>();
StringBuilder builder = new StringBuilder();
if (items != null && !items.isEmpty()) {
for (MessageItem item : items) {
String forward = ClipManager.createMessageTree(realm, item.getUniqueId()) + "\n";
int begin = getSizeOfEncodedChars(builder.toString());
builder.append(forward);
ReferenceElement reference = ReferencesManager.createForwardReference(item, begin, getSizeOfEncodedChars(builder.toString()) - 1);
references.add(reference);
}
}
builder.append(text);
text = builder.toString();
message = createMessagePacket(text, messageItem.getStanzaId());
for (ReferenceElement element : references) {
message.addExtension(element);
}
} else if (text != null) {
message = createMessagePacket(text, messageItem.getStanzaId());
}
if (message != null) {
ChatStateManager.getInstance().updateOutgoingMessage(AbstractChat.this, message);
CarbonManager.getInstance().updateOutgoingMessage(AbstractChat.this, message);
message.addExtension(new OriginIdElement(messageItem.getStanzaId()));
if (delayTimestamp != null) {
message.addExtension(new DelayInformation(delayTimestamp));
}
final String messageId = messageItem.getUniqueId();
try {
StanzaSender.sendStanza(account, message, new StanzaListener() {
@Override
public void processStanza(Stanza packet) throws SmackException.NotConnectedException {
Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
MessageItem acknowledgedMessage = realm.where(MessageItem.class).equalTo(MessageItem.Fields.UNIQUE_ID, messageId).findFirst();
if (acknowledgedMessage != null) {
acknowledgedMessage.setAcknowledged(true);
}
}
});
realm.close();
}
});
} catch (NetworkException e) {
return false;
}
}
if (message == null) {
messageItem.setError(true);
messageItem.setErrorDescription("Internal error: message is null");
} else {
message.setFrom(account.getFullJid());
messageItem.setOriginalStanza(message.toXML().toString());
}
if (delayTimestamp != null) {
messageItem.setDelayTimestamp(delayTimestamp.getTime());
}
if (messageItem.getTimestamp() == null) {
messageItem.setTimestamp(currentTime.getTime());
}
messageItem.setSent(true);
return true;
}
use of org.jivesoftware.smack.StanzaListener in project Smack by igniterealtime.
the class MamIntegrationTest method mamTest.
@SmackIntegrationTest
public void mamTest() throws TimeoutException, Exception {
EntityBareJid userOne = conOne.getUser().asEntityBareJid();
EntityBareJid userTwo = conTwo.getUser().asEntityBareJid();
final String messageBody = "Test MAM message (" + testRunId + ')';
Message message = conTwo.getStanzaFactory().buildMessageStanza().to(userTwo).setBody(messageBody).build();
final String messageId = message.getStanzaId();
final SimpleResultSyncPoint messageReceived = new SimpleResultSyncPoint();
final StanzaListener stanzaListener = new StanzaListener() {
@Override
public void processStanza(Stanza stanza) {
Message message = (Message) stanza;
if (message.getBody().equals(messageBody)) {
messageReceived.signal();
}
}
};
conTwo.addAsyncStanzaListener(stanzaListener, MessageWithBodiesFilter.INSTANCE);
try {
conOne.sendStanza(message);
messageReceived.waitForResult(timeout);
} finally {
conTwo.removeAsyncStanzaListener(stanzaListener);
}
MamQueryArgs mamQueryArgs = MamQueryArgs.builder().setResultPageSizeTo(1).limitResultsToJid(userOne).queryLastPage().build();
MamQuery mamQuery = mamManagerConTwo.queryArchive(mamQueryArgs);
assertEquals(1, mamQuery.getMessages().size());
Message mamMessage = mamQuery.getMessages().get(0);
assertEquals(messageId, mamMessage.getStanzaId());
assertEquals(messageBody, mamMessage.getBody());
assertEquals(conOne.getUser(), mamMessage.getFrom());
assertEquals(userTwo, mamMessage.getTo());
}
Aggregations