From 86449c7a60a3f39e400ec98a8f8b9e3c6804f41e Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 30 Jan 2018 19:09:30 +0300 Subject: web: handle service component authentication exceptions --- .../JuickServerComponentConfiguration.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'juick-server-web/src/main/java/com/juick/server') diff --git a/juick-server-web/src/main/java/com/juick/server/configuration/JuickServerComponentConfiguration.java b/juick-server-web/src/main/java/com/juick/server/configuration/JuickServerComponentConfiguration.java index 3e8b3865..23f550a5 100644 --- a/juick-server-web/src/main/java/com/juick/server/configuration/JuickServerComponentConfiguration.java +++ b/juick-server-web/src/main/java/com/juick/server/configuration/JuickServerComponentConfiguration.java @@ -2,6 +2,9 @@ package com.juick.server.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; @@ -11,6 +14,7 @@ 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; @@ -24,6 +28,7 @@ import java.util.List; @Configuration @EnableScheduling public class JuickServerComponentConfiguration { + private static final Logger logger = LoggerFactory.getLogger(JuickServerComponentConfiguration.class); @Value("${websocket_url:ws://localhost:8080/}") private String baseUri; @Value("${api_user:juick}") @@ -44,10 +49,15 @@ public class JuickServerComponentConfiguration { return rest; } @Bean - public WebSocketConnectionManager connectionManager() throws IOException { - ResponseEntity response = rest().exchange("https://api.juick.com/auth", - HttpMethod.GET, null, String.class); - String hash = jsonMapper.readValue(response.getBody(), String.class); + public WebSocketConnectionManager connectionManager() { + String hash = StringUtils.EMPTY; + try { + ResponseEntity 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); -- cgit v1.2.3