aboutsummaryrefslogtreecommitdiff
path: root/juick-notifications/src/main/java/com/juick/components/configuration/JuickServerWebsocketConfiguration.java
blob: a29566aa3dce3fbd35303755cb653fd4281eb4b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.juick.components.configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.juick.components.Notifications;
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.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 Notifications notifications;
    @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 = response.getBody();
        } catch (HttpClientErrorException e) {
            logger.warn("service component is not authenticated", e);
        }
        String websocketURI = UriComponentsBuilder.fromUriString(baseUri)
                .queryParam("hash", hash).build().toUriString();
        WebSocketConnectionManager manager = new WebSocketConnectionManager(client(), notifications, websocketURI);
        return manager;
    }
    @Bean
    public StandardWebSocketClient client() {
        return new StandardWebSocketClient();
    }
}