Search in sources :

Example 1 with AndFilter

use of org.jivesoftware.smack.filter.AndFilter in project camel by apache.

the class XmppConsumer method doStart.

@Override
protected void doStart() throws Exception {
    try {
        connection = endpoint.createConnection();
    } catch (SmackException e) {
        if (endpoint.isTestConnectionOnStartup()) {
            throw new RuntimeException("Could not connect to XMPP server.", e);
        } else {
            LOG.warn(e.getMessage());
            if (getExceptionHandler() != null) {
                getExceptionHandler().handleException(e.getMessage(), e);
            }
            scheduleDelayedStart();
            return;
        }
    }
    chatManager = ChatManager.getInstanceFor(connection);
    chatManager.addChatListener(this);
    OrFilter pubsubPacketFilter = new OrFilter();
    if (endpoint.isPubsub()) {
        //xep-0060: pubsub#notification_type can be 'headline' or 'normal'
        pubsubPacketFilter.addFilter(new MessageTypeFilter(Type.headline));
        pubsubPacketFilter.addFilter(new MessageTypeFilter(Type.normal));
        connection.addPacketListener(this, pubsubPacketFilter);
    }
    if (endpoint.getRoom() == null) {
        privateChat = chatManager.getThreadChat(endpoint.getChatId());
        if (privateChat != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Adding listener to existing chat opened to " + privateChat.getParticipant());
            }
            privateChat.addMessageListener(this);
        } else {
            privateChat = ChatManager.getInstanceFor(connection).createChat(endpoint.getParticipant(), endpoint.getChatId(), this);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Opening private chat to " + privateChat.getParticipant());
            }
        }
    } else {
        // add the presence packet listener to the connection so we only get packets that concerns us
        // we must add the listener before creating the muc
        final AndFilter packetFilter = new AndFilter(new PacketTypeFilter(Presence.class));
        connection.addPacketListener(this, packetFilter);
        muc = new MultiUserChat(connection, endpoint.resolveRoom(connection));
        muc.addMessageListener(this);
        DiscussionHistory history = new DiscussionHistory();
        // we do not want any historical messages
        history.setMaxChars(0);
        muc.join(endpoint.getNickname(), null, history, SmackConfiguration.getDefaultPacketReplyTimeout());
        if (LOG.isInfoEnabled()) {
            LOG.info("Joined room: {} as: {}", muc.getRoom(), endpoint.getNickname());
        }
    }
    this.startRobustConnectionMonitor();
    super.doStart();
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) AndFilter(org.jivesoftware.smack.filter.AndFilter) MultiUserChat(org.jivesoftware.smackx.muc.MultiUserChat) DiscussionHistory(org.jivesoftware.smackx.muc.DiscussionHistory) SmackException(org.jivesoftware.smack.SmackException) Presence(org.jivesoftware.smack.packet.Presence) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) OrFilter(org.jivesoftware.smack.filter.OrFilter)

Example 2 with AndFilter

use of org.jivesoftware.smack.filter.AndFilter in project Spark by igniterealtime.

the class ChatContainer method addChatRoom.

/**
 * Adds a new ChatRoom to Spark.
 *
 * @param room the ChatRoom to add.
 */
public synchronized void addChatRoom(final ChatRoom room) {
    createFrameIfNeeded();
    room.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.LIGHT_GRAY));
    AndFilter presenceFilter = new AndFilter(new StanzaTypeFilter(Presence.class), FromMatchesFilter.createBare(room.getRoomname()));
    // Next, create a packet listener. We use an anonymous inner class for brevity.
    StanzaListener myListener = stanza -> SwingUtilities.invokeLater(() -> handleRoomPresence((Presence) stanza));
    room.registeredToFrame(chatFrame);
    SparkManager.getConnection().addAsyncStanzaListener(myListener, presenceFilter);
    // Add to PresenceMap
    presenceMap.put(room.getRoomname(), myListener);
    String tooltip;
    if (room instanceof ChatRoomImpl) {
        tooltip = ((ChatRoomImpl) room).getParticipantJID();
        String nickname = SparkManager.getUserManager().getUserNicknameFromJID(((ChatRoomImpl) room).getParticipantJID());
        tooltip = "<html><body><b>Contact:&nbsp;</b>" + nickname + "<br><b>JID:&nbsp;</b>" + tooltip;
    } else {
        tooltip = room.getRoomname();
    }
    // Create ChatRoom UI and dock
    SparkTab tab = addTab(room.getTabTitle(), room.getTabIcon(), room, tooltip);
    tab.addMouseListener(new MouseAdapter() {

        public void mouseReleased(MouseEvent e) {
            checkTabPopup(e);
        }

        public void mousePressed(MouseEvent e) {
            checkTabPopup(e);
        }
    });
    room.addMessageListener(this);
    // Remove brand panel
    final String title = getTabAt(0).getActualText();
    if (title.equals(WELCOME_TITLE)) {
        chatFrame.setTitle(room.getRoomTitle());
    }
    final TimerTask visibleTask = new SwingTimerTask() {

        public void doRun() {
            checkVisibility(room);
        }
    };
    TaskEngine.getInstance().schedule(visibleTask, 100);
    // Add to ChatRoomList
    chatRoomList.add(room);
    // Notify users that the chat room has been opened.
    fireChatRoomOpened(room);
    // Focus Chat
    focusChat();
    // Add Room listeners to override issue with input maps and keybinding on the mac.
    if (Spark.isMac()) {
        room.getChatInputEditor().addKeyListener(this);
    }
}
Also used : java.util(java.util) SparkTabbedPaneListener(org.jivesoftware.spark.component.tabbedPane.SparkTabbedPaneListener) SettingsManager(org.jivesoftware.sparkimpl.settings.local.SettingsManager) Res(org.jivesoftware.resource.Res) Log(org.jivesoftware.spark.util.log.Log) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl) MainWindow(org.jivesoftware.MainWindow) SparkToaster(org.jivesoftware.sparkimpl.plugin.alerts.SparkToaster) ChangeListener(javax.swing.event.ChangeListener) GroupChatRoomListener(org.jivesoftware.spark.ui.conferences.GroupChatRoomListener) AndFilter(org.jivesoftware.smack.filter.AndFilter) TaskEngine(org.jivesoftware.spark.util.TaskEngine) ChangeEvent(javax.swing.event.ChangeEvent) FromMatchesFilter(org.jivesoftware.smack.filter.FromMatchesFilter) Presence(org.jivesoftware.smack.packet.Presence) ChatManager(org.jivesoftware.spark.ChatManager) LocalPreferences(org.jivesoftware.sparkimpl.settings.local.LocalPreferences) SparkRes(org.jivesoftware.resource.SparkRes) SwingWorker(org.jivesoftware.spark.util.SwingWorker) SparkTabbedPane(org.jivesoftware.spark.component.tabbedPane.SparkTabbedPane) SparkTab(org.jivesoftware.spark.component.tabbedPane.SparkTab) GroupChatRoom(org.jivesoftware.spark.ui.rooms.GroupChatRoom) XmppStringUtils(org.jxmpp.util.XmppStringUtils) Spark(org.jivesoftware.Spark) java.awt(java.awt) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) List(java.util.List) SparkManager(org.jivesoftware.spark.SparkManager) java.awt.event(java.awt.event) SwingTimerTask(org.jivesoftware.spark.util.SwingTimerTask) Message(org.jivesoftware.smack.packet.Message) Pattern(java.util.regex.Pattern) StanzaListener(org.jivesoftware.smack.StanzaListener) javax.swing(javax.swing) AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) SparkTab(org.jivesoftware.spark.component.tabbedPane.SparkTab) SwingTimerTask(org.jivesoftware.spark.util.SwingTimerTask) SwingTimerTask(org.jivesoftware.spark.util.SwingTimerTask) Presence(org.jivesoftware.smack.packet.Presence) StanzaListener(org.jivesoftware.smack.StanzaListener) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl)

Example 3 with AndFilter

use of org.jivesoftware.smack.filter.AndFilter in project ecf by eclipse.

the class MultiUserChat method getRegistrationForm.

/**
 * Returns the room's registration form that an unaffiliated user, can use to become a member
 * of the room or <tt>null</tt> if no registration is possible. Some rooms may restrict the
 * privilege to register members and allow only room admins to add new members.<p>
 *
 * If the user requesting registration requirements is not allowed to register with the room
 * (e.g. because that privilege has been restricted), the room will return a "Not Allowed"
 * error to the user (error code 405).
 *
 * @return the registration Form that contains the fields to complete together with the
 * instrucions or <tt>null</tt> if no registration is possible.
 * @throws XMPPException if an error occurs asking the registration form for the room or a
 * 405 error if the user is not allowed to register with the room.
 */
public Form getRegistrationForm() throws XMPPException {
    Registration reg = new Registration();
    reg.setType(IQ.Type.GET);
    reg.setTo(room);
    PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class));
    PacketCollector collector = connection.createPacketCollector(filter);
    connection.sendPacket(reg);
    IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    collector.cancel();
    if (result == null) {
        throw new XMPPException("No response from server.");
    } else if (result.getType() == IQ.Type.ERROR) {
        throw new XMPPException(result.getError());
    }
    return Form.getFormFrom(result);
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) Registration(org.jivesoftware.smack.packet.Registration) IQ(org.jivesoftware.smack.packet.IQ) PacketCollector(org.jivesoftware.smack.PacketCollector) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) XMPPException(org.jivesoftware.smack.XMPPException) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 4 with AndFilter

use of org.jivesoftware.smack.filter.AndFilter in project ecf by eclipse.

the class EntityCapsManager method init.

private void init() {
    Connection connection = weakRefConnection.get();
    instances.put(connection, this);
    connection.addConnectionListener(new ConnectionListener() {

        public void connectionClosed() {
            // Unregister this instance since the connection has been closed
            presenceSend = false;
            instances.remove(weakRefConnection.get());
        }

        public void connectionClosedOnError(Exception e) {
            presenceSend = false;
        }

        public void reconnectionFailed(Exception e) {
        // ignore
        }

        public void reconnectingIn(int seconds) {
        // ignore
        }

        public void reconnectionSuccessful() {
        // ignore
        }
    });
    // This calculates the local entity caps version
    updateLocalEntityCaps();
    if (SmackConfiguration.autoEnableEntityCaps())
        enableEntityCaps();
    PacketFilter packetFilter = new AndFilter(new PacketTypeFilter(Presence.class), new PacketExtensionFilter(ELEMENT, NAMESPACE));
    connection.addPacketListener(new PacketListener() {

        // Listen for remote presence stanzas with the caps extension
        // If we receive such a stanza, record the JID and nodeVer
        public void processPacket(Packet packet) {
            if (!entityCapsEnabled())
                return;
            CapsExtension ext = (CapsExtension) packet.getExtension(EntityCapsManager.ELEMENT, EntityCapsManager.NAMESPACE);
            String hash = ext.getHash().toLowerCase();
            if (!SUPPORTED_HASHES.containsKey(hash))
                return;
            String from = packet.getFrom();
            String node = ext.getNode();
            String ver = ext.getVer();
            jidCaps.put(from, new NodeVerHash(node, ver, hash));
        }
    }, packetFilter);
    packetFilter = new AndFilter(new PacketTypeFilter(Presence.class), new NotFilter(new PacketExtensionFilter(ELEMENT, NAMESPACE)));
    connection.addPacketListener(new PacketListener() {

        public void processPacket(Packet packet) {
            // always remove the JID from the map, even if entityCaps are
            // disabled
            String from = packet.getFrom();
            jidCaps.remove(from);
        }
    }, packetFilter);
    packetFilter = new PacketTypeFilter(Presence.class);
    connection.addPacketSendingListener(new PacketListener() {

        public void processPacket(Packet packet) {
            presenceSend = true;
        }
    }, packetFilter);
    // Intercept presence packages and add caps data when intended.
    // XEP-0115 specifies that a client SHOULD include entity capabilities
    // with every presence notification it sends.
    PacketFilter capsPacketFilter = new PacketTypeFilter(Presence.class);
    PacketInterceptor packetInterceptor = new PacketInterceptor() {

        public void interceptPacket(Packet packet) {
            if (!entityCapsEnabled)
                return;
            CapsExtension caps = new CapsExtension(ENTITY_NODE, getCapsVersion(), "sha-1");
            packet.addExtension(caps);
        }
    };
    connection.addPacketInterceptor(packetInterceptor, capsPacketFilter);
    // It's important to do this as last action. Since it changes the
    // behavior of the SDM in some ways
    sdm.setEntityCapsManager(this);
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) Connection(org.jivesoftware.smack.Connection) XMPPConnection(org.jivesoftware.smack.XMPPConnection) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) ConnectionListener(org.jivesoftware.smack.ConnectionListener) CapsExtension(org.jivesoftware.smackx.entitycaps.packet.CapsExtension) PacketListener(org.jivesoftware.smack.PacketListener) PacketInterceptor(org.jivesoftware.smack.PacketInterceptor) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XMPPException(org.jivesoftware.smack.XMPPException) AndFilter(org.jivesoftware.smack.filter.AndFilter) PacketExtensionFilter(org.jivesoftware.smack.filter.PacketExtensionFilter) NotFilter(org.jivesoftware.smack.filter.NotFilter) Presence(org.jivesoftware.smack.packet.Presence)

Example 5 with AndFilter

use of org.jivesoftware.smack.filter.AndFilter in project ecf by eclipse.

the class FileTransferManager method initListeners.

private void initListeners() {
    listeners = new ArrayList<FileTransferListener>();
    connection.addPacketListener(new PacketListener() {

        public void processPacket(Packet packet) {
            fireNewRequest((StreamInitiation) packet);
        }
    }, new AndFilter(new PacketTypeFilter(StreamInitiation.class), new IQTypeFilter(IQ.Type.SET)));
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) StreamInitiation(org.jivesoftware.smackx.packet.StreamInitiation) AndFilter(org.jivesoftware.smack.filter.AndFilter) IQTypeFilter(org.jivesoftware.smack.filter.IQTypeFilter) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) PacketListener(org.jivesoftware.smack.PacketListener)

Aggregations

AndFilter (org.jivesoftware.smack.filter.AndFilter)34 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)15 Presence (org.jivesoftware.smack.packet.Presence)15 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)13 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)9 StanzaCollector (org.jivesoftware.smack.StanzaCollector)8 Message (org.jivesoftware.smack.packet.Message)8 XMPPException (org.jivesoftware.smack.XMPPException)7 FromMatchesFilter (org.jivesoftware.smack.filter.FromMatchesFilter)7 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)7 IQ (org.jivesoftware.smack.packet.IQ)7 StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)6 Registration (org.jivesoftware.smack.packet.Registration)6 PacketCollector (org.jivesoftware.smack.PacketCollector)5 Packet (org.jivesoftware.smack.packet.Packet)5 Stanza (org.jivesoftware.smack.packet.Stanza)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 SmackIntegrationTest (org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)4 MUCInitialPresence (org.jivesoftware.smackx.packet.MUCInitialPresence)4