aboutsummaryrefslogtreecommitdiff
path: root/juick-api/src/main/java/com/juick/api/Main.java
diff options
context:
space:
mode:
Diffstat (limited to 'juick-api/src/main/java/com/juick/api/Main.java')
-rw-r--r--juick-api/src/main/java/com/juick/api/Main.java131
1 files changed, 59 insertions, 72 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");