2003-08-13 14:59:24 +02:00
|
|
|
/**
|
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2003-08-13 14:59:24 +02:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2004-11-03 00:37:00 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2003-08-13 14:59:24 +02:00
|
|
|
*
|
2004-11-03 00:37:00 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-08-13 14:59:24 +02:00
|
|
|
*
|
2004-11-03 00:37:00 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2003-08-13 14:59:24 +02:00
|
|
|
*/
|
|
|
|
|
2014-02-15 11:35:08 +01:00
|
|
|
package org.jivesoftware.smackx.xevent;
|
2003-08-13 14:59:24 +02:00
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
2009-06-04 15:09:25 +02:00
|
|
|
import java.util.List;
|
2014-05-29 21:04:56 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.WeakHashMap;
|
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
2014-02-02 23:39:07 +01:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2003-08-13 14:59:24 +02:00
|
|
|
|
2014-05-29 21:04:56 +02:00
|
|
|
import org.jivesoftware.smack.Manager;
|
2014-03-19 14:22:20 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
2017-06-14 17:12:43 +02:00
|
|
|
import org.jivesoftware.smack.StanzaListener;
|
2014-03-10 09:45:50 +01:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2014-06-10 18:56:50 +02:00
|
|
|
import org.jivesoftware.smack.filter.AndFilter;
|
|
|
|
import org.jivesoftware.smack.filter.MessageTypeFilter;
|
|
|
|
import org.jivesoftware.smack.filter.NotFilter;
|
2015-02-26 18:41:17 +01:00
|
|
|
import org.jivesoftware.smack.filter.StanzaExtensionFilter;
|
|
|
|
import org.jivesoftware.smack.filter.StanzaFilter;
|
2009-06-04 15:09:25 +02:00
|
|
|
import org.jivesoftware.smack.packet.Message;
|
2015-02-05 11:17:27 +01:00
|
|
|
import org.jivesoftware.smack.packet.Stanza;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2014-02-15 11:35:08 +01:00
|
|
|
import org.jivesoftware.smackx.xevent.packet.MessageEvent;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2015-02-14 17:15:02 +01:00
|
|
|
import org.jxmpp.jid.Jid;
|
2003-08-13 14:59:24 +02:00
|
|
|
|
|
|
|
/**
|
2014-05-29 21:04:56 +02:00
|
|
|
*
|
2003-08-19 15:11:51 +02:00
|
|
|
* Manages message events requests and notifications. A MessageEventManager provides a high
|
2003-08-13 14:59:24 +02:00
|
|
|
* level access to request for notifications and send event notifications. It also provides
|
|
|
|
* an easy way to hook up custom logic when requests or notifications are received.
|
|
|
|
*
|
|
|
|
* @author Gaston Dombiak
|
2014-05-29 21:04:56 +02:00
|
|
|
* @see <a href="http://xmpp.org/extensions/xep-0022.html">XEP-22: Message Events</a>
|
2003-08-13 14:59:24 +02:00
|
|
|
*/
|
2015-04-06 10:45:12 +02:00
|
|
|
public final class MessageEventManager extends Manager {
|
2014-02-26 21:38:02 +01:00
|
|
|
private static final Logger LOGGER = Logger.getLogger(MessageEventManager.class.getName());
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2014-12-27 13:37:02 +01:00
|
|
|
private static final Map<XMPPConnection, MessageEventManager> INSTANCES = new WeakHashMap<>();
|
2003-08-13 14:59:24 +02:00
|
|
|
|
2015-02-26 18:41:17 +01:00
|
|
|
private static final StanzaFilter PACKET_FILTER = new AndFilter(new StanzaExtensionFilter(
|
2014-06-10 18:56:50 +02:00
|
|
|
new MessageEvent()), new NotFilter(MessageTypeFilter.ERROR));
|
2003-08-13 14:59:24 +02:00
|
|
|
|
2017-12-13 23:10:11 +01:00
|
|
|
private final List<MessageEventNotificationListener> messageEventNotificationListeners = new CopyOnWriteArrayList<>();
|
|
|
|
private final List<MessageEventRequestListener> messageEventRequestListeners = new CopyOnWriteArrayList<>();
|
2014-05-29 21:04:56 +02:00
|
|
|
|
2018-03-29 12:35:11 +02:00
|
|
|
public static synchronized MessageEventManager getInstanceFor(XMPPConnection connection) {
|
2014-05-29 21:04:56 +02:00
|
|
|
MessageEventManager messageEventManager = INSTANCES.get(connection);
|
|
|
|
if (messageEventManager == null) {
|
|
|
|
messageEventManager = new MessageEventManager(connection);
|
2014-12-27 13:37:02 +01:00
|
|
|
INSTANCES.put(connection, messageEventManager);
|
2014-05-29 21:04:56 +02:00
|
|
|
}
|
|
|
|
return messageEventManager;
|
|
|
|
}
|
2003-08-13 14:59:24 +02:00
|
|
|
|
|
|
|
/**
|
2003-08-19 15:11:51 +02:00
|
|
|
* Creates a new message event manager.
|
2003-08-13 14:59:24 +02:00
|
|
|
*
|
2017-12-13 23:10:11 +01:00
|
|
|
* @param connection an XMPPConnection to a XMPP server.
|
2003-08-13 14:59:24 +02:00
|
|
|
*/
|
2014-05-29 21:04:56 +02:00
|
|
|
private MessageEventManager(XMPPConnection connection) {
|
|
|
|
super(connection);
|
|
|
|
// Listens for all message event packets and fire the proper message event listeners.
|
2015-03-01 10:28:15 +01:00
|
|
|
connection.addAsyncStanzaListener(new StanzaListener() {
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2017-01-03 11:12:34 +01:00
|
|
|
public void processStanza(Stanza packet) {
|
2014-05-29 21:04:56 +02:00
|
|
|
Message message = (Message) packet;
|
2017-12-13 23:10:11 +01:00
|
|
|
MessageEvent messageEvent = message.getExtension("x", "jabber:x:event");
|
2014-05-29 21:04:56 +02:00
|
|
|
if (messageEvent.isMessageEventRequest()) {
|
|
|
|
// Fire event for requests of message events
|
|
|
|
for (String eventType : messageEvent.getEventTypes())
|
|
|
|
fireMessageEventRequestListeners(
|
|
|
|
message.getFrom(),
|
2015-02-05 11:17:27 +01:00
|
|
|
message.getStanzaId(),
|
2014-05-29 21:04:56 +02:00
|
|
|
eventType.concat("NotificationRequested"));
|
|
|
|
} else
|
|
|
|
// Fire event for notifications of message events
|
|
|
|
for (String eventType : messageEvent.getEventTypes())
|
|
|
|
fireMessageEventNotificationListeners(
|
|
|
|
message.getFrom(),
|
2015-02-05 11:17:27 +01:00
|
|
|
messageEvent.getStanzaId(),
|
2014-05-29 21:04:56 +02:00
|
|
|
eventType.concat("Notification"));
|
2014-11-09 11:02:49 +01:00
|
|
|
}
|
2014-05-29 21:04:56 +02:00
|
|
|
}, PACKET_FILTER);
|
2003-08-13 14:59:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-08-19 15:11:51 +02:00
|
|
|
* Adds event notification requests to a message. For each event type that
|
2017-12-13 23:10:11 +01:00
|
|
|
* the user wishes event notifications from the message recipient for, <tt>true</tt>
|
2003-08-19 15:11:51 +02:00
|
|
|
* should be passed in to this method.
|
2003-08-13 14:59:24 +02:00
|
|
|
*
|
2003-08-19 15:11:51 +02:00
|
|
|
* @param message the message to add the requested notifications.
|
|
|
|
* @param offline specifies if the offline event is requested.
|
|
|
|
* @param delivered specifies if the delivered event is requested.
|
|
|
|
* @param displayed specifies if the displayed event is requested.
|
|
|
|
* @param composing specifies if the composing event is requested.
|
2003-08-13 14:59:24 +02:00
|
|
|
*/
|
2003-08-19 15:11:51 +02:00
|
|
|
public static void addNotificationsRequests(Message message, boolean offline,
|
2018-04-06 10:21:46 +02:00
|
|
|
boolean delivered, boolean displayed, boolean composing) {
|
2003-08-13 14:59:24 +02:00
|
|
|
// Create a MessageEvent Package and add it to the message
|
|
|
|
MessageEvent messageEvent = new MessageEvent();
|
|
|
|
messageEvent.setOffline(offline);
|
|
|
|
messageEvent.setDelivered(delivered);
|
|
|
|
messageEvent.setDisplayed(displayed);
|
|
|
|
messageEvent.setComposing(composing);
|
|
|
|
message.addExtension(messageEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a message event request listener. The listener will be fired anytime a request for
|
|
|
|
* event notification is received.
|
|
|
|
*
|
|
|
|
* @param messageEventRequestListener a message event request listener.
|
|
|
|
*/
|
|
|
|
public void addMessageEventRequestListener(MessageEventRequestListener messageEventRequestListener) {
|
2014-05-29 21:04:56 +02:00
|
|
|
messageEventRequestListeners.add(messageEventRequestListener);
|
|
|
|
|
2003-08-13 14:59:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a message event request listener. The listener will be fired anytime a request for
|
|
|
|
* event notification is received.
|
|
|
|
*
|
|
|
|
* @param messageEventRequestListener a message event request listener.
|
|
|
|
*/
|
|
|
|
public void removeMessageEventRequestListener(MessageEventRequestListener messageEventRequestListener) {
|
2014-05-29 21:04:56 +02:00
|
|
|
messageEventRequestListeners.remove(messageEventRequestListener);
|
2003-08-13 14:59:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a message event notification listener. The listener will be fired anytime a notification
|
|
|
|
* event is received.
|
|
|
|
*
|
|
|
|
* @param messageEventNotificationListener a message event notification listener.
|
|
|
|
*/
|
|
|
|
public void addMessageEventNotificationListener(MessageEventNotificationListener messageEventNotificationListener) {
|
2014-05-29 21:04:56 +02:00
|
|
|
messageEventNotificationListeners.add(messageEventNotificationListener);
|
2003-08-13 14:59:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a message event notification listener. The listener will be fired anytime a notification
|
|
|
|
* event is received.
|
|
|
|
*
|
|
|
|
* @param messageEventNotificationListener a message event notification listener.
|
|
|
|
*/
|
|
|
|
public void removeMessageEventNotificationListener(MessageEventNotificationListener messageEventNotificationListener) {
|
2014-05-29 21:04:56 +02:00
|
|
|
messageEventNotificationListeners.remove(messageEventNotificationListener);
|
2003-08-13 14:59:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fires message event request listeners.
|
|
|
|
*/
|
|
|
|
private void fireMessageEventRequestListeners(
|
2015-02-14 17:15:02 +01:00
|
|
|
Jid from,
|
2003-08-13 14:59:24 +02:00
|
|
|
String packetID,
|
|
|
|
String methodName) {
|
|
|
|
try {
|
2014-05-29 21:04:56 +02:00
|
|
|
Method method =
|
2003-08-13 14:59:24 +02:00
|
|
|
MessageEventRequestListener.class.getDeclaredMethod(
|
|
|
|
methodName,
|
2016-12-10 11:39:58 +01:00
|
|
|
new Class<?>[] { Jid.class, String.class, MessageEventManager.class });
|
2014-05-29 21:04:56 +02:00
|
|
|
for (MessageEventRequestListener listener : messageEventRequestListeners) {
|
|
|
|
method.invoke(listener, new Object[] { from, packetID, this });
|
2003-08-13 14:59:24 +02:00
|
|
|
}
|
2014-02-02 23:39:07 +01:00
|
|
|
} catch (Exception e) {
|
2016-12-10 11:48:20 +01:00
|
|
|
LOGGER.log(Level.SEVERE, "Error while invoking MessageEventRequestListener's " + methodName, e);
|
2003-08-13 14:59:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fires message event notification listeners.
|
|
|
|
*/
|
|
|
|
private void fireMessageEventNotificationListeners(
|
2015-02-14 17:15:02 +01:00
|
|
|
Jid from,
|
2003-08-13 14:59:24 +02:00
|
|
|
String packetID,
|
|
|
|
String methodName) {
|
|
|
|
try {
|
2014-05-29 21:04:56 +02:00
|
|
|
Method method =
|
2003-08-13 14:59:24 +02:00
|
|
|
MessageEventNotificationListener.class.getDeclaredMethod(
|
|
|
|
methodName,
|
2016-12-10 11:47:24 +01:00
|
|
|
new Class<?>[] { Jid.class, String.class });
|
2014-05-29 21:04:56 +02:00
|
|
|
for (MessageEventNotificationListener listener : messageEventNotificationListeners) {
|
|
|
|
method.invoke(listener, new Object[] { from, packetID });
|
2003-08-13 14:59:24 +02:00
|
|
|
}
|
2014-02-02 23:39:07 +01:00
|
|
|
} catch (Exception e) {
|
2016-12-10 11:48:20 +01:00
|
|
|
LOGGER.log(Level.SEVERE, "Error while invoking MessageEventNotificationListener's " + methodName, e);
|
2003-08-13 14:59:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-29 12:15:32 +02:00
|
|
|
* Sends the notification that the message was delivered to the sender of the original message.
|
2003-08-13 14:59:24 +02:00
|
|
|
*
|
|
|
|
* @param to the recipient of the notification.
|
2003-08-19 15:11:51 +02:00
|
|
|
* @param packetID the id of the message to send.
|
2014-03-19 14:22:20 +01:00
|
|
|
* @throws NotConnectedException
|
2015-02-14 09:43:44 +01:00
|
|
|
* @throws InterruptedException
|
2003-08-13 14:59:24 +02:00
|
|
|
*/
|
2015-02-14 17:15:02 +01:00
|
|
|
public void sendDeliveredNotification(Jid to, String packetID) throws NotConnectedException, InterruptedException {
|
2003-08-13 14:59:24 +02:00
|
|
|
// Create the message to send
|
|
|
|
Message msg = new Message(to);
|
|
|
|
// Create a MessageEvent Package and add it to the message
|
|
|
|
MessageEvent messageEvent = new MessageEvent();
|
|
|
|
messageEvent.setDelivered(true);
|
2015-02-05 11:17:27 +01:00
|
|
|
messageEvent.setStanzaId(packetID);
|
2003-08-13 14:59:24 +02:00
|
|
|
msg.addExtension(messageEvent);
|
|
|
|
// Send the packet
|
2015-03-04 21:44:43 +01:00
|
|
|
connection().sendStanza(msg);
|
2003-08-13 14:59:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-29 12:15:32 +02:00
|
|
|
* Sends the notification that the message was displayed to the sender of the original message.
|
2003-08-13 14:59:24 +02:00
|
|
|
*
|
|
|
|
* @param to the recipient of the notification.
|
2003-08-19 15:11:51 +02:00
|
|
|
* @param packetID the id of the message to send.
|
2014-03-19 14:22:20 +01:00
|
|
|
* @throws NotConnectedException
|
2015-02-14 09:43:44 +01:00
|
|
|
* @throws InterruptedException
|
2003-08-13 14:59:24 +02:00
|
|
|
*/
|
2015-02-14 17:15:02 +01:00
|
|
|
public void sendDisplayedNotification(Jid to, String packetID) throws NotConnectedException, InterruptedException {
|
2003-08-13 14:59:24 +02:00
|
|
|
// Create the message to send
|
|
|
|
Message msg = new Message(to);
|
|
|
|
// Create a MessageEvent Package and add it to the message
|
|
|
|
MessageEvent messageEvent = new MessageEvent();
|
|
|
|
messageEvent.setDisplayed(true);
|
2015-02-05 11:17:27 +01:00
|
|
|
messageEvent.setStanzaId(packetID);
|
2003-08-13 14:59:24 +02:00
|
|
|
msg.addExtension(messageEvent);
|
|
|
|
// Send the packet
|
2015-03-04 21:44:43 +01:00
|
|
|
connection().sendStanza(msg);
|
2003-08-13 14:59:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-29 12:15:32 +02:00
|
|
|
* Sends the notification that the receiver of the message is composing a reply.
|
2003-08-13 14:59:24 +02:00
|
|
|
*
|
|
|
|
* @param to the recipient of the notification.
|
2003-08-19 15:11:51 +02:00
|
|
|
* @param packetID the id of the message to send.
|
2014-03-19 14:22:20 +01:00
|
|
|
* @throws NotConnectedException
|
2015-02-14 09:43:44 +01:00
|
|
|
* @throws InterruptedException
|
2003-08-13 14:59:24 +02:00
|
|
|
*/
|
2015-02-14 17:15:02 +01:00
|
|
|
public void sendComposingNotification(Jid to, String packetID) throws NotConnectedException, InterruptedException {
|
2003-08-13 14:59:24 +02:00
|
|
|
// Create the message to send
|
|
|
|
Message msg = new Message(to);
|
|
|
|
// Create a MessageEvent Package and add it to the message
|
|
|
|
MessageEvent messageEvent = new MessageEvent();
|
|
|
|
messageEvent.setComposing(true);
|
2015-02-05 11:17:27 +01:00
|
|
|
messageEvent.setStanzaId(packetID);
|
2003-08-13 14:59:24 +02:00
|
|
|
msg.addExtension(messageEvent);
|
|
|
|
// Send the packet
|
2015-03-04 21:44:43 +01:00
|
|
|
connection().sendStanza(msg);
|
2003-08-13 14:59:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-08-19 15:11:51 +02:00
|
|
|
* Sends the notification that the receiver of the message has cancelled composing a reply.
|
2003-08-13 14:59:24 +02:00
|
|
|
*
|
|
|
|
* @param to the recipient of the notification.
|
2003-08-19 15:11:51 +02:00
|
|
|
* @param packetID the id of the message to send.
|
2014-03-19 14:22:20 +01:00
|
|
|
* @throws NotConnectedException
|
2015-02-14 09:43:44 +01:00
|
|
|
* @throws InterruptedException
|
2003-08-13 14:59:24 +02:00
|
|
|
*/
|
2015-02-14 17:15:02 +01:00
|
|
|
public void sendCancelledNotification(Jid to, String packetID) throws NotConnectedException, InterruptedException {
|
2003-08-13 14:59:24 +02:00
|
|
|
// Create the message to send
|
|
|
|
Message msg = new Message(to);
|
|
|
|
// Create a MessageEvent Package and add it to the message
|
|
|
|
MessageEvent messageEvent = new MessageEvent();
|
|
|
|
messageEvent.setCancelled(true);
|
2015-02-05 11:17:27 +01:00
|
|
|
messageEvent.setStanzaId(packetID);
|
2003-08-13 14:59:24 +02:00
|
|
|
msg.addExtension(messageEvent);
|
|
|
|
// Send the packet
|
2015-03-04 21:44:43 +01:00
|
|
|
connection().sendStanza(msg);
|
2003-08-13 14:59:24 +02:00
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|