1 11 12 package org.jivesoftware.messenger.interceptor; 13 14 import org.jivesoftware.messenger.Session; 15 import org.jivesoftware.util.Log; 16 import org.xmpp.packet.Packet; 17 18 import java.util.*; 19 import java.util.concurrent.ConcurrentHashMap ; 20 import java.util.concurrent.CopyOnWriteArrayList ; 21 22 37 public class InterceptorManager { 38 39 private static InterceptorManager instance = new InterceptorManager(); 40 41 private List <PacketInterceptor> globalInterceptors = 42 new CopyOnWriteArrayList <PacketInterceptor>(); 43 private Map <String , List <PacketInterceptor>> usersInterceptors = 44 new ConcurrentHashMap <String , List <PacketInterceptor>>(); 45 46 51 public static InterceptorManager getInstance() { 52 return instance; 53 } 54 55 61 public List <PacketInterceptor> getInterceptors() { 62 return Collections.unmodifiableList(globalInterceptors); 63 } 64 65 71 public void addInterceptor(PacketInterceptor interceptor) { 72 if (interceptor == null) { 73 throw new NullPointerException ("Parameter interceptor was null."); 74 } 75 if (globalInterceptors.contains(interceptor)) { 77 globalInterceptors.remove(interceptor); 78 } 79 globalInterceptors.add(interceptor); 80 } 81 82 89 public void addInterceptor(int index, PacketInterceptor interceptor) { 90 if (index < 0 || (index > globalInterceptors.size())) { 91 throw new IndexOutOfBoundsException ("Index " + index + " invalid."); 92 } 93 if (interceptor == null) { 94 throw new NullPointerException ("Parameter interceptor was null."); 95 } 96 if (globalInterceptors.contains(interceptor)) { 98 int oldIndex = globalInterceptors.indexOf(interceptor); 99 if (oldIndex < index) { 100 index -= 1; 101 } 102 globalInterceptors.remove(interceptor); 103 } 104 105 globalInterceptors.add(index, interceptor); 106 } 107 108 114 public boolean removeInterceptor(PacketInterceptor interceptor) { 115 return globalInterceptors.remove(interceptor); 116 } 117 118 126 public List <PacketInterceptor> getUserInterceptors(String username) { 127 List <PacketInterceptor> userInterceptors = usersInterceptors.get(username); 128 if (userInterceptors == null) { 129 return Collections.emptyList(); 130 } 131 else { 132 return Collections.unmodifiableList(userInterceptors); 133 } 134 } 135 136 145 public void addUserInterceptor(String username, int index, PacketInterceptor interceptor) { 146 List <PacketInterceptor> userInterceptors = usersInterceptors.get(username); 147 if (userInterceptors == null) { 148 userInterceptors = new CopyOnWriteArrayList <PacketInterceptor>(); 149 usersInterceptors.put(username, userInterceptors); 150 } 151 else { 152 if (index < 0 || (index > userInterceptors.size())) { 153 throw new IndexOutOfBoundsException ("Index " + index + " invalid."); 154 } 155 if (interceptor == null) { 156 throw new NullPointerException ("Parameter interceptor was null."); 157 } 158 159 if (userInterceptors.contains(interceptor)) { 161 int oldIndex = userInterceptors.indexOf(interceptor); 162 if (oldIndex < index) { 163 index -= 1; 164 } 165 userInterceptors.remove(interceptor); 166 } 167 } 168 userInterceptors.add(index, interceptor); 169 } 170 171 179 public boolean removeUserInterceptor(String username, PacketInterceptor interceptor) { 180 boolean answer = false; 181 List <PacketInterceptor> userInterceptors = usersInterceptors.get(username); 182 if (userInterceptors != null) { 183 answer = userInterceptors.remove(interceptor); 184 if (userInterceptors.isEmpty()) { 186 usersInterceptors.remove(username); 187 } 188 } 189 return answer; 190 } 191 192 213 public void invokeInterceptors(Packet packet, Session session, boolean read, boolean processed) 214 throws PacketRejectedException 215 { 216 for (PacketInterceptor interceptor : globalInterceptors) { 218 try { 219 interceptor.interceptPacket(packet, session, read, processed); 220 } 221 catch (PacketRejectedException e) { 222 if (processed) { 223 Log.error("Post interceptor cannot reject packet.", e); 224 } 225 else { 226 throw e; 228 } 229 } 230 catch (Exception e) { 231 Log.error("Error in interceptor", e); 232 } 233 } 234 String username = session.getAddress().getNode(); 236 if (username != null) { 237 Collection<PacketInterceptor> userInterceptors = usersInterceptors.get(username); 238 if (userInterceptors != null && !userInterceptors.isEmpty()) { 239 for (PacketInterceptor interceptor : userInterceptors) { 240 try { 241 interceptor.interceptPacket(packet, session, read, processed); 242 } 243 catch (PacketRejectedException e) { 244 if (processed) { 245 Log.error("Post interceptor cannot reject packet.", e); 246 } 247 else { 248 throw e; 250 } 251 } 252 catch (Exception e) { 253 Log.error("Error in interceptor", e); 254 } 255 } 256 } 257 } 258 } 259 } | Popular Tags |