private static Consumer<ByteStreamEvent> createSessionListener(final String sessionId, final CompletableFuture<ByteStreamSession> completableFuture) { return e -> { if (sessionId.equals(e.getSessionId())) { // Auto-accept the inbound stream e.accept().whenComplete((byteStreamSession, throwable) -> { if (throwable != null) { completableFuture.completeExceptionally(throwable); } else { completableFuture.complete(byteStreamSession); } }); } }; }
/** * Discovers the capabilities of another XMPP entity. * * @param jid The JID, which should usually be a full JID. * @return The async result with the capabilities in form of a info node, which contains the identities, the features and service discovery extensions. * @see <a href="https://xmpp.org/extensions/xep-0115.html#discover">6.2 Discovering Capabilities</a> */ public final AsyncResult<InfoNode> discoverCapabilities(Jid jid) { InfoNode infoNode = ENTITY_CAPABILITIES.get(jid); if (infoNode == null) { // Make sure, that for the same JID no multiple concurrent queries are sent. One is enough. return REQUESTS.computeIfAbsent(jid, key -> serviceDiscoveryManager.discoverInformation(jid) .whenComplete((result, e) -> { if (result != null) { ENTITY_CAPABILITIES.put(jid, result); } REQUESTS.remove(jid); })); } return new AsyncResult<>(CompletableFuture.completedFuture(infoNode)); }
private <S extends Stanza, E extends EventObject> AsyncResult<S> sendAndAwait(S stanza, Function<E, S> stanzaMapper, final Predicate<S> filter, Function<S, SendTask<S>> sendFunction, Consumer<Consumer<E>> addListener, Consumer<Consumer<E>> removeListener, Duration timeout) { CompletableFuture<S> completableFuture = new CompletableFuture<>(); final Consumer<E> listener = e -> { S st = stanzaMapper.apply(e); if (filter.test(st)) { if (st.getError() != null) { completableFuture.completeExceptionally(new StanzaErrorException(st)); } completableFuture.complete(st); } }; addListener.accept(listener); SendTask<S> sendTask = sendFunction.apply(stanza); // When the sending failed, immediately complete the future with the exception. sendTask.onFailed((throwable, s) -> completableFuture.completeExceptionally(throwable)); return new AsyncResult<>(completableFuture // When a response has received, mark the requesting stanza as acknowledged. // This is especially important for Bind and Roster IQs, so that they won't be resend after login. .whenComplete((result, e) -> removeFromQueue(sendTask.getStanza())) .applyToEither(CompletionStages.timeoutAfter(timeout.toMillis(), TimeUnit.MILLISECONDS, () -> new NoResponseException("Timeout reached, while waiting on a response for request: " + stanza)), Function.identity())) // When either a timeout happened or response has received, remove the listener. .whenComplete((result, e) -> removeListener.accept(listener)); }
serviceDiscoveryManager.discoverInformation(entity, nodeToDiscover).whenComplete((infoDiscovery, e1) -> { if (e1 != null) { processNextHash(entityCapabilities, hashedIterator, entity, caps); serviceDiscoveryManager.discoverInformation(entity, nodeToDiscover).whenComplete((result, e2) -> { if (e2 != null) { logger.log(Level.WARNING, "Failed to discover information for entity '{0}' for node '{1}'", new Object[]{entity, nodeToDiscover});
/** * Publishes an avatar to your VCard. * * @param imageData The avatar image data, which must be in PNG format. {@code null} resets the avatar. * @return The async result. * @see <a href="https://xmpp.org/extensions/xep-0153.html#publish">3.1 User Publishes Avatar</a> */ public final AsyncResult<Void> publishAvatar(byte[] imageData) { String hash = imageData != null ? XmppUtils.hash(imageData) : null; AvatarMetadata.Info info = imageData != null ? new AvatarMetadata.Info(imageData.length, hash, hash) : null; // Try publishing to vCard first. If this fails, don't immediately throw an exception, but try PEP first. return publishToVCard(imageData, null, hash) .whenComplete((result, e) -> { if (e != null) { logger.warning("Failed to publish avatar to vCard."); } }) .thenCompose((aVoid) -> publishToPersonalEventingService(imageData, hash, info)) .thenRun(() -> { }); }
Jid room = presence.getFrom().asBareJid(); return room.equals(roomJid) && isSelfPresence(presence, nick); }).whenComplete((presence, e) -> { if (e != null) { userHasExited();
)).whenComplete((byteStreamSession, throwable) -> {
return xmppSession.query(IQ.set(target, new Socks5ByteStream(sessionId, streamHosts, hash))).whenComplete((a, e) ->
} else { getAvatarByVCard(contact).whenComplete((avatarResult, ex) -> { if (ex == null) { notifyListeners(contact, avatarResult); getAvatarByVCard(xmppSession.getConnectedResource().asBareJid()).whenComplete((avatarResult, ex) -> { if (ex == null) { notifyListeners(xmppSession.getConnectedResource().asBareJid(), avatarResult); pubSubService.node(AvatarData.NAMESPACE).getItems(item.getId()).whenComplete((items, ex) -> { if (ex != null) { logger.log(Level.WARNING, () -> String.format("Failed to retrieve avatar '%s' from PEP service for user '%s'", item.getId(), message.getFrom()));