package com.juick.components; import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.components.mpns.MPNSError; import com.juick.components.mpns.MPNSToken; import org.apache.commons.collections4.MapUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.*; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.io.IOException; import java.util.stream.Collectors; /** * 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; private RestTemplate wnsService; @PostConstruct public void authenticate() throws IOException { String url = "https://login.live.com/accesstoken.srf"; MultiValueMap form = new LinkedMultiValueMap<>(); form.add("grant_type", "client_credentials"); form.add("client_id", applicationSip); form.add("client_secret", applicationSecret); form.add("scope", "notify.windows.com"); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); wnsService = new RestTemplate(); HttpEntity> entity = new HttpEntity<>(form, httpHeaders); ResponseEntity response = wnsService.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.setContentType(MediaType.TEXT_XML); httpHeaders.set("Authorization", accessToken); httpHeaders.set("X-WNS-Type", "wns/toast"); HttpEntity requestEntity = new HttpEntity<>(xml, httpHeaders); try { UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url); wnsService.exchange(builder.build(true).toUri(), HttpMethod.POST, requestEntity, Void.class); } catch (HttpClientErrorException ex) { HttpStatus statusCode = ex.getStatusCode(); if (statusCode == HttpStatus.GONE) { // expired logger.info("{} is scheduled to remove", url); listener.invalidToken("mpns", url); } else { String headersContent = ex.getResponseHeaders().entrySet().stream() .filter(x -> x.getKey().startsWith("X-WNS-") || x.getKey().startsWith("WWW-")) .map(x -> x.getKey() + ": " + String.join(",", x.getValue())) .collect(Collectors.joining("\n")); throw new IOException(headersContent); } } } public void setListener(NotificationClientListener listener) { this.listener = listener; } }