From 11c210116aaa3f92cc30cb577b289c4a36ece4c7 Mon Sep 17 00:00:00 2001
From: Vitaly Takmazov
Date: Mon, 9 Apr 2018 16:20:57 +0300
Subject: server: fix xmpp tests
---
.../main/java/com/juick/server/XMPPConnection.java | 188 ++++++++++-----------
.../java/com/juick/server/tests/ServerTests.java | 8 +-
2 files changed, 95 insertions(+), 101 deletions(-)
diff --git a/juick-server-xmpp/src/main/java/com/juick/server/XMPPConnection.java b/juick-server-xmpp/src/main/java/com/juick/server/XMPPConnection.java
index ab0271b0..fd451aeb 100644
--- a/juick-server-xmpp/src/main/java/com/juick/server/XMPPConnection.java
+++ b/juick-server-xmpp/src/main/java/com/juick/server/XMPPConnection.java
@@ -172,65 +172,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
}
});
router.addInboundMessageListener(e -> {
- Message message = e.getMessage();
- if (message.getType() != null && message.getType().equals(Message.Type.ERROR)) {
- StanzaError error = message.getError();
- if (error != null && error.getCondition().equals(Condition.RESOURCE_CONSTRAINT)) {
- // offline query is full, deactivating this jid
- if (userService.setActiveStatusForJID(message.getFrom().toEscapedString(), UserService.ActiveStatus.Inactive)) {
- logger.info("{} is inactive now", message.getFrom());
- return;
- }
- }
- return;
- }
- Jid jid = message.getTo();
- if (jid.getDomain().equals(router.getDomain().toEscapedString()) || jid.equals(this.jid)) {
- com.juick.Message jmsg = message.getExtension(com.juick.Message.class);
- if (jmsg != null) {
- if (jid.getLocal().equals("pm")) {
- applicationEventPublisher.publishEvent(new MessageEvent(this, jmsg));
- } else {
- if (MessageUtils.isReply(jmsg)) {
- // to get quote and attachment
- com.juick.Message reply = messagesService.getReply(jmsg.getMid(), jmsg.getRid());
- applicationEventPublisher.publishEvent(new MessageEvent(this, reply));
- } else if (!MessageUtils.isPM(jmsg)) {
- applicationEventPublisher.publishEvent(new MessageEvent(this,
- messagesService.getMessage(jmsg.getMid())));
- }
- }
- } else {
- URI attachment = URI.create(StringUtils.EMPTY);
- OobX oobX = message.getExtension(OobX.class);
- if (oobX != null) {
- attachment = oobX.getUri();
- }
- try {
- Jid from = message.getFrom();
- if (from.getDomain().equals("uid.juick.com")) {
- User fromUser = userService.getUserByUID(NumberUtils.toInt(from.getLocal(),
- 0)).orElse(new User());
- incomingMessageJuick(fromUser, from, message.getBody(), attachment);
- } else {
- incomingMessage(message);
- }
- } catch (Exception e1) {
- logger.warn("message exception", e1);
- }
- }
- } else if (jid.getDomain().endsWith(jid.getDomain()) && (jid.getDomain().equals(this.jid.getDomain())
- || jid.getDomain().endsWith("." + jid.getDomain()))) {
- if (logger.isInfoEnabled()) {
- try {
- logger.info("unhandled message: {}", stanzaToString(message));
- } catch (JAXBException | XMLStreamException ex) {
- logger.error("JAXB exception", ex);
- }
- }
- } else {
- router.send(ClientMessage.from(message));
- }
+ incomingMessage(e.getMessage());
});
router.addInboundIQListener(e -> {
IQ iq = e.getIQ();
@@ -575,6 +517,17 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
}
public void incomingMessage(Message msg) {
+ if (msg.getType() != null && msg.getType().equals(Message.Type.ERROR)) {
+ StanzaError error = msg.getError();
+ if (error != null && error.getCondition().equals(Condition.RESOURCE_CONSTRAINT)) {
+ // offline query is full, deactivating this jid
+ if (userService.setActiveStatusForJID(msg.getFrom().toEscapedString(), UserService.ActiveStatus.Inactive)) {
+ logger.info("{} is inactive now", msg.getFrom());
+ return;
+ }
+ }
+ return;
+ }
User user_from;
user_from = userService.getUserByJID(msg.getFrom().asBareJid().toEscapedString());
if (user_from == null) {
@@ -582,47 +535,88 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
sendReply(msg.getFrom(), "Для того, чтобы начать пользоваться сервисом, пожалуйста пройдите быструю регистрацию: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nЕсли у вас уже есть учетная запись на Juick, вы сможете присоединить этот JabberID к ней.\n\nTo start using Juick, please sign up: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nIf you already have an account on Juick, you will be proposed to attach this JabberID to your existing account.");
return;
}
- URI attachmentURI = URI.create(StringUtils.EMPTY);
- OobX oobX = msg.getExtension(OobX.class);
- if (oobX != null) {
- attachmentURI = oobX.getUri();
- }
- if (msg.getTo().equals(jid)) {
- incomingMessageJuick(user_from, msg.getFrom(), StringUtils.defaultString(msg.getBody()), attachmentURI);
- } else {
- int uid_to = userService.getUIDbyName(msg.getTo().getLocal());
-
- if (uid_to == 0) {
- Message reply = new Message();
- reply.setFrom(msg.getTo());
- reply.setTo(msg.getFrom());
- reply.setType(Message.Type.ERROR);
- reply.setId(msg.getId());
- reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.ITEM_NOT_FOUND));
- router.send(ClientMessage.from(reply));
- return;
- }
+ Jid jid = msg.getTo();
+ if (jid.getDomain().equals(router.getDomain().toEscapedString()) || jid.equals(this.jid)) {
+ com.juick.Message jmsg = msg.getExtension(com.juick.Message.class);
+ if (jmsg != null) {
+ if (jid.getLocal().equals("pm")) {
+ applicationEventPublisher.publishEvent(new MessageEvent(this, jmsg));
+ } else {
+ if (MessageUtils.isReply(jmsg)) {
+ // to get quote and attachment
+ com.juick.Message reply = messagesService.getReply(jmsg.getMid(), jmsg.getRid());
+ applicationEventPublisher.publishEvent(new MessageEvent(this, reply));
+ } else if (!MessageUtils.isPM(jmsg)) {
+ applicationEventPublisher.publishEvent(new MessageEvent(this,
+ messagesService.getMessage(jmsg.getMid())));
+ }
+ }
+ } else {
+ URI attachment = URI.create(StringUtils.EMPTY);
+ OobX oobX = msg.getExtension(OobX.class);
+ if (oobX != null) {
+ attachment = oobX.getUri();
+ }
+ try {
+ Jid from = msg.getFrom();
+ if (from.getDomain().equals("uid.juick.com")) {
+ User fromUser = userService.getUserByUID(NumberUtils.toInt(from.getLocal(),
+ 0)).orElse(new User());
+ incomingMessageJuick(fromUser, from, msg.getBody(), attachment);
+ } else {
+ if (msg.getTo().equals(jid)) {
+ incomingMessageJuick(user_from, msg.getFrom(), StringUtils.defaultString(msg.getBody()), attachment);
+ } else {
+ int uid_to = userService.getUIDbyName(msg.getTo().getLocal());
+
+ if (uid_to == 0) {
+ Message reply = new Message();
+ reply.setFrom(msg.getTo());
+ reply.setTo(msg.getFrom());
+ reply.setType(Message.Type.ERROR);
+ reply.setId(msg.getId());
+ reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.ITEM_NOT_FOUND));
+ router.send(ClientMessage.from(reply));
+ return;
+ }
- boolean success = false;
- if (!userService.isInBLAny(uid_to, user_from.getUid())) {
- success = pmQueriesService.createPM(user_from.getUid(), uid_to, msg.getBody());
- }
+ boolean success = false;
+ if (!userService.isInBLAny(uid_to, user_from.getUid())) {
+ success = pmQueriesService.createPM(user_from.getUid(), uid_to, msg.getBody());
+ }
- if (success) {
- com.juick.Message jmsg = new com.juick.Message();
- jmsg.setUser(user_from);
- jmsg.setTo(userService.getUserByUID(uid_to).get());
- jmsg.setText(msg.getBody());
- applicationEventPublisher.publishEvent(new MessageEvent(this, jmsg));
- } else {
- Message reply = new Message();
- reply.setFrom(msg.getTo());
- reply.setTo(msg.getFrom());
- reply.setType(Message.Type.ERROR);
- reply.setId(msg.getId());
- reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.NOT_ALLOWED));
- router.send(ClientMessage.from(reply));
+ if (success) {
+ com.juick.Message pm = new com.juick.Message();
+ pm.setUser(user_from);
+ pm.setTo(userService.getUserByUID(uid_to).get());
+ pm.setText(msg.getBody());
+ applicationEventPublisher.publishEvent(new MessageEvent(this, pm));
+ } else {
+ Message reply = new Message();
+ reply.setFrom(msg.getTo());
+ reply.setTo(msg.getFrom());
+ reply.setType(Message.Type.ERROR);
+ reply.setId(msg.getId());
+ reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.NOT_ALLOWED));
+ router.send(ClientMessage.from(reply));
+ }
+ }
+ }
+ } catch (Exception e1) {
+ logger.warn("message exception", e1);
+ }
+ }
+ } else if (jid.getDomain().endsWith(jid.getDomain()) && (jid.getDomain().equals(this.jid.getDomain())
+ || jid.getDomain().endsWith("." + jid.getDomain()))) {
+ if (logger.isInfoEnabled()) {
+ try {
+ logger.info("unhandled message: {}", stanzaToString(msg));
+ } catch (JAXBException | XMLStreamException ex) {
+ logger.error("JAXB exception", ex);
+ }
}
+ } else {
+ router.send(ClientMessage.from(msg));
}
}
private void incomingMessageJuick(User user_from, Jid from, String command, @Nonnull URI attachment) {
diff --git a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
index 1ba65721..d30b1303 100644
--- a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
+++ b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
@@ -125,6 +125,8 @@ public class ServerTests {
private ImagesService imagesService;
@Value("${hostname:localhost}")
private Jid jid;
+ @Value("${xmppbot_jid:juick@localhost}")
+ private Jid botJid;
@Value("${upload_tmp_dir:#{systemEnvironment['TEMP'] ?: '/tmp'}}")
private String tmpDir;
@Value("${img_path:#{systemEnvironment['TEMP'] ?: '/tmp'}}")
@@ -531,12 +533,10 @@ public class ServerTests {
renhaId = userService.createUser("renha", "umnnbt");
Jid from = Jid.of("renha@serverstorageisfull.tld");
jdbcTemplate.update("INSERT INTO jids(user_id,jid,active) VALUES(?,?,?)", renhaId, from.toEscapedString(), 1);
- String xmlMessage = "Reply by @LexXПохоже нынче можно публично заявлять о своем веганстве. Your contact offline message queue is full. The message has been discarded.";
- Stanza msg = server.parse(xmlMessage);
- assertThat(from, equalTo(msg.getFrom()));
+ String xmlMessage = "Reply by @LexXПохоже нынче можно публично заявлять о своем веганстве. Your contact offline message queue is full. The message has been discarded.";
Function isActive = f -> jdbcTemplate.queryForObject("SELECT active FROM jids WHERE user_id=?", Integer.class, f) == 1;
assertThat(isActive.apply(renhaId), equalTo(true));
- router.incomingMessage((ServerMessage)msg);
+ server.onStanzaReceived(xmlMessage);
assertThat(isActive.apply(renhaId), equalTo(false));
commandsManager.processCommand(userService.getUserByJID(from.asBareJid().toEscapedString()), "On", URI.create(StringUtils.EMPTY));
assertTrue(isActive.apply(renhaId));
--
cgit v1.2.3