From 5b2bd7f928bbf9d3233ff029ed5c09ac46daf0de Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 28 Nov 2016 14:37:02 +0300 Subject: all components using jackson now, org.json serializer moved to compatibility tests package --- .../java/com/juick/components/Notifications.java | 28 ++++++++++++--------- .../java/com/juick/components/mpns/MPNSError.java | 29 ++++++++++++++++++++++ .../java/com/juick/components/mpns/MPNSToken.java | 29 ++++++++++++++++++++++ 3 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 juick-notifications/src/main/java/com/juick/components/mpns/MPNSError.java create mode 100644 juick-notifications/src/main/java/com/juick/components/mpns/MPNSToken.java (limited to 'juick-notifications/src') diff --git a/juick-notifications/src/main/java/com/juick/components/Notifications.java b/juick-notifications/src/main/java/com/juick/components/Notifications.java index cb120ce9..e0155a0a 100644 --- a/juick-notifications/src/main/java/com/juick/components/Notifications.java +++ b/juick-notifications/src/main/java/com/juick/components/Notifications.java @@ -17,8 +17,10 @@ */ package com.juick.components; +import com.fasterxml.jackson.databind.ObjectMapper; import com.google.android.gcm.server.*; -import com.juick.json.MessageSerializer; +import com.juick.components.mpns.MPNSError; +import com.juick.components.mpns.MPNSToken; import com.notnoop.apns.APNS; import com.notnoop.apns.ApnsService; import org.apache.commons.lang3.StringEscapeUtils; @@ -35,7 +37,6 @@ import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.apache.http.util.TextUtils; -import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.ParameterizedTypeReference; @@ -71,6 +72,8 @@ public class Notifications implements AutoCloseable { private final int xmppPort; private final String xmppPushPassword; + private final ObjectMapper mapper; + @Inject private ApnsService apns; @@ -84,6 +87,7 @@ public class Notifications implements AutoCloseable { xmppHost = env.getProperty("xmpp_host", "localhost"); xmppPort = NumberUtils.toInt(env.getProperty("xmpp_port"), 5347); xmppPushPassword = env.getProperty("push_xmpp_password", ""); + mapper = new ObjectMapper(); } @PostConstruct @@ -114,11 +118,11 @@ public class Notifications implements AutoCloseable { } if (!regids.isEmpty()) { - MessageSerializer messageSerializer = new MessageSerializer(); - String json = messageSerializer.serialize(jmsg).toString(); - logger.info(json); - Message message = new Message.Builder().addData("message", json).build(); try { + ObjectMapper messageSerializer = new ObjectMapper(); + String json = messageSerializer.writeValueAsString(jmsg); + logger.info(json); + Message message = new Message.Builder().addData("message", json).build(); MulticastResult result = GCMSender.send(message, regids, 3); List results = result.getResults(); for (int i = 0; i < results.size(); i++) { @@ -238,15 +242,15 @@ public class Notifications implements AutoCloseable { HttpResponse response = client.execute(httppost); int statusCode = response.getStatusLine().getStatusCode(); String responseContent = EntityUtils.toString(response.getEntity(), Consts.UTF_8); - JSONObject json = new JSONObject(responseContent); if (statusCode != 200) { - throw new IOException(json.opt("error") + ": " + json.opt("error_description")); + MPNSError error = mapper.readValue(responseContent, MPNSError.class); + throw new IOException(error.getError() + ": " + error.getErrorDescription()); } - String tokenType = (String) json.get("token_type"); - if (tokenType.length() >= 1) { - tokenType = Character.toUpperCase(tokenType.charAt(0)) + tokenType.substring(1); + MPNSToken token = mapper.readValue(responseContent, MPNSToken.class); + if (token.getTokenType().length() >= 1) { + token.setTokenType(Character.toUpperCase(token.getTokenType().charAt(0)) + token.getTokenType().substring(1)); } - return tokenType + " " + json.get("access_token"); + return token.getTokenType() + " " + token.getAccessToken(); } void sendWNS(final String wnsToken, final String url, final String xml) throws IOException { diff --git a/juick-notifications/src/main/java/com/juick/components/mpns/MPNSError.java b/juick-notifications/src/main/java/com/juick/components/mpns/MPNSError.java new file mode 100644 index 00000000..de27641e --- /dev/null +++ b/juick-notifications/src/main/java/com/juick/components/mpns/MPNSError.java @@ -0,0 +1,29 @@ +package com.juick.components.mpns; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Created by vitalyster on 28.11.2016. + */ +public class MPNSError { + private String error; + private String errorDescription; + + @JsonProperty("error") + public String getError() { + return error; + } + + public void setError(String error) { + this.error = error; + } + + @JsonProperty("error_description") + public String getErrorDescription() { + return errorDescription; + } + + public void setErrorDescription(String errorDescription) { + this.errorDescription = errorDescription; + } +} diff --git a/juick-notifications/src/main/java/com/juick/components/mpns/MPNSToken.java b/juick-notifications/src/main/java/com/juick/components/mpns/MPNSToken.java new file mode 100644 index 00000000..68baf9cd --- /dev/null +++ b/juick-notifications/src/main/java/com/juick/components/mpns/MPNSToken.java @@ -0,0 +1,29 @@ +package com.juick.components.mpns; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Created by vitalyster on 28.11.2016. + */ +public class MPNSToken { + private String tokenType; + private String accessToken; + + @JsonProperty("token_type") + public String getTokenType() { + return tokenType; + } + + public void setTokenType(String tokenType) { + this.tokenType = tokenType; + } + + @JsonProperty("access_token") + public String getAccessToken() { + return accessToken; + } + + public void setAccessToken(String accessToken) { + this.accessToken = accessToken; + } +} -- cgit v1.2.3