diff options
author | Vitaly Takmazov | 2018-03-16 13:42:49 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-03-16 13:42:49 +0300 |
commit | 7d85a992b139c619c677e7158f8402ed0f6f0f1b (patch) | |
tree | bdfc5248578f1a1c339aa5a80a51ec88ea32f885 /juick-notifications | |
parent | f6314696f047749ad812148d946d603c5c0e664a (diff) |
server: move token-based api configuration to juick-notifications
Diffstat (limited to 'juick-notifications')
2 files changed, 72 insertions, 1 deletions
diff --git a/juick-notifications/src/main/java/com/juick/components/configuration/JuickServerWebsocketConfiguration.java b/juick-notifications/src/main/java/com/juick/components/configuration/JuickServerWebsocketConfiguration.java new file mode 100644 index 00000000..e94a3a8e --- /dev/null +++ b/juick-notifications/src/main/java/com/juick/components/configuration/JuickServerWebsocketConfiguration.java @@ -0,0 +1,72 @@ +package com.juick.components.configuration; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.juick.server.component.JuickServerComponent; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.http.client.InterceptingClientHttpRequestFactory; +import org.springframework.http.client.support.BasicAuthorizationInterceptor; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.socket.client.WebSocketConnectionManager; +import org.springframework.web.socket.client.standard.StandardWebSocketClient; +import org.springframework.web.util.UriComponentsBuilder; + +import javax.inject.Inject; +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +@Lazy +@Configuration +@EnableScheduling +public class JuickServerWebsocketConfiguration { + private static final Logger logger = LoggerFactory.getLogger(JuickServerWebsocketConfiguration.class); + @Value("${websocket_url:ws://localhost:8080/ws/}") + private String baseUri; + @Value("${api_user:juick}") + private String serviceUser; + @Value("${api_password:secret}") + private String servicePassword; + @Inject + ObjectMapper jsonMapper; + @Inject + private JuickServerComponent juickServerComponent; + @Bean + public RestTemplate rest() { + RestTemplate rest = new RestTemplate(); + List<ClientHttpRequestInterceptor> interceptors = Collections.singletonList( + new BasicAuthorizationInterceptor(serviceUser, servicePassword)); + + rest.setRequestFactory(new InterceptingClientHttpRequestFactory(rest.getRequestFactory(), interceptors)); + return rest; + } + @Bean + public WebSocketConnectionManager connectionManager() { + String hash = StringUtils.EMPTY; + try { + ResponseEntity<String> response = rest().exchange("https://api.juick.com/auth", + HttpMethod.GET, null, String.class); + hash = jsonMapper.readValue(response.getBody(), String.class); + } catch (HttpClientErrorException | IOException e) { + logger.warn("service component is not authenticated", e); + } + String websocketURI = UriComponentsBuilder.fromUriString(baseUri) + .queryParam("hash", hash).build().toUriString(); + WebSocketConnectionManager manager = new WebSocketConnectionManager(client(), juickServerComponent, websocketURI); + return manager; + } + @Bean + public StandardWebSocketClient client() { + return new StandardWebSocketClient(); + } +} diff --git a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java b/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java index d2522a58..920e095a 100644 --- a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java +++ b/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java @@ -4,7 +4,6 @@ import com.juick.components.Notifications; import com.juick.server.component.JuickServerComponent; import com.juick.server.component.JuickServerReconnectManager; import com.juick.server.configuration.BaseWebConfiguration; -import com.juick.server.configuration.JuickServerWebsocketConfiguration; import org.springframework.context.annotation.*; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.web.servlet.config.annotation.EnableWebMvc; |