From 2be5fc6e0397b53173aa21298142d5fa66877fa5 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 6 Feb 2017 12:14:29 +0300 Subject: juick-xmpp: ConnectionRouter uses babbler now --- .../main/java/com/juick/components/XMPPServer.java | 47 +++++++++++++++++----- 1 file changed, 37 insertions(+), 10 deletions(-) (limited to 'juick-xmpp/src/main/java/com/juick/components/XMPPServer.java') diff --git a/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java b/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java index 4a182cc9..72dba642 100644 --- a/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java +++ b/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java @@ -2,9 +2,6 @@ package com.juick.components; import com.juick.components.s2s.*; import com.juick.service.*; -import com.juick.xmpp.Stanza; -import com.juick.xmpp.StanzaChild; -import com.juick.xmpp.extensions.JuickMessage; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; @@ -13,9 +10,17 @@ import org.slf4j.LoggerFactory; import org.springframework.core.env.Environment; import org.xmlpull.v1.XmlPullParserException; import rocks.xmpp.addr.Jid; +import rocks.xmpp.core.session.Extension; +import rocks.xmpp.core.session.XmppSessionConfiguration; +import rocks.xmpp.core.stanza.model.Stanza; +import rocks.xmpp.util.XmppUtils; import javax.inject.Inject; +import javax.xml.bind.JAXBException; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; import java.io.IOException; +import java.io.StringWriter; import java.net.ServerSocket; import java.net.Socket; import java.security.KeyManagementException; @@ -46,8 +51,6 @@ public class XMPPServer implements AutoCloseable { private final List outCache = Collections.synchronizedList(new ArrayList<>()); private final List stanzaListeners = Collections.synchronizedList(new ArrayList<>()); - final public HashMap childParsers = new HashMap<>(); - @Inject public MessagesService messagesService; @Inject @@ -63,12 +66,18 @@ public class XMPPServer implements AutoCloseable { private ServerSocket listener; + private BasicXmppSession session; + public XMPPServer(Environment env, ExecutorService service) { this.service = service; + XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() + .extensions(Extension.of(com.juick.Message.class)) + .build(); logger.info("component initialized"); try { HOSTNAME = env.getProperty("hostname"); + session = BasicXmppSession.create(HOSTNAME, configuration); componentName = env.getProperty("componentname"); int componentPort = NumberUtils.toInt(env.getProperty("component_port"), 5347); int s2sPort = NumberUtils.toInt(env.getProperty("s2s_port"), 5269); @@ -79,8 +88,6 @@ public class XMPPServer implements AutoCloseable { jid = Jid.of(env.getProperty("xmppbot_jid")); boolean disabled = BooleanUtils.toBoolean(env.getProperty("xmpp_disabled", "false")); - childParsers.put(JuickMessage.XMLNS, new JuickMessage()); - if (!disabled) { router = new ConnectionRouter(this, componentName, componentPort, env.getProperty("xmpp_password")); service.submit(router); @@ -196,7 +203,19 @@ public class XMPPServer implements AutoCloseable { } public void sendOut(Stanza s) { - sendOut(s.to.getDomain(), s.toString()); + try { + StringWriter stanzaWriter = new StringWriter(); + XMLStreamWriter xmppStreamWriter = XmppUtils.createXmppStreamWriter( + session.getConfiguration().getXmlOutputFactory().createXMLStreamWriter(stanzaWriter)); + session.createMarshaller().marshal(s, xmppStreamWriter); + xmppStreamWriter.flush(); + xmppStreamWriter.close(); + String xml = stanzaWriter.toString(); + logger.info("s2s (out): {}", xml); + sendOut(s.getTo().getDomain(), xml); + } catch (XMLStreamException | JAXBException e1) { + logger.info("jaxb exception", e1); + } } public void sendOut(String hostname, String xml) { @@ -282,11 +301,19 @@ public class XMPPServer implements AutoCloseable { } } - public void onStanzaReceived(String type, Stanza xmlValue) { - stanzaListeners.forEach(l -> l.stanzaReceived(type, xmlValue)); + public void onStanzaReceived(Stanza xmlValue) { + stanzaListeners.forEach(l -> l.stanzaReceived(xmlValue)); } public Jid getJid() { return jid; } + + public BasicXmppSession getSession() { + return session; + } + + public void setSession(BasicXmppSession session) { + this.session = session; + } } -- cgit v1.2.3