diff options
author | Vitaly Takmazov | 2017-10-18 17:52:08 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2017-10-18 17:52:08 +0300 |
commit | ab1107c209edc34180397db4614ac3c185d6a52a (patch) | |
tree | ac0dfcfdf53369859950f48c756278e7261e9afe /juick-ws/src/main/java/com | |
parent | 0b9d212606984602445437e919075ff2d790e256 (diff) |
ws: httpclient -> RestTemplate
Diffstat (limited to 'juick-ws/src/main/java/com')
4 files changed, 25 insertions, 23 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 24304494..550067b7 100644 --- a/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java +++ b/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java @@ -19,6 +19,7 @@ package com.juick.ws; import com.juick.Message; import com.juick.User; +import com.juick.server.helpers.AnonymousUser; import com.juick.server.protocol.JuickProtocol; import com.juick.server.protocol.ProtocolListener; import com.juick.service.MessagesService; @@ -26,8 +27,6 @@ import com.juick.service.SubscriptionService; import com.juick.service.UserService; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; -import org.apache.http.NameValuePair; -import org.apache.http.client.utils.URLEncodedUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpHeaders; @@ -37,13 +36,14 @@ 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; +import org.springframework.web.util.UriComponentsBuilder; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.net.URI; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -81,18 +81,13 @@ public class WebsocketComponent extends TextWebSocketHandler implements Protocol Collections.singletonList(session.getRemoteAddress().toString())).get(0); // Auth - User visitor = new User(); - List<NameValuePair> params = URLEncodedUtils.parse(hLocation, StandardCharsets.UTF_8); - for (NameValuePair param : params) { - if (param.getName().equals("hash")) { - String hash = param.getValue(); - if (hash.length() == 16) { - visitor = userService.getUserByHash(hash); - } else { - logger.info("wrong hash for {} from {}", visitor.getUid(), hXRealIP); - } - break; - } + User visitor = AnonymousUser.INSTANCE; + UriComponents uriComponents = UriComponentsBuilder.fromUri(hLocation).build(); + List<String> hash = uriComponents.getQueryParams().get("hash"); + if (hash != null && hash.get(0).length() == 16) { + visitor = userService.getUserByHash(hash.get(0)); + } else { + logger.info("wrong hash for {} from {}", visitor.getUid(), hXRealIP); } logger.info("user {} connected to {} from {}", visitor.getUid(), hLocation.getPath(), hXRealIP); 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 c99d7a6d..b76545b8 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 @@ -25,25 +25,26 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; +import org.springframework.core.Ordered; import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.socket.config.annotation.EnableWebSocket; import org.springframework.web.socket.config.annotation.ServletWebSocketHandlerRegistry; 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.annotation.Resource; -import javax.inject.Inject; - /** * Created by aalexeev on 11/24/16. */ @Configuration -@ComponentScan(basePackages = {"com.juick.server.protocol"}) +@ComponentScan(basePackages = {"com.juick.ws.controllers"}) @EnableScheduling @EnableWebSocket +@EnableWebMvc @PropertySource("classpath:juick.conf") -class WebsocketConfiguration extends BaseWebConfiguration implements WebSocketConfigurer { +public class WebsocketConfiguration extends BaseWebConfiguration implements WebSocketConfigurer { @Bean public XMPPConnection xmpp() { return new XMPPConnection(); @@ -60,7 +61,7 @@ class WebsocketConfiguration extends BaseWebConfiguration implements WebSocketCo @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { - ((ServletWebSocketHandlerRegistry) registry).setOrder(2); + ((ServletWebSocketHandlerRegistry) registry).setOrder(Ordered.LOWEST_PRECEDENCE); registry.addHandler(wsHandler(), "/**").setAllowedOrigins("*"); } @@ -71,4 +72,8 @@ class WebsocketConfiguration extends BaseWebConfiguration implements WebSocketCo container.setMaxBinaryMessageBufferSize(8192); return container; } + @Override + public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { + configurer.enable(); + } } diff --git a/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketInitializer.java b/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketInitializer.java index aca3cd47..42f0fac2 100644 --- a/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketInitializer.java +++ b/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketInitializer.java @@ -17,6 +17,8 @@ package com.juick.ws.configuration; +import com.juick.configuration.DataConfiguration; +import com.juick.server.configuration.StorageConfiguration; import org.apache.commons.codec.CharEncoding; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; @@ -30,7 +32,7 @@ public class WebsocketInitializer extends AbstractAnnotationConfigDispatcherServ @Override protected Class<?>[] getRootConfigClasses() { - return new Class<?>[]{ WebsocketConfiguration.class }; + return new Class<?>[]{ WebsocketConfiguration.class, DataConfiguration.class, StorageConfiguration.class}; } @Override diff --git a/juick-ws/src/main/java/com/juick/ws/controllers/ApiController.java b/juick-ws/src/main/java/com/juick/ws/controllers/ApiController.java index 812a7eee..dc59193b 100644 --- a/juick-ws/src/main/java/com/juick/ws/controllers/ApiController.java +++ b/juick-ws/src/main/java/com/juick/ws/controllers/ApiController.java @@ -35,7 +35,7 @@ public class ApiController { private WebsocketComponent wsHandler; @RequestMapping(value = "/api/status", method = RequestMethod.GET, - produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + produces = MediaType.APPLICATION_JSON_UTF8_VALUE, headers = "Connection!=Upgrade") public Status status() { return Status.getStatus(String.valueOf(wsHandler.getClients().size())); } |