diff options
Diffstat (limited to 'juick-www/src')
4 files changed, 61 insertions, 73 deletions
diff --git a/juick-www/src/main/java/com/juick/www/WebApp.java b/juick-www/src/main/java/com/juick/www/WebApp.java index f254b5fa..94c12528 100644 --- a/juick-www/src/main/java/com/juick/www/WebApp.java +++ b/juick-www/src/main/java/com/juick/www/WebApp.java @@ -18,21 +18,10 @@ package com.juick.www; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.juick.Message; import com.juick.Tag; import com.juick.service.TagService; import org.apache.commons.io.IOUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.DisposableBean; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; -import rocks.xmpp.addr.Jid; -import rocks.xmpp.core.XmppException; -import rocks.xmpp.core.session.Extension; -import rocks.xmpp.core.session.XmppSessionConfiguration; -import rocks.xmpp.core.session.debug.LogbackDebugger; -import rocks.xmpp.extensions.component.accept.ExternalComponent; import javax.annotation.PostConstruct; import javax.inject.Inject; @@ -49,25 +38,11 @@ import java.util.stream.Stream; * @author Ugnich Anton */ @Component -public class WebApp implements DisposableBean { - private static Logger logger = LoggerFactory.getLogger(WebApp.class); - - private ExternalComponent xmpp; - +public class WebApp { @Inject private TagService tagService; @Inject private ObjectMapper jsonMapper; - @Value("${xmpp_host:localhost}") - private String xmppHost; - @Value("${xmpp_password:secret}") - private String xmppPassword; - @Value("${www_xmpp_jid:www.juick.local}") - private String xmppJid; - @Value("${xmpp_port:5347}") - private int xmppPort; - @Value("${xmpp_disabled:false}") - private boolean isXmppDisabled; private String scriptsUrl; private String styleUrl; @@ -80,39 +55,6 @@ public class WebApp implements DisposableBean { new TypeReference<HashMap<String, String>>() {}); scriptsUrl = manifest.get("scripts.js"); styleUrl = manifest.get("style.css"); - if (!isXmppDisabled) { - setupXmppComponent(Jid.of(xmppJid), - xmppPassword, xmppPort); - } - } - - @Override - public void destroy() { - try { - if (xmpp != null) - xmpp.close(); - logger.error("ExternalComponent on WWW destroyed"); - } catch (Exception e) { - logger.error("exception on destroy", e); - } - } - - public void setupXmppComponent(final Jid componentJid, final String password, final int port) { - XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() - .extensions(Extension.of(Message.class)) - .debugger(LogbackDebugger.class) - .build(); - xmpp = ExternalComponent.create(componentJid.toString(), password, configuration, xmppHost, port); - xmpp.addConnectionListener(e -> logger.error(e.toString(), e.getCause())); - try { - xmpp.connect(); - } catch (XmppException e) { - logger.error("xmpp extension", e); - } - } - - public ExternalComponent getXmpp() { - return xmpp; } public List<Tag> parseTags(String tagsStr) { diff --git a/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java index 250197f7..f8258d5e 100644 --- a/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java +++ b/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java @@ -44,7 +44,7 @@ import java.util.Collections; @Configuration @EnableCaching @Import({WwwServletConfiguration.class, WebSecurityConfig.class, SapeConfiguration.class, SearchConfiguration.class, - DataConfiguration.class, StorageConfiguration.class}) + DataConfiguration.class, StorageConfiguration.class, XMPPConfiguration.class}) public class WwwAppConfiguration extends BaseWebConfiguration { @Inject private UserService userService; diff --git a/juick-www/src/main/java/com/juick/www/configuration/XMPPConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/XMPPConfiguration.java new file mode 100644 index 00000000..1396f9f9 --- /dev/null +++ b/juick-www/src/main/java/com/juick/www/configuration/XMPPConfiguration.java @@ -0,0 +1,43 @@ +package com.juick.www.configuration; + +import com.juick.Message; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import rocks.xmpp.core.XmppException; +import rocks.xmpp.core.session.Extension; +import rocks.xmpp.core.session.XmppSessionConfiguration; +import rocks.xmpp.core.session.debug.LogbackDebugger; +import rocks.xmpp.extensions.component.accept.ExternalComponent; + +@Configuration +public class XMPPConfiguration { + private static Logger logger = LoggerFactory.getLogger(XMPPConfiguration.class); + @Value("${xmpp_host:localhost}") + private String xmppHost; + @Value("${xmpp_password:secret}") + private String xmppPassword; + @Value("${www_xmpp_jid:www.juick.local}") + private String xmppJid; + @Value("${xmpp_port:5347}") + private int xmppPort; + @Value("${xmpp_disabled:false}") + private boolean isXmppDisabled; + @Bean + public ExternalComponent xmpp() { + XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() + .extensions(Extension.of(Message.class)) + .debugger(LogbackDebugger.class) + .build(); + ExternalComponent xmpp = ExternalComponent.create(xmppJid, xmppPassword, configuration, xmppHost, xmppPort); + xmpp.addConnectionListener(e -> logger.error(e.toString(), e.getCause())); + if (!isXmppDisabled) try { + xmpp.connect(); + } catch (XmppException e) { + logger.error("xmpp extension", e); + } + return xmpp; + } +} 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 6d7de920..a252bb69 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 @@ -38,6 +38,7 @@ import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import rocks.xmpp.addr.Jid; 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; @@ -69,6 +70,8 @@ public class NewMessage { private PMQueriesService pmQueriesService; @Inject private WebApp webApp; + @Inject + private ExternalComponent xmpp; @Value("${img_path:#{systemEnvironment['TEMP'] ?: '/tmp'}}") private String imgDir; @Value("${upload_tmp_dir:#{systemEnvironment['TEMP'] ?: '/tmp'}}") @@ -147,18 +150,18 @@ public class NewMessage { logger.warn("invalid uri: {} exception {}", attachmentURL, e); } } - if (webApp.getXmpp() != null) { + if (xmpp.isConnected()) { xmsg.setBody("@" + jmsg.getUser().getName() + ":" + MessageUtils.getTagsString(jmsg) + "\n" + body + "\n\n#" + mid + " http://juick.com/" + mid); xmsg.setTo(Jid.of("juick@s2s.juick.com")); - webApp.getXmpp().send(xmsg); + xmpp.send(xmsg); xmsg.setTo(Jid.of("juick@ws.juick.com")); - webApp.getXmpp().send(xmsg); + xmpp.send(xmsg); xmsg.setTo(Jid.of("jubo@nologin.ru")); - webApp.getXmpp().send(xmsg); + xmpp.send(xmsg); } else { logger.warn("XMPP unavailable"); } @@ -275,16 +278,16 @@ public class NewMessage { } } - if (webApp.getXmpp() != null) { + if (xmpp.isConnected()) { xmsg.setBody("Reply by @" + jmsg.getUser().getName() + ":\n>" + quote + "\n" + body + "\n\n#" + mid + "/" + ridnew + " http://juick.com/" + mid + "#" + ridnew); xmsg.setTo(Jid.of("juick@s2s.juick.com")); - webApp.getXmpp().send(xmsg); + xmpp.send(xmsg); xmsg.setTo(Jid.of("juick@ws.juick.com")); - webApp.getXmpp().send(xmsg); + xmpp.send(xmsg); } else { logger.warn("XMPP unavailable"); } @@ -310,7 +313,7 @@ public class NewMessage { boolean res = messagesService.recommendMessage(mid, visitor.getUid()); if (res) { - if (webApp.getXmpp() != null) { + if (xmpp.isConnected()) { Message xmsg = new Message(); xmsg.setFrom(Jid.of("juick@juick.com")); xmsg.setTo(Jid.of("recomm@s2s.juick.com")); @@ -318,7 +321,7 @@ public class NewMessage { jmsg.setMid(mid); jmsg.setUser(visitor); xmsg.addExtension(jmsg); - webApp.getXmpp().send(xmsg); + xmpp.send(xmsg); } else { logger.warn("XMPP unavailable"); } @@ -353,7 +356,7 @@ public class NewMessage { } if (pmQueriesService.createPM(visitor.getUid(), userTo.getUid(), body)) { - if (webApp.getXmpp() != null) { + if (xmpp.isConnected()) { Message msg = new Message(); msg.setFrom(Jid.of("juick@juick.com")); com.juick.Message jmsg = new com.juick.Message(); @@ -362,7 +365,7 @@ public class NewMessage { jmsg.setTo(userTo); msg.addExtension(jmsg); msg.setTo(Jid.of(String.format("%d@ws.juick.com", userTo.getUid()))); - webApp.getXmpp().send(msg); + xmpp.send(msg); List<String> jids = userService.getJIDsbyUID(userTo.getUid()); for (String jid : jids) { @@ -376,7 +379,7 @@ public class NewMessage { mm.setFrom(Jid.of("juick", "juick.com", "Juick")); mm.setBody("Private message from @" + jmsg.getUser().getName() + ":\n" + body); } - webApp.getXmpp().send(mm); + xmpp.send(mm); } } else { logger.warn("XMPP unavailable"); @@ -419,7 +422,7 @@ public class NewMessage { String attachmentUrl = String.format("juick://%s", attachmentFName); msg.addExtension(new OobX(new URI(attachmentUrl), "!!!!Juick!!")); } - webApp.getXmpp().sendMessage(msg); + xmpp.sendMessage(msg); } catch (URISyntaxException e1) { logger.warn("attachment error", e1); } |