1 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 26 public class OfflineMessageStrategy extends BasicModule { 27 28 private static int quota = 100*1024; 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 (); 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 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 if (message.getFrom() == null) { 101 return; 102 } 103 try { 104 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 router.route(errorResponse); 112 } 113 catch (Exception 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 quota = JiveGlobals.getProperty("xmpp.offline.quota"); 125 if (quota != null && quota.length() > 0) { 126 OfflineMessageStrategy.quota = Integer.parseInt(quota); 127 } 128 String type = JiveGlobals.getProperty("xmpp.offline.type"); 129 if (type != null && type.length() > 0) { 130 OfflineMessageStrategy.type = Type.valueOf(type); 131 } 132 } 133 134 137 public enum Type { 138 139 142 bounce, 143 144 147 drop, 148 149 152 store, 153 154 157 store_and_bounce, 158 159 162 store_and_drop; 163 } 164 } | Popular Tags |