aboutsummaryrefslogtreecommitdiff
path: root/juick-notifications/src/main/java/com/juick/components/MPNSClient.java
blob: 035d9eb24e592f103dd6783e03ec0e46ce2ae838 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.juick.components;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.juick.components.mpns.MPNSError;
import com.juick.components.mpns.MPNSToken;
import com.juick.service.BaseRestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.io.IOException;

/**
 * Created by vital on 29.03.2017.
 */
public class MPNSClient {

    private static Logger logger = LoggerFactory.getLogger(MPNSClient.class);

    private String accessToken;

    private NotificationClientListener listener;

    @Inject
    private ObjectMapper jsonMapper;
    @Value("${wns_application_sip}")
    private String applicationSip;
    @Value("${wns_client_secret}")
    private String applicationSecret;
    @Inject
    private BaseRestService restService;

    @PostConstruct
    public void authenticate() throws IOException {
        String url = "https://login.live.com/accesstoken.srf";
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("grant_type", "client_credentials");
        httpHeaders.add("client_id", applicationSip);
        httpHeaders.add("client_secret", applicationSecret);
        httpHeaders.add("scope", "notify.windows.com");
        HttpEntity<String> entity = new HttpEntity<>(httpHeaders);
        ResponseEntity<String> response = restService.getRest().exchange(url, HttpMethod.POST, entity, String.class);
        String responseBody = response.getBody();
        HttpStatus statusCode = response.getStatusCode();
        if (statusCode != HttpStatus.OK) {
            MPNSError error = jsonMapper.readValue(responseBody, MPNSError.class);
            throw new IOException(error.getError() + ": " + error.getErrorDescription());
        }
        MPNSToken token = jsonMapper.readValue(responseBody, MPNSToken.class);
        if (token.getTokenType().length() >= 1) {
            token.setTokenType(Character.toUpperCase(token.getTokenType().charAt(0)) + token.getTokenType().substring(1));
        }
        accessToken = token.getTokenType() + " " + token.getAccessToken();
        logger.info("MPNS authenticated");
    }

    void sendNotification(final String url, final String xml) throws IOException {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Type", "text/xml");
        httpHeaders.add("Authorization", accessToken);
        httpHeaders.add("X-WNS-Type", "wns/toast");
        HttpEntity<String> requestEntity = new HttpEntity<>(httpHeaders);
        ResponseEntity<Void> responseEntity = restService.getRest().exchange(url, HttpMethod.POST, requestEntity, Void.class);
        HttpStatus statusCode = responseEntity.getStatusCode();
        if (statusCode != HttpStatus.OK) {
            if (statusCode == HttpStatus.GONE) {
                // expired
                logger.info("{} is scheduled to remove", url);
                listener.invalidToken("mpns", url);
            } else {
                String headersContent = responseEntity.getHeaders().toString();
                throw new IOException(headersContent);
            }
        }
    }


    public void setListener(NotificationClientListener listener) {
        this.listener = listener;
    }
}