diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/com/juick/server/XMPPManager.java | 82 | ||||
-rw-r--r-- | src/main/java/com/juick/server/api/Messages.java | 7 |
2 files changed, 47 insertions, 42 deletions
diff --git a/src/main/java/com/juick/server/XMPPManager.java b/src/main/java/com/juick/server/XMPPManager.java index 51b3b04e..12021006 100644 --- a/src/main/java/com/juick/server/XMPPManager.java +++ b/src/main/java/com/juick/server/XMPPManager.java @@ -34,6 +34,7 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.Resource; import rocks.xmpp.addr.Jid; import rocks.xmpp.core.XmppException; import rocks.xmpp.core.session.Extension; @@ -87,7 +88,7 @@ public class XMPPManager implements NotificationListener { private static final Logger logger = LoggerFactory.getLogger("com.juick.server.xmpp"); - private ExternalComponent router; + private ExternalComponent xmpp; @Inject private CommandsManager commandsManager; @Value("${xmppbot_jid:juick@localhost}") @@ -102,6 +103,8 @@ public class XMPPManager implements NotificationListener { private String password; @Value("${upload_tmp_dir:#{systemEnvironment['TEMP'] ?: '/tmp'}}") private String tmpDir; + @Value("classpath:juick.png") + private Resource vCardImage; @Inject private MessagesService messagesService; @@ -120,22 +123,22 @@ public class XMPPManager implements NotificationListener { @PostConstruct public void init() { - logger.info("stream router start connecting to {}", componentPort); + logger.info("xmpp component start connecting to {}", componentPort); XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() .extensions(Extension.of(com.juick.Message.class), Extension.of(MessageQuery.class)) .debugger(LogbackDebugger.class) .defaultResponseTimeout(Duration.ofMillis(120000)) .build(); - router = ExternalComponent.create(componentName, password, configuration, componentHost, componentPort); - ServiceDiscoveryManager serviceDiscoveryManager = router.getManager(ServiceDiscoveryManager.class); + xmpp = ExternalComponent.create(componentName, password, configuration, componentHost, componentPort); + ServiceDiscoveryManager serviceDiscoveryManager = xmpp.getManager(ServiceDiscoveryManager.class); serviceDiscoveryManager.addIdentity(Identity.clientBot().withName("Juick")); - EntityCapabilitiesManager entityCapabilitiesManager = router.getManager(EntityCapabilitiesManager.class); + EntityCapabilitiesManager entityCapabilitiesManager = xmpp.getManager(EntityCapabilitiesManager.class); entityCapabilitiesManager.setNode("https://juick.com/caps"); - MessageDeliveryReceiptsManager messageDeliveryReceiptsManager = router.getManager(MessageDeliveryReceiptsManager.class); + MessageDeliveryReceiptsManager messageDeliveryReceiptsManager = xmpp.getManager(MessageDeliveryReceiptsManager.class); messageDeliveryReceiptsManager.setEnabled(true); - PingManager pingManager = router.getManager(PingManager.class); + PingManager pingManager = xmpp.getManager(PingManager.class); pingManager.setEnabled(true); - SoftwareVersionManager softwareVersionManager = router.getManager(SoftwareVersionManager.class); + SoftwareVersionManager softwareVersionManager = xmpp.getManager(SoftwareVersionManager.class); softwareVersionManager.setSoftwareVersion(new SoftwareVersion("Juick", "2.x", System.getProperty("os.name", "generic"))); VCard vCard = new VCard(); @@ -143,21 +146,20 @@ public class XMPPManager implements NotificationListener { vCard.setBirthday(LocalDate.of(2008, 10, 22)); try { vCard.setUrl(new URL("http://juick.com/")); - vCard.setPhoto(new VCard.Image("image/png", IOUtils.toByteArray( - getClass().getClassLoader().getResource("juick.png")))); + vCard.setPhoto(new VCard.Image("image/png", IOUtils.toByteArray(vCardImage.getInputStream()))); } catch (MalformedURLException e) { logger.error("invalid url", e); } catch (IOException e) { logger.warn("invalid resource", e); } - router.addIQHandler(MessageQuery.class, iq -> { + xmpp.addIQHandler(MessageQuery.class, iq -> { Message warningMessage = new Message(iq.getFrom(), Message.Type.CHAT); warningMessage.setFrom(jid); warningMessage.setBody("Your XMPP client constantly polls us with XMPP query which is unsupported for years, please find http://juick.com/query#messages in your client code and remove that"); - router.send(warningMessage); + xmpp.send(warningMessage); return iq.createError(new StanzaError(Condition.BAD_REQUEST, "Please stop this spam")); }); - router.addIQHandler(VCard.class, new AbstractIQHandler(IQ.Type.GET) { + xmpp.addIQHandler(VCard.class, new AbstractIQHandler(IQ.Type.GET) { @Override protected IQ processRequest(IQ iq) { if (iq.getTo().equals(jid) || iq.getTo().asBareJid().equals(jid.asBareJid()) @@ -183,16 +185,16 @@ public class XMPPManager implements NotificationListener { return iq.createError(Condition.BAD_REQUEST); } }); - router.addInboundMessageListener(e -> { + xmpp.addInboundMessageListener(e -> { ClientMessage result = incomingMessage(e.getMessage()); if (result != null) { - router.send(result); + xmpp.send(result); } }); - FileTransferManager fileTransferManager = router.getManager(FileTransferManager.class); + FileTransferManager fileTransferManager = xmpp.getManager(FileTransferManager.class); fileTransferManager.addFileTransferOfferListener(e -> { try { - List<String> allowedTypes = new ArrayList<String>() {{ + List<String> allowedTypes = new ArrayList<>() {{ add("png"); add("jpg"); }}; @@ -213,7 +215,7 @@ public class XMPPManager implements NotificationListener { userService.getUserByJID(initiator.asBareJid().toEscapedString()), initiator, jid.getLocal(), StringUtils.defaultString(e.getDescription()).trim(), URI.create(String.format("juick://%s", targetFilename))); if (result != null) { - router.send(result); + xmpp.send(result); } } catch (Exception e1) { logger.error("ft error", e1); @@ -226,7 +228,7 @@ public class XMPPManager implements NotificationListener { msg.setFrom(jid); msg.setTo(e.getInitiator()); msg.setBody("File transfer failed, please report to us"); - router.sendMessage(msg); + xmpp.sendMessage(msg); } else if (st.getStatus().equals(FileTransfer.Status.CANCELED)) { logger.info("transfer cancelled"); } @@ -241,24 +243,24 @@ public class XMPPManager implements NotificationListener { logger.error("ft error", e1); } }); - router.addConnectionListener(event -> { + xmpp.addConnectionListener(event -> { if (event.getType().equals(rocks.xmpp.core.session.ConnectionEvent.Type.RECONNECTION_SUCCEEDED)) { logger.info("component connected"); } }); - router.addSessionStatusListener(event -> { + xmpp.addSessionStatusListener(event -> { logger.info("event: " + event.getStatus(), event.getThrowable()); if (event.getStatus().equals(XmppSession.Status.AUTHENTICATED)) { logger.info("Authenticated, broadcasting..."); broadcastPresence(null); } }); - router.addInboundPresenceListener(event -> { + xmpp.addInboundPresenceListener(event -> { incomingPresence(event.getPresence()); }); executorService.submit(() -> { try { - router.connect(); + xmpp.connect(); } catch (XmppException e) { logger.warn("xmpp exception", e); } @@ -300,7 +302,7 @@ public class XMPPManager implements NotificationListener { } for (String jid : jids) { msg.setTo(Jid.of(jid)); - router.send(ClientMessage.from(msg)); + xmpp.send(ClientMessage.from(msg)); } } @@ -329,7 +331,7 @@ public class XMPPManager implements NotificationListener { for (User user : users) { for (String jid : userService.getJIDsbyUID(user.getUid())) { msg.setTo(Jid.of(jid)); - router.send(ClientMessage.from(msg)); + xmpp.send(ClientMessage.from(msg)); } } } @@ -355,7 +357,7 @@ public class XMPPManager implements NotificationListener { mm.setFrom(jid); mm.setBody("Private message from @" + msg.getUser().getName() + ":\n" + msg.getText()); } - router.send(ClientMessage.from(mm)); + xmpp.send(ClientMessage.from(mm)); }); } else if (MessageUtils.isReply(msg)) { sendJuickComment(msg, subscribers); @@ -394,7 +396,7 @@ public class XMPPManager implements NotificationListener { xmppMessage.addExtension(jmsg); xmppMessage.setBody(String.format("%s recommended your post #%d. %s", liker.getName(), jmsg.getMid(), PlainTextFormatter.formatUrl(jmsg))); - router.send(ClientMessage.from(xmppMessage)); + xmpp.send(ClientMessage.from(xmppMessage)); }); } @@ -436,7 +438,7 @@ public class XMPPManager implements NotificationListener { for (User user : users) { for (String jid : userService.getJIDsbyUID(user.getUid())) { msg.setTo(Jid.of(jid)); - router.send(ClientMessage.from(msg)); + xmpp.send(ClientMessage.from(msg)); } } } @@ -448,7 +450,7 @@ public class XMPPManager implements NotificationListener { Presence p = new Presence(Jid.of(userJid)); p.setFrom(jid); p.setPriority((byte) 10); - router.send(ClientPresence.from(p)); + xmpp.send(ClientPresence.from(p)); }); } @@ -476,7 +478,7 @@ public class XMPPManager implements NotificationListener { reply.setFrom(p.getTo().asBareJid()); reply.setTo(p.getFrom().asBareJid()); reply.setType(Presence.Type.UNSUBSCRIBE); - router.send(ClientPresence.from(reply)); + xmpp.send(ClientPresence.from(reply)); } else if (p.getType().equals(Presence.Type.PROBE)) { int uid_to = 0; if (!toJuick) { @@ -496,7 +498,7 @@ public class XMPPManager implements NotificationListener { if (!userService.getActiveJIDs().contains(p.getFrom().asBareJid().toEscapedString())) { reply.setStatus("Send ON to enable notifications"); } - router.send(ClientPresence.from(reply)); + xmpp.send(ClientPresence.from(reply)); } else { Presence reply = new Presence(); reply.setFrom(p.getTo()); @@ -504,7 +506,7 @@ public class XMPPManager implements NotificationListener { reply.setType(Presence.Type.ERROR); reply.setId(p.getId()); reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.ITEM_NOT_FOUND)); - router.send(ClientPresence.from(reply)); + xmpp.send(ClientPresence.from(reply)); } } else if (p.getType().equals(Presence.Type.SUBSCRIBE)) { boolean canSubscribe = false; @@ -522,12 +524,12 @@ public class XMPPManager implements NotificationListener { reply.setFrom(p.getTo()); reply.setTo(p.getFrom()); reply.setType(Presence.Type.SUBSCRIBED); - router.send(ClientPresence.from(reply)); + xmpp.send(ClientPresence.from(reply)); reply.setFrom(reply.getFrom().withResource(jid.getResource())); reply.setPriority((byte) 10); reply.setType(null); - router.send(ClientPresence.from(reply)); + xmpp.send(ClientPresence.from(reply)); } else { Presence reply = new Presence(); reply.setFrom(p.getTo()); @@ -535,7 +537,7 @@ public class XMPPManager implements NotificationListener { reply.setType(Presence.Type.ERROR); reply.setId(p.getId()); reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.ITEM_NOT_FOUND)); - router.send(ClientPresence.from(reply)); + xmpp.send(ClientPresence.from(reply)); } } else if (p.getType().equals(Presence.Type.UNSUBSCRIBE)) { if (!toJuick) { @@ -549,7 +551,7 @@ public class XMPPManager implements NotificationListener { reply.setFrom(p.getTo()); reply.setTo(p.getFrom()); reply.setType(Presence.Type.UNSUBSCRIBED); - router.send(ClientPresence.from(reply)); + xmpp.send(ClientPresence.from(reply)); } } @@ -566,7 +568,7 @@ public class XMPPManager implements NotificationListener { return null; } Jid to = msg.getTo(); - if (to.getDomain().equals(router.getDomain().toEscapedString()) || to.equals(this.jid)) { + if (to.getDomain().equals(xmpp.getDomain().toEscapedString()) || to.equals(this.jid)) { User user_from = userService.getUserByJID(msg.getFrom().asBareJid().toEscapedString()); if ((user_from == null || user_from.isAnonymous()) && !msg.getFrom().equals(jid)) { String signuphash = userService.getSignUpHashByJID(msg.getFrom().asBareJid().toEscapedString()); @@ -630,7 +632,7 @@ public class XMPPManager implements NotificationListener { userService.getActiveJIDs().forEach(j -> { try { presence.setTo(Jid.of(j)); - router.send(ClientPresence.from(presence)); + xmpp.send(ClientPresence.from(presence)); } catch (IllegalArgumentException ex) { logger.warn("Invalid jid: {}", j, ex); } @@ -640,8 +642,8 @@ public class XMPPManager implements NotificationListener { @PreDestroy public void close() throws Exception { broadcastPresence(Presence.Type.UNAVAILABLE); - if (router != null) { - router.close(); + if (xmpp != null) { + xmpp.close(); } } } diff --git a/src/main/java/com/juick/server/api/Messages.java b/src/main/java/com/juick/server/api/Messages.java index 5c791df4..5a4d8c31 100644 --- a/src/main/java/com/juick/server/api/Messages.java +++ b/src/main/java/com/juick/server/api/Messages.java @@ -31,7 +31,9 @@ import com.juick.service.MessagesService; import com.juick.service.TagService; import com.juick.service.UserService; import org.apache.commons.io.IOUtils; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.core.io.Resource; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; @@ -67,6 +69,8 @@ public class Messages { private ApplicationEventPublisher applicationEventPublisher; @Inject private WebApp webApp; + @Value("classpath:Transparent.gif") + private Resource invisiblePixel; // TODO: serialize image urls @@ -204,8 +208,7 @@ public class Messages { userService.updateLastSeen(visitor); applicationEventPublisher.publishEvent( new MessageReadEvent(this, visitor, msg)); - return IOUtils.toByteArray( - Objects.requireNonNull(getClass().getClassLoader().getResource("Transparent.gif"))); + return IOUtils.toByteArray(invisiblePixel.getInputStream()); } throw new HttpBadRequestException(); } |