From 5b6ac36436bbed6da7937f0fc83eb734ad929910 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 17 Oct 2017 15:42:28 +0300 Subject: notifications: drop httpclient dependency --- .../main/java/com/juick/components/MPNSClient.java | 71 ++++++++-------------- .../juick/components/test/NotificationTests.java | 7 +-- 2 files changed, 27 insertions(+), 51 deletions(-) (limited to 'juick-notifications') 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 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 entity = new HttpEntity<>(httpHeaders); + ResponseEntity 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 requestEntity = new HttpEntity<>(httpHeaders); + ResponseEntity 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; diff --git a/juick-notifications/src/test/java/com/juick/components/test/NotificationTests.java b/juick-notifications/src/test/java/com/juick/components/test/NotificationTests.java index 9ca9c187..cc02b07d 100644 --- a/juick-notifications/src/test/java/com/juick/components/test/NotificationTests.java +++ b/juick-notifications/src/test/java/com/juick/components/test/NotificationTests.java @@ -32,14 +32,9 @@ import org.slf4j.LoggerFactory; import org.springframework.web.client.RestTemplate; import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; import static org.mockito.ArgumentMatchers.argThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; /** * Created by vitalyster on 22.11.2016. -- cgit v1.2.3