From b75b5a3d1a3bf001923d49923dcebee95ba2327e Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 29 Jun 2017 17:09:49 +0300 Subject: notifications and configurations refactoring --- .../java/com/juick/components/Notifications.java | 29 +++++++++++----------- .../configuration/NotificationsInitializer.java | 3 ++- .../NotificationsMvcConfiguration.java | 28 --------------------- .../service/NotificationsTokenService.java | 8 +++--- 4 files changed, 21 insertions(+), 47 deletions(-) delete mode 100644 juick-notifications/src/main/java/com/juick/components/configuration/NotificationsMvcConfiguration.java (limited to 'juick-notifications/src/main/java/com/juick/components') 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 0a315f76..f0ab3b81 100644 --- a/juick-notifications/src/main/java/com/juick/components/Notifications.java +++ b/juick-notifications/src/main/java/com/juick/components/Notifications.java @@ -19,7 +19,8 @@ package com.juick.components; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.android.gcm.server.*; -import com.juick.TokensList; +import com.juick.DeviceRegistration; +import com.juick.User; import com.notnoop.apns.APNS; import com.notnoop.apns.ApnsService; import org.apache.commons.lang3.math.NumberUtils; @@ -72,29 +73,29 @@ public class Notifications implements NotificationClientListener, AutoCloseable boolean isReply = jmsg.getRid() > 0; int pmTo = NumberUtils.toInt(msg.getTo().getLocal(), 0); - final List tokensList = new ArrayList<>(); + final List users = new ArrayList<>(); if (isPM) { - tokensList.addAll(rest.exchange(String.format("http://api.juick.com/notifications?type=gcm&uid=%d", + users.addAll(rest.exchange(String.format("http://api.juick.com/notifications?uid=%d", pmTo), - HttpMethod.GET, null, new ParameterizedTypeReference>() { + HttpMethod.GET, null, new ParameterizedTypeReference>() { }).getBody()); } else { if (isReply) { - tokensList.addAll(rest.exchange(String.format("http://api.juick.com/notifications?type=gcm&uid=%d&mid=%d&rid=%d", + users.addAll(rest.exchange(String.format("http://api.juick.com/notifications?uid=%d&mid=%d&rid=%d", jmsg.getUser().getUid(), jmsg.getMid(), jmsg.getRid()), - HttpMethod.GET, null, new ParameterizedTypeReference>() { + HttpMethod.GET, null, new ParameterizedTypeReference>() { }).getBody()); } else { - tokensList.addAll(rest.exchange(String.format("http://api.juick.com/notifications?type=gcm&uid=%s&mid=%s", + users.addAll(rest.exchange(String.format("http://api.juick.com/notifications?uid=%s&mid=%s", jmsg.getUser().getUid(), jmsg.getMid()), - HttpMethod.GET, null, new ParameterizedTypeReference>() { + HttpMethod.GET, null, new ParameterizedTypeReference>() { }).getBody()); } } // GCM - List regids = tokensList.stream().filter(t -> t.getType().equals("gcm")) - .flatMap(t -> t.getTokens().stream()).collect(Collectors.toList()); + List regids = users.stream().flatMap(u -> u.getDevices().stream()).filter(d -> d.getType().equals("gcm")) + .map(DeviceRegistration::getToken).collect(Collectors.toList()); if (!regids.isEmpty()) { try { String json = jsonMapper.writeValueAsString(jmsg); @@ -124,8 +125,8 @@ public class Notifications implements NotificationClientListener, AutoCloseable } /*** WinPhone ***/ - List urls = tokensList.stream().filter(t -> t.getType().equals("mpns")) - .flatMap(t -> t.getTokens().stream()).collect(Collectors.toList()); + List urls = users.stream().flatMap(u -> u.getDevices().stream()).filter(d -> d.getType().equals("mpns")) + .map(DeviceRegistration::getToken).collect(Collectors.toList()); if (urls.isEmpty()) { logger.info("WNS: no recipients"); @@ -160,8 +161,8 @@ public class Notifications implements NotificationClientListener, AutoCloseable } /*** iOS ***/ - List tokens = tokensList.stream().filter(t -> t.getType().equals("apns")) - .flatMap(t -> t.getTokens().stream()).collect(Collectors.toList()); + List tokens = users.stream().flatMap(u -> u.getDevices().stream()).filter(d -> d.getType().equals("apns")) + .map(DeviceRegistration::getToken).collect(Collectors.toList()); if (!tokens.isEmpty()) { for (String token : tokens) { String payload = APNS.newPayload().alertTitle("@" + jmsg.getUser().getName()).alertBody(jmsg.getText()).build(); diff --git a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsInitializer.java b/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsInitializer.java index 4249d988..8ecf72ea 100644 --- a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsInitializer.java +++ b/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsInitializer.java @@ -1,5 +1,6 @@ package com.juick.components.configuration; +import com.juick.server.configuration.BaseWebConfiguration; import org.apache.commons.codec.CharEncoding; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; @@ -21,7 +22,7 @@ public class NotificationsInitializer extends AbstractAnnotationConfigDispatcher @Override protected Class[] getServletConfigClasses() { - return new Class[]{NotificationsMvcConfiguration.class}; + return new Class[]{ BaseWebConfiguration.class }; } @Override diff --git a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsMvcConfiguration.java b/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsMvcConfiguration.java deleted file mode 100644 index 3d828832..00000000 --- a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsMvcConfiguration.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.juick.components.configuration; - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.converter.HttpMessageConverter; -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; - -import javax.inject.Inject; -import java.util.List; - -/** - * Created by vitalyster on 28.06.2016. - */ -@Configuration -@ComponentScan(basePackages = {"com.juick.components.controllers"}) -public class NotificationsMvcConfiguration extends WebMvcConfigurationSupport { - @Inject - ObjectMapper jsonMapper; - - @Override - protected void configureMessageConverters(List> converters) { - MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(jsonMapper); - converters.add(converter); - super.configureMessageConverters(converters); - } -} diff --git a/juick-notifications/src/main/java/com/juick/components/service/NotificationsTokenService.java b/juick-notifications/src/main/java/com/juick/components/service/NotificationsTokenService.java index 4dd949c3..7b9a88a3 100644 --- a/juick-notifications/src/main/java/com/juick/components/service/NotificationsTokenService.java +++ b/juick-notifications/src/main/java/com/juick/components/service/NotificationsTokenService.java @@ -1,6 +1,6 @@ package com.juick.components.service; -import com.juick.TokensList; +import com.juick.DeviceRegistration; import com.juick.service.BaseRestService; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpEntity; @@ -12,6 +12,7 @@ import org.springframework.web.client.RestTemplate; import javax.inject.Inject; import java.util.List; +import java.util.stream.Collectors; /** * Created by vitalyster on 15.12.2016. @@ -27,9 +28,8 @@ public class NotificationsTokenService extends BaseRestService implements TokenS @Override public void deleteTokens(String type, List devices) { if (devices.size() > 0) { - TokensList list = new TokensList(); - list.setType(type); - list.setTokens(devices); + List list = devices.stream() + .map(d -> new DeviceRegistration(type, d)).collect(Collectors.toList()); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON_UTF8); getRest().exchange("http://api.juick.com/notifications", -- cgit v1.2.3