aboutsummaryrefslogtreecommitdiff
path: root/juick-ws/src/main/java/com
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2017-08-21 12:52:40 +0300
committerGravatar Vitaly Takmazov2017-08-21 12:52:40 +0300
commit007951db3ae9f9ba89a726960b0ef1639d63f3df (patch)
treeca1bacf483b29c89d6b703e1474f71302a9651de /juick-ws/src/main/java/com
parent8be8dca3f8bb97d347214f32dd4d77fac07147f7 (diff)
ws: cleanup components initialization
Diffstat (limited to 'juick-ws/src/main/java/com')
-rw-r--r--juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java8
-rw-r--r--juick-ws/src/main/java/com/juick/ws/XMPPConnection.java45
-rw-r--r--juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java10
3 files changed, 23 insertions, 40 deletions
diff --git a/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java b/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java
index f56ba936..24304494 100644
--- a/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java
+++ b/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java
@@ -57,13 +57,13 @@ public class WebsocketComponent extends TextWebSocketHandler implements Protocol
private final List<SocketSubscribed> clients = Collections.synchronizedList(new ArrayList<SocketSubscribed>());
@Inject
- UserService userService;
+ private UserService userService;
@Inject
- MessagesService messagesService;
+ private MessagesService messagesService;
@Inject
- SubscriptionService subscriptionService;
+ private SubscriptionService subscriptionService;
@Inject
- JuickProtocol protocol;
+ private JuickProtocol protocol;
@PostConstruct
public void init() {
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());
diff --git a/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java b/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java
index 21c013e9..a2c2d827 100644
--- a/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java
+++ b/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java
@@ -20,9 +20,7 @@ package com.juick.ws.configuration;
import com.juick.server.configuration.BaseWebConfiguration;
import com.juick.server.protocol.JuickProtocol;
import com.juick.ws.WebsocketComponent;
-import com.juick.ws.XMPPConnection;
import org.springframework.context.annotation.*;
-import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
@@ -31,8 +29,6 @@ import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
-import javax.inject.Inject;
-
/**
* Created by aalexeev on 11/24/16.
*/
@@ -43,8 +39,6 @@ import javax.inject.Inject;
@PropertySource("classpath:juick.conf")
@Import(BaseWebConfiguration.class)
class WebsocketConfiguration extends WebMvcConfigurationSupport implements WebSocketConfigurer {
- @Inject
- private Environment env;
@Bean
public WebsocketComponent wsHandler() {
@@ -52,10 +46,6 @@ class WebsocketConfiguration extends WebMvcConfigurationSupport implements WebSo
}
@Bean
- public XMPPConnection ws() {
- return new XMPPConnection(env, wsHandler());
- }
- @Bean
public JuickProtocol protocol() {
return new JuickProtocol("https://juick.com/");
}