Search in sources :

Example 31 with XMPPException

use of org.jivesoftware.smack.XMPPException in project camel by apache.

the class XmppPubSubProducer method process.

public void process(Exchange exchange) throws Exception {
    try {
        if (connection == null) {
            connection = endpoint.createConnection();
        }
        // make sure we are connected
        if (!connection.isConnected()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Reconnecting to: " + XmppEndpoint.getConnectionMessage(connection));
            }
            connection.connect();
        }
    } catch (XMPPException e) {
        throw new RuntimeExchangeException("Cannot connect to XMPP Server: " + ((connection != null) ? XmppEndpoint.getConnectionMessage(connection) : endpoint.getHost()), exchange, e);
    }
    try {
        Object body = exchange.getIn().getBody(Object.class);
        if (body instanceof PubSub) {
            PubSub pubsubpacket = (PubSub) body;
            endpoint.getBinding().populateXmppPacket(pubsubpacket, exchange);
            exchange.getIn().setHeader(XmppConstants.DOC_HEADER, pubsubpacket);
            connection.sendPacket(pubsubpacket);
        } else {
            throw new Exception("Message does not contain a pubsub packet");
        }
    } catch (XMPPException xmppe) {
        throw new RuntimeExchangeException("Cannot send XMPP pubsub: from " + endpoint.getUser() + " to: " + XmppEndpoint.getConnectionMessage(connection), exchange, xmppe);
    } catch (Exception e) {
        throw new RuntimeExchangeException("Cannot send XMPP pubsub: from " + endpoint.getUser() + " to: " + XmppEndpoint.getConnectionMessage(connection), exchange, e);
    }
}
Also used : PubSub(org.jivesoftware.smackx.pubsub.packet.PubSub) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) XMPPException(org.jivesoftware.smack.XMPPException) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 32 with XMPPException

use of org.jivesoftware.smack.XMPPException in project Openfire by igniterealtime.

the class XMPPPresenceHandler method handlePresenceMode.

/**
	 * Handles incoming presence stanzas that relate to presence status / mode
	 * changes. Ignores others.
	 * 
	 * @param presence
	 *            the stanza
	 */
private void handlePresenceMode(final org.jivesoftware.smack.packet.Presence presence) {
    if (!session.getBuddyManager().isActivated()) {
        session.getBuddyManager().storePendingStatus(session.getTransport().convertIDToJID(presence.getFrom()), ((XMPPTransport) session.getTransport()).convertXMPPStatusToGateway(presence.getType(), presence.getMode()), presence.getStatus());
    } else {
        // TODO: Need to handle resources and priorities!
        try {
            final XMPPBuddy xmppBuddy = session.getBuddyManager().getBuddy(session.getTransport().convertIDToJID(presence.getFrom()));
            Log.debug("XMPP: Presence changed detected type " + presence.getType() + " and mode " + presence.getMode() + " for " + presence.getFrom());
            xmppBuddy.setPresenceAndStatus(((XMPPTransport) session.getTransport()).convertXMPPStatusToGateway(presence.getType(), presence.getMode()), presence.getStatus());
            if (JiveGlobals.getBooleanProperty("plugin.gateway." + session.getTransport().getType() + ".avatars", true)) {
                PacketExtension pe = presence.getExtension("x", NameSpace.VCARD_TEMP_X_UPDATE);
                if (pe != null) {
                    DefaultPacketExtension dpe = (DefaultPacketExtension) pe;
                    String hash = dpe.getValue("photo");
                    final String from = presence.getFrom();
                    if (hash != null) {
                        Avatar curAvatar = xmppBuddy.getAvatar();
                        if (curAvatar == null || !curAvatar.getLegacyIdentifier().equals(hash)) {
                            new Thread() {

                                @Override
                                public void run() {
                                    VCard vcard = new VCard();
                                    try {
                                        vcard.load(session.conn, from);
                                        xmppBuddy.setAvatar(new Avatar(xmppBuddy.getJID(), from, vcard.getAvatar()));
                                    } catch (XMPPException e) {
                                        Log.debug("XMPP: Failed to load XMPP avatar: ", e);
                                    } catch (IllegalArgumentException e) {
                                        Log.debug("XMPP: Got null avatar, ignoring.");
                                    }
                                }
                            }.start();
                        }
                    }
                }
            }
        } catch (NotFoundException e) {
            Log.debug("XMPP: Received presence notification for contact that's not in the buddy manager of user " + session.getJID() + ". GTalk is known to do this occasionally: " + presence.getFrom());
        // We cannot add this buddy to the buddy manager, as that would result into an auto-accept of the contact sending the data.
        }
    }
}
Also used : PacketExtension(org.jivesoftware.smack.packet.PacketExtension) DefaultPacketExtension(org.jivesoftware.smack.packet.DefaultPacketExtension) DefaultPacketExtension(org.jivesoftware.smack.packet.DefaultPacketExtension) NotFoundException(org.jivesoftware.util.NotFoundException) XMPPException(org.jivesoftware.smack.XMPPException) VCard(org.jivesoftware.smackx.packet.VCard) Avatar(net.sf.kraken.avatars.Avatar)

Example 33 with XMPPException

use of org.jivesoftware.smack.XMPPException in project Smack by igniterealtime.

the class FileTransferTest method testFileTransfer.

public void testFileTransfer() throws Exception {
    final byte[] testTransfer = "This is a test transfer".getBytes();
    final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();
    FileTransferManager manager1 = new FileTransferManager(getConnection(0));
    manager1.addFileTransferListener(new FileTransferListener() {

        public void fileTransferRequest(final FileTransferRequest request) {
            new Thread(new Runnable() {

                public void run() {
                    IncomingFileTransfer transfer = request.accept();
                    InputStream stream;
                    try {
                        stream = transfer.recieveFile();
                    } catch (XMPPException e) {
                        exception = e;
                        return;
                    }
                    byte[] testRecieve = new byte[testTransfer.length];
                    int receiveCount = 0;
                    try {
                        while (receiveCount != -1) {
                            receiveCount = stream.read(testRecieve);
                        }
                    } catch (IOException e) {
                        exception = e;
                    } finally {
                        try {
                            stream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    try {
                        queue.put(testRecieve);
                    } catch (InterruptedException e) {
                        exception = e;
                    }
                }
            }).start();
        }
    });
    // Send the file from user1 to user0
    FileTransferManager manager2 = new FileTransferManager(getConnection(1));
    OutgoingFileTransfer outgoing = manager2.createOutgoingFileTransfer(getFullJID(0));
    OutputStream stream = outgoing.sendFile("test.txt", testTransfer.length, "The great work of robin hood");
    stream.write(testTransfer);
    stream.flush();
    stream.close();
    if (exception != null) {
        exception.printStackTrace();
        fail();
    }
    byte[] array = queue.take();
    assertEquals("Recieved file not equal to sent file.", testTransfer, array);
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) SynchronousQueue(java.util.concurrent.SynchronousQueue) XMPPException(org.jivesoftware.smack.XMPPException)

Example 34 with XMPPException

use of org.jivesoftware.smack.XMPPException in project Smack by igniterealtime.

the class LastActivityManagerTest method testOnlinePermisionDenied.

/**
	 * This is a test to check if a denied LastActivity response is handled correctly.
	 */
public void testOnlinePermisionDenied() {
    TCPConnection conn0 = getConnection(0);
    TCPConnection conn2 = getConnection(2);
    // Send a message as the last activity action from connection 2 to
    // connection 0
    conn2.sendStanza(new Message(getBareJID(0)));
    // Wait 1 seconds to have some idle time
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
        fail("Thread sleep interrupted");
    }
    try {
        LastActivityManager.getLastActivity(conn0, getFullJID(2));
        fail("No error was received from the server. User was able to get info of other user not in his roster.");
    } catch (XMPPException e) {
        assertNotNull("No error was returned from the server", e.getXMPPError());
        assertEquals("Forbidden error was not returned from the server", 403, e.getXMPPError().getCode());
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) TCPConnection(org.jivesoftware.smack.TCPConnection) XMPPException(org.jivesoftware.smack.XMPPException)

Example 35 with XMPPException

use of org.jivesoftware.smack.XMPPException in project Smack by igniterealtime.

the class MultipleRecipientManagerTest method testNoReply.

/**
     * Ensures that replying is not allowed when disabled.
     */
public void testNoReply() throws XMPPException {
    StanzaCollector collector1 = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
    StanzaCollector collector2 = getConnection(2).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
    StanzaCollector collector3 = getConnection(3).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
    // Send the intial message with multiple recipients
    Message message = new Message();
    message.setBody("Hola");
    List<String> to = Arrays.asList(new String[] { getBareJID(1) });
    List<String> cc = Arrays.asList(new String[] { getBareJID(2) });
    List<String> bcc = Arrays.asList(new String[] { getBareJID(3) });
    MultipleRecipientManager.send(getConnection(0), message, to, cc, bcc, null, null, true);
    // Get the message and ensure it's ok
    Message message1 = (Message) collector1.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection 1 never received the message", message1);
    MultipleRecipientInfo info = MultipleRecipientManager.getMultipleRecipientInfo(message1);
    assertNotNull("Message 1 does not contain MultipleRecipientInfo", info);
    assertTrue("Message 1 should be not 'replyable'", info.shouldNotReply());
    assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size());
    assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size());
    // Prepare and send the reply
    Message reply1 = new Message();
    reply1.setBody("This is my reply");
    try {
        MultipleRecipientManager.reply(getConnection(1), message1, reply1);
        fail("It was possible to send a reply to a not replyable message");
    } catch (XMPPException e) {
    // Exception was expected since replying was not allowed
    }
    // Check that connection2 recevied 1 messages
    message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection2 didn't receive the 1 message", message1);
    message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNull("XMPPConnection2 received 2 messages", message1);
    // Check that connection3 recevied only 1 message (was BCC in the first message)
    message1 = (Message) collector3.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection3 didn't receive the 1 message", message1);
    message1 = (Message) collector3.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNull("XMPPConnection2 received 2 messages", message1);
    collector1.cancel();
    collector2.cancel();
    collector3.cancel();
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) StanzaCollector(org.jivesoftware.smack.StanzaCollector) XMPPException(org.jivesoftware.smack.XMPPException)

Aggregations

XMPPException (org.jivesoftware.smack.XMPPException)61 ArrayList (java.util.ArrayList)15 JingleSessionRequestListener (org.jivesoftware.smackx.jingle.listeners.JingleSessionRequestListener)13 JingleMediaManager (org.jivesoftware.smackx.jingle.media.JingleMediaManager)13 IOException (java.io.IOException)11 SmackException (org.jivesoftware.smack.SmackException)11 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)9 XMPPConnection (org.jivesoftware.smack.XMPPConnection)9 Message (org.jivesoftware.smack.packet.Message)8 PayloadType (org.jivesoftware.smackx.jingle.media.PayloadType)6 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)5 FixedResolver (org.jivesoftware.smackx.jingle.nat.FixedResolver)5 FixedTransportManager (org.jivesoftware.smackx.jingle.nat.FixedTransportManager)5 TransportCandidate (org.jivesoftware.smackx.jingle.nat.TransportCandidate)5 Chat (org.jivesoftware.smack.Chat)4 TCPConnection (org.jivesoftware.smack.TCPConnection)4 Form (org.jivesoftware.smackx.Form)4 JingleSessionListener (org.jivesoftware.smackx.jingle.listeners.JingleSessionListener)4 JmfMediaManager (org.jivesoftware.smackx.jingle.mediaimpl.jmf.JmfMediaManager)4 ICETransportManager (org.jivesoftware.smackx.jingle.nat.ICETransportManager)4