aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-05-07 19:57:10 +0300
committerGravatar Vitaly Takmazov2016-05-07 19:57:10 +0300
commitb1d5d5801e90ef0d4e282a78543f1435b8b7d223 (patch)
tree70dcd3a4a67c4a15847b3c17357897709f2334ee /src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java
parentf2a2660e753fa11478fd3edadfdff9c000b22cc9 (diff)
STARTTLS
Diffstat (limited to 'src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java')
-rw-r--r--src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java150
1 files changed, 65 insertions, 85 deletions
diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java
index 4634ca99..48d2efd5 100644
--- a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java
+++ b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java
@@ -14,22 +14,18 @@ import org.xmlpull.v1.XmlPullParser;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
-import java.net.InetSocketAddress;
-import java.nio.channels.AsynchronousSocketChannel;
-import java.nio.channels.Channels;
-import java.nio.channels.CompletionHandler;
+import java.net.Socket;
import java.util.List;
import java.util.logging.Level;
/**
- *
* @author ugnich
*/
public class ConnectionRouter extends Connection implements Runnable {
private String componentName;
- ConnectionRouter(String componentName) {
+ ConnectionRouter(String componentName) throws Exception {
this.componentName = componentName;
}
@@ -38,96 +34,80 @@ public class ConnectionRouter extends Connection implements Runnable {
LOGGER.info("STREAM ROUTER START");
try {
- socket = AsynchronousSocketChannel.open();
- socket.connect(new InetSocketAddress("localhost", 5347), socket, new CompletionHandler<Void, AsynchronousSocketChannel>() {
- @Override
- public void completed(Void result, AsynchronousSocketChannel client) {
- try {
- parser.setInput(new InputStreamReader(Channels.newInputStream(client)));
-
- parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
- writer = new OutputStreamWriter(Channels.newOutputStream(client));
-
- String msg = "<stream:stream xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams' to='s2s'>";
- writer.write(msg);
- writer.flush();
-
- parser.next(); // stream:stream
- streamID = parser.getAttributeValue(null, "id");
- if (streamID == null || streamID.isEmpty()) {
- throw new Exception("FAIL ON FIRST PACKET");
- }
+ socket = new Socket("localhost", 5347);
+ parser.setInput(new InputStreamReader(socket.getInputStream()));
- msg = "<handshake>" + SHA1.encode(streamID + "secret") + "</handshake>";
- writer.write(msg);
- writer.flush();
+ parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
+ writer = new OutputStreamWriter(socket.getOutputStream());
- parser.next();
- if (!parser.getName().equals("handshake")) {
- throw new Exception("NO HANDSHAKE");
- }
- XmlUtils.skip(parser);
- LOGGER.info("STREAM ROUTER OPEN");
+ String msg = "<stream:stream xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams' to='s2s'>";
+ writer.write(msg);
+ writer.flush();
- while (parser.next() != XmlPullParser.END_DOCUMENT) {
- if (parser.getEventType() != XmlPullParser.START_TAG) {
- continue;
- }
+ parser.next(); // stream:stream
+ streamID = parser.getAttributeValue(null, "id");
+ if (streamID == null || streamID.isEmpty()) {
+ throw new Exception("FAIL ON FIRST PACKET");
+ }
- String tag = parser.getName();
- String to = parser.getAttributeValue(null, "to");
- if (to != null && (tag.equals("message") || tag.equals("presence") || tag.equals("iq"))) {
- JID jid = new JID(to);
- if (jid.Host != null) {
- if (jid.Host.equals(componentName)) {
- if (tag.equals("message")) {
- Message xmsg = Message.parse(parser, XMPPComponent.childParsers);
- LOGGER.info("STREAM ROUTER (PROCESS): " + xmsg.toString());
- JuickMessage jmsg = (JuickMessage) xmsg.getChild(JuickMessage.XMLNS);
- if (jmsg != null) {
- if (jid.Username != null && jid.Username.equals("recomm")) {
- sendJuickRecommendation(jmsg);
- } else {
- if (jmsg.getRID() > 0) {
- sendJuickComment(jmsg);
- } else if (jmsg.getMID() > 0) {
- sendJuickMessage(jmsg);
- }
- }
- }
- }
- } else if (jid.Host.endsWith(XMPPComponent.HOSTNAME) && (jid.Host.equals(XMPPComponent.HOSTNAME) || jid.Host.endsWith("." + XMPPComponent.HOSTNAME))) {
- String xml = XmlUtils.parseToString(parser, true);
- LOGGER.info("STREAM ROUTER: " + xml);
+ msg = "<handshake>" + SHA1.encode(streamID + "secret") + "</handshake>";
+ writer.write(msg);
+ writer.flush();
+
+ parser.next();
+ if (!parser.getName().equals("handshake")) {
+ throw new Exception("NO HANDSHAKE");
+ }
+ XmlUtils.skip(parser);
+ LOGGER.info("STREAM ROUTER OPEN");
+
+ while (parser.next() != XmlPullParser.END_DOCUMENT) {
+ if (parser.getEventType() != XmlPullParser.START_TAG) {
+ continue;
+ }
+
+ String tag = parser.getName();
+ String to = parser.getAttributeValue(null, "to");
+ if (to != null && (tag.equals("message") || tag.equals("presence") || tag.equals("iq"))) {
+ JID jid = new JID(to);
+ if (jid.Host != null) {
+ if (jid.Host.equals(componentName)) {
+ if (tag.equals("message")) {
+ Message xmsg = Message.parse(parser, XMPPComponent.childParsers);
+ LOGGER.info("STREAM ROUTER (PROCESS): " + xmsg.toString());
+ JuickMessage jmsg = (JuickMessage) xmsg.getChild(JuickMessage.XMLNS);
+ if (jmsg != null) {
+ if (jid.Username != null && jid.Username.equals("recomm")) {
+ sendJuickRecommendation(jmsg);
} else {
- String xml = XmlUtils.parseToString(parser, true);
- LOGGER.info("STREAM ROUTER (OUT): " + xml);
- XMPPComponent.sendOut(jid.Host, xml);
+ if (jmsg.getRID() > 0) {
+ sendJuickComment(jmsg);
+ } else if (jmsg.getMID() > 0) {
+ sendJuickMessage(jmsg);
+ }
}
- } else {
- LOGGER.info("STREAM ROUTER (NO TO): " + XmlUtils.parseToString(parser, true));
}
- } else {
- LOGGER.info("STREAM ROUTER: " + XmlUtils.parseToString(parser, true));
}
+ } else if (jid.Host.endsWith(XMPPComponent.HOSTNAME) && (jid.Host.equals(XMPPComponent.HOSTNAME) || jid.Host.endsWith("." + XMPPComponent.HOSTNAME))) {
+ String xml = XmlUtils.parseToString(parser, true);
+ LOGGER.info("STREAM ROUTER: " + xml);
+ } else {
+ String xml = XmlUtils.parseToString(parser, true);
+ LOGGER.info("STREAM ROUTER (OUT): " + xml);
+ XMPPComponent.sendOut(jid.Host, xml);
}
-
- LOGGER.warning("STREAM ROUTER FINISHED");
- } catch (Exception e) {
- LOGGER.log(Level.SEVERE, "xmpp router exception", e);
+ } else {
+ LOGGER.info("STREAM ROUTER (NO TO): " + XmlUtils.parseToString(parser, true));
}
+ } else {
+ LOGGER.info("STREAM ROUTER: " + XmlUtils.parseToString(parser, true));
}
+ }
- @Override
- public void failed(Throwable exc, AsynchronousSocketChannel attachment) {
- LOGGER.log(Level.WARNING, "s2s component failed to connect", exc);
- }
- });
- Thread.currentThread().join();
+ LOGGER.warning("STREAM ROUTER FINISHED");
} catch (Exception e) {
- LOGGER.log(Level.SEVERE, "NIO2 error", e);
+ LOGGER.log(Level.SEVERE, "xmpp router exception", e);
}
-
}
@Override
@@ -142,7 +122,7 @@ public class ConnectionRouter extends Connection implements Runnable {
}
}
- public void sendJuickMessage(JuickMessage jmsg) {
+ public void sendJuickMessage(JuickMessage jmsg) throws Exception {
List<String> jids;
synchronized (XMPPComponent.sqlSync) {
@@ -183,7 +163,7 @@ public class ConnectionRouter extends Connection implements Runnable {
}
}
- public void sendJuickComment(JuickMessage jmsg) {
+ public void sendJuickComment(JuickMessage jmsg) throws Exception {
List<String> jids;
String replyQuote;
@@ -231,7 +211,7 @@ public class ConnectionRouter extends Connection implements Runnable {
return quote;
}
- public void sendJuickRecommendation(JuickMessage recomm) {
+ public void sendJuickRecommendation(JuickMessage recomm) throws Exception {
List<String> jids;
JuickMessage jmsg;
synchronized (XMPPComponent.sqlSync) {