diff options
Diffstat (limited to 'juick-api/src/main/java/com')
-rw-r--r-- | juick-api/src/main/java/com/juick/api/Main.java | 131 | ||||
-rw-r--r-- | juick-api/src/main/java/com/juick/api/Messages.java | 26 | ||||
-rw-r--r-- | juick-api/src/main/java/com/juick/api/PM.java | 33 |
3 files changed, 87 insertions, 103 deletions
diff --git a/juick-api/src/main/java/com/juick/api/Main.java b/juick-api/src/main/java/com/juick/api/Main.java index c102bf18..2ca126d0 100644 --- a/juick-api/src/main/java/com/juick/api/Main.java +++ b/juick-api/src/main/java/com/juick/api/Main.java @@ -23,17 +23,19 @@ import com.juick.server.MessagesQueries; import com.juick.server.SubscriptionsQueries; import com.juick.server.TagQueries; import com.juick.server.UserQueries; -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 com.juick.xmpp.extensions.Nickname; -import com.juick.xmpp.extensions.XOOB; import net.coobird.thumbnailator.Thumbnails; import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.lang3.math.NumberUtils; import org.springframework.jdbc.core.JdbcTemplate; +import rocks.xmpp.addr.Jid; +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 rocks.xmpp.extensions.nick.model.Nickname; +import rocks.xmpp.extensions.oob.model.x.OobX; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; @@ -44,7 +46,8 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.lang.reflect.InvocationTargetException; -import java.net.Socket; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; @@ -52,19 +55,16 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.Properties; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - /** * * @author Ugnich Anton */ @WebServlet(name = "Main", urlPatterns = {"/"}) @MultipartConfig -public class Main extends HttpServlet implements Stream.StreamListener { +public class Main extends HttpServlet { JdbcTemplate jdbc; - Stream xmpp; + ExternalComponent xmpp; Messages messages; Users users; PM pm; @@ -104,27 +104,15 @@ public class Main extends HttpServlet implements Stream.StreamListener { } public void setupXmppComponent(final String host, final int port, final String jid, final String password) { - ExecutorService executorService = Executors.newSingleThreadExecutor(); - executorService.submit(() -> { - try { - Socket socket = new Socket(host, port); - xmpp = new StreamComponent(new JID(jid), socket.getInputStream(), socket.getOutputStream(), password); - xmpp.addListener(Main.this); - xmpp.startParsing(); - } catch (IOException e) { - log("XMPP exception", e); - } - }); - } - - @Override - public void onStreamFail(Exception e) { - log("XMPP failed", e); - } - - @Override - public void onStreamReady() { - log("XMPP STREAM READY"); + XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() + .extensions(Extension.of(com.juick.Message.class)) + .build(); + xmpp = ExternalComponent.create(jid, password, configuration, host, port); + try { + xmpp.connect(); + } catch (XmppException e) { + log("xmpp extension", e); + } } /** @@ -246,7 +234,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { } } - public void doPostMessage(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, Stream xmpp, int vuid) + public void doPostMessage(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, XmppSession xmpp, int vuid) throws ServletException, IOException { String body = request.getParameter("body"); if (body == null || body.length() < 1 || body.length() > 4096) { @@ -298,18 +286,15 @@ public class Main extends HttpServlet implements Stream.StreamListener { String attachmentType = attachmentFName != null ? attachmentFName.substring(attachmentFName.length() - 3) : null; int mid = MessagesQueries.createMessage(sql, vuid, body, attachmentType, tags); SubscriptionsQueries.subscribeMessage(sql, mid, vuid); - JuickMessage jmsg = new JuickMessage(MessagesQueries.getMessage(sql, mid)); + com.juick.Message jmsg = MessagesQueries.getMessage(sql, mid); if (xmpp != null) { 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); - xmsg.addChild(jmsg); - - Nickname nick = new Nickname(); - nick.Nickname = "@" + jmsg.getUser().getName(); - xmsg.addChild(nick); + xmsg.addExtension(jmsg); + xmsg.addExtension(new Nickname("@" + jmsg.getUser().getName())); if (attachmentFName != null) { String fname = mid + "." + attachmentType; @@ -325,34 +310,35 @@ public class Main extends HttpServlet implements Stream.StreamListener { .toFile(Paths.get(imgDir, "ps", fname).toFile()); body = attachmentURL + "\n" + body; - XOOB xoob = new XOOB(); - xoob.URL = attachmentURL; - xmsg.addChild(xoob); + try { + OobX xoob = new OobX(new URI(attachmentURL)); + xmsg.addExtension(xoob); + } catch (URISyntaxException e) { + log("invalid uri: " + attachmentURL, e); + } } String tagsStr2 = ""; 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 { log("XMPP unavailable"); @@ -361,7 +347,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { Main.replyJSON(request, response, serializer.serialize(jmsg).toString()); } - public void doPostComment(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, Stream xmpp, int vuid) + public void doPostComment(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, XmppSession xmpp, int vuid) throws ServletException, IOException { int mid = NumberUtils.toInt(request.getParameter("mid"), 0); if (mid == 0) { @@ -421,23 +407,21 @@ public class Main extends HttpServlet implements Stream.StreamListener { int ridnew = MessagesQueries.createReply(sql, mid, rid, vuid, body, attachmentType); SubscriptionsQueries.subscribeMessage(sql, mid, vuid); - JuickMessage jmsg = new JuickMessage(MessagesQueries.getReply(sql, mid, ridnew)); + com.juick.Message jmsg = MessagesQueries.getReply(sql, mid, ridnew); if (xmpp != null) { Message xmsg = new Message(); - xmsg.from = new JID("juick", "juick.com", null); - xmsg.type = Message.Type.chat; - xmsg.thread = "juick-" + mid; - xmsg.addChild(jmsg); + xmsg.setFrom(Jid.of("juick@juick.com")); + xmsg.setType(Message.Type.CHAT); + xmsg.setThread("juick-" + mid); + 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; @@ -453,20 +437,23 @@ public class Main extends HttpServlet implements Stream.StreamListener { .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) { + log("invalid uri: " + attachmentURL, e); + } } - 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 { log("XMPP unavailable"); diff --git a/juick-api/src/main/java/com/juick/api/Messages.java b/juick-api/src/main/java/com/juick/api/Messages.java index de845d45..93abb06b 100644 --- a/juick-api/src/main/java/com/juick/api/Messages.java +++ b/juick-api/src/main/java/com/juick/api/Messages.java @@ -7,15 +7,14 @@ import com.juick.json.MessageSerializer; import com.juick.server.MessagesQueries; import com.juick.server.TagQueries; import com.juick.server.UserQueries; -import com.juick.xmpp.JID; -import com.juick.xmpp.Message; -import com.juick.xmpp.Stream; -import com.juick.xmpp.extensions.JuickMessage; import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.util.StringUtils; +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; @@ -140,7 +139,7 @@ public class Messages { } public void doSetPrivacy(HttpServletRequest request, - HttpServletResponse response, Stream xmpp, int vuid) + HttpServletResponse response, XmppSession xmpp, int vuid) throws ServletException, IOException { int mid = NumberUtils.toInt(request.getParameter("mid"), 0); com.juick.User user = MessagesQueries.getMessageAuthor(sql, mid); @@ -152,7 +151,7 @@ public class Messages { } public void doSetPopular(HttpServletRequest request, - HttpServletResponse response, Stream xmpp) + HttpServletResponse response, XmppSession xmpp) throws ServletException, IOException { int mid = NumberUtils.toInt(request.getParameter("mid"), 0); int popular = NumberUtils.toInt(request.getParameter("popular"), 0); @@ -165,17 +164,16 @@ public class Messages { com.juick.Message m = MessagesQueries.getMessage(sql, mid); if (m != null) { Message msg = new Message(); - msg.from = new JID("juick", "juick.com", null); - msg.to = new JID(null, "crosspost.juick.com", null); - JuickMessage jmsg = new JuickMessage(m); - jmsg.setUser(UserQueries.getUserByUID(sql, 11574).get()); - msg.childs.add(jmsg); + msg.setFrom(Jid.of("juick@juick.com")); + msg.setTo(Jid.of("crosspost.juick.com")); + m.setUser(UserQueries.getUserByUID(sql, 11574).get()); + msg.addExtension(m); - msg.to.Username = "twitter"; + msg.setTo(Jid.of("twitter@crosspost.juick.com")); xmpp.send(msg); - msg.to.Username = "fb"; + msg.setTo(Jid.of("fb@crosspost.juick.com")); xmpp.send(msg); - msg.to.Username = "vk"; + msg.setTo(Jid.of("vk@crosspost.juick.com")); xmpp.send(msg); } else { throw new Exception("Message not found"); diff --git a/juick-api/src/main/java/com/juick/api/PM.java b/juick-api/src/main/java/com/juick/api/PM.java index d8f17366..86d9c534 100644 --- a/juick-api/src/main/java/com/juick/api/PM.java +++ b/juick-api/src/main/java/com/juick/api/PM.java @@ -4,11 +4,10 @@ import com.juick.json.MessageSerializer; import com.juick.server.PMQueries; 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 org.springframework.jdbc.core.JdbcTemplate; +import rocks.xmpp.addr.Jid; +import rocks.xmpp.core.session.XmppSession; +import rocks.xmpp.core.stanza.model.Message; import java.io.IOException; import java.util.List; @@ -53,8 +52,8 @@ public class PM { } } - public void doPostPM(HttpServletRequest request, - HttpServletResponse response, Stream xmpp, int vuid) + public void doPostPM(HttpServletRequest request, + HttpServletResponse response, XmppSession xmpp, int vuid) throws ServletException, IOException { String uname = request.getParameter("uname"); int uid = 0; @@ -75,15 +74,15 @@ public class PM { if (PMQueries.createPM(sql, vuid, uid, body)) { 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(UserQueries.getUserByUID(sql, vuid).get()); 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); Main.replyJSON(request, response, messageSerializer.serialize(jmsg).toString()); @@ -91,14 +90,14 @@ public class PM { List<String> 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, vuid, 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); } |