1 11 12 package org.jivesoftware.messenger.net; 13 14 import org.dom4j.Element; 15 import org.jivesoftware.messenger.PacketRouter; 16 import org.jivesoftware.messenger.auth.UnauthorizedException; 17 import org.jivesoftware.messenger.interceptor.PacketRejectedException; 18 import org.jivesoftware.messenger.server.IncomingServerSession; 19 import org.jivesoftware.util.JiveGlobals; 20 import org.jivesoftware.util.Log; 21 import org.xmlpull.v1.XmlPullParserException; 22 import org.xmpp.packet.*; 23 24 import java.io.IOException ; 25 import java.net.Socket ; 26 import java.util.concurrent.LinkedBlockingQueue ; 27 import java.util.concurrent.ThreadPoolExecutor ; 28 import java.util.concurrent.TimeUnit ; 29 30 45 public class ServerSocketReader extends SocketReader { 46 47 50 private ThreadPoolExecutor threadPool; 51 52 public ServerSocketReader(PacketRouter router, String serverName, Socket socket, 53 SocketConnection connection) { 54 super(router, serverName, socket, connection); 55 int maxThreads = JiveGlobals.getIntProperty("xmpp.server.processing.threads", 50); 58 threadPool = 59 new ThreadPoolExecutor (1, maxThreads, 60, TimeUnit.SECONDS, 60 new LinkedBlockingQueue <Runnable >(), 61 new ThreadPoolExecutor.CallerRunsPolicy ()); 62 } 63 64 69 protected void processIQ(final IQ packet) throws UnauthorizedException { 70 try { 71 packetReceived(packet); 72 threadPool.execute(new Runnable () { 74 public void run() { 75 try { 76 ServerSocketReader.super.processIQ(packet); 77 } 78 catch (UnauthorizedException e) { 79 Log.error("Error processing packet", e); 80 } 81 } 82 }); 83 } 84 catch (PacketRejectedException e) { 85 Log.debug("IQ rejected: " + packet.toXML(), e); 86 } 87 } 88 89 94 protected void processPresence(final Presence packet) throws UnauthorizedException { 95 try { 96 packetReceived(packet); 97 threadPool.execute(new Runnable () { 99 public void run() { 100 try { 101 ServerSocketReader.super.processPresence(packet); 102 } 103 catch (UnauthorizedException e) { 104 Log.error("Error processing packet", e); 105 } 106 } 107 }); 108 } 109 catch (PacketRejectedException e) { 110 Log.debug("Presence rejected: " + packet.toXML(), e); 111 } 112 } 113 114 119 protected void processMessage(final Message packet) throws UnauthorizedException { 120 try { 121 packetReceived(packet); 122 threadPool.execute(new Runnable () { 124 public void run() { 125 try { 126 ServerSocketReader.super.processMessage(packet); 127 } 128 catch (UnauthorizedException e) { 129 Log.error("Error processing packet", e); 130 } 131 } 132 }); 133 } 134 catch (PacketRejectedException e) { 135 Log.debug("Message rejected: " + packet.toXML(), e); 136 } 137 } 138 139 146 protected boolean processUnknowPacket(Element doc) { 147 if ("db".equals(doc.getNamespacePrefix()) && "result".equals(doc.getName())) { 149 if (!((IncomingServerSession) session).validateSubsequentDomain(doc)) { 150 open = false; 151 } 152 return true; 153 } 154 else if ("db".equals(doc.getNamespacePrefix()) && "verify".equals(doc.getName())) { 155 ((IncomingServerSession) session).verifyReceivedKey(doc); 158 return true; 159 } 160 return false; 161 } 162 163 172 private void packetReceived(Packet packet) throws PacketRejectedException { 173 if (packet.getTo() == null || packet.getFrom() == null) { 174 Log.debug("Closing IncomingServerSession due to packet with no TO or FROM: " + 175 packet.toXML()); 176 StreamError error = new StreamError(StreamError.Condition.improper_addressing); 178 connection.deliverRawText(error.toXML()); 179 connection.close(); 181 open = false; 182 throw new PacketRejectedException("Packet with no TO or FROM attributes"); 183 } 184 else if (!((IncomingServerSession) session).isValidDomain(packet.getFrom().getDomain())) { 185 Log.debug("Closing IncomingServerSession due to packet with invalid domain: " + 186 packet.toXML()); 187 StreamError error = new StreamError(StreamError.Condition.invalid_from); 189 connection.deliverRawText(error.toXML()); 190 connection.close(); 192 open = false; 193 throw new PacketRejectedException("Packet with no TO or FROM attributes"); 194 } 195 } 196 197 protected void shutdown() { 198 super.shutdown(); 199 threadPool.shutdown(); 202 } 203 204 boolean createSession(String namespace) throws UnauthorizedException, XmlPullParserException, 205 IOException { 206 if ("jabber:server".equals(namespace)) { 207 session = IncomingServerSession.createSession(serverName, reader, connection); 209 return true; 210 } 211 return false; 212 } 213 } 214 | Popular Tags |