From f764bb226f0db20a4e4c2258f2304e93f1a47557 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 15 Feb 2018 10:50:40 +0300 Subject: server: handle messages from service ws user --- .../main/java/com/juick/server/ServerManager.java | 11 ++++++--- .../java/com/juick/server/WebsocketManager.java | 28 +++++++++++++++++++++- 2 files changed, 35 insertions(+), 4 deletions(-) (limited to 'juick-server/src/main/java') diff --git a/juick-server/src/main/java/com/juick/server/ServerManager.java b/juick-server/src/main/java/com/juick/server/ServerManager.java index 79ccb1e4..1db31e23 100644 --- a/juick-server/src/main/java/com/juick/server/ServerManager.java +++ b/juick-server/src/main/java/com/juick/server/ServerManager.java @@ -164,13 +164,18 @@ public class ServerManager implements AutoCloseable { xmsg.setBody(body); try { if (StringUtils.isNotEmpty(attachmentName)) { - String attachmentUrl = String.format("juick://%s", attachmentName); - xmsg.addExtension(new OobX(new URI(attachmentUrl), "!!!!Juick!!")); + URI httpUri = URI.create(attachmentName); + if (httpUri.isAbsolute()) { + xmsg.addExtension(new OobX(httpUri)); + } else { + String attachmentUrl = String.format("juick://%s", attachmentName); + xmsg.addExtension(new OobX(new URI(attachmentUrl), "!!!!Juick!!")); + } } - sendMessage(xmsg); } catch (URISyntaxException e1) { logger.warn("attachment error", e1); } + sendMessage(xmsg); } private void onJuickPM(final int uid_to, final com.juick.Message jmsg) throws JsonProcessingException { 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 6e3fbea2..475fd8b6 100644 --- a/juick-server/src/main/java/com/juick/server/WebsocketManager.java +++ b/juick-server/src/main/java/com/juick/server/WebsocketManager.java @@ -17,6 +17,8 @@ package com.juick.server; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.juick.Message; import com.juick.User; import com.juick.server.helpers.AnonymousUser; import com.juick.server.util.HttpBadRequestException; @@ -28,10 +30,12 @@ import com.juick.service.UserService; import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.PingMessage; +import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.handler.TextWebSocketHandler; import org.springframework.web.util.UriComponents; @@ -58,10 +62,16 @@ public class WebsocketManager extends TextWebSocketHandler { private MessagesService messagesService; @Inject private SubscriptionService subscriptionService; + @Value("${service_user:juick}") + private String serviceUser; + @Inject + private ObjectMapper jsonMapper; + @Inject + private ServerManager serverManager; @Override - public void afterConnectionEstablished(WebSocketSession session) throws Exception { + public void afterConnectionEstablished(WebSocketSession session) { URI hLocation; String hXRealIP; @@ -127,6 +137,22 @@ public class WebsocketManager extends TextWebSocketHandler { } + @Override + protected void handleTextMessage(WebSocketSession session, TextMessage message) { + clients.stream().filter(c -> c.session.equals(session)).findFirst().ifPresent( c -> { + if (c.visitor.getName().equals(serviceUser)) { + try { + Message draft = jsonMapper.readValue(message.asBytes(), Message.class); + serverManager.processMessage(draft.getUser(), draft.getText(), draft.getAttachment().getUrl()); + } catch (IOException e) { + throw new HttpBadRequestException(); + } + } else { + throw new HttpForbiddenException(); + } + }); + } + @Scheduled(fixedRate = 30000) public void ping() { clients.forEach(c -> { -- cgit v1.2.3