001/**
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.jivesoftware.smackx.xevent;
019
020import java.lang.reflect.Method;
021import java.util.List;
022import java.util.Map;
023import java.util.WeakHashMap;
024import java.util.concurrent.CopyOnWriteArrayList;
025import java.util.logging.Level;
026import java.util.logging.Logger;
027
028import org.jivesoftware.smack.Manager;
029import org.jivesoftware.smack.SmackException.NotConnectedException;
030import org.jivesoftware.smack.StanzaListener;
031import org.jivesoftware.smack.XMPPConnection;
032import org.jivesoftware.smack.filter.AndFilter;
033import org.jivesoftware.smack.filter.MessageTypeFilter;
034import org.jivesoftware.smack.filter.NotFilter;
035import org.jivesoftware.smack.filter.StanzaExtensionFilter;
036import org.jivesoftware.smack.filter.StanzaFilter;
037import org.jivesoftware.smack.packet.Message;
038import org.jivesoftware.smack.packet.Stanza;
039
040import org.jivesoftware.smackx.xevent.packet.MessageEvent;
041
042import org.jxmpp.jid.Jid;
043
044/**
045 * 
046 * Manages message events requests and notifications. A MessageEventManager provides a high
047 * level access to request for notifications and send event notifications. It also provides 
048 * an easy way to hook up custom logic when requests or notifications are received. 
049 *
050 * @author Gaston Dombiak
051 * @see <a href="http://xmpp.org/extensions/xep-0022.html">XEP-22: Message Events</a>
052 */
053public final class MessageEventManager extends Manager {
054    private static final Logger LOGGER = Logger.getLogger(MessageEventManager.class.getName());
055
056    private static final Map<XMPPConnection, MessageEventManager> INSTANCES = new WeakHashMap<>();
057
058    private static final StanzaFilter PACKET_FILTER = new AndFilter(new StanzaExtensionFilter(
059                    new MessageEvent()), new NotFilter(MessageTypeFilter.ERROR));
060
061    private List<MessageEventNotificationListener> messageEventNotificationListeners = new CopyOnWriteArrayList<MessageEventNotificationListener>();
062    private List<MessageEventRequestListener> messageEventRequestListeners = new CopyOnWriteArrayList<MessageEventRequestListener>();
063
064    public synchronized static MessageEventManager getInstanceFor(XMPPConnection connection) {
065        MessageEventManager messageEventManager = INSTANCES.get(connection);
066        if (messageEventManager == null) {
067            messageEventManager = new MessageEventManager(connection);
068            INSTANCES.put(connection, messageEventManager);
069        }
070        return messageEventManager;
071    }
072
073    /**
074     * Creates a new message event manager.
075     *
076     * @param con an XMPPConnection to a XMPP server.
077     */
078    private MessageEventManager(XMPPConnection connection) {
079        super(connection);
080        // Listens for all message event packets and fire the proper message event listeners.
081        connection.addAsyncStanzaListener(new StanzaListener() {
082            @Override
083            public void processStanza(Stanza packet) {
084                Message message = (Message) packet;
085                MessageEvent messageEvent =
086                    (MessageEvent) message.getExtension("x", "jabber:x:event");
087                if (messageEvent.isMessageEventRequest()) {
088                    // Fire event for requests of message events
089                    for (String eventType : messageEvent.getEventTypes())
090                        fireMessageEventRequestListeners(
091                            message.getFrom(),
092                            message.getStanzaId(),
093                            eventType.concat("NotificationRequested"));
094                } else
095                    // Fire event for notifications of message events
096                    for (String eventType : messageEvent.getEventTypes())
097                        fireMessageEventNotificationListeners(
098                            message.getFrom(),
099                            messageEvent.getStanzaId(),
100                            eventType.concat("Notification"));
101            }
102        }, PACKET_FILTER);
103    }
104
105    /**
106     * Adds event notification requests to a message. For each event type that
107     * the user wishes event notifications from the message recepient for, <tt>true</tt>
108     * should be passed in to this method.
109     * 
110     * @param message the message to add the requested notifications.
111     * @param offline specifies if the offline event is requested.
112     * @param delivered specifies if the delivered event is requested.
113     * @param displayed specifies if the displayed event is requested.
114     * @param composing specifies if the composing event is requested.
115     */
116    public static void addNotificationsRequests(Message message, boolean offline,
117            boolean delivered, boolean displayed, boolean composing)
118    {
119        // Create a MessageEvent Package and add it to the message
120        MessageEvent messageEvent = new MessageEvent();
121        messageEvent.setOffline(offline);
122        messageEvent.setDelivered(delivered);
123        messageEvent.setDisplayed(displayed);
124        messageEvent.setComposing(composing);
125        message.addExtension(messageEvent);
126    }
127
128    /**
129     * Adds a message event request listener. The listener will be fired anytime a request for
130     * event notification is received.
131     *
132     * @param messageEventRequestListener a message event request listener.
133     */
134    public void addMessageEventRequestListener(MessageEventRequestListener messageEventRequestListener) {
135        messageEventRequestListeners.add(messageEventRequestListener);
136
137    }
138
139    /**
140     * Removes a message event request listener. The listener will be fired anytime a request for
141     * event notification is received.
142     *
143     * @param messageEventRequestListener a message event request listener.
144     */
145    public void removeMessageEventRequestListener(MessageEventRequestListener messageEventRequestListener) {
146        messageEventRequestListeners.remove(messageEventRequestListener);
147    }
148
149    /**
150     * Adds a message event notification listener. The listener will be fired anytime a notification
151     * event is received.
152     *
153     * @param messageEventNotificationListener a message event notification listener.
154     */
155    public void addMessageEventNotificationListener(MessageEventNotificationListener messageEventNotificationListener) {
156        messageEventNotificationListeners.add(messageEventNotificationListener);
157    }
158
159    /**
160     * Removes a message event notification listener. The listener will be fired anytime a notification
161     * event is received.
162     *
163     * @param messageEventNotificationListener a message event notification listener.
164     */
165    public void removeMessageEventNotificationListener(MessageEventNotificationListener messageEventNotificationListener) {
166        messageEventNotificationListeners.remove(messageEventNotificationListener);
167    }
168
169    /**
170     * Fires message event request listeners.
171     */
172    private void fireMessageEventRequestListeners(
173        Jid from,
174        String packetID,
175        String methodName) {
176        try {
177            Method method =
178                MessageEventRequestListener.class.getDeclaredMethod(
179                    methodName,
180                    new Class<?>[] { Jid.class, String.class, MessageEventManager.class });
181            for (MessageEventRequestListener listener : messageEventRequestListeners) {
182                method.invoke(listener, new Object[] { from, packetID, this });
183            }
184        } catch (Exception e) {
185            LOGGER.log(Level.SEVERE, "Error while invoking MessageEventRequestListener's  " + methodName, e);
186        }
187    }
188
189    /**
190     * Fires message event notification listeners.
191     */
192    private void fireMessageEventNotificationListeners(
193        Jid from,
194        String packetID,
195        String methodName) {
196        try {
197            Method method =
198                MessageEventNotificationListener.class.getDeclaredMethod(
199                    methodName,
200                    new Class<?>[] { Jid.class, String.class });
201            for (MessageEventNotificationListener listener : messageEventNotificationListeners) {
202                method.invoke(listener, new Object[] { from, packetID });
203            }
204        } catch (Exception e) {
205            LOGGER.log(Level.SEVERE, "Error while invoking MessageEventNotificationListener's " + methodName, e);
206        }
207    }
208
209    /**
210     * Sends the notification that the message was delivered to the sender of the original message.
211     * 
212     * @param to the recipient of the notification.
213     * @param packetID the id of the message to send.
214     * @throws NotConnectedException 
215     * @throws InterruptedException 
216     */
217    public void sendDeliveredNotification(Jid to, String packetID) throws NotConnectedException, InterruptedException {
218        // Create the message to send
219        Message msg = new Message(to);
220        // Create a MessageEvent Package and add it to the message
221        MessageEvent messageEvent = new MessageEvent();
222        messageEvent.setDelivered(true);
223        messageEvent.setStanzaId(packetID);
224        msg.addExtension(messageEvent);
225        // Send the packet
226        connection().sendStanza(msg);
227    }
228
229    /**
230     * Sends the notification that the message was displayed to the sender of the original message.
231     * 
232     * @param to the recipient of the notification.
233     * @param packetID the id of the message to send.
234     * @throws NotConnectedException 
235     * @throws InterruptedException 
236     */
237    public void sendDisplayedNotification(Jid to, String packetID) throws NotConnectedException, InterruptedException {
238        // Create the message to send
239        Message msg = new Message(to);
240        // Create a MessageEvent Package and add it to the message
241        MessageEvent messageEvent = new MessageEvent();
242        messageEvent.setDisplayed(true);
243        messageEvent.setStanzaId(packetID);
244        msg.addExtension(messageEvent);
245        // Send the packet
246        connection().sendStanza(msg);
247    }
248
249    /**
250     * Sends the notification that the receiver of the message is composing a reply.
251     * 
252     * @param to the recipient of the notification.
253     * @param packetID the id of the message to send.
254     * @throws NotConnectedException 
255     * @throws InterruptedException 
256     */
257    public void sendComposingNotification(Jid to, String packetID) throws NotConnectedException, InterruptedException {
258        // Create the message to send
259        Message msg = new Message(to);
260        // Create a MessageEvent Package and add it to the message
261        MessageEvent messageEvent = new MessageEvent();
262        messageEvent.setComposing(true);
263        messageEvent.setStanzaId(packetID);
264        msg.addExtension(messageEvent);
265        // Send the packet
266        connection().sendStanza(msg);
267    }
268
269    /**
270     * Sends the notification that the receiver of the message has cancelled composing a reply.
271     * 
272     * @param to the recipient of the notification.
273     * @param packetID the id of the message to send.
274     * @throws NotConnectedException 
275     * @throws InterruptedException 
276     */
277    public void sendCancelledNotification(Jid to, String packetID) throws NotConnectedException, InterruptedException {
278        // Create the message to send
279        Message msg = new Message(to);
280        // Create a MessageEvent Package and add it to the message
281        MessageEvent messageEvent = new MessageEvent();
282        messageEvent.setCancelled(true);
283        messageEvent.setStanzaId(packetID);
284        msg.addExtension(messageEvent);
285        // Send the packet
286        connection().sendStanza(msg);
287    }
288}