aboutsummaryrefslogtreecommitdiff
path: root/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java
diff options
context:
space:
mode:
Diffstat (limited to 'juick-xmpp/src/main/java/com/juick/components/XMPPServer.java')
-rw-r--r--juick-xmpp/src/main/java/com/juick/components/XMPPServer.java47
1 files changed, 37 insertions, 10 deletions
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<CacheEntry> outCache = Collections.synchronizedList(new ArrayList<>());
private final List<StanzaListener> stanzaListeners = Collections.synchronizedList(new ArrayList<>());
- final public HashMap<String, StanzaChild> 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;
+ }
}