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.StanzaListener;
030import org.jivesoftware.smack.SmackException.NotConnectedException;
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;
039import org.jivesoftware.smackx.xevent.packet.MessageEvent;
040
041/**
042 * 
043 * Manages message events requests and notifications. A MessageEventManager provides a high
044 * level access to request for notifications and send event notifications. It also provides 
045 * an easy way to hook up custom logic when requests or notifications are received. 
046 *
047 * @author Gaston Dombiak
048 * @see <a href="http://xmpp.org/extensions/xep-0022.html">XEP-22: Message Events</a>
049 */
050public class MessageEventManager extends Manager {
051    private static final Logger LOGGER = Logger.getLogger(MessageEventManager.class.getName());
052    
053    private static final Map<XMPPConnection, MessageEventManager> INSTANCES = new WeakHashMap<>();
054
055    private static final StanzaFilter PACKET_FILTER = new AndFilter(new StanzaExtensionFilter(
056                    new MessageEvent()), new NotFilter(MessageTypeFilter.ERROR));
057
058    private List<MessageEventNotificationListener> messageEventNotificationListeners = new CopyOnWriteArrayList<MessageEventNotificationListener>();
059    private List<MessageEventRequestListener> messageEventRequestListeners = new CopyOnWriteArrayList<MessageEventRequestListener>();
060
061    public synchronized static MessageEventManager getInstanceFor(XMPPConnection connection) {
062        MessageEventManager messageEventManager = INSTANCES.get(connection);
063        if (messageEventManager == null) {
064            messageEventManager = new MessageEventManager(connection);
065            INSTANCES.put(connection, messageEventManager);
066        }
067        return messageEventManager;
068    }
069
070    /**
071     * Creates a new message event manager.
072     *
073     * @param con an XMPPConnection to a XMPP server.
074     */
075    private MessageEventManager(XMPPConnection connection) {
076        super(connection);
077        // Listens for all message event packets and fire the proper message event listeners.
078        connection.addAsyncStanzaListener(new StanzaListener() {
079            public void processPacket(Stanza packet) {
080                Message message = (Message) packet;
081                MessageEvent messageEvent =
082                    (MessageEvent) message.getExtension("x", "jabber:x:event");
083                if (messageEvent.isMessageEventRequest()) {
084                    // Fire event for requests of message events
085                    for (String eventType : messageEvent.getEventTypes())
086                        fireMessageEventRequestListeners(
087                            message.getFrom(),
088                            message.getStanzaId(),
089                            eventType.concat("NotificationRequested"));
090                } else
091                    // Fire event for notifications of message events
092                    for (String eventType : messageEvent.getEventTypes())
093                        fireMessageEventNotificationListeners(
094                            message.getFrom(),
095                            messageEvent.getStanzaId(),
096                            eventType.concat("Notification"));
097            }
098        }, PACKET_FILTER);
099    }
100
101    /**
102     * Adds event notification requests to a message. For each event type that
103     * the user wishes event notifications from the message recepient for, <tt>true</tt>
104     * should be passed in to this method.
105     * 
106     * @param message the message to add the requested notifications.
107     * @param offline specifies if the offline event is requested.
108     * @param delivered specifies if the delivered event is requested.
109     * @param displayed specifies if the displayed event is requested.
110     * @param composing specifies if the composing event is requested.
111     */
112    public static void addNotificationsRequests(Message message, boolean offline,
113            boolean delivered, boolean displayed, boolean composing)
114    {
115        // Create a MessageEvent Package and add it to the message
116        MessageEvent messageEvent = new MessageEvent();
117        messageEvent.setOffline(offline);
118        messageEvent.setDelivered(delivered);
119        messageEvent.setDisplayed(displayed);
120        messageEvent.setComposing(composing);
121        message.addExtension(messageEvent);
122    }
123
124    /**
125     * Adds a message event request listener. The listener will be fired anytime a request for
126     * event notification is received.
127     *
128     * @param messageEventRequestListener a message event request listener.
129     */
130    public void addMessageEventRequestListener(MessageEventRequestListener messageEventRequestListener) {
131        messageEventRequestListeners.add(messageEventRequestListener);
132
133    }
134
135    /**
136     * Removes a message event request listener. The listener will be fired anytime a request for
137     * event notification is received.
138     *
139     * @param messageEventRequestListener a message event request listener.
140     */
141    public void removeMessageEventRequestListener(MessageEventRequestListener messageEventRequestListener) {
142        messageEventRequestListeners.remove(messageEventRequestListener);
143    }
144
145    /**
146     * Adds a message event notification listener. The listener will be fired anytime a notification
147     * event is received.
148     *
149     * @param messageEventNotificationListener a message event notification listener.
150     */
151    public void addMessageEventNotificationListener(MessageEventNotificationListener messageEventNotificationListener) {
152        messageEventNotificationListeners.add(messageEventNotificationListener);
153    }
154
155    /**
156     * Removes a message event notification listener. The listener will be fired anytime a notification
157     * event is received.
158     *
159     * @param messageEventNotificationListener a message event notification listener.
160     */
161    public void removeMessageEventNotificationListener(MessageEventNotificationListener messageEventNotificationListener) {
162        messageEventNotificationListeners.remove(messageEventNotificationListener);
163    }
164
165    /**
166     * Fires message event request listeners.
167     */
168    private void fireMessageEventRequestListeners(
169        String from,
170        String packetID,
171        String methodName) {
172        try {
173            Method method =
174                MessageEventRequestListener.class.getDeclaredMethod(
175                    methodName,
176                    new Class[] { String.class, String.class, MessageEventManager.class });
177            for (MessageEventRequestListener listener : messageEventRequestListeners) {
178                method.invoke(listener, new Object[] { from, packetID, this });
179            }
180        } catch (Exception e) {
181            LOGGER.log(Level.SEVERE, "Error while invoking MessageEventRequestListener", e);
182        }
183    }
184
185    /**
186     * Fires message event notification listeners.
187     */
188    private void fireMessageEventNotificationListeners(
189        String from,
190        String packetID,
191        String methodName) {
192        try {
193            Method method =
194                MessageEventNotificationListener.class.getDeclaredMethod(
195                    methodName,
196                    new Class[] { String.class, String.class });
197            for (MessageEventNotificationListener listener : messageEventNotificationListeners) {
198                method.invoke(listener, new Object[] { from, packetID });
199            }
200        } catch (Exception e) {
201            LOGGER.log(Level.SEVERE, "Error while invoking MessageEventNotificationListener", e);
202        }
203    }
204
205    /**
206     * Sends the notification that the message was delivered to the sender of the original message
207     * 
208     * @param to the recipient of the notification.
209     * @param packetID the id of the message to send.
210     * @throws NotConnectedException 
211     */
212    public void sendDeliveredNotification(String to, String packetID) throws NotConnectedException {
213        // Create the message to send
214        Message msg = new Message(to);
215        // Create a MessageEvent Package and add it to the message
216        MessageEvent messageEvent = new MessageEvent();
217        messageEvent.setDelivered(true);
218        messageEvent.setStanzaId(packetID);
219        msg.addExtension(messageEvent);
220        // Send the packet
221        connection().sendStanza(msg);
222    }
223
224    /**
225     * Sends the notification that the message was displayed to the sender of the original message
226     * 
227     * @param to the recipient of the notification.
228     * @param packetID the id of the message to send.
229     * @throws NotConnectedException 
230     */
231    public void sendDisplayedNotification(String to, String packetID) throws NotConnectedException {
232        // Create the message to send
233        Message msg = new Message(to);
234        // Create a MessageEvent Package and add it to the message
235        MessageEvent messageEvent = new MessageEvent();
236        messageEvent.setDisplayed(true);
237        messageEvent.setStanzaId(packetID);
238        msg.addExtension(messageEvent);
239        // Send the packet
240        connection().sendStanza(msg);
241    }
242
243    /**
244     * Sends the notification that the receiver of the message is composing a reply
245     * 
246     * @param to the recipient of the notification.
247     * @param packetID the id of the message to send.
248     * @throws NotConnectedException 
249     */
250    public void sendComposingNotification(String to, String packetID) throws NotConnectedException {
251        // Create the message to send
252        Message msg = new Message(to);
253        // Create a MessageEvent Package and add it to the message
254        MessageEvent messageEvent = new MessageEvent();
255        messageEvent.setComposing(true);
256        messageEvent.setStanzaId(packetID);
257        msg.addExtension(messageEvent);
258        // Send the packet
259        connection().sendStanza(msg);
260    }
261
262    /**
263     * Sends the notification that the receiver of the message has cancelled composing a reply.
264     * 
265     * @param to the recipient of the notification.
266     * @param packetID the id of the message to send.
267     * @throws NotConnectedException 
268     */
269    public void sendCancelledNotification(String to, String packetID) throws NotConnectedException {
270        // Create the message to send
271        Message msg = new Message(to);
272        // Create a MessageEvent Package and add it to the message
273        MessageEvent messageEvent = new MessageEvent();
274        messageEvent.setCancelled(true);
275        messageEvent.setStanzaId(packetID);
276        msg.addExtension(messageEvent);
277        // Send the packet
278        connection().sendStanza(msg);
279    }
280}