From d0d7d6ade88b23a5b2ae26502b9977d03a0a0ab8 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 23 Oct 2017 10:59:29 +0300 Subject: notifications: fix mnps notifications --- .../main/java/com/juick/components/MPNSClient.java | 25 ++++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'juick-notifications/src/main/java') 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> entity = new HttpEntity<>(form, httpHeaders); - ResponseEntity response = restService.getRest().exchange(url, HttpMethod.POST, entity, String.class); + ResponseEntity 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 requestEntity = new HttpEntity<>(httpHeaders); - ResponseEntity responseEntity = restService.getRest().exchange(url, HttpMethod.POST, requestEntity, Void.class); - HttpStatus statusCode = responseEntity.getStatusCode(); - if (statusCode != HttpStatus.OK) { + HttpEntity 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); } } -- cgit v1.2.3