diff options
Diffstat (limited to 'juick-notifications/src')
4 files changed, 133 insertions, 4 deletions
diff --git a/juick-notifications/src/main/java/com/juick/components/CleanUp.java b/juick-notifications/src/main/java/com/juick/components/CleanUp.java new file mode 100644 index 00000000..06d96471 --- /dev/null +++ b/juick-notifications/src/main/java/com/juick/components/CleanUp.java @@ -0,0 +1,52 @@ +package com.juick.components; + +import com.juick.server.helpers.TokensList; +import com.notnoop.apns.ApnsService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +import javax.inject.Inject; +import java.util.ArrayList; +import java.util.Collection; + +/** + * Created by vitalyster on 22.11.2016. + */ +@Component +public class CleanUp { + + private static Logger logger = LoggerFactory.getLogger(CleanUp.class); + + @Inject + ApnsService apns; + @Inject + RestTemplate rest; + + @Scheduled(fixedRate = 600000) + public void cleanupTokens() { + logger.info("initializing apns tokens cleanup"); + Collection<String> devices = apns.getInactiveDevices().keySet(); + int count = devices.size(); + if (count > 0) { + logger.info(String.format("%d tokens to delete", count)); + TokensList list = new TokensList(); + list.setType("apns"); + list.setTokens(new ArrayList<>(devices)); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON_UTF8); + rest.exchange("http://api.juick.com/notifications", + HttpMethod.DELETE, new HttpEntity<>(list, headers), new ParameterizedTypeReference<Void>() { + }); + } else { + logger.info("No APNS tokens to delete"); + } + } +} 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 ebc69bd1..cb120ce9 100644 --- a/juick-notifications/src/main/java/com/juick/components/Notifications.java +++ b/juick-notifications/src/main/java/com/juick/components/Notifications.java @@ -48,6 +48,7 @@ import rocks.xmpp.core.session.XmppSessionConfiguration; import rocks.xmpp.extensions.component.accept.ExternalComponent; import javax.annotation.PostConstruct; +import javax.inject.Inject; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -70,10 +71,12 @@ public class Notifications implements AutoCloseable { private final int xmppPort; private final String xmppPushPassword; + @Inject + private ApnsService apns; + public Notifications(final Environment env, final RestTemplate rest) { this.rest = rest; - wns_application_sip = env.getProperty("wns_application_sip", ""); wns_client_secret = env.getProperty("wns_client_secret", ""); GCMSender = new Sender(env.getProperty("gcm_key", ""), Endpoint.GCM); @@ -191,12 +194,10 @@ public class Notifications implements AutoCloseable { }).getBody()); } if (!tokens.isEmpty()) { - ApnsService service = APNS.newService().withCert("/etc/juick/ios.p12", "juick") - .withSandboxDestination().build(); for (String token : tokens) { String payload = APNS.newPayload().alertTitle("@" + jmsg.getUser().getName()).alertBody(jmsg.getText()).build(); logger.info("APNS: " + token); - service.push(token, payload); + apns.push(token, payload); } } else { logger.info("APNS: no recipients"); diff --git a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java b/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java index c7747aa1..1bb4694a 100644 --- a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java +++ b/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java @@ -2,6 +2,8 @@ package com.juick.components.configuration; import com.juick.components.Notifications; import com.juick.configuration.DataConfiguration; +import com.notnoop.apns.APNS; +import com.notnoop.apns.ApnsService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -41,4 +43,9 @@ public class NotificationsAppConfiguration { public Notifications push() { return new Notifications(env, rest()); } + @Bean + public ApnsService apns() { + return APNS.newService().withCert(env.getProperty("ios_pkcs12_file"), env.getProperty("ios_pkcs12_password")) + .withSandboxDestination().build(); + } } 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 new file mode 100644 index 00000000..c4a4ed24 --- /dev/null +++ b/juick-notifications/src/test/java/com/juick/components/test/NotificationTests.java @@ -0,0 +1,69 @@ +package com.juick.components.test; + +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.LoggingEvent; +import ch.qos.logback.core.Appender; +import com.juick.components.CleanUp; +import com.notnoop.apns.ApnsService; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentMatcher; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.slf4j.LoggerFactory; +import org.springframework.web.client.RestTemplate; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import static org.mockito.Matchers.argThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Created by vitalyster on 22.11.2016. + */ +public class NotificationTests { + @InjectMocks + CleanUp cleanUp; + + @Mock + ApnsService service; + @Mock + RestTemplate rest; + + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void CleanUpTest() { + Map<String, Date> inactiveDevices = new HashMap<>(); + inactiveDevices.put("yoyoyo", new Date()); + when(service.getInactiveDevices()).thenReturn(inactiveDevices); + ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); + final Appender mockAppender = mock(Appender.class); + when(mockAppender.getName()).thenReturn("MOCK"); + root.addAppender(mockAppender); + cleanUp.cleanupTokens(); + verify(mockAppender).doAppend(argThat(new ArgumentMatcher() { + @Override + public boolean matches(final Object argument) { + return ((LoggingEvent)argument).getFormattedMessage().contains("1 tokens to delete"); + } + })); + when(service.getInactiveDevices()).thenReturn(new HashMap<>()); + cleanUp.cleanupTokens(); + verify(mockAppender).doAppend(argThat(new ArgumentMatcher() { + @Override + public boolean matches(final Object argument) { + return ((LoggingEvent)argument).getFormattedMessage().contains("No APNS tokens to delete"); + } + })); + } +} |