KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > OfflineMessageStrategy


1 /**
2  * $RCSfile: OfflineMessageStrategy.java,v $
3  * $Revision: 1.15 $
4  * $Date: 2005/04/21 23:59:37 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger;
13
14 import org.jivesoftware.messenger.container.BasicModule;
15 import org.jivesoftware.util.JiveGlobals;
16 import org.jivesoftware.util.Log;
17 import org.xmpp.packet.JID;
18 import org.xmpp.packet.Message;
19 import org.xmpp.packet.PacketError;
20
21 /**
22  * Controls what is done with offline messages.
23  *
24  * @author Iain Shigeoka
25  */

26 public class OfflineMessageStrategy extends BasicModule {
27
28     private static int quota = 100*1024; // Default to 100 K.
29
private static Type type = Type.store_and_bounce;
30
31     private OfflineMessageStore messageStore;
32     private JID serverAddress;
33     private PacketRouter router;
34
35     public OfflineMessageStrategy() {
36         super("Offline Message Strategy");
37     }
38
39     public int getQuota() {
40         return quota;
41     }
42
43     public void setQuota(int quota) {
44         OfflineMessageStrategy.quota = quota;
45         JiveGlobals.setProperty("xmpp.offline.quota", Integer.toString(quota));
46     }
47
48     public OfflineMessageStrategy.Type getType() {
49         return type;
50     }
51
52     public void setType(OfflineMessageStrategy.Type type) {
53         if (type == null) {
54             throw new IllegalArgumentException JavaDoc();
55         }
56         OfflineMessageStrategy.type = type;
57         JiveGlobals.setProperty("xmpp.offline.type", type.toString());
58     }
59
60     public void storeOffline(Message message) {
61         if (message != null) {
62             // Do nothing if the message was sent to the server itself or to an anonymous user
63
if (message.getTo() == null || serverAddress.equals(message.getTo()) ||
64                     message.getTo().getNode() == null) {
65                 return;
66             }
67
68             if (type == Type.bounce) {
69                 bounce(message);
70             }
71             else if (type == Type.store) {
72                 store(message);
73             }
74             else if (type == Type.store_and_bounce) {
75                 if (underQuota(message)) {
76                     store(message);
77                 }
78                 else {
79                     bounce(message);
80                 }
81             }
82             else if (type == Type.store_and_drop) {
83                 if (underQuota(message)) {
84                     store(message);
85                 }
86             }
87         }
88     }
89
90     private boolean underQuota(Message message) {
91         return quota > messageStore.getSize(message.getTo().getNode()) + message.toXML().length();
92     }
93
94     private void store(Message message) {
95         messageStore.addMessage(message);
96     }
97
98     private void bounce(Message message) {
99         // Do nothing if the sender was the server itself
100
if (message.getFrom() == null) {
101             return;
102         }
103         try {
104             // Generate a rejection response to the sender
105
Message errorResponse = message.createCopy();
106             errorResponse.setError(new PacketError(PacketError.Condition.item_not_found,
107                     PacketError.Type.continue_processing));
108             errorResponse.setFrom(message.getTo());
109             errorResponse.setTo(message.getFrom());
110             // Send the response
111
router.route(errorResponse);
112         }
113         catch (Exception JavaDoc e) {
114             Log.error(e);
115         }
116     }
117
118     public void initialize(XMPPServer server) {
119         super.initialize(server);
120         messageStore = server.getOfflineMessageStore();
121         router = server.getPacketRouter();
122         serverAddress = new JID(server.getServerInfo().getName());
123
124         String JavaDoc quota = JiveGlobals.getProperty("xmpp.offline.quota");
125         if (quota != null && quota.length() > 0) {
126             OfflineMessageStrategy.quota = Integer.parseInt(quota);
127         }
128         String JavaDoc type = JiveGlobals.getProperty("xmpp.offline.type");
129         if (type != null && type.length() > 0) {
130             OfflineMessageStrategy.type = Type.valueOf(type);
131         }
132     }
133
134     /**
135      * Strategy types.
136      */

137     public enum Type {
138
139         /**
140          * All messages are bounced to the sender.
141          */

142         bounce,
143
144         /**
145          * All messages are silently dropped.
146          */

147         drop,
148
149         /**
150          * All messages are stored.
151          */

152         store,
153
154         /**
155          * Messages are stored up to the storage limit, and then bounced.
156          */

157         store_and_bounce,
158
159         /**
160          * Messages are stored up to the storage limit, and then silently dropped.
161          */

162         store_and_drop;
163     }
164 }
Popular Tags