1 11 package org.jivesoftware.messenger; 12 13 import org.xmpp.packet.Packet; 14 import org.xmpp.packet.JID; 15 import org.xmpp.packet.StreamError; 16 import org.xmpp.component.*; 17 import org.xmpp.component.ComponentManager; 18 import org.jivesoftware.messenger.auth.UnauthorizedException; 19 import org.jivesoftware.messenger.auth.AuthFactory; 20 import org.jivesoftware.util.Log; 21 import org.jivesoftware.util.LocaleUtils; 22 import org.jivesoftware.util.JiveGlobals; 23 import org.dom4j.io.XPPPacketReader; 24 import org.dom4j.Element; 25 import org.xmlpull.v1.XmlPullParser; 26 import org.xmlpull.v1.XmlPullParserException; 27 28 import java.io.Writer ; 29 import java.io.IOException ; 30 31 36 public class ComponentSession extends Session { 37 38 private ExternalComponent component = new ExternalComponent(); 39 40 52 public static Session createSession(String serverName, XPPPacketReader reader, 53 Connection connection) throws UnauthorizedException, IOException , 54 XmlPullParserException 55 { 56 XmlPullParser xpp = reader.getXPPParser(); 57 Session session; 58 String domain = xpp.getAttributeValue("", "to"); 59 60 String subdomain = domain; 62 int index = domain.indexOf(serverName); 63 if (index > -1) { 64 subdomain = domain.substring(0, index -1); 65 } 66 67 Writer writer = connection.getWriter(); 68 StringBuilder sb = new StringBuilder (); 70 sb.append("<?xml version='1.0' encoding='"); 71 sb.append(CHARSET); 72 sb.append("'?>"); 73 sb.append("<stream:stream "); 74 sb.append("xmlns:stream=\"http://etherx.jabber.org/streams\" "); 75 sb.append("xmlns=\"jabber:component:accept\" from=\""); 76 sb.append(domain); 77 sb.append("\">"); 78 79 if (domain == null) { 81 StreamError error = new StreamError(StreamError.Condition.bad_format); 83 sb.append(error.toXML()); 84 sb.append("</stream:stream>"); 85 writer.write(sb.toString()); 86 writer.flush(); 87 connection.close(); 89 return null; 90 } 91 String secretKey = JiveGlobals.getProperty("component.external.secretKey"); 94 if (secretKey == null) { 95 Log.error("Setup for external components is incomplete. Property " + 96 "component.external.secretKey does not exist."); 97 StreamError error = new StreamError(StreamError.Condition.internal_server_error); 99 sb.append(error.toXML()); 100 sb.append("</stream:stream>"); 101 writer.write(sb.toString()); 102 writer.flush(); 103 connection.close(); 105 return null; 106 } 107 if (InternalComponentManager.getInstance().getComponent(subdomain) != null) { 109 StreamError error = new StreamError(StreamError.Condition.conflict); 112 sb.append(error.toXML()); 113 sb.append("</stream:stream>"); 114 writer.write(sb.toString()); 115 writer.flush(); 116 connection.close(); 118 return null; 119 } 120 121 session = SessionManager.getInstance().createComponentSession(connection); 123 session.setAddress(new JID(null, domain , null)); 125 126 try { 127 sb = new StringBuilder (); 129 sb.append("<?xml version='1.0' encoding='"); 130 sb.append(CHARSET); 131 sb.append("'?>"); 132 sb.append("<stream:stream "); 133 sb.append("xmlns:stream=\"http://etherx.jabber.org/streams\" "); 134 sb.append("xmlns=\"jabber:component:accept\" from=\""); 135 sb.append(domain); 136 sb.append("\" id=\""); 137 sb.append(session.getStreamID().toString()); 138 sb.append("\">"); 139 writer.write(sb.toString()); 140 writer.flush(); 141 142 Element doc = reader.parseDocument().getRootElement(); 144 String digest = "handshake".equals(doc.getName()) ? doc.getStringValue() : ""; 145 String anticipatedDigest = AuthFactory.createDigest(session.getStreamID().getID(), 146 secretKey); 147 if (!anticipatedDigest.equalsIgnoreCase(digest)) { 149 sb = new StringBuilder (); 152 StreamError error = new StreamError(StreamError.Condition.not_authorized); 154 sb.append(error.toXML()); 155 sb.append("</stream:stream>"); 156 writer.write(sb.toString()); 157 writer.flush(); 158 connection.close(); 160 return null; 161 } 162 else { 163 session.setStatus(Session.STATUS_AUTHENTICATED); 165 writer.write("<handshake></handshake>"); 167 writer.flush(); 168 ExternalComponent component = ((ComponentSession) session).getExternalComponent(); 170 InternalComponentManager.getInstance().addComponent(subdomain, component); 171 return session; 172 } 173 } 174 catch (Exception e) { 175 Log.error("An error occured while creating a ComponentSession", e); 176 connection.close(); 178 return null; 179 } 180 } 181 182 public ComponentSession(String serverName, Connection conn, StreamID id) { 183 super(serverName, conn, id); 184 } 185 186 public void process(Packet packet) throws PacketException { 187 component.processPacket(packet); 190 } 191 192 public ExternalComponent getExternalComponent() { 193 return component; 194 } 195 196 206 public class ExternalComponent implements Component { 207 208 public void processPacket(Packet packet) { 209 if (conn != null && !conn.isClosed()) { 210 try { 211 conn.deliver(packet); 212 } 213 catch (Exception e) { 214 Log.error(LocaleUtils.getLocalizedString("admin.error"), e); 215 conn.close(); 216 } 217 } 218 } 219 220 public String getName() { 221 return null; 222 } 223 224 public String getDescription() { 225 return null; 226 } 227 228 public void initialize(JID jid, ComponentManager componentManager) { 229 } 230 231 public void start() { 232 } 233 234 public void shutdown() { 235 } 236 } 237 238 public void packetReceived(Packet packet) { 239 } 241 } | Popular Tags |