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 ++++++++++++---------- 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'juick-notifications/src/main/java/com/juick/components/Notifications.java') 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 { -- cgit v1.2.3