diff options
author | Vitaly Takmazov | 2018-04-03 17:55:46 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-04-03 17:55:54 +0300 |
commit | a5f045a3780ef8530afa7493cf090771b8fdcfe1 (patch) | |
tree | 56e4cbc6949dc9bd39672ccf379324338f0eef76 | |
parent | c53675e5eb19a1a05ae5edcd0322075d51628b96 (diff) |
refactoring: all messages are processed with XMPPConnection
11 files changed, 82 insertions, 149 deletions
diff --git a/juick-common/src/main/java/com/juick/server/util/HttpUtils.java b/juick-common/src/main/java/com/juick/server/util/HttpUtils.java index 35f594f3..f86e52c5 100644 --- a/juick-common/src/main/java/com/juick/server/util/HttpUtils.java +++ b/juick-common/src/main/java/com/juick/server/util/HttpUtils.java @@ -24,6 +24,7 @@ import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.io.InputStream; +import java.net.URI; import java.net.URL; import java.net.URLConnection; import java.nio.file.Files; @@ -37,7 +38,7 @@ import java.util.UUID; public class HttpUtils { private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class); - public static String receiveMultiPartFile(MultipartFile attach, String tmpDir) { + public static URI receiveMultiPartFile(MultipartFile attach, String tmpDir) { if (attach !=null && !attach.isEmpty()) { String partname = attach.getOriginalFilename(); if (partname != null && partname.length() > 0) { @@ -50,16 +51,16 @@ public class HttpUtils { try { Files.write(Paths.get(tmpDir, attachmentFName), attach.getBytes()); - return attachmentFName; + return URI.create(String.format("juick://%s", attachmentFName)); } catch (IOException e) { logger.warn("file receive error", e); } } } } - return StringUtils.EMPTY; + return URI.create(""); } - public static String downloadImage(URL url, String tmpDir) throws Exception { + public static URI downloadImage(URL url, String tmpDir) throws Exception { URLConnection urlConn; try { urlConn = url.openConnection(); @@ -86,7 +87,7 @@ public class HttpUtils { String attachmentFName = DigestUtils.md5Hex(UUID.randomUUID().toString()) + "." + attachmentType; Files.copy(is, Paths.get(tmpDir, attachmentFName)); - return attachmentFName; + return URI.create(String.format("juick://%s", attachmentFName)); } catch (Exception e) { logger.error(String.format("Failed download image by url: %s", url.toString()), e); throw e; diff --git a/juick-server-xmpp/src/main/java/com/juick/server/CommandsManager.java b/juick-server-xmpp/src/main/java/com/juick/server/CommandsManager.java index 6458382f..26dafa69 100644 --- a/juick-server-xmpp/src/main/java/com/juick/server/CommandsManager.java +++ b/juick-server-xmpp/src/main/java/com/juick/server/CommandsManager.java @@ -44,9 +44,7 @@ import javax.annotation.PostConstruct; import javax.inject.Inject; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.net.MalformedURLException; import java.net.URI; -import java.nio.file.Paths; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -470,7 +468,7 @@ public class CommandsManager { int newrid = messagesService.createReply(mid, rid, user.getUid(), txt, attachmentType); if (StringUtils.isNotEmpty(attachmentType)) { String attachmentFName = attachment.getScheme().equals("juick") ? attachment.getHost() - : HttpUtils.downloadImage(attachment.toURL(), tmpDir); + : HttpUtils.downloadImage(attachment.toURL(), tmpDir).getHost(); String fname = String.format("%d-%d.%s", mid, newrid, attachmentType); ImageUtils.saveImageWithPreviews(attachmentFName, fname, tmpDir, imgDir); } 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 04c3e2ab..40209fad 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 @@ -57,6 +57,7 @@ import rocks.xmpp.extensions.filetransfer.FileTransferManager; import rocks.xmpp.extensions.nick.model.Nickname; import rocks.xmpp.extensions.oob.model.x.OobX; import rocks.xmpp.extensions.ping.PingManager; +import rocks.xmpp.extensions.receipts.MessageDeliveryReceiptsManager; import rocks.xmpp.extensions.vcard.temp.model.VCard; import rocks.xmpp.extensions.version.SoftwareVersionManager; import rocks.xmpp.extensions.version.model.SoftwareVersion; @@ -178,7 +179,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { router.addInboundMessageListener(e -> { Message message = e.getMessage(); Jid jid = message.getTo(); - if (jid.getDomain().equals(router.getDomain().toEscapedString()) || jid.equals(jid)) { + 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("recomm")) { @@ -199,13 +200,21 @@ public class XMPPConnection implements StanzaListener, NotificationListener { } } } else { - String attachment = StringUtils.EMPTY; + URI attachment = URI.create(""); OobX oobX = message.getExtension(OobX.class); if (oobX != null) { - attachment = oobX.getUri().toString(); + attachment = oobX.getUri(); } try { - processMessage(userService.getUserByUID(NumberUtils.toInt(message.getFrom().getLocal(), 0)).orElse(new User()), message.getBody(), attachment); + 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 { + incomingMessageJuick(userService.getUserByJID(from.asBareJid().toEscapedString()), from, + message.getBody(), attachment); + } } catch (Exception e1) { logger.warn("message exception", e1); } @@ -220,14 +229,14 @@ public class XMPPConnection implements StanzaListener, NotificationListener { } } } else { - s2s(ClientMessage.from(message)); + router.send(ClientMessage.from(message)); } }); router.addInboundIQListener(e -> { IQ iq = e.getIQ(); Jid jid = iq.getTo(); if (!jid.getDomain().equals(this.jid.getDomain())) { - s2s(iq); + router.send(iq); } }); FileTransferManager fileTransferManager = router.getManager(FileTransferManager.class); @@ -249,7 +258,8 @@ public class XMPPConnection implements StanzaListener, NotificationListener { if (st.getStatus().equals(FileTransfer.Status.COMPLETED)) { logger.info("transfer completed"); try { - processMessage(userService.getUserByJID(e.getInitiator().toEscapedString()), e.getDescription(), targetFilename); + Jid initiator = e.getInitiator(); + incomingMessageJuick(userService.getUserByJID(initiator.toEscapedString()), initiator, e.getDescription(), URI.create(String.format("juick://%s", targetFilename))); } catch (Exception e1) { logger.error("ft error", e1); } @@ -301,19 +311,6 @@ public class XMPPConnection implements StanzaListener, NotificationListener { return stanzaWriter.toString(); } - private void s2s(Stanza stanza) { - try { - String xml = stanzaToString(stanza); - logger.info("stream router (out): {}", xml); - xmpp.sendOut(stanza); - } catch (XMLStreamException | JAXBException e) { - logger.error("JAXB exception", e); - } - - } - - - private void sendJuickMessage(com.juick.Message jmsg) { List<String> jids = new ArrayList<>(); @@ -353,7 +350,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { } for (String jid : jids) { msg.setTo(Jid.of(jid)); - s2s(ClientMessage.from(msg)); + router.send(ClientMessage.from(msg)); } } @@ -385,7 +382,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { for (User user : users) { for (String jid : userService.getJIDsbyUID(user.getUid())) { msg.setTo(Jid.of(jid)); - s2s(ClientMessage.from(msg)); + router.send(ClientMessage.from(msg)); } } } @@ -409,7 +406,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { mm.setFrom(jid); mm.setBody("Private message from @" + msg.getUser().getName() + ":\n" + msg.getText()); } - s2s(ClientMessage.from(mm)); + router.send(ClientMessage.from(mm)); }); } else if (isReply) { sendJuickComment(msg); @@ -427,7 +424,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { reply.setTo(jidTo); reply.setType(Message.Type.CHAT); reply.setBody(txt); - s2s(ClientMessage.from(reply)); + router.send(ClientMessage.from(reply)); } @Override @@ -481,7 +478,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { for (User user : users) { for (String jid : userService.getJIDsbyUID(user.getUid())) { msg.setTo(Jid.of(jid)); - s2s(ClientMessage.from(msg)); + router.send(ClientMessage.from(msg)); } } } @@ -493,7 +490,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { Presence p = new Presence(Jid.of(userJid)); p.setFrom(jid); p.setPriority((byte) 10); - s2s(ClientPresence.from(p)); + router.send(ClientPresence.from(p)); }); } @@ -506,7 +503,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { reply.setFrom(p.getTo().asBareJid()); reply.setTo(p.getFrom().asBareJid()); reply.setType(Presence.Type.UNSUBSCRIBE); - s2s(ClientPresence.from(reply)); + router.send(ClientPresence.from(reply)); } else if (p.getType().equals(Presence.Type.PROBE)) { int uid_to = 0; if (!toJuick) { @@ -521,7 +518,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { if (!userService.getActiveJIDs().contains(p.getFrom().asBareJid().toEscapedString())) { reply.setStatus("Send ON to enable notifications"); } - s2s(ClientPresence.from(reply)); + router.send(ClientPresence.from(reply)); } else { Presence reply = new Presence(); reply.setFrom(p.getTo()); @@ -529,7 +526,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { reply.setType(Presence.Type.ERROR); reply.setId(p.getId()); reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.ITEM_NOT_FOUND)); - s2s(ClientPresence.from(reply)); + router.send(ClientPresence.from(reply)); } } else if (p.getType().equals(Presence.Type.SUBSCRIBE)) { boolean canSubscribe = false; @@ -547,12 +544,12 @@ public class XMPPConnection implements StanzaListener, NotificationListener { reply.setFrom(p.getTo()); reply.setTo(p.getFrom()); reply.setType(Presence.Type.SUBSCRIBED); - s2s(ClientPresence.from(reply)); + router.send(ClientPresence.from(reply)); reply.setFrom(reply.getFrom().withResource(jid.getResource())); reply.setPriority((byte) 10); reply.setType(null); - s2s(ClientPresence.from(reply)); + router.send(ClientPresence.from(reply)); } else { Presence reply = new Presence(); reply.setFrom(p.getTo()); @@ -560,7 +557,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { reply.setType(Presence.Type.ERROR); reply.setId(p.getId()); reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.ITEM_NOT_FOUND)); - s2s(ClientPresence.from(reply)); + router.send(ClientPresence.from(reply)); } } else if (p.getType().equals(Presence.Type.UNSUBSCRIBE)) { if (!toJuick) { @@ -574,11 +571,11 @@ public class XMPPConnection implements StanzaListener, NotificationListener { reply.setFrom(p.getTo()); reply.setTo(p.getFrom()); reply.setType(Presence.Type.UNSUBSCRIBED); - s2s(ClientPresence.from(reply)); + router.send(ClientPresence.from(reply)); } } - public void incomingMessage(Message msg) { + public void incomingMessage(Message msg) throws Exception { if (msg.getType() != null && msg.getType().equals(Message.Type.ERROR)) { StanzaError error = msg.getError(); if (error != null && error.getCondition().equals(Condition.RESOURCE_CONSTRAINT)) { @@ -596,73 +593,21 @@ public class XMPPConnection implements StanzaListener, NotificationListener { String username = msg.getTo().getLocal(); User user_from; - String signuphash = StringUtils.EMPTY; user_from = userService.getUserByJID(msg.getFrom().asBareJid().toEscapedString()); - if (user_from == null) { - signuphash = userService.getSignUpHashByJID(msg.getFrom().asBareJid().toEscapedString()); + URI attachmentURI = URI.create(""); + OobX oobX = msg.getExtension(OobX.class); + if (oobX != null) { + attachmentURI = oobX.getUri(); } - + incomingMessageJuick(user_from, msg.getFrom(), msg.getBody(), attachmentURI); + } + public com.juick.Message incomingMessageJuick(User user_from, Jid from, String command, URI attachment) throws Exception { if (user_from == null) { - Message reply = new Message(); - reply.setFrom(msg.getTo()); - reply.setTo(msg.getFrom()); - reply.setType(Message.Type.CHAT); - reply.setBody("Для того, чтобы начать пользоваться сервисом, пожалуйста пройдите быструю регистрацию: 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."); - s2s(ClientMessage.from(reply)); - return; + String signuphash = userService.getSignUpHashByJID(from.asBareJid().toEscapedString()); + sendReply(from, "Для того, чтобы начать пользоваться сервисом, пожалуйста пройдите быструю регистрацию: 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 null; } - if (username.equals(jid.getLocal())) { - try { - OobX oobX = msg.getExtension(OobX.class); - if (oobX != null) { - incomingMessageJuick(user_from, msg.getFrom(), msg.getBody().trim(), oobX.getUri()); - } else { - incomingMessageJuick(user_from, msg.getFrom(), msg.getBody().trim(), null); - } - } catch (Exception e) { - logger.info("xmpp exception", e); - } - return; - } - - int uid_to = userService.getUIDbyName(username); - - 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)); - s2s(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()); - } - - 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)); - s2s(ClientMessage.from(reply)); - } - - return; - } - public com.juick.Message incomingMessageJuick(User user_from, Jid from, String command, URI attachment) throws Exception { int commandlen = command.length(); // COMPATIBILITY @@ -683,7 +628,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { subscriptionService.subscribeMessage(mid, user_from.getUid()); if (StringUtils.isNotEmpty(attachmentType)) { String attachmentFName = attachment.getScheme().equals("juick") ? attachment.getHost() - : HttpUtils.downloadImage(attachment.toURL(), tmpDir); + : HttpUtils.downloadImage(attachment.toURL(), tmpDir).getHost(); String fname = String.format("%d.%s", mid, attachmentType); ImageUtils.saveImageWithPreviews(attachmentFName, fname, tmpDir, imgDir); } @@ -692,15 +637,6 @@ public class XMPPConnection implements StanzaListener, NotificationListener { return msg; } } - public com.juick.Message processMessage(User visitor, String body, String attachmentName) throws Exception { - if (StringUtils.isNotEmpty(attachmentName)) { - URI httpUri = URI.create(attachmentName); - if (!httpUri.isAbsolute()) { - attachmentName = String.format("juick://%s", attachmentName); - } - } - return incomingMessageJuick(visitor, Jid.of(String.valueOf(visitor.getUid()), "uid.juick.com", "perl"), body, URI.create(attachmentName)); - } @Override public void stanzaReceived(Stanza xmlValue) { @@ -711,7 +647,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { } } else if (xmlValue instanceof Message) { Message msg = (Message) xmlValue; - incomingMessage(msg); + router.send(msg); } else if (xmlValue instanceof IQ) { IQ iq = (IQ) xmlValue; router.send(iq); @@ -727,7 +663,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { userService.getActiveJIDs().forEach(j -> { try { presence.setTo(Jid.of(j)); - s2s(ClientPresence.from(presence)); + router.send(ClientPresence.from(presence)); } catch (IllegalArgumentException ex) { logger.warn("Invalid jid: {}", j, ex); } diff --git a/juick-server-xmpp/src/main/java/com/juick/server/xmpp/router/XMPPRouter.java b/juick-server-xmpp/src/main/java/com/juick/server/xmpp/router/XMPPRouter.java index e70eb168..6edecf05 100644 --- a/juick-server-xmpp/src/main/java/com/juick/server/xmpp/router/XMPPRouter.java +++ b/juick-server-xmpp/src/main/java/com/juick/server/xmpp/router/XMPPRouter.java @@ -10,9 +10,11 @@ import org.xmlpull.v1.XmlPullParserException; import rocks.xmpp.addr.Jid; import rocks.xmpp.core.stanza.model.IQ; import rocks.xmpp.core.stanza.model.Message; +import rocks.xmpp.core.stanza.model.Presence; import rocks.xmpp.core.stanza.model.Stanza; import rocks.xmpp.core.stanza.model.server.ServerIQ; import rocks.xmpp.core.stanza.model.server.ServerMessage; +import rocks.xmpp.core.stanza.model.server.ServerPresence; import rocks.xmpp.util.XmppUtils; import javax.annotation.PostConstruct; @@ -166,7 +168,7 @@ public class XMPPRouter implements StreamHandler { } else if (input instanceof IQ) { sendOut(ServerIQ.from((IQ)input)); } else { - logger.warn("unhandled stanza: {}", stanza); + sendOut(ServerPresence.from((Presence) input)); } } diff --git a/juick-server/src/main/java/com/juick/server/MessengerManager.java b/juick-server/src/main/java/com/juick/server/MessengerManager.java index ca206bd4..55330c3d 100644 --- a/juick-server/src/main/java/com/juick/server/MessengerManager.java +++ b/juick-server/src/main/java/com/juick/server/MessengerManager.java @@ -97,7 +97,7 @@ public class MessengerManager implements ApplicationListener<MessageEvent> { logger.info("Received text message from '{}' at '{}' with content: {} (mid: {})", senderId, timestamp, text, messageId); try { - xmppConnection.processMessage(user_from, text, null); + xmppConnection.incomingMessageJuick(user_from, null, text, null); } catch (Exception e) { logger.warn("messenger error", e); } diff --git a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java index 020d2433..320ab9ae 100644 --- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java +++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java @@ -53,6 +53,7 @@ import org.springframework.web.util.UriComponentsBuilder; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.io.IOException; +import java.net.URI; import java.net.URL; import java.util.*; @@ -129,7 +130,7 @@ public class TelegramBotManager implements NotificationListener { if (user_from.getUid() == 0) { telegramSignupNotify(message.from().id().longValue(), userService.getSignUpHashByTelegramID(message.from().id().longValue(), username)); } else { - String attachment = StringUtils.EMPTY; + URI attachment = URI.create(StringUtils.EMPTY); if (message.photo() != null) { String fileId = Arrays.stream(message.photo()).max(Comparator.comparingInt(PhotoSize::fileSize)) .orElse(new PhotoSize()).fileId(); @@ -149,7 +150,7 @@ public class TelegramBotManager implements NotificationListener { if (StringUtils.isBlank(text)) { text = StringUtils.EMPTY; } - if (StringUtils.isNotEmpty(text) || StringUtils.isNotEmpty(attachment)) { + if (StringUtils.isNotEmpty(text) || StringUtils.isNotEmpty(attachment.toString())) { if (text.equalsIgnoreCase("LOGIN") || text.equalsIgnoreCase("PING") || text.equalsIgnoreCase("HELP") @@ -183,7 +184,7 @@ public class TelegramBotManager implements NotificationListener { int rid = Integer.valueOf(uriComponents.getFragment()); prefix = String.format("#%d/%d ", mid, rid); } - xmppConnection.processMessage(user_from, prefix + text, attachment); + xmppConnection.incomingMessageJuick(user_from, null,prefix + text, attachment); telegramNotify(message.from().id().longValue(), "Reply sent", StringUtils.EMPTY); } else { logger.warn("invalid path: {}", path); @@ -200,7 +201,7 @@ public class TelegramBotManager implements NotificationListener { "Can not reply to this message", replyMessage.messageId(), StringUtils.EMPTY); } } else { - xmppConnection.processMessage(user_from, text, attachment); + xmppConnection.incomingMessageJuick(user_from, null, text, attachment); telegramNotify(message.from().id().longValue(), "Message sent", StringUtils.EMPTY); } } diff --git a/juick-server/src/main/java/com/juick/server/WebsocketManager.java b/juick-server/src/main/java/com/juick/server/WebsocketManager.java index 498bf064..fc7035ab 100644 --- a/juick-server/src/main/java/com/juick/server/WebsocketManager.java +++ b/juick-server/src/main/java/com/juick/server/WebsocketManager.java @@ -151,7 +151,7 @@ public class WebsocketManager extends TextWebSocketHandler { Message draft = jsonMapper.readValue(message.asBytes(), Message.class); if (draft.getUser() != null && (draft.getText() != null || draft.getAttachment() != null)) { String attachmentFileName = draft.getAttachment() == null ? "" : draft.getAttachment().getUrl(); - xmppConnection.processMessage(draft.getUser(), draft.getText(), attachmentFileName); + xmppConnection.incomingMessageJuick(draft.getUser(), null, draft.getText(), URI.create(attachmentFileName)); } } catch (Exception e) { throw new HttpBadRequestException(); diff --git a/juick-server/src/main/java/com/juick/server/api/Post.java b/juick-server/src/main/java/com/juick/server/api/Post.java index 312df52d..7c8fb2ea 100644 --- a/juick-server/src/main/java/com/juick/server/api/Post.java +++ b/juick-server/src/main/java/com/juick/server/api/Post.java @@ -101,9 +101,9 @@ public class Post { } body = body.replace("\r", StringUtils.EMPTY); - String attachmentFName = HttpUtils.receiveMultiPartFile(attach, tmpDir); + URI attachmentFName = HttpUtils.receiveMultiPartFile(attach, tmpDir); - if (StringUtils.isBlank(attachmentFName) && img != null && img.length() > 10) { + if (StringUtils.isBlank(attachmentFName.toString()) && img != null && img.length() > 10) { try { URL imgUrl = new URL(img); attachmentFName = HttpUtils.downloadImage(imgUrl, tmpDir); @@ -112,11 +112,11 @@ public class Post { throw new HttpBadRequestException(); } } - xmppConnection.processMessage(visitor, body, attachmentFName); + xmppConnection.incomingMessageJuick(visitor, null, body, attachmentFName); } @PostMapping("/upload") public String doUploadFile(@RequestParam(required = true) MultipartFile attach) { - return HttpUtils.receiveMultiPartFile(attach, tmpDir); + return HttpUtils.receiveMultiPartFile(attach, tmpDir).toString(); } @RequestMapping(value = "/comment", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @@ -158,9 +158,9 @@ public class Post { throw new HttpForbiddenException(); } - String attachmentFName = HttpUtils.receiveMultiPartFile(attach, tmpDir); + URI attachmentFName = HttpUtils.receiveMultiPartFile(attach, tmpDir); - if (StringUtils.isBlank(attachmentFName) && img != null && img.length() > 10) { + if (StringUtils.isBlank(attachmentFName.toString()) && img != null && img.length() > 10) { try { attachmentFName = HttpUtils.downloadImage(new URL(img), tmpDir); } catch (Exception e) { @@ -169,7 +169,7 @@ public class Post { } } - return xmppConnection.processMessage(visitor, body, attachmentFName); + return xmppConnection.incomingMessageJuick(visitor, null, body, attachmentFName); } Session session = Session.getDefaultInstance(new Properties()); @@ -231,7 +231,7 @@ public class Post { body[0] = rid > 0 ? String.format("#%d/%d %s", mid, rid, body[0]) : String.format("#%d %s", mid, body[0]); } - xmppConnection.processMessage(visitor, body[0], attachmentFName[0]); + xmppConnection.incomingMessageJuick(visitor, null, body[0], URI.create(String.format("juick://%s", attachmentFName[0]))); } else { logger.info("not registered: {}", from); } 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 28f7c645..2930d61b 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 @@ -524,7 +524,7 @@ public class ServerTests { assertThat(server.getJid(), equalTo(jid)); } @Test - public void botIsUpAndProcessingResourceConstraints() { + public void botIsUpAndProcessingResourceConstraints() throws Exception { int renhaId; renhaId = userService.createUser("renha", "umnnbt"); Jid from = Jid.of("renha@serverstorageisfull.tld"); @@ -552,7 +552,7 @@ public class ServerTests { Tag yo = tagService.getTag("yo", true); Message msg = router.incomingMessageJuick(user, Jid.of("test@localhost"), "*yo yoyo", URI.create("http://static.juick.com/settings/facebook.png")); assertThat(msg.getAttachmentType(), is("png")); - Message msgreply = router.incomingMessageJuick(user, Jid.of("test@localhost"), "#" + msg.getMid() + " yyy", URI.create("juick://" + HttpUtils.downloadImage(URI.create("http://static.juick.com/settings/xmpp.png").toURL(), tmpDir))); + Message msgreply = router.incomingMessageJuick(user, Jid.of("test@localhost"), "#" + msg.getMid() + " yyy", HttpUtils.downloadImage(URI.create("http://static.juick.com/settings/xmpp.png").toURL(), tmpDir)); assertThat(msgreply.getAttachmentType(), equalTo("png")); int mid = messagesService.createMessage(uid, "yoyo", null, Collections.singletonList(yo)); assertEquals("should be message", true, diff --git a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java index ee4a6025..b93d6f57 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java +++ b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java @@ -132,9 +132,9 @@ public class NewMessage { throw new HttpForbiddenException(); } - String attachmentFName = HttpUtils.receiveMultiPartFile(attach, tmpDir); + URI attachmentFName = HttpUtils.receiveMultiPartFile(attach, tmpDir); - if (StringUtils.isBlank(attachmentFName) && img != null && img.length() > 10) { + if (StringUtils.isBlank(attachmentFName.toString()) && img != null && img.length() > 10) { try { URL imgUrl = new URL(img); attachmentFName = HttpUtils.downloadImage(imgUrl, tmpDir); @@ -144,7 +144,7 @@ public class NewMessage { } } - String attachmentType = StringUtils.isNotEmpty(attachmentFName) ? attachmentFName.substring(attachmentFName.length() - 3) : null; + String attachmentType = StringUtils.isNotEmpty(attachmentFName.toString()) ? attachmentFName.toString().substring(attachmentFName.toString().length() - 3) : null; int ridnew = messagesService.createReply(mid, rid, visitor.getUid(), body, attachmentType); subscriptionService.subscribeMessage(mid, visitor.getUid()); @@ -163,11 +163,11 @@ public class NewMessage { } xmsg.addExtension(new Nickname("@" + jmsg.getUser().getName())); - if (StringUtils.isNotEmpty(attachmentFName)) { + if (StringUtils.isNotEmpty(attachmentFName.toString())) { String fname = mid + "-" + ridnew + "." + attachmentType; String attachmentURL = "http://i.juick.com/photos-1024/" + fname; - ImageUtils.saveImageWithPreviews(attachmentFName, fname, tmpDir, imgDir); + ImageUtils.saveImageWithPreviews(attachmentFName.getHost(), fname, tmpDir, imgDir); body = attachmentURL + "\n" + body; try { @@ -246,9 +246,9 @@ public class NewMessage { } String body = StringUtils.isNotEmpty(bodyParam) ? bodyParam.replace("\r", StringUtils.EMPTY) : StringUtils.EMPTY; - String attachmentFName = HttpUtils.receiveMultiPartFile(attach, tmpDir); + URI attachmentFName = HttpUtils.receiveMultiPartFile(attach, tmpDir); - if (StringUtils.isBlank(attachmentFName) && StringUtils.isNotBlank(img)) { + if (StringUtils.isBlank(attachmentFName.toString()) && StringUtils.isNotBlank(img)) { try { URL imgUrl = new URL(img); attachmentFName = HttpUtils.downloadImage(imgUrl, tmpDir); @@ -262,15 +262,10 @@ public class NewMessage { msg.setFrom(Jid.of(String.valueOf(visitor.getUid()), "uid.juick.com", "perl")); msg.setTo(botJid); msg.setBody(body); - try { - if (StringUtils.isNotEmpty(attachmentFName)) { - String attachmentUrl = String.format("juick://%s", attachmentFName); - msg.addExtension(new OobX(new URI(attachmentUrl), "!!!!Juick!!")); - } - xmpp.sendMessage(msg); - } catch (URISyntaxException e1) { - logger.warn("attachment error", e1); + if (StringUtils.isNotEmpty(attachmentFName.toString())) { + msg.addExtension(new OobX(attachmentFName, "!!!!Juick!!")); } + xmpp.sendMessage(msg); if (StringUtils.isBlank(referer) || referer.substring(0, 21).equals("http://juick.com/post") || referer.substring(0, 22).equals("https://juick.com/post")) { return "redirect:/?show=my"; diff --git a/juick-www/src/main/java/com/juick/www/controllers/Settings.java b/juick-www/src/main/java/com/juick/www/controllers/Settings.java index ffc66841..15f18052 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/Settings.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Settings.java @@ -158,7 +158,7 @@ public class Settings { info.setCountry(request.getParameter("country")); info.setUrl(request.getParameter("url")); info.setDescription(request.getParameter("descr")); - String avatarTmpPath = HttpUtils.receiveMultiPartFile(avatar, tmpDir); + String avatarTmpPath = HttpUtils.receiveMultiPartFile(avatar, tmpDir).getHost(); if (StringUtils.isNotEmpty(avatarTmpPath)) { ImageUtils.saveAvatar(avatarTmpPath, visitor.getUid(), tmpDir, imgDir); } |