use of org.jivesoftware.smackx.mood.element.MoodElement in project Smack by igniterealtime.
the class MoodManager method clearMood.
public void clearMood() throws InterruptedException, SmackException.NotLoggedInException, SmackException.NoResponseException, SmackException.NotConnectedException, XMPPException.XMPPErrorException, PubSubException.NotALeafNodeException {
MoodElement element = buildMood(null, null, null);
publishMood(element);
}
use of org.jivesoftware.smackx.mood.element.MoodElement in project Smack by igniterealtime.
the class MoodProvider method parse.
@Override
public MoodElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
String text = null;
Mood mood = null;
MoodConcretisation concretisation = null;
outerloop: while (true) {
XmlPullParser.Event tag = parser.next();
switch(tag) {
case START_ELEMENT:
String name = parser.getName();
String namespace = parser.getNamespace();
if (MoodElement.ELEM_TEXT.equals(name)) {
text = parser.nextText();
continue outerloop;
}
if (!MoodElement.NAMESPACE.equals(namespace)) {
LOGGER.log(Level.FINE, "Foreign namespace " + namespace + " detected. Try to find suitable MoodConcretisationProvider.");
MoodConcretisationProvider<?> provider = (MoodConcretisationProvider<?>) ProviderManager.getExtensionProvider(name, namespace);
if (provider != null) {
concretisation = provider.parse(parser);
} else {
LOGGER.log(Level.FINE, "No provider for <" + name + " xmlns:'" + namespace + "'/> found. Ignore.");
}
continue outerloop;
}
try {
mood = Mood.valueOf(name);
continue outerloop;
} catch (IllegalArgumentException e) {
throw new XmlPullParserException("Unknown mood value: " + name + " encountered.");
}
case END_ELEMENT:
if (MoodElement.ELEMENT.equals(parser.getName())) {
MoodElement.MoodSubjectElement subjectElement = (mood == null && concretisation == null) ? null : new MoodElement.MoodSubjectElement(mood, concretisation);
return new MoodElement(subjectElement, text);
}
break;
default:
// Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
break;
}
}
}
use of org.jivesoftware.smackx.mood.element.MoodElement in project Smack by igniterealtime.
the class MoodIntegrationTest method registerListenerAndWait.
/**
* Registers a listener for User Tune data. This implicitly publishes a CAPS update to include a notification
* filter for the mood node. This method blocks until the server has indicated that this update has been
* received.
*
* @param moodManager The MoodManager instance for the connection that is expected to receive data.
* @param discoManager The ServiceDiscoveryManager instance for the connection that is expected to publish data.
* @param listener A listener instance for Mood data that is to be registered.
*
* @throws Exception if the test fails
*/
public void registerListenerAndWait(MoodManager moodManager, ServiceDiscoveryManager discoManager, PepEventListener<MoodElement> listener) throws Exception {
final SimpleResultSyncPoint notificationFilterReceived = new SimpleResultSyncPoint();
final EntityCapabilitiesChangedListener notificationFilterReceivedListener = info -> {
if (info.containsFeature(MoodManager.MOOD_NODE + "+notify")) {
notificationFilterReceived.signal();
}
};
discoManager.addEntityCapabilitiesChangedListener(notificationFilterReceivedListener);
try {
moodManager.addMoodListener(listener);
notificationFilterReceived.waitForResult(timeout);
} finally {
discoManager.removeEntityCapabilitiesChangedListener(notificationFilterReceivedListener);
}
}
use of org.jivesoftware.smackx.mood.element.MoodElement in project Smack by igniterealtime.
the class MoodIntegrationTest method testNotificationAfterFilterChange.
/**
* Verifies that a notification for a previously sent publication is received as soon as notification filtering
* has been adjusted to allow for the notification to be delivered.
*
* @throws Exception if the test fails
*/
@SmackIntegrationTest
public void testNotificationAfterFilterChange() throws Exception {
Mood data = Mood.cautious;
IntegrationTestRosterUtil.ensureBothAccountsAreSubscribedToEachOther(conOne, conTwo, timeout);
final SimpleResultSyncPoint moodReceived = new SimpleResultSyncPoint();
final PepEventListener<MoodElement> moodListener = (jid, moodElement, id, message) -> {
if (moodElement.getMood().equals(data)) {
moodReceived.signal();
}
};
// TODO Ensure that pre-existing filtering notification excludes mood.
try {
// Publish the data
publishAndWait(mm1, ServiceDiscoveryManager.getInstanceFor(conOne), data);
// Adds listener, which implicitly publishes a disco/info filter for mood notification.
registerListenerAndWait(mm2, ServiceDiscoveryManager.getInstanceFor(conTwo), moodListener);
// Wait for the data to be received.
try {
Object result = moodReceived.waitForResult(timeout);
// Explicitly assert the success case.
Assertions.assertNotNull(result, "Expected to receive a PEP notification, but did not.");
} catch (TimeoutException e) {
Assertions.fail("Expected to receive a PEP notification, but did not.");
}
} finally {
unregisterListener(mm2, moodListener);
}
}
use of org.jivesoftware.smackx.mood.element.MoodElement in project Smack by igniterealtime.
the class MoodConcretisationTest method concretisationTest.
@Test
public void concretisationTest() throws Exception {
ProviderManager.addExtensionProvider(EcstaticMoodConcretisation.ELEMENT, EcstaticMoodConcretisation.NAMESPACE, EcstaticMoodConcretisationProvider.INSTANCE);
String xml = "<mood xmlns='http://jabber.org/protocol/mood'>" + "<happy>" + "<ecstatic xmlns='https://example.org/'/>" + "</happy>" + "<text>Yay, the mood spec has been approved!</text>" + "</mood>";
MoodElement element = new MoodElement(new MoodElement.MoodSubjectElement(Mood.happy, new EcstaticMoodConcretisation()), "Yay, the mood spec has been approved!");
assertXmlSimilar(xml, element.toXML().toString());
XmlPullParser parser = TestUtils.getParser(xml);
MoodElement parsed = MoodProvider.INSTANCE.parse(parser);
assertXmlSimilar(xml, parsed.toXML().toString());
assertTrue(parsed.hasConcretisation());
assertTrue(parsed.hasText());
assertEquals(EcstaticMoodConcretisation.ELEMENT, parsed.getMoodConcretisation().getMood());
}
Aggregations