diff options
Diffstat (limited to 'juick-ws/src/main/java')
-rw-r--r-- | juick-ws/src/main/java/com/juick/ws/XMPPConnection.java | 89 |
1 files changed, 35 insertions, 54 deletions
diff --git a/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java b/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java index 7a6feee1..1b6eb3dc 100644 --- a/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java +++ b/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java @@ -5,11 +5,6 @@ import com.juick.json.MessageSerializer; import com.juick.server.MessagesQueries; import com.juick.server.SubscriptionsQueries; import com.juick.util.ThreadHelper; -import com.juick.xmpp.JID; -import com.juick.xmpp.Message; -import com.juick.xmpp.Stream; -import com.juick.xmpp.StreamComponent; -import com.juick.xmpp.extensions.JuickMessage; import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,9 +14,14 @@ import org.springframework.core.env.Environment; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.util.Assert; import org.springframework.web.socket.TextMessage; +import rocks.xmpp.core.XmppException; +import rocks.xmpp.core.session.Extension; +import rocks.xmpp.core.session.XmppSession; +import rocks.xmpp.core.session.XmppSessionConfiguration; +import rocks.xmpp.core.stanza.model.Message; +import rocks.xmpp.extensions.component.accept.ExternalComponent; import java.io.IOException; -import java.net.Socket; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.stream.Collectors; @@ -29,7 +29,7 @@ import java.util.stream.Collectors; /** * @author ugnich */ -public class XMPPConnection implements InitializingBean, DisposableBean, Stream.StreamListener, Message.MessageListener { +public class XMPPConnection implements InitializingBean, DisposableBean { private static final Logger logger = LoggerFactory.getLogger(XMPPConnection.class); private final JdbcTemplate jdbc; @@ -40,7 +40,7 @@ public class XMPPConnection implements InitializingBean, DisposableBean, Stream. private final int xmppPort; private final String wsJid; - private Stream xmpp; + private XmppSession xmpp; public XMPPConnection( @@ -62,19 +62,34 @@ public class XMPPConnection implements InitializingBean, DisposableBean, Stream. } @Override - public void afterPropertiesSet() throws Exception { + public void afterPropertiesSet() { + XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() + .extensions(Extension.of(com.juick.Message.class)) + .build(); + xmpp = ExternalComponent.create(wsJid, xmppPassword, configuration, "localhost", xmppPort); + xmpp.addInboundMessageListener(e -> { + Message msg = e.getMessage(); + com.juick.Message jmsg = msg.getExtension(com.juick.Message.class); + if (jmsg != null) { + logger.info("got msg: " + ms.serialize(jmsg).toString()); + if (jmsg.getMid() == 0) { + int uid_to = NumberUtils.toInt(msg.getTo().getLocal(), 0); + if (uid_to > 0) { + onJuickPM(uid_to, jmsg); + } + } else if (jmsg.getRid() == 0) { + onJuickMessagePost(jmsg); + } else { + // to get quote + com.juick.Message reply = MessagesQueries.getReply(jdbc, jmsg.getMid(), jmsg.getRid()); + onJuickMessageReply(reply); + } + } + }); try { - Socket socket = new Socket("localhost", xmppPort); - xmpp = new StreamComponent(new JID(wsJid), socket.getInputStream(), socket.getOutputStream(), xmppPassword); - xmpp.addChildParser(new JuickMessage()); - xmpp.addListener((Stream.StreamListener) this); - xmpp.addListener((Message.MessageListener) this); - - service.submit(() -> xmpp.startParsing()); - - logger.info("XMPPConnection initialized"); - } catch (IOException e) { - logger.error("XMPPConnection initialization error", e); + xmpp.connect(); + } catch (XmppException e) { + logger.warn("xmpp extension", e); } } @@ -85,40 +100,6 @@ public class XMPPConnection implements InitializingBean, DisposableBean, Stream. logger.info("XMPPConnection destroyed"); } - @Override - public void onStreamReady() { - logger.info("XMPP stream ready"); - } - - @Override - public void onStreamFail(final Exception ex) { - logger.error("XMPP stream failed", ex); - } - - @Override - public void onMessage(final com.juick.xmpp.Message msg) { - JuickMessage jmsg = (JuickMessage) msg.getChild(JuickMessage.XMLNS); - if (jmsg != null) { - logger.info("got jmsg: " + ms.serialize(jmsg).toString()); - if (jmsg.getMid() == 0) { - int uid_to = 0; - try { - uid_to = Integer.parseInt(msg.to.Username); - } catch (Exception e) { - } - if (uid_to > 0) { - onJuickPM(uid_to, jmsg); - } - } else if (jmsg.getRid() == 0) { - onJuickMessagePost(jmsg); - } else { - // to get quote - com.juick.Message reply = MessagesQueries.getReply(jdbc, jmsg.getMid(), jmsg.getRid()); - onJuickMessageReply(reply); - } - } - } - MessageSerializer messageSerializer = new MessageSerializer(); private void onJuickPM(final int uid_to, final com.juick.Message jmsg) { |