Browse Source

Add utility methods and extend listener argument list

fallbackIndication
Paul Schaub 1 month ago
parent
commit
32e6f89f25
No known key found for this signature in database GPG Key ID: 62BEE9264BF17311
4 changed files with 61 additions and 12 deletions
  1. +9
    -1
      smack-experimental/src/main/java/org/jivesoftware/smackx/fallback_indication/FallbackIndicationListener.java
  2. +45
    -7
      smack-experimental/src/main/java/org/jivesoftware/smackx/fallback_indication/FallbackIndicationManager.java
  3. +2
    -2
      smack-experimental/src/main/java/org/jivesoftware/smackx/fallback_indication/element/FallbackIndicationElement.java
  4. +5
    -2
      smack-experimental/src/main/java/org/jivesoftware/smackx/fallback_indication/provider/FallbackIndicationElementProvider.java

+ 9
- 1
smack-experimental/src/main/java/org/jivesoftware/smackx/fallback_indication/FallbackIndicationListener.java View File

@@ -17,9 +17,17 @@
package org.jivesoftware.smackx.fallback_indication;

import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smackx.fallback_indication.element.FallbackIndicationElement;

public interface FallbackIndicationListener {

void onFallbackIndicationReceived(Message message);
/**
* Listener method that gets called when a {@link Message} containing a {@link FallbackIndicationElement} is received.
*
* @param message message
* @param indicator Fallback Indication
* @param fallbackBody body that is marked as fallback
*/
void onFallbackIndicationReceived(Message message, FallbackIndicationElement indicator, String fallbackBody);

}

+ 45
- 7
smack-experimental/src/main/java/org/jivesoftware/smackx/fallback_indication/FallbackIndicationManager.java View File

@@ -24,9 +24,11 @@ import java.util.concurrent.CopyOnWriteArraySet;
import org.jivesoftware.smack.AsyncButOrdered;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPConnectionRegistry;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.StanzaExtensionFilter;
import org.jivesoftware.smack.filter.StanzaFilter;
@@ -38,6 +40,7 @@ import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.fallback_indication.element.FallbackIndicationElement;

import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.EntityBareJid;

/**
* Smacks API for XEP-0428: Fallback Indication.
@@ -69,13 +72,13 @@ public final class FallbackIndicationManager extends Manager {
@Override
public void processStanza(Stanza packet) {
Message message = (Message) packet;
if (FallbackIndicationElement.hasFallbackIndication(message)) {
asyncButOrdered.performAsyncButOrdered(message.getFrom().asBareJid(), () -> {
for (FallbackIndicationListener l : listeners) {
l.onFallbackIndicationReceived((Message) packet);
}
});
}
FallbackIndicationElement indicator = FallbackIndicationElement.fromMessage(message);
String body = message.getBody();
asyncButOrdered.performAsyncButOrdered(message.getFrom().asBareJid(), () -> {
for (FallbackIndicationListener l : listeners) {
l.onFallbackIndicationReceived(message, indicator, body);
}
});
}
};

@@ -94,6 +97,41 @@ public final class FallbackIndicationManager extends Manager {
return manager;
}

/**
* Determine, whether or not a user supports Fallback Indications.
*
* @param jid BareJid of the user.
* @return feature support
*
* @throws XMPPException.XMPPErrorException if a protocol level error happens
* @throws SmackException.NotConnectedException if the connection is not connected
* @throws InterruptedException if the thread is being interrupted
* @throws SmackException.NoResponseException if the server doesn't send a response in time
*/
public boolean userSupportsFallbackIndications(EntityBareJid jid)
throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException,
SmackException.NoResponseException {
return ServiceDiscoveryManager.getInstanceFor(connection())
.supportsFeature(jid, FallbackIndicationElement.NAMESPACE);
}

/**
* Determine, whether or not the server supports Fallback Indications.
*
* @return server side feature support
*
* @throws XMPPException.XMPPErrorException if a protocol level error happens
* @throws SmackException.NotConnectedException if the connection is not connected
* @throws InterruptedException if the thread is being interrupted
* @throws SmackException.NoResponseException if the server doesn't send a response in time
*/
public boolean serverSupportsFallbackIndications()
throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException,
SmackException.NoResponseException {
return ServiceDiscoveryManager.getInstanceFor(connection())
.serverSupportsFeature(FallbackIndicationElement.NAMESPACE);
}

/**
* Set the body of the message to the provided fallback message and add a {@link FallbackIndicationElement}.
*


+ 2
- 2
smack-experimental/src/main/java/org/jivesoftware/smackx/fallback_indication/element/FallbackIndicationElement.java View File

@@ -42,10 +42,10 @@ public class FallbackIndicationElement implements ExtensionElement {
}

public static boolean hasFallbackIndication(Message message) {
return message.hasExtension(FallbackIndicationElement.ELEMENT, FallbackIndicationElement.NAMESPACE);
return message.hasExtension(ELEMENT, NAMESPACE);
}

public static FallbackIndicationElement fromMessage(Message message) {
return message.getExtension(FallbackIndicationElement.ELEMENT, FallbackIndicationElement.NAMESPACE);
return message.getExtension(ELEMENT, NAMESPACE);
}
}

+ 5
- 2
smack-experimental/src/main/java/org/jivesoftware/smackx/fallback_indication/provider/FallbackIndicationElementProvider.java View File

@@ -27,8 +27,11 @@ import org.jivesoftware.smackx.fallback_indication.element.FallbackIndicationEle

public class FallbackIndicationElementProvider extends ExtensionElementProvider<FallbackIndicationElement> {

private static final FallbackIndicationElement ELEMENT_INSTANCE = new FallbackIndicationElement();

@Override
public FallbackIndicationElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
return new FallbackIndicationElement();
public FallbackIndicationElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException {
return ELEMENT_INSTANCE;
}
}

Loading…
Cancel
Save