diff options
Diffstat (limited to 'juick-server/src/main/java/com')
-rw-r--r-- | juick-server/src/main/java/com/juick/server/ServerManager.java | 11 | ||||
-rw-r--r-- | juick-server/src/main/java/com/juick/server/WebsocketManager.java | 28 |
2 files changed, 35 insertions, 4 deletions
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 79ccb1e41..1db31e232 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 6e3fbea23..475fd8b64 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 -> { |