aboutsummaryrefslogtreecommitdiff
path: root/juick-notifications/src/main/java/com/juick/components/MPNSClient.java
diff options
context:
space:
mode:
Diffstat (limited to 'juick-notifications/src/main/java/com/juick/components/MPNSClient.java')
-rw-r--r--juick-notifications/src/main/java/com/juick/components/MPNSClient.java71
1 files changed, 26 insertions, 45 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 165bb94c..035d9eb2 100644
--- a/juick-notifications/src/main/java/com/juick/components/MPNSClient.java
+++ b/juick-notifications/src/main/java/com/juick/components/MPNSClient.java
@@ -3,25 +3,15 @@ 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.http.*;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.entity.UrlEncodedFormEntity;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.client.HttpClientBuilder;
-import org.apache.http.message.BasicNameValuePair;
-import org.apache.http.util.EntityUtils;
+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;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.stream.Collectors;
/**
* Created by vital on 29.03.2017.
@@ -40,27 +30,26 @@ public class MPNSClient {
private String applicationSip;
@Value("${wns_client_secret}")
private String applicationSecret;
+ @Inject
+ private BaseRestService restService;
@PostConstruct
public void authenticate() throws IOException {
- HttpClient client = HttpClientBuilder.create().build();
String url = "https://login.live.com/accesstoken.srf";
- List<NameValuePair> formParams = new ArrayList<>();
- formParams.add(new BasicNameValuePair("grant_type", "client_credentials"));
- formParams.add(new BasicNameValuePair("client_id", applicationSip));
- formParams.add(new BasicNameValuePair("client_secret", applicationSecret));
- formParams.add(new BasicNameValuePair("scope", "notify.windows.com"));
- UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, Consts.UTF_8);
- HttpPost httppost = new HttpPost(url);
- httppost.setEntity(entity);
- HttpResponse response = client.execute(httppost);
- int statusCode = response.getStatusLine().getStatusCode();
- String responseContent = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
- if (statusCode != HttpStatus.SC_OK) {
- MPNSError error = jsonMapper.readValue(responseContent, MPNSError.class);
+ 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(responseContent, MPNSToken.class);
+ MPNSToken token = jsonMapper.readValue(responseBody, MPNSToken.class);
if (token.getTokenType().length() >= 1) {
token.setTokenType(Character.toUpperCase(token.getTokenType().charAt(0)) + token.getTokenType().substring(1));
}
@@ -69,33 +58,25 @@ public class MPNSClient {
}
void sendNotification(final String url, final String xml) throws IOException {
- HttpClient client = HttpClientBuilder.create().build();
- StringEntity entity = new StringEntity(xml, Consts.UTF_8);
- HttpPost httpPost = new HttpPost(url);
- httpPost.setHeader("Content-Type", "text/xml");
- httpPost.setHeader("Authorization", accessToken);
- httpPost.setHeader("X-WNS-Type", "wns/toast");
- httpPost.setEntity(entity);
- HttpResponse response = client.execute(httpPost);
- int statusCode = response.getStatusLine().getStatusCode();
- if (statusCode != HttpStatus.SC_OK) {
- if (statusCode == HttpStatus.SC_GONE) {
+ 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 = stringifyWnsHttpHeaders(response.getAllHeaders());
+ String headersContent = responseEntity.getHeaders().toString();
throw new IOException(headersContent);
}
}
}
- private String stringifyWnsHttpHeaders(final Header[] allHeaders) {
- return Arrays.stream(allHeaders)
- .filter(x -> x.getName().startsWith("X-WNS-") || x.getName().startsWith("WWW-"))
- .map(x -> x.getName() + ": " + x.getValue())
- .collect(Collectors.joining("\n"));
- }
public void setListener(NotificationClientListener listener) {
this.listener = listener;