@Override public void processStanza(Stanza packet) { XMPPConnection conn = getConnection(); // we don't need this listener anymore conn.removeAsyncStanzaListener(this); DiscoverItems query = (DiscoverItems) packet; List<DiscoverItems.Item> items = query.getItems(); for (DiscoverItems.Item item : items) { DiscoverInfo info = new DiscoverInfo(); info.setTo(item.getEntityID()); StanzaFilter filter = new StanzaIdFilter(info.getStanzaId()); conn.addAsyncStanzaListener(new DiscoverInfoListener(getInstance()), filter); sendPacket(info); } }
@SuppressWarnings("deprecation") protected void bindResourceAndEstablishSession(String resource) throws XMPPErrorException, IOException, SmackException { // Wait until either: // - the servers last features stanza has been parsed // - the timeout occurs LOGGER.finer("Waiting for last features to be received before continuing with resource binding"); lastFeaturesReceived.checkIfSuccessOrWait(); if (!hasFeature(Bind.ELEMENT, Bind.NAMESPACE)) { // Server never offered resource binding, which is REQURIED in XMPP client and // server implementations as per RFC6120 7.2 throw new ResourceBindingNotOfferedException(); } // Resource binding, see RFC6120 7. // Note that we can not use IQReplyFilter here, since the users full JID is not yet // available. It will become available right after the resource has been successfully bound. Bind bindResource = Bind.newSet(resource); PacketCollector packetCollector = createPacketCollectorAndSend(new StanzaIdFilter(bindResource), bindResource); Bind response = packetCollector.nextResultOrThrow(); // Set the connections user to the result of resource binding. It is important that we don't infer the user // from the login() arguments and the configurations service name, as, for example, when SASL External is used, // the username is not given to login but taken from the 'external' certificate. user = response.getJid(); serviceName = XmppStringUtils.parseDomain(user); Session.Feature sessionFeature = getFeature(Session.ELEMENT, Session.NAMESPACE); // Only bind the session if it's announced as stream feature by the server, is not optional and not disabled // For more information see http://tools.ietf.org/html/draft-cridland-xmpp-session-01 if (sessionFeature != null && !sessionFeature.isOptional() && !getConfiguration().isLegacySessionDisabled()) { Session session = new Session(); packetCollector = createPacketCollectorAndSend(new StanzaIdFilter(session), session); packetCollector.nextResultOrThrow(); } }
@Test public void abstractListFilterToStringTest() { AndFilter andFilter = new AndFilter(); andFilter.addFilter(new StanzaIdFilter("foo")); andFilter.addFilter(new ThreadFilter("42")); andFilter.addFilter(MessageWithBodiesFilter.INSTANCE); final String res =andFilter.toString(); assertEquals("AndFilter: (StanzaIdFilter: id=foo, ThreadFilter: thread=42, MessageWithBodiesFilter)", res); }
protected void setupConnectedReceiver() { if (mConnReceiver == null) { mConnReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { // unregister the broadcast receiver unregisterReceiver(mConnReceiver); mConnReceiver = null; // prepare public key packet Stanza iq = prepareKeyPacket(); if (iq != null) { // setup packet filter for response StanzaFilter filter = new StanzaIdFilter(iq.getStanzaId()); getConnection().addAsyncStanzaListener(RegisterKeyPairListener.this, filter); // send the key out sendPacket(iq); // now wait for a response } // TODO else? } }; IntentFilter filter = new IntentFilter(ACTION_CONNECTED); registerReceiver(mConnReceiver, filter); } }
public void uploadAndListen() { configure(); // prepare public key packet Stanza iq = prepareKeyPacket(); // setup packet filter for response StanzaFilter filter = new StanzaIdFilter(iq.getStanzaId()); getConnection().addAsyncStanzaListener(this, filter); // send the key out sendPacket(iq); // now wait for a response }
@CommandHandler(name = ACTION_SERVERLIST) private boolean handleServerList(boolean canConnect) { if (canConnect && isConnected()) { ServerlistCommand p = new ServerlistCommand(); p.setTo(XmppStringUtils.completeJidFrom("network", mServer.getNetwork())); StanzaFilter filter = new StanzaIdFilter(p.getStanzaId()); // TODO cache the listener (it shouldn't change) mConnection.addAsyncStanzaListener(new StanzaListener() { public void processStanza(Stanza packet) throws NotConnectedException { Intent i = new Intent(ACTION_SERVERLIST); List<String> _items = ((ServerlistCommand.ServerlistCommandData) packet) .getItems(); if (_items != null && _items.size() != 0 && packet.getError() == null) { String[] items = new String[_items.size()]; _items.toArray(items); i.putExtra(EXTRA_FROM, packet.getFrom().toString()); i.putExtra(EXTRA_JIDLIST, items); } mLocalBroadcastManager.sendBroadcast(i); } }, filter); sendPacket(p); } return false; }
private PacketCollector createPacketCollectorAndSend(IQ req) throws NotConnectedException { PacketCollector collector = connection().createPacketCollectorAndSend(new StanzaIdFilter(req.getStanzaId()), req); return collector; }
@Override public void processStanza(Stanza packet) { XMPPConnection conn = getConnection(); EndpointServer server = getServer(); // we don't need this listener anymore conn.removeAsyncStanzaListener(this); DiscoverInfo query = (DiscoverInfo) packet; List<DiscoverInfo.Feature> features = query.getFeatures(); for (DiscoverInfo.Feature feat : features) { /* * TODO do not request info about push if disabled by user. * Of course if user enables push notification we should * reissue this discovery again. */ if (PushRegistration.NAMESPACE.equals(feat.getVar())) { // push notifications are enabled on this server // request items to check if gcm is supported and obtain the server id DiscoverItems items = new DiscoverItems(); items.setNode(PushRegistration.NAMESPACE); items.setTo(server.getNetwork()); StanzaFilter filter = new StanzaIdFilter(items.getStanzaId()); conn.addAsyncStanzaListener(new PushDiscoverItemsListener(getInstance()), filter); sendPacket(items); } /* * TODO upload info should be requested only when needed and * cached. This discovery should of course be issued before any * media message gets requeued. * Actually, delay any message from being requeued if at least * 1 media message is present; do the discovery first. */ else if (HTTPFileUpload.NAMESPACE.equals(feat.getVar())) { Log.d(MessageCenterService.TAG, "got upload service: " + packet.getFrom()); addUploadService(new HTTPFileUploadService(conn, packet.getFrom().asBareJid()), 0); // resend pending messages resendPendingMessages(true, false); } } }
private void sendPrivacyListCommand(final String to, final int action) { IQ p; if (action == PRIVACY_BLOCK || action == PRIVACY_REJECT) { // blocking command: block p = BlockingCommand.block(to); } else if (action == PRIVACY_UNBLOCK) { // blocking command: block p = BlockingCommand.unblock(to); } else { // unsupported action throw new IllegalArgumentException("unsupported action: " + action); } if (action == PRIVACY_REJECT) { // send unsubscribed too Presence unsub = new Presence(Presence.Type.unsubscribe); unsub.setTo(to); sendPacket(unsub); } // setup packet filter for response StanzaFilter filter = new StanzaIdFilter(p.getStanzaId()); StanzaListener listener = new StanzaListener() { public void processStanza(Stanza packet) { if (packet instanceof IQ && ((IQ) packet).getType() == IQ.Type.result) { UsersProvider.setBlockStatus(MessageCenterService.this, to, action == PRIVACY_BLOCK || action == PRIVACY_REJECT); // invalidate cached contact Contact.invalidate(to); // broadcast result broadcast((action == PRIVACY_BLOCK || action == PRIVACY_REJECT) ? ACTION_BLOCKED : ACTION_UNBLOCKED, EXTRA_FROM, to); } } }; mConnection.addAsyncStanzaListener(listener, filter); // send IQ sendPacket(p); }