Search in sources :

Example 51 with Message

use of org.jivesoftware.smack.packet.Message in project perun by CESNET.

the class PerunNotifJabberSender method send.

@Override
public Set<Integer> send(List<PerunNotifMessageDto> dtosToSend) {
    Set<Integer> usedPools = new HashSet<Integer>();
    try {
        ConnectionConfiguration config = new ConnectionConfiguration(jabberServer, port, serviceName);
        XMPPConnection connection = new XMPPConnection(config);
        connection.connect();
        SASLAuthentication.supportSASLMechanism("PLAIN", 0);
        connection.login(username, password);
        for (PerunNotifMessageDto messageDto : dtosToSend) {
            PerunNotifReceiver receiver = messageDto.getReceiver();
            PoolMessage dto = messageDto.getPoolMessage();
            Message message = new Message();
            message.setSubject(messageDto.getSubject());
            message.setBody(messageDto.getMessageToSend());
            message.setType(Message.Type.headline);
            String myReceiverId = dto.getKeyAttributes().get(receiver.getTarget());
            if (myReceiverId == null || myReceiverId.isEmpty()) {
                //Can be set one static account
                message.setTo(receiver.getTarget());
            } else {
                //We try to resolve id
                Integer id = null;
                try {
                    id = Integer.valueOf(myReceiverId);
                } catch (NumberFormatException ex) {
                    logger.error("Cannot resolve id: {}, error: {}", Arrays.asList(id, ex.getMessage()));
                    logger.debug("ST:", ex);
                }
                if (id != null) {
                    try {
                        User user = perun.getUsersManagerBl().getUserById(session, id);
                        Attribute emailAttribute = perun.getAttributesManagerBl().getAttribute(session, user, "urn:perun:user:attribute-def:def:jabber");
                        if (emailAttribute != null && StringUtils.hasText(emailAttribute.toString())) {
                            message.setTo((String) emailAttribute.getValue());
                        }
                    } catch (UserNotExistsException ex) {
                        logger.error("Cannot found user with id: {}, ex: {}", Arrays.asList(id, ex.getMessage()));
                        logger.debug("ST:", ex);
                    } catch (AttributeNotExistsException ex) {
                        logger.warn("Cannot found email for user with id: {}, ex: {}", Arrays.asList(id, ex.getMessage()));
                        logger.debug("ST:", ex);
                    } catch (Exception ex) {
                        logger.error("Error during user email recognition, ex: {}", ex.getMessage());
                        logger.debug("ST:", ex);
                    }
                }
            }
            connection.sendPacket(message);
            usedPools.addAll(messageDto.getUsedPoolIds());
        }
        connection.disconnect();
    } catch (XMPPException ex) {
        logger.error("Error during jabber establish connection.", ex);
    }
    return null;
}
Also used : PoolMessage(cz.metacentrum.perun.notif.dto.PoolMessage) Message(org.jivesoftware.smack.packet.Message) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) PerunNotifReceiver(cz.metacentrum.perun.notif.entities.PerunNotifReceiver) XMPPConnection(org.jivesoftware.smack.XMPPConnection) PerunNotifMessageDto(cz.metacentrum.perun.notif.dto.PerunNotifMessageDto) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) XMPPException(org.jivesoftware.smack.XMPPException) ConnectionConfiguration(org.jivesoftware.smack.ConnectionConfiguration) PoolMessage(cz.metacentrum.perun.notif.dto.PoolMessage) XMPPException(org.jivesoftware.smack.XMPPException)

Example 52 with Message

use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.

the class Chat method sendMessage.

/**
     * Sends the specified text as a message to the other chat participant.
     * This is a convenience method for:
     *
     * <pre>
     *     Message message = chat.createMessage();
     *     message.setBody(messageText);
     *     chat.sendMessage(message);
     * </pre>
     *
     * @param text the text to send.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public void sendMessage(String text) throws NotConnectedException, InterruptedException {
    Message message = new Message();
    message.setBody(text);
    sendMessage(message);
}
Also used : Message(org.jivesoftware.smack.packet.Message)

Example 53 with Message

use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.

the class ChatConnectionTest method validateMessageTypeWithDefaults4.

@Test
public void validateMessageTypeWithDefaults4() {
    Message incomingChat = createChatPacket("134", true);
    incomingChat.setType(Type.headline);
    assertNull(listener.getNewChat());
}
Also used : Message(org.jivesoftware.smack.packet.Message) Test(org.junit.Test)

Example 54 with Message

use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.

the class ChatConnectionTest method validateMessageTypeWithDefaults1.

@Test
public void validateMessageTypeWithDefaults1() {
    Message incomingChat = createChatPacket("134", true);
    incomingChat.setType(Type.chat);
    processServerMessage(incomingChat);
    assertNotNull(listener.getNewChat());
}
Also used : Message(org.jivesoftware.smack.packet.Message) Test(org.junit.Test)

Example 55 with Message

use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.

the class JivePropertiesExtensionTest method checkProvider.

@Test
public void checkProvider() throws Exception {
    // @formatter:off
    String properties = "<message from='romeo@example.net/orchard' to='juliet@example.com/balcony'>" + "<body>Neither, fair saint, if either thee dislike.</body>" + "<properties xmlns='http://www.jivesoftware.com/xmlns/xmpp/properties'>" + "<property>" + "<name>FooBar</name>" + "<value type='integer'>42</value>" + "</property>" + "</properties>" + "</message>";
    // @formatter:on
    Message message = (Message) PacketParserUtils.parseStanza(properties);
    JivePropertiesExtension jpe = JivePropertiesExtension.from(message);
    assertNotNull(jpe);
    Integer integer = (Integer) jpe.getProperty("FooBar");
    assertNotNull(integer);
    int fourtytwo = integer;
    assertEquals(42, fourtytwo);
}
Also used : Message(org.jivesoftware.smack.packet.Message) JivePropertiesExtension(org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension) Test(org.junit.Test)

Aggregations

Message (org.jivesoftware.smack.packet.Message)166 Test (org.junit.Test)57 Presence (org.jivesoftware.smack.packet.Presence)21 XMPPException (org.jivesoftware.smack.XMPPException)15 StanzaCollector (org.jivesoftware.smack.StanzaCollector)14 NetworkException (com.xabber.android.data.NetworkException)13 Stanza (org.jivesoftware.smack.packet.Stanza)13 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)13 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)12 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)12 AccountItem (com.xabber.android.data.account.AccountItem)11 XMPPConnection (org.jivesoftware.smack.XMPPConnection)11 Date (java.util.Date)10 Jid (org.jxmpp.jid.Jid)10 Chat (org.jivesoftware.smack.Chat)9 AccountJid (com.xabber.android.data.entity.AccountJid)7 ArrayList (java.util.ArrayList)7 XmlPullParser (org.xmlpull.v1.XmlPullParser)7 InputStream (java.io.InputStream)6 Forwarded (org.jivesoftware.smackx.forward.packet.Forwarded)6