From 1c65b6d29c0fc69d861ffa0e624f73981b62cca6 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 15 Nov 2016 14:51:36 +0300 Subject: juick-www: ugnich xmpp -> babbler --- juick-www/src/main/java/com/juick/www/Main.java | 64 ++++--------- .../src/main/java/com/juick/www/NewMessage.java | 102 ++++++++++----------- juick-www/src/main/java/com/juick/www/PM.java | 31 +++---- 3 files changed, 83 insertions(+), 114 deletions(-) (limited to 'juick-www/src/main/java') diff --git a/juick-www/src/main/java/com/juick/www/Main.java b/juick-www/src/main/java/com/juick/www/Main.java index 4a873cf3..82f93e00 100644 --- a/juick-www/src/main/java/com/juick/www/Main.java +++ b/juick-www/src/main/java/com/juick/www/Main.java @@ -17,18 +17,18 @@ */ package com.juick.www; +import com.juick.Message; import com.juick.server.UserQueries; -import com.juick.xmpp.JID; -import com.juick.xmpp.Stream; -import com.juick.xmpp.StreamComponent; import com.mitchellbosecke.pebble.error.PebbleException; -import net.jodah.failsafe.Execution; -import net.jodah.failsafe.RetryPolicy; import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.math.NumberUtils; import org.springframework.jdbc.core.JdbcTemplate; -import org.xmlpull.v1.XmlPullParserException; +import rocks.xmpp.addr.Jid; +import rocks.xmpp.core.XmppException; +import rocks.xmpp.core.session.Extension; +import rocks.xmpp.core.session.XmppSessionConfiguration; +import rocks.xmpp.extensions.component.accept.ExternalComponent; import ru.sape.Sape; import javax.servlet.ServletException; @@ -38,14 +38,10 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.net.Socket; import java.net.URISyntaxException; import java.net.URLEncoder; import java.util.Objects; import java.util.Properties; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; /** * @@ -53,12 +49,12 @@ import java.util.concurrent.TimeUnit; */ @WebServlet(name = "Main", urlPatterns = {"/"}) @MultipartConfig(fileSizeThreshold = 1024 * 1024, maxRequestSize = 1024 * 1024 * 10) -public class Main extends HttpServlet implements Stream.StreamListener { +public class Main extends HttpServlet { JdbcTemplate sql; JdbcTemplate sqlSearch; String sqlSearchConnStr = "jdbc:mysql://127.0.0.1:9306?autoReconnect=true&useUnicode=yes&characterEncoding=utf8&maxAllowedPacket=512000"; - Stream xmpp; + ExternalComponent xmpp; Home home = new Home(); Discover discover = new Discover(); PM pm = new PM(); @@ -73,8 +69,6 @@ public class Main extends HttpServlet implements Stream.StreamListener { SignUp signup = new SignUp(); Settings settings; RSS rss = new RSS(); - ExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); - Execution execution; @Override public void init() throws ServletException { @@ -94,7 +88,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { sqlSearch = new JdbcTemplate(dataSourceSearch); boolean isXmppDisabled = BooleanUtils.toBoolean(conf.getProperty("xmpp_disabled")); if (!isXmppDisabled) { - setupXmppComponent(new JID(conf.getProperty("www_xmpp_jid", "www.juick.local")), + setupXmppComponent(Jid.of(conf.getProperty("www_xmpp_jid", "www.juick.local")), conf.getProperty("xmpp_password"), NumberUtils.toInt(conf.getProperty("xmpp_port", ""), 5347)); } twitterAuth = new TwitterAuth(conf.getProperty("twitter_consumer_key"), @@ -116,38 +110,18 @@ public class Main extends HttpServlet implements Stream.StreamListener { } } - public void setupXmppComponent(final JID componentJid, final String password, final int port) { - @SuppressWarnings("unchecked") RetryPolicy retryPolicy = new RetryPolicy() - .withBackoff(1, 30, TimeUnit.SECONDS) - .withJitter(0.1) - .retryOn(IOException.class, XmlPullParserException.class); - execution = new Execution(retryPolicy); - executorService.submit(() -> { - while (!execution.isComplete()) { - try { - Socket socket = new Socket("localhost", port); - xmpp = new StreamComponent(componentJid, socket.getInputStream(), socket.getOutputStream(), password); - xmpp.addListener(Main.this); - xmpp.startParsing(); - } catch (IOException e) { - log("XMPP router disconnected, reconnecting..."); - } - } - }); - } - - @Override - public void onStreamFail(Exception e) { - log("XMPP STREAM FAIL:" + e); - execution.recordFailure(e); - } - - @Override - public void onStreamReady() { - log("XMPP STREAM READY"); + public void setupXmppComponent(final Jid componentJid, final String password, final int port) { + XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() + .extensions(Extension.of(Message.class)) + .build(); + xmpp = ExternalComponent.create(componentJid.toString(), password, configuration, "localhost", port); + try { + xmpp.connect(); + } catch (XmppException e) { + log("xmpp extension", e); + } } - /** * Handles the HTTP GET method. * @param request servlet request diff --git a/juick-www/src/main/java/com/juick/www/NewMessage.java b/juick-www/src/main/java/com/juick/www/NewMessage.java index 310ede73..29c834d6 100644 --- a/juick-www/src/main/java/com/juick/www/NewMessage.java +++ b/juick-www/src/main/java/com/juick/www/NewMessage.java @@ -19,19 +19,17 @@ package com.juick.www; import com.juick.Tag; import com.juick.server.*; -import com.juick.xmpp.JID; -import com.juick.xmpp.Message; -import com.juick.xmpp.Stream; -import com.juick.xmpp.extensions.JuickMessage; -import com.juick.xmpp.extensions.JuickUser; -import com.juick.xmpp.extensions.Nickname; -import com.juick.xmpp.extensions.XOOB; import net.coobird.thumbnailator.Thumbnails; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.jdbc.core.JdbcTemplate; +import rocks.xmpp.addr.Jid; +import rocks.xmpp.core.session.XmppSession; +import rocks.xmpp.core.stanza.model.Message; +import rocks.xmpp.extensions.nick.model.Nickname; +import rocks.xmpp.extensions.oob.model.x.OobX; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -39,6 +37,8 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.net.URLEncoder; import java.nio.file.Files; @@ -148,7 +148,7 @@ public class NewMessage { out.println("

"); } - public void doPostMessage(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, Stream xmpp, com.juick.User visitor) throws ServletException, IOException { + public void doPostMessage(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, XmppSession xmpp, com.juick.User visitor) throws ServletException, IOException { String body = request.getParameter("body"); if (body == null || body.length() < 1 || body.length() > 4096) { response.sendError(400); @@ -201,16 +201,12 @@ public class NewMessage { SubscriptionsQueries.subscribeMessage(sql, mid, visitor.getUid()); Message xmsg = new Message(); - xmsg.from = new JID("juick", "juick.com", null); - xmsg.type = Message.Type.chat; - xmsg.thread = "juick-" + mid; - - JuickMessage jmsg = new JuickMessage(MessagesQueries.getMessage(sql, mid)); - xmsg.addChild(jmsg); - - Nickname nick = new Nickname(); - nick.Nickname = "@" + jmsg.getUser().getName(); - xmsg.addChild(nick); + xmsg.setFrom(Jid.of("juick@juick.com")); + xmsg.setType(Message.Type.CHAT); + xmsg.setThread("juick-" + mid); + com.juick.Message jmsg = MessagesQueries.getMessage(sql, mid); + xmsg.addExtension(jmsg); + xmsg.addExtension(new Nickname("@" + jmsg.getUser().getName())); if (attachmentFName != null) { String fname = mid + "." + attachmentType; @@ -226,9 +222,11 @@ public class NewMessage { .toFile(Paths.get(imgDir, "ps", fname).toFile()); body = attachmentURL + "\n" + body; - XOOB xoob = new XOOB(); - xoob.URL = attachmentURL; - xmsg.addChild(xoob); + try { + xmsg.addExtension(new OobX(new URI(attachmentURL))); + } catch (URISyntaxException e) { + logger.warn("invalid uri: " + attachmentURL, e); + } } if (xmpp != null) { @@ -236,25 +234,23 @@ public class NewMessage { for (String tag : tagsArr) { tagsStr2 += " *" + tag; } - xmsg.body = "@" + jmsg.getUser().getName() + ":" + tagsStr2 + "\n" + body + "\n\n#" + mid + " http://juick.com/" + mid; + xmsg.setBody("@" + jmsg.getUser().getName() + ":" + tagsStr2 + "\n" + body + "\n\n#" + mid + " http://juick.com/" + mid); - xmsg.to = new JID("juick", "s2s.juick.com", null); + xmsg.setTo(Jid.of("juick@s2s.juick.com")); xmpp.send(xmsg); - xmsg.to.Host = "ws.juick.com"; + xmsg.setTo(Jid.of("juick@ws.juick.com")); xmpp.send(xmsg); - xmsg.to.Host = "push.juick.com"; + xmsg.setTo(Jid.of("juick@push.juick.com")); xmpp.send(xmsg); - xmsg.to.Host = "crosspost.juick.com"; - xmsg.to.Username = "twitter"; + xmsg.setTo(Jid.of("twitter@crosspost.juick.com")); xmpp.send(xmsg); - xmsg.to.Username = "fb"; + xmsg.setTo(Jid.of("fb@crosspost.juick.com")); xmpp.send(xmsg); - xmsg.to.Host = "nologin.ru"; - xmsg.to.Username = "jubo"; + xmsg.setTo(Jid.of("jubo@nologin.ru")); xmpp.send(xmsg); } else { logger.warn("XMPP unavailable"); @@ -308,7 +304,7 @@ public class NewMessage { } } - public void doPostComment(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, Stream xmpp, com.juick.User visitor) throws ServletException, IOException { + public void doPostComment(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, XmppSession xmpp, com.juick.User visitor) throws ServletException, IOException { int mid = NumberUtils.toInt(request.getParameter("mid"), 0); if (mid == 0) { response.sendError(400); @@ -367,21 +363,18 @@ public class NewMessage { SubscriptionsQueries.subscribeMessage(sql, mid, visitor.getUid()); Message xmsg = new Message(); - xmsg.from = new JID("juick", "juick.com", null); - xmsg.type = Message.Type.chat; - xmsg.thread = "juick-" + mid; + xmsg.setFrom(Jid.of("juick@juick.com")); + xmsg.setType(Message.Type.CHAT); + xmsg.setThread("juick-" + mid); - JuickMessage jmsg = new JuickMessage(MessagesQueries.getReply(sql, mid, ridnew)); - xmsg.addChild(jmsg); + com.juick.Message jmsg = MessagesQueries.getReply(sql, mid, ridnew); + xmsg.addExtension(jmsg); String quote = reply != null ? reply.getText() : msg.getText(); if (quote.length() >= 50) { quote = quote.substring(0, 47) + "..."; } - - Nickname nick = new Nickname(); - nick.Nickname = "@" + jmsg.getUser().getName(); - xmsg.addChild(nick); + xmsg.addExtension(new Nickname("@" + jmsg.getUser().getName())); if (attachmentFName != null) { String fname = mid + "-" + ridnew + "." + attachmentType; @@ -397,22 +390,25 @@ public class NewMessage { .toFile(Paths.get(imgDir, "ps", fname).toFile()); body = attachmentURL + "\n" + body; - XOOB xoob = new XOOB(); - xoob.URL = attachmentURL; - xmsg.addChild(xoob); + try { + xmsg.addExtension(new OobX(new URI(attachmentURL))); + } catch (URISyntaxException e) { + logger.warn("invalid uri: " + attachmentURL, e); + } } if (xmpp != null) { - xmsg.body = "Reply by @" + jmsg.getUser().getName() + ":\n>" + quote + "\n" + body + "\n\n#" + mid + "/" + ridnew + " http://juick.com/" + mid + "#" + ridnew; + xmsg.setBody("Reply by @" + jmsg.getUser().getName() + ":\n>" + quote + "\n" + body + "\n\n#" + + mid + "/" + ridnew + " http://juick.com/" + mid + "#" + ridnew); - xmsg.to = new JID("juick", "s2s.juick.com", null); + xmsg.setTo(Jid.of("juick@s2s.juick.com")); xmpp.send(xmsg); - xmsg.to.Host = "ws.juick.com"; + xmsg.setTo(Jid.of("juick@ws.juick.com")); xmpp.send(xmsg); - xmsg.to.Host = "push.juick.com"; + xmsg.setTo(Jid.of("juick@push.juick.com")); xmpp.send(xmsg); } else { logger.warn("XMPP unavailable"); @@ -421,7 +417,7 @@ public class NewMessage { Utils.sendTemporaryRedirect(response, "/" + msg.getUser().getName() + "/" + mid + "#" + ridnew); } - public void doPostRecomm(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, Stream xmpp, com.juick.User visitor) throws ServletException, IOException { + public void doPostRecomm(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, XmppSession xmpp, com.juick.User visitor) throws ServletException, IOException { int mid = NumberUtils.toInt(request.getParameter("mid"), 0); if (mid == 0) { response.sendError(400); @@ -442,12 +438,12 @@ public class NewMessage { if (res) { if (xmpp != null) { Message xmsg = new Message(); - xmsg.from = new JID("juick", "juick.com", null); - xmsg.to = new JID("recomm", "s2s.juick.com", null); - JuickMessage jmsg = new JuickMessage(); + xmsg.setFrom(Jid.of("juick@juick.com")); + xmsg.setTo(Jid.of("recomm@s2s.juick.com")); + com.juick.Message jmsg = new com.juick.Message(); jmsg.setMid(mid); - jmsg.setUser(new JuickUser(visitor)); - xmsg.addChild(jmsg); + jmsg.setUser(visitor); + xmsg.addExtension(jmsg); xmpp.send(xmsg); } else { logger.warn("XMPP unavailable"); diff --git a/juick-www/src/main/java/com/juick/www/PM.java b/juick-www/src/main/java/com/juick/www/PM.java index 2e35ec1c..95e2623a 100644 --- a/juick-www/src/main/java/com/juick/www/PM.java +++ b/juick-www/src/main/java/com/juick/www/PM.java @@ -21,15 +21,14 @@ import com.juick.server.PMQueries; import com.juick.server.TagQueries; import com.juick.server.UserQueries; import com.juick.util.UserUtils; -import com.juick.xmpp.JID; -import com.juick.xmpp.Message; -import com.juick.xmpp.Stream; -import com.juick.xmpp.extensions.JuickMessage; import com.mitchellbosecke.pebble.error.PebbleException; import com.mitchellbosecke.pebble.template.PebbleTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.jdbc.core.JdbcTemplate; +import rocks.xmpp.addr.Jid; +import rocks.xmpp.core.session.XmppSession; +import rocks.xmpp.core.stanza.model.Message; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -87,7 +86,7 @@ public class PM { } } - public void doPostPM(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, Stream xmpp, com.juick.User visitor) throws ServletException, IOException { + public void doPostPM(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, XmppSession xmpp, com.juick.User visitor) throws ServletException, IOException { String uname = request.getParameter("uname"); if (uname.startsWith("@")) { uname = uname.substring(1); @@ -111,28 +110,28 @@ public class PM { if (PMQueries.createPM(sql, visitor.getUid(), uid, body)) { if (xmpp != null) { Message msg = new Message(); - msg.from = new JID("juick", "juick.com", null); - msg.to = new JID(Integer.toString(uid), "push.juick.com", null); - JuickMessage jmsg = new JuickMessage(); + msg.setFrom(Jid.of("juick@juick.com")); + msg.setTo(Jid.of(String.format("%d@push.juick.com", uid))); + com.juick.Message jmsg = new com.juick.Message(); jmsg.setUser(visitor); jmsg.setText(body); - msg.childs.add(jmsg); + msg.addExtension(jmsg); xmpp.send(msg); - msg.to.Host = "ws.juick.com"; + msg.setTo(Jid.of(String.format("%d@ws.juick.com", uid))); xmpp.send(msg); List jids = UserQueries.getJIDsbyUID(sql, uid); for (String jid : jids) { Message mm = new Message(); - mm.to = new JID(jid); - mm.type = Message.Type.chat; + mm.setTo(Jid.of(jid)); + mm.setType(Message.Type.CHAT); if (PMQueries.havePMinRoster(sql, visitor.getUid(), jid)) { - mm.from = new JID(jmsg.getUser().getName(), "juick.com", "Juick"); - mm.body = body; + mm.setFrom(Jid.of(jmsg.getUser().getName(), "juick.com", "Juick")); + mm.setBody(body); } else { - mm.from = new JID("juick", "juick.com", "Juick"); - mm.body = "Private message from @" + jmsg.getUser().getName() + ":\n" + body; + mm.setFrom(Jid.of("juick", "juick.com", "Juick")); + mm.setBody("Private message from @" + jmsg.getUser().getName() + ":\n" + body); } xmpp.send(mm); } -- cgit v1.2.3