aboutsummaryrefslogtreecommitdiff
path: root/juick-notifications/src/main/java/com/juick/components
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-12-16 13:25:45 +0300
committerGravatar Vitaly Takmazov2016-12-16 13:46:12 +0300
commit18a8595d2150edc92084c94b9dbb8f4309d3ed36 (patch)
tree4bfa27eb16d2f3147671bcad36a4236a02e54ecd /juick-notifications/src/main/java/com/juick/components
parenta9892924f06ae12eab99144defe10020f13653d2 (diff)
juick-notifications: MPNS cleanup
Diffstat (limited to 'juick-notifications/src/main/java/com/juick/components')
-rw-r--r--juick-notifications/src/main/java/com/juick/components/CleanUp.java11
-rw-r--r--juick-notifications/src/main/java/com/juick/components/Notifications.java48
2 files changed, 39 insertions, 20 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
index 7b800fec..9714e597 100644
--- a/juick-notifications/src/main/java/com/juick/components/CleanUp.java
+++ b/juick-notifications/src/main/java/com/juick/components/CleanUp.java
@@ -8,8 +8,8 @@ import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.inject.Inject;
-import java.util.ArrayList;
import java.util.Collection;
+import java.util.stream.Collectors;
/**
* Created by vitalyster on 22.11.2016.
@@ -34,12 +34,15 @@ public class CleanUp {
int count = devices.size();
if (count > 0) {
logger.info("{} tokens to delete", count);
- tokenService.deleteTokens("apns", new ArrayList<>(devices));
+ tokenService.deleteTokens("apns", devices.stream().collect(Collectors.toList()));
} else {
- logger.info("No APNS tokens to delete");
+ logger.debug("No APNS tokens to delete");
}
logger.debug("initializing GCM tokens cleanup: {} tokens", push.getInvalidGCMTokens().size());
- tokenService.deleteTokens("gcm", push.getInvalidGCMTokens());
+ tokenService.deleteTokens("gcm", push.getInvalidGCMTokens().stream().collect(Collectors.toList()));
push.cleanupGCMTokens();
+ logger.debug("initializing MPNS tokens cleanup: {} tokens", push.getInvalidMPNSTokens().size());
+ tokenService.deleteTokens("mpns", push.getInvalidMPNSTokens().stream().collect(Collectors.toList()));
+ push.cleanupMPNSTokens();
}
}
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 6de648cf..0a4d1292 100644
--- a/juick-notifications/src/main/java/com/juick/components/Notifications.java
+++ b/juick-notifications/src/main/java/com/juick/components/Notifications.java
@@ -26,10 +26,7 @@ import com.notnoop.apns.APNS;
import com.notnoop.apns.ApnsService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.math.NumberUtils;
-import org.apache.http.Consts;
-import org.apache.http.Header;
-import org.apache.http.HttpResponse;
-import org.apache.http.NameValuePair;
+import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
@@ -52,10 +49,7 @@ 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;
-import java.util.Collections;
-import java.util.List;
+import java.util.*;
import java.util.stream.Collectors;
/**
@@ -77,7 +71,8 @@ public class Notifications implements AutoCloseable {
private final ObjectMapper mapper;
- private final List<String> invalidGCMTokens;
+ private final Set<String> invalidGCMTokens;
+ private final Set<String> invalidMPNSTokens;
@Inject
private ApnsService apns;
@@ -96,7 +91,8 @@ public class Notifications implements AutoCloseable {
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
- invalidGCMTokens = Collections.synchronizedList(new ArrayList<>());
+ invalidGCMTokens = Collections.synchronizedSet(new HashSet<>());
+ invalidMPNSTokens = Collections.synchronizedSet(new HashSet<>());
}
@PostConstruct
@@ -263,7 +259,7 @@ public class Notifications implements AutoCloseable {
HttpResponse response = client.execute(httppost);
int statusCode = response.getStatusLine().getStatusCode();
String responseContent = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
- if (statusCode != 200) {
+ if (statusCode != HttpStatus.SC_OK) {
MPNSError error = mapper.readValue(responseContent, MPNSError.class);
throw new IOException(error.getError() + ": " + error.getErrorDescription());
}
@@ -284,9 +280,15 @@ public class Notifications implements AutoCloseable {
httpPost.setEntity(entity);
HttpResponse response = client.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
- if (statusCode != 200) {
- String headersContent = stringifyWnsHttpHeaders(response.getAllHeaders());
- throw new IOException(headersContent);
+ if (statusCode != HttpStatus.SC_OK) {
+ if (statusCode == HttpStatus.SC_GONE) {
+ // expired
+ logger.info("{} is scheduled to remove", url);
+ addInvalidMPNSToken(url);
+ } else {
+ String headersContent = stringifyWnsHttpHeaders(response.getAllHeaders());
+ throw new IOException(headersContent);
+ }
}
}
@@ -302,13 +304,27 @@ public class Notifications implements AutoCloseable {
invalidGCMTokens.add(token);
}
}
- public List<String> getInvalidGCMTokens() {
+ public Set<String> getInvalidGCMTokens() {
return invalidGCMTokens;
}
public void cleanupGCMTokens() {
- logger.info("removed {} tokens", invalidGCMTokens.size());
+ logger.info("removed {} GCM tokens", invalidGCMTokens.size());
synchronized (invalidGCMTokens) {
invalidGCMTokens.clear();
}
}
+ public void addInvalidMPNSToken(String token) {
+ synchronized (invalidMPNSTokens) {
+ invalidMPNSTokens.add(token);
+ }
+ }
+ public Set<String> getInvalidMPNSTokens() {
+ return invalidMPNSTokens;
+ }
+ public void cleanupMPNSTokens() {
+ logger.info("removed {} MPNS tokens", invalidMPNSTokens.size());
+ synchronized (invalidMPNSTokens) {
+ invalidMPNSTokens.clear();
+ }
+ }
}