diff options
author | Vitaly Takmazov | 2017-10-23 10:59:29 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2017-10-23 12:06:07 +0300 |
commit | d0d7d6ade88b23a5b2ae26502b9977d03a0a0ab8 (patch) | |
tree | 1c0b5e2c709d8d518542b1e28d9fc6f44583c88b /juick-notifications/src | |
parent | f0e7e6866ae4fd4f518950480bc26fa823b7d919 (diff) |
notifications: fix mnps notifications
Diffstat (limited to 'juick-notifications/src')
-rw-r--r-- | juick-notifications/src/main/java/com/juick/components/MPNSClient.java | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/juick-notifications/src/main/java/com/juick/components/MPNSClient.java b/juick-notifications/src/main/java/com/juick/components/MPNSClient.java index 498d64dd..0c136af6 100644 --- a/juick-notifications/src/main/java/com/juick/components/MPNSClient.java +++ b/juick-notifications/src/main/java/com/juick/components/MPNSClient.java @@ -3,17 +3,20 @@ 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.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 javax.annotation.PostConstruct; import javax.inject.Inject; import java.io.IOException; +import java.util.stream.Collectors; /** * Created by vital on 29.03.2017. @@ -32,8 +35,7 @@ public class MPNSClient { private String applicationSip; @Value("${wns_client_secret}") private String applicationSecret; - @Inject - private BaseRestService restService; + private RestTemplate wnsService; @PostConstruct public void authenticate() throws IOException { @@ -45,8 +47,9 @@ public class MPNSClient { form.add("scope", "notify.windows.com"); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + wnsService = new RestTemplate(); HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(form, httpHeaders); - ResponseEntity<String> response = restService.getRest().exchange(url, HttpMethod.POST, entity, String.class); + ResponseEntity<String> response = wnsService.exchange(url, HttpMethod.POST, entity, String.class); String responseBody = response.getBody(); HttpStatus statusCode = response.getStatusCode(); if (statusCode != HttpStatus.OK) { @@ -66,16 +69,20 @@ public class MPNSClient { httpHeaders.setContentType(MediaType.TEXT_XML); httpHeaders.set("Authorization", accessToken); httpHeaders.set("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) { + HttpEntity<String> requestEntity = new HttpEntity<>(xml, httpHeaders); + try { + wnsService.exchange(url, 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 = responseEntity.getHeaders().toString(); + 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); } } |