@Override public void processStanza(Stanza stanza) throws NotConnectedException, InterruptedException { Message message = (Message) stanza; if (!shouldAcceptMessage(message)) { return; } final EntityBareJid to = message.getTo().asEntityBareJidOrThrow(); final Chat chat = chatWith(to); for (OutgoingChatMessageListener listener : outgoingListeners) { listener.newOutgoingMessage(to, message, chat); } } }, OUTGOING_MESSAGE_FILTER);
@Test public void checkResultsParse() throws Exception { Message message = PacketParserUtils.parseStanza(exampleResultMessage); MamResultExtension mamResultExtension = MamResultExtension.from(message); Assert.assertEquals(mamResultExtension.getQueryId(), "f27"); Assert.assertEquals(mamResultExtension.getId(), "28482-98726-73623"); GregorianCalendar calendar = new GregorianCalendar(2010, 7 - 1, 10, 23, 8, 25); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = calendar.getTime(); Forwarded forwarded = mamResultExtension.getForwarded(); Assert.assertEquals(forwarded.getDelayInformation().getStamp(), date); Message forwardedMessage = (Message) forwarded.getForwardedStanza(); Assert.assertEquals(forwardedMessage.getFrom().toString(), "witch@shakespeare.lit"); Assert.assertEquals(forwardedMessage.getTo().toString(), "macbeth@shakespeare.lit"); Assert.assertEquals(forwardedMessage.getBody(), "Hail to thee"); }
@Test public void checkMamResultProvider() throws Exception { XmlPullParser parser = PacketParserUtils.getParserFor(exampleMamResultXml); MamResultExtension mamResultExtension = new MamResultProvider().parse(parser); Assert.assertEquals(mamResultExtension.getQueryId(), "f27"); Assert.assertEquals(mamResultExtension.getId(), "28482-98726-73623"); GregorianCalendar calendar = new GregorianCalendar(2010, 7 - 1, 10, 23, 8, 25); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = calendar.getTime(); Forwarded forwarded = mamResultExtension.getForwarded(); Assert.assertEquals(forwarded.getDelayInformation().getStamp(), date); Message message = (Message) forwarded.getForwardedStanza(); Assert.assertEquals(message.getFrom().toString(), "romeo@montague.lit/orchard"); Assert.assertEquals(message.getTo().toString(), "juliet@capulet.lit/balcony"); Assert.assertEquals(message.getBody(), "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo."); }
assertEquals(messageBody, mamMessage.getBody()); assertEquals(conOne.getUser(), mamMessage.getFrom()); assertEquals(userTwo, mamMessage.getTo());
@Override protected Map<String, Object> extractStandardHeaders(Message source) { Map<String, Object> headers = new HashMap<>(); Jid from = source.getFrom(); if (from != null) { headers.put(XmppHeaders.FROM, from.toString()); } String subject = source.getSubject(); if (StringUtils.hasText(subject)) { headers.put(XmppHeaders.SUBJECT, subject); } String thread = source.getThread(); if (StringUtils.hasText(thread)) { headers.put(XmppHeaders.THREAD, thread); } Jid to = source.getTo(); if (to != null) { headers.put(XmppHeaders.TO, to.toString()); } Message.Type type = source.getType(); if (type != null) { headers.put(XmppHeaders.TYPE, type); } return headers; }
@Test //INT-2275 public void testOutboundChannelAdapterInsideChain() throws Exception { MessageChannel channel = context.getBean("outboundChainChannel", MessageChannel.class); Message<?> message = MessageBuilder.withPayload("hello").setHeader(XmppHeaders.TO, "artem").build(); XMPPConnection connection = context.getBean("testConnection", XMPPConnection.class); doAnswer(invocation -> { Object[] args = invocation.getArguments(); org.jivesoftware.smack.packet.Message xmppMessage = (org.jivesoftware.smack.packet.Message) args[0]; assertEquals("artem", xmppMessage.getTo().toString()); assertEquals("hello", xmppMessage.getBody()); return null; }).when(connection).sendStanza(Mockito.any(org.jivesoftware.smack.packet.Message.class)); channel.send(message); verify(connection, times(1)).sendStanza(Mockito.any(org.jivesoftware.smack.packet.Message.class)); Mockito.reset(connection); }
@SuppressWarnings("rawtypes") @Test public void withHeaderMapper() throws Exception { Object pollingConsumer = context.getBean("withHeaderMapper"); assertTrue(pollingConsumer instanceof PollingConsumer); assertEquals(headerMapper, TestUtils.getPropertyValue(pollingConsumer, "handler.headerMapper")); MessageChannel channel = context.getBean("outboundEventChannel", MessageChannel.class); Message<?> message = MessageBuilder.withPayload("hello").setHeader(XmppHeaders.TO, "oleg"). setHeader("foobar", "foobar").build(); XMPPConnection connection = context.getBean("testConnection", XMPPConnection.class); doAnswer(invocation -> { Object[] args = invocation.getArguments(); org.jivesoftware.smack.packet.Message xmppMessage = (org.jivesoftware.smack.packet.Message) args[0]; assertEquals("oleg", xmppMessage.getTo().toString()); assertEquals("foobar", JivePropertiesManager.getProperty(xmppMessage, "foobar")); return null; }).when(connection).sendStanza(Mockito.any(org.jivesoftware.smack.packet.Message.class)); channel.send(message); verify(connection, times(1)).sendStanza(Mockito.any(org.jivesoftware.smack.packet.Message.class)); Mockito.reset(connection); }
@Test public void testExtensionProvider() throws Exception { XMPPConnection connection = mock(XMPPConnection.class); ChatMessageSendingMessageHandler handler = new ChatMessageSendingMessageHandler(connection); GcmExtensionProvider extensionElementProvider = spy(new GcmExtensionProvider()); handler.setExtensionProvider(extensionElementProvider); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); final String json = "{\"foo\": \"bar\"}"; Message<?> message = MessageBuilder.withPayload(" <f foo='foo'>" + json + "</f> ") .setHeader(XmppHeaders.TO, "kermit@frog.com") .build(); handler.handleMessage(message); ArgumentCaptor<org.jivesoftware.smack.packet.Message> argumentCaptor = ArgumentCaptor.forClass(org.jivesoftware.smack.packet.Message.class); verify(connection).sendStanza(argumentCaptor.capture()); org.jivesoftware.smack.packet.Message smackMessage = argumentCaptor.getValue(); assertNull(smackMessage.getBody()); assertEquals("kermit@frog.com", smackMessage.getTo().toString()); GcmPacketExtension gcmPacketExtension = GcmPacketExtension.from(smackMessage); assertNotNull(gcmPacketExtension); assertEquals(json, gcmPacketExtension.getJson()); verify(extensionElementProvider).from(eq(json)); }
.sendStanza(Mockito.argThat((org.jivesoftware.smack.packet.Message smackMessage) -> { boolean bodyMatches = smackMessage.getBody().equals("Test Message"); boolean toMatches = smackMessage.getTo().equals("kermit@frog.com"); return bodyMatches & toMatches; })); .sendStanza(Mockito.argThat((org.jivesoftware.smack.packet.Message smackMessage) -> { boolean bodyMatches = smackMessage.getBody().equals("Hello Kitty"); boolean toMatches = smackMessage.getTo().equals("kermit@frog.com"); boolean threadIdMatches = smackMessage.getThread().equals("123"); return bodyMatches & toMatches & threadIdMatches; .sendStanza(Mockito.argThat((org.jivesoftware.smack.packet.Message smackMessage) -> { boolean bodyMatches = smackMessage.getBody() == null; boolean toMatches = smackMessage.getTo().equals("kermit@frog.com"); GcmPacketExtension gcmPacketExtension = GcmPacketExtension.from(smackMessage); boolean jsonMatches = gcmPacketExtension != null && gcmPacketExtension.getJson().equals(json);
@Test public void fromHeadersStandardOutbound() { DefaultXmppHeaderMapper mapper = new DefaultXmppHeaderMapper(); Map<String, Object> headerMap = new HashMap<>(); headerMap.put("userDefined1", "foo"); headerMap.put("userDefined2", "bar"); headerMap.put(XmppHeaders.THREAD, "test.thread"); headerMap.put(XmppHeaders.TO, "test.to"); headerMap.put(XmppHeaders.FROM, "test.from"); headerMap.put(XmppHeaders.SUBJECT, "test.subject"); headerMap.put(XmppHeaders.TYPE, "headline"); MessageHeaders headers = new MessageHeaders(headerMap); Message target = new Message(); mapper.fromHeadersToRequest(headers, target); // "standard" XMPP headers assertEquals("test.thread", target.getThread()); assertEquals("test.to", target.getTo().toString()); assertEquals("test.from", target.getFrom().toString()); assertEquals("test.subject", target.getSubject()); assertEquals(Message.Type.headline, target.getType()); // user-defined headers not included by default assertNull(JivePropertiesManager.getProperty(target, "userDefined1")); assertNull(JivePropertiesManager.getProperty(target, "userDefined2")); // transient headers should not be copied assertNull(JivePropertiesManager.getProperty(target, "id")); assertNull(JivePropertiesManager.getProperty(target, "timestamp")); }
assertNull(target.getTo()); assertNull(target.getFrom()); assertNull(target.getSubject());
/** * Converts the given XMPP destination into a {@code xmpp} URI. */ public static URI toUri(Message requestMessage) throws URISyntaxException { return new URI(XmppTransportConstants.XMPP_URI_SCHEME, requestMessage.getTo().asUnescapedString(), null); }
/** * Converts the given XMPP destination into a {@code xmpp} URI. */ public static URI toUri(Message requestMessage) throws URISyntaxException { return new URI(XmppTransportConstants.XMPP_URI_SCHEME, requestMessage.getTo().asUnescapedString(), null); }
/** * Converts the given XMPP destination into a {@code xmpp} URI. */ public static URI toUri(Message requestMessage) throws URISyntaxException { return new URI(XmppTransportConstants.XMPP_URI_SCHEME, requestMessage.getTo().asUnescapedString(), null); }
public void fireOutgoingMessageSending( Message message ) { for ( final MessageEventListener listener : messageEventListeners ) { try { listener.sendingMessage( message ); } catch ( Exception e ) { Log.error( "A MessageEventListener ('" + listener + "') threw an exception while processing an outgoing message (to '" + message.getTo() + "').", e ); } } }
/** * Notifies all <code>GlobalMessageListeners</code> of a new message sent. * * @param chatRoom the <code>ChatRoom</code> where the message was sent from. * @param message the <code>Message</code> sent. */ public void fireGlobalMessageSentListeners( ChatRoom chatRoom, Message message ) { for ( GlobalMessageListener listener : globalMessageListeners ) { try { listener.messageSent( chatRoom, message ); } catch ( Exception e ) { Log.error( "A GlobalMessageListener ('" + listener + "') threw an exception while processing an outgoing chat message (to '" + message.getTo() + "') in a chat room ('" + chatRoom + "').", e ); } } }
public Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); org.jivesoftware.smack.packet.Message xmppMessage = (org.jivesoftware.smack.packet.Message) args[0]; assertEquals("artem", xmppMessage.getTo()); assertEquals("hello", xmppMessage.getBody()); return null; }
public Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); org.jivesoftware.smack.packet.Message xmppMessage = (org.jivesoftware.smack.packet.Message) args[0]; assertEquals("oleg", xmppMessage.getTo()); assertEquals("foobar", JivePropertiesManager.getProperty(xmppMessage, "foobar")); return null; } })
private void sendCancelMessage(Message message) { IQ error = FileTransferNegotiator.createIQ(message.getPacketID(), message.getFrom(), message.getTo(), IQ.Type.ERROR); error.setError(new XMPPError(504)); connection.sendPacket(error); }
private void checkEvents(Jid from, String packetID, MessageEvent messageEvent) { if (messageEvent.isDelivered() || messageEvent.isDisplayed()) { // Create the message to send Message msg = new Message(from); // Create a MessageEvent Package and add it to the message MessageEvent event = new MessageEvent(); if (messageEvent.isDelivered()) { event.setDelivered(true); } if (messageEvent.isDisplayed()) { event.setDisplayed(true); } event.setStanzaId(packetID); msg.addExtension(event); // Send the packet try { SparkManager.getConnection().sendStanza(msg); } catch ( SmackException.NotConnectedException | InterruptedException e ) { Log.warning( "Unable to send message to " + msg.getTo(), e ); } } }