diff options
author | Vitaly Takmazov | 2017-10-17 15:42:28 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2017-10-17 15:42:28 +0300 |
commit | 5b6ac36436bbed6da7937f0fc83eb734ad929910 (patch) | |
tree | 5ce9eeecee708dbe5f45c4db35de8395d8746112 | |
parent | d16275ecf30c76a913b965a18eb69098f94a5008 (diff) |
notifications: drop httpclient dependency
3 files changed, 27 insertions, 52 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; 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. diff --git a/juick-server-web/build.gradle b/juick-server-web/build.gradle index 7c43d653..ffb6363c 100644 --- a/juick-server-web/build.gradle +++ b/juick-server-web/build.gradle @@ -21,7 +21,6 @@ dependencies { providedCompile "org.slf4j:log4j-over-slf4j:${rootProject.slf4jVersion}" providedCompile "org.slf4j:jul-to-slf4j:${rootProject.slf4jVersion}" - compile "org.apache.httpcomponents:httpclient:4.5.3" providedCompile "org.apache.commons:commons-lang3:3.6" providedCompile "org.apache.commons:commons-collections4:4.1" providedCompile "org.apache.commons:commons-text:1.1" |