1 11 12 package org.jivesoftware.messenger.net; 13 14 import org.dom4j.Element; 15 import org.dom4j.io.XPPPacketReader; 16 import org.jivesoftware.messenger.*; 17 import org.jivesoftware.messenger.auth.UnauthorizedException; 18 import org.jivesoftware.messenger.interceptor.InterceptorManager; 19 import org.jivesoftware.messenger.interceptor.PacketRejectedException; 20 import org.jivesoftware.util.LocaleUtils; 21 import org.jivesoftware.util.Log; 22 import org.xmlpull.v1.XmlPullParser; 23 import org.xmlpull.v1.XmlPullParserException; 24 import org.xmlpull.v1.XmlPullParserFactory; 25 import org.xmpp.packet.*; 26 27 import java.io.EOFException ; 28 import java.io.IOException ; 29 import java.io.InputStreamReader ; 30 import java.io.Writer ; 31 import java.net.Socket ; 32 import java.net.SocketException ; 33 34 39 public class SocketReadThread extends Thread { 40 41 44 private static String CHARSET = "UTF-8"; 45 48 private static XmlPullParserFactory factory = null; 49 50 private Socket socket; 51 private Session session; 52 private SocketConnection connection; 53 private String serverName; 54 57 private PacketRouter router; 58 private boolean clearSignout = false; 59 XPPPacketReader reader = null; 60 61 static { 62 try { 63 factory = XmlPullParserFactory.newInstance(); 64 } 65 catch (XmlPullParserException e) { 66 Log.error("Error creating a parser factory", e); 67 } 68 } 69 70 78 public SocketReadThread(PacketRouter router, String serverName, Socket socket, 79 SocketConnection connection) 80 { 81 super("SRT reader"); 82 this.serverName = serverName; 83 this.router = router; 84 this.connection = connection; 85 this.socket = socket; 86 } 87 88 92 public void run() { 93 try { 94 reader = new XPPPacketReader(); 95 reader.setXPPFactory(factory); 96 97 reader.getXPPParser().setInput(new InputStreamReader (socket.getInputStream(), 98 CHARSET)); 99 100 createSession(); 102 103 if (session != null) { 105 readStream(); 106 } 107 108 } 109 catch (EOFException eof) { 110 } 112 catch (SocketException se) { 113 } 116 catch (XmlPullParserException ie) { 117 if (clearSignout == false) { 120 if (session != null && session.getStatus() == Session.STATUS_AUTHENTICATED) { 121 if (session instanceof ClientSession) { 122 Presence presence = ((ClientSession) session).getPresence(); 123 if (presence != null) { 124 Presence packet = presence.createCopy(); 126 packet.setType(Presence.Type.unavailable); 127 packet.setFrom(session.getAddress()); 128 router.route(packet); 129 clearSignout = true; 130 } 131 } 132 } 133 } 134 } 139 catch (Exception e) { 140 if (session != null) { 141 Log.warn(LocaleUtils.getLocalizedString("admin.error.stream"), e); 142 } 143 } 144 finally { 145 if (session != null) { 146 Log.debug("Logging off " + session.getAddress() + " on " + connection); 147 try { 148 sleep(3000); 152 session.getConnection().close(); 153 } 154 catch (Exception e) { 155 Log.warn(LocaleUtils.getLocalizedString("admin.error.connection") 156 + "\n" + socket.toString()); 157 } 158 } 159 else { 160 Log.error(LocaleUtils.getLocalizedString("admin.error.connection") 161 + "\n" + socket.toString()); 162 } 163 } 164 } 165 166 169 private void readStream() throws Exception { 170 while (true) { 171 Element doc = reader.parseDocument().getRootElement(); 172 173 if (doc == null) { 174 return; 177 } 178 179 String tag = doc.getName(); 180 if ("message".equals(tag)) { 181 Message packet = null; 182 try { 183 packet = new Message(doc); 184 } 185 catch(IllegalArgumentException e) { 186 Message reply = new Message(); 188 reply.setID(doc.attributeValue("id")); 189 reply.setTo(session.getAddress()); 190 reply.getElement().addAttribute("from", doc.attributeValue("to")); 191 reply.setError(PacketError.Condition.jid_malformed); 192 session.process(reply); 193 continue; 194 } 195 session.packetReceived(packet); 198 try { 199 InterceptorManager.getInstance().invokeInterceptors(packet, session, true, 201 false); 202 router.route(packet); 203 InterceptorManager.getInstance().invokeInterceptors(packet, session, true, 205 true); 206 session.incrementClientPacketCount(); 207 } 208 catch (PacketRejectedException e) { 209 Message reply = new Message(); 211 reply.setID(packet.getID()); 212 reply.setTo(session.getAddress()); 213 reply.setFrom(packet.getTo()); 214 reply.setError(PacketError.Condition.not_allowed); 215 session.process(reply); 216 } 217 } 218 else if ("presence".equals(tag)) { 219 Presence packet = null; 220 try { 221 packet = new Presence(doc); 222 } 223 catch(IllegalArgumentException e) { 224 Presence reply = new Presence(); 226 reply.setID(doc.attributeValue("id")); 227 reply.setTo(session.getAddress()); 228 reply.getElement().addAttribute("from", doc.attributeValue("to")); 229 reply.setError(PacketError.Condition.jid_malformed); 230 session.process(reply); 231 continue; 232 } 233 session.packetReceived(packet); 236 try { 237 InterceptorManager.getInstance().invokeInterceptors(packet, session, true, 239 false); 240 router.route(packet); 241 InterceptorManager.getInstance().invokeInterceptors(packet, session, true, 243 true); 244 session.incrementClientPacketCount(); 245 clearSignout = (Presence.Type.unavailable == packet.getType() ? true : false); 247 } 248 catch (PacketRejectedException e) { 249 Presence reply = new Presence(); 251 reply.setID(packet.getID()); 252 reply.setTo(session.getAddress()); 253 reply.setFrom(packet.getTo()); 254 reply.setError(PacketError.Condition.not_allowed); 255 session.process(reply); 256 } 257 } 258 else if ("iq".equals(tag)) { 259 IQ packet = null; 260 try { 261 packet = getIQ(doc); 262 } 263 catch(IllegalArgumentException e) { 264 IQ reply = new IQ(); 266 if (!doc.elements().isEmpty()) { 267 reply.setChildElement(((Element) doc.elements().get(0)).createCopy()); 268 } 269 reply.setID(doc.attributeValue("id")); 270 reply.setTo(session.getAddress()); 271 if (doc.attributeValue("to") != null) { 272 reply.getElement().addAttribute("from", doc.attributeValue("to")); 273 } 274 reply.setError(PacketError.Condition.jid_malformed); 275 session.process(reply); 276 continue; 277 } 278 session.packetReceived(packet); 281 try { 282 InterceptorManager.getInstance().invokeInterceptors(packet, session, true, 284 false); 285 router.route(packet); 286 InterceptorManager.getInstance().invokeInterceptors(packet, session, true, 288 true); 289 session.incrementClientPacketCount(); 290 } 291 catch (PacketRejectedException e) { 292 IQ reply = new IQ(); 294 reply.setChildElement(packet.getChildElement().createCopy()); 295 reply.setID(packet.getID()); 296 reply.setTo(session.getAddress()); 297 reply.setFrom(packet.getTo()); 298 reply.setError(PacketError.Condition.not_allowed); 299 session.process(reply); 300 } 301 } 302 else { 303 throw new XmlPullParserException(LocaleUtils.getLocalizedString( 304 "admin.error.packet.tag") + tag); 305 } 306 } 307 } 308 309 private IQ getIQ(Element doc) { 310 Element query = doc.element("query"); 311 if (query != null && "jabber:iq:roster".equals(query.getNamespaceURI())) { 312 return new Roster(doc); 313 } 314 else { 315 return new IQ(doc); 316 } 317 } 318 319 328 private void createSession() throws UnauthorizedException, XmlPullParserException, IOException { 329 XmlPullParser xpp = reader.getXPPParser(); 330 for (int eventType = xpp.getEventType(); eventType != XmlPullParser.START_TAG;) { 331 eventType = xpp.next(); 332 } 333 334 if ("jabber:client".equals(xpp.getNamespace(null))) { 336 session = ClientSession.createSession(serverName, reader, connection); 338 } 339 else if ("jabber:component:accept".equals(xpp.getNamespace(null))) { 340 session = ComponentSession.createSession(serverName, reader, connection); 342 } 343 else { 344 Writer writer = connection.getWriter(); 345 StringBuilder sb = new StringBuilder (); 346 sb.append("<?xml version='1.0' encoding='"); 347 sb.append(CHARSET); 348 sb.append("'?>"); 349 sb.append("<stream:error>"); 351 sb.append("<bad-namespace-prefix xmlns=\"urn:ietf:params:xml:ns:xmpp-streams\"/>"); 352 sb.append("</stream:error>"); 353 sb.append("</stream:stream>"); 354 writer.write(sb.toString()); 355 writer.flush(); 356 connection.close(); 358 } 359 } 360 } 361 | Popular Tags |