diff options
Diffstat (limited to 'juick-ws/src/main/java/com/juick/ws/XMPPConnection.java')
-rw-r--r-- | juick-ws/src/main/java/com/juick/ws/XMPPConnection.java | 45 |
1 files changed, 19 insertions, 26 deletions
diff --git a/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java b/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java index 906fa125..84158445 100644 --- a/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java +++ b/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java @@ -17,7 +17,6 @@ package com.juick.ws; -import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.User; @@ -26,7 +25,7 @@ import com.juick.service.SubscriptionService; import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.core.env.Environment; +import org.springframework.beans.factory.annotation.Value; import org.springframework.util.Assert; import org.springframework.web.socket.TextMessage; import rocks.xmpp.core.XmppException; @@ -38,6 +37,7 @@ import rocks.xmpp.core.stanza.model.Message; import rocks.xmpp.extensions.component.accept.ExternalComponent; import rocks.xmpp.util.XmppUtils; +import javax.annotation.PostConstruct; import javax.inject.Inject; import javax.xml.bind.JAXBException; import javax.xml.stream.XMLStreamException; @@ -53,36 +53,29 @@ import java.util.stream.Collectors; public class XMPPConnection implements AutoCloseable { private static final Logger logger = LoggerFactory.getLogger(XMPPConnection.class); - private final WebsocketComponent wsHandler; - private final String xmppPassword; - private final ObjectMapper ms; - private final int xmppPort; - private final String wsJid; + @Inject + private WebsocketComponent wsHandler; + @Value("${xmpp_password:secret}") + private String xmppPassword; + @Inject + private ObjectMapper jsonMapper; + @Value("${xmpp_port:5347}") + private int xmppPort; + @Value("${ws_jid:ws.juick.local}") + private String wsJid; private XmppSession xmpp; @Inject - MessagesService messagesService; + private MessagesService messagesService; @Inject - SubscriptionService subscriptionService; + private SubscriptionService subscriptionService; - public XMPPConnection( - final Environment env, final WebsocketComponent wsHandler) { - Assert.notNull(env, "Environment must be initialized"); + @PostConstruct + public void init() { Assert.notNull(wsHandler, "WebsocketComponent must be initialized"); - this.wsHandler = wsHandler; - - xmppPassword = env.getProperty("xmpp_password"); - xmppPort = NumberUtils.toInt(env.getProperty("xmpp_port"), 5347); - wsJid = env.getProperty("ws_jid", "ws.juick.local"); - - ms = new ObjectMapper(); - ms.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); - ms.setSerializationInclusion(JsonInclude.Include.NON_NULL); - ms.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); - XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() .extensions(Extension.of(com.juick.Message.class)) .debugger(LogbackDebugger.class) @@ -143,7 +136,7 @@ public class XMPPConnection implements AutoCloseable { } private void onJuickPM(final int uid_to, final com.juick.Message jmsg) throws JsonProcessingException { - String json = ms.writeValueAsString(jmsg); + String json = jsonMapper.writeValueAsString(jmsg); synchronized (wsHandler.getClients()) { wsHandler.getClients().stream().filter(c -> !c.legacy && c.visitor.getUid() == uid_to).forEach(c -> { try { @@ -157,7 +150,7 @@ public class XMPPConnection implements AutoCloseable { } private void onJuickMessagePost(final com.juick.Message jmsg) throws JsonProcessingException { - String json = ms.writeValueAsString(jmsg); + String json = jsonMapper.writeValueAsString(jmsg); List<Integer> uids = subscriptionService.getSubscribedUsers(jmsg.getUser().getUid(), jmsg.getMid()) .stream().map(User::getUid).collect(Collectors.toList()); synchronized (wsHandler.getClients()) { @@ -186,7 +179,7 @@ public class XMPPConnection implements AutoCloseable { } private void onJuickMessageReply(final com.juick.Message jmsg) throws JsonProcessingException { - String json = ms.writeValueAsString(jmsg); + String json = jsonMapper.writeValueAsString(jmsg); List<Integer> threadUsers = subscriptionService.getUsersSubscribedToComments(jmsg.getMid(), jmsg.getUser().getUid()) .stream().map(User::getUid).collect(Collectors.toList()); |