private String getProviderId(EntityRef device, Notifier notifier) throws Exception { try { Object value = em.getProperty(device, notifier.getName() + NOTIFIER_ID_POSTFIX); if (value == null) { value = em.getProperty(device, notifier.getUuid() + NOTIFIER_ID_POSTFIX); } return value != null ? value.toString() : null; } catch (Exception e) { logger.error("Error getting notifier for device {}, proceeding with rest of batch", device, e); return null; } }
@JsonIgnore public InputStream getP12CertificateStream() { byte[] cert = getP12Certificate(); return cert != null ? new ByteArrayInputStream(cert) : null; }
public static ProviderAdapter getProviderAdapter(Notifier notifier, EntityManager entityManager){ ProviderAdapter adapter = null; switch(notifier.getProvider().toLowerCase()){ case "apple" : adapter = new APNsAdapter(entityManager,notifier); break; case "google" : adapter = new GCMAdapter(entityManager ,notifier); break; case "windows" : adapter = new WNSAdapter(entityManager ,notifier); break; case "noop" : adapter = new TestAdapter(notifier); break; default: throw new IllegalArgumentException(notifier.getProvider() + " did not match any known adapter, valid arguments are apple,google,windows" //ignore noop its internal ); } return adapter; }
public WNSAdapter(EntityManager entityManager, Notifier notifier) { this.entityManager = entityManager; this.notifier = notifier; this.service = new WnsService(notifier.getSid(), notifier.getApiKey(), notifier.getLogging()); }
private static SSLContext getSSLContext(Notifier notifier) { try { KeyStore keyStore = KeyStore.getInstance("PKCS12"); String password = notifier.getCertificatePassword(); char[] passChars =(password != null ? password : "").toCharArray(); InputStream stream = notifier.getP12CertificateStream(); keyStore.load(stream,passChars); SSLContext context = SSLContextUtil.createDefaultSSLContext(keyStore, passChars); return context; }catch (Exception e){ throw new RuntimeException("Error getting certificate",e); } } }
@Test public void twoBatchNotification() throws Exception { app.clear(); String payload = "Hello, World!"; Map<String, String> payloads = new HashMap<String, String>(1); payloads.put(notifier.getUuid().toString(), payload); app.put("payloads", payloads); app.put("queued", System.currentTimeMillis()); app.put("debug", true); Entity e = app.testRequest(ServiceAction.POST, 1, "devices", "notifications").getEntity(); app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid()); Notification notification = app.getEntityManager().get(e.getUuid(), Notification.class); assertEquals( notification.getPayloads().get(notifier.getUuid().toString()), payload); // reduce Batch size to 1 Field field = GCMAdapter.class.getDeclaredField("BATCH_SIZE"); field.setAccessible(true); int multicastSize = field.getInt(GCMAdapter.class); try { field.setInt(GCMAdapter.class, 1); // perform push // notification = notificationWaitForComplete(notification); checkReceipts(notification, 2); } finally { field.setInt(GCMAdapter.class, multicastSize); } }
assertEquals(notifier2.getName(), notifierName1); assertEquals(notifier2.getProvider(), PROVIDER); assertEquals(notifier2.getEnvironment(), environment1); String key2 = notifier2.getName() + NOTIFIER_ID_POSTFIX; app.put(key2, PUSH_TOKEN); app.testRequest(ServiceAction.PUT, 1, "devices", device1).getEntity();
@Ignore("Pending https://issues.apache.org/jira/browse/USERGRID-1113. Mock doesn't work") @Test public void goodAPNsCreation() throws Exception { app.clear(); app.put("provider", "apple"); app.put("environment", "development"); InputStream fis = getClass().getClassLoader().getResourceAsStream( "pushtest_dev_recent.p12"); byte[] certBytes = IOUtils.toByteArray(fis); app.put("p12Certificate", certBytes); fis.close(); Notifier notifier = (Notifier) app .testRequest(ServiceAction.POST, 1, false, new Object[] { "notifiers" }).getEntity() .toTypedEntity(); assertEquals(app.get("provider"), notifier.getProvider()); assertEquals(app.get("environment"), notifier.getEnvironment()); assertArrayEquals(notifier.getP12Certificate(), certBytes); }
public EntityPushManager( Notifier notifier, EntityManager entityManager, BlockingQueue<SimpleApnsPushNotification> queue, PushManagerConfiguration configuration) { super(getApnsEnvironment(notifier), getSSLContext(notifier), null, null, queue, configuration, notifier.getName()); this.notifier = notifier; this.entityManager = entityManager; }
@Test public void windows() throws Exception { EntityManager em = setup.getEmf().getEntityManager(app.getId()); Notifier notifier = new Notifier(); notifier.setLogging(true); notifier.setSid("ms-app://s-1-15-2-2411381248-444863693-3819932088-4077691928-1194867744-112853457-373132695"); notifier.setApiKey("QAHlxzMXhg5eP5q9bf/W3komtUXegf7B"); WNSAdapter adapter = new WNSAdapter(em, notifier); adapter.testConnection(); } }
.toTypedEntity(); assertArrayEquals(notifier.getP12Certificate(), certBytes); app.getEntityManager().get(notifier.getUuid()); fail("Should have failed to retrieve the encrypted entity."); } catch (IllegalStateException e) {
@Override public void testConnection() throws Exception { Sender sender = new Sender(notifier.getApiKey()); Message message = new Message.Builder().addData("registration_id", "").build(); List<String> ids = new ArrayList<>(); ids.add("device_token"); try { MulticastResult result = sender.send(message, ids, 1); if (logger.isTraceEnabled()) { logger.trace("testConnection result: {}", result); } } catch (InvalidRequestException e){ // do nothing, we don't have a valid device token to test with if (logger.isTraceEnabled()) { logger.trace("no valid device token"); } } catch (IOException e) { if(isInvalidRequestException(e)){ throw new InvalidRequestException(401, Constants.ERROR_INVALID_REGISTRATION); }else { throw new ConnectionException(e.getMessage(), e); } } }
@Test public void singlePushNotificationMultipleDevices() throws Exception { app.clear(); String payload = "Hello, World!"; Map<String, String> payloads = new HashMap<String, String>(1); payloads.put(notifier.getUuid().toString(), payload); app.put("payloads", payloads); app.put("queued", System.currentTimeMillis()); app.put("debug", true); app.put("expire", System.currentTimeMillis() + 300000); // add 5 minutes to current time Entity e = app.testRequest(ServiceAction.POST, 1, "devices", "*", "notifications").getEntity(); app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid()); Notification notification = app.getEntityManager().get(e.getUuid(), Notification.class); assertEquals( notification.getPayloads().get(notifier.getUuid().toString()), payload); // perform push // notification = notificationWaitForComplete(notification); checkReceipts(notification, 2); }
assertEquals(notifier2.getName(), "apNs2"); assertEquals(notifier2.getProvider(), PROVIDER); assertEquals(notifier2.getEnvironment(), "development");
String payload = getPayload(); Map<String, String> payloads = new HashMap<String, String>(1); payloads.put(notifier.getName(), payload); app.put("payloads", payloads); app.put("deliver", System.currentTimeMillis() - 1000); // some time in the past payload = getPayload(); payloads = new HashMap<String, String>(1); payloads.put(notifier.getName(), payload); app.put("payloads", payloads); app.put("deliver", "notatime"); // some time in the past
if (ids.size() == 0) return; Sender sender = new Sender(notifier.getApiKey()); Message.Builder builder = new Message.Builder(); if(payload.containsKey(ttlKey)){
private void replaceProviderId(EntityRef device, Notifier notifier, String newProviderId) throws Exception { Object value = em.getProperty(device, notifier.getName() + ApplicationQueueManager.NOTIFIER_ID_POSTFIX); if (value != null) { em.setProperty(device, notifier.getName() + ApplicationQueueManager.NOTIFIER_ID_POSTFIX, newProviderId); } else { value = em.getProperty(device, notifier.getUuid() + ApplicationQueueManager.NOTIFIER_ID_POSTFIX); if (value != null) { em.setProperty(device, notifier.getUuid() + ApplicationQueueManager.NOTIFIER_ID_POSTFIX, newProviderId); } } }
@Test public void singlePushNotificationWithMapPayload() throws Exception { app.clear(); String payload = "{\"message\":\"Hello, World!\", \"campaign\":\"Hello Campaign\"}"; Map<String, String> payloads = new HashMap<String, String>(1); payloads.put(notifier.getUuid().toString(), payload); app.put("payloads", payloads); app.put("queued", System.currentTimeMillis()); app.put("debug", true); app.put("expire", System.currentTimeMillis() + 300000); // add 5 minutes to current time Entity e = app.testRequest(ServiceAction.POST, 1, "devices", device1.getUuid(), "notifications").getEntity(); app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid()); Notification notification = app.getEntityManager().get(e.getUuid(), Notification.class); assertEquals( notification.getPayloads().get(notifier.getUuid().toString()), payload); // perform push // notification = notificationWaitForComplete(notification); checkReceipts(notification, 1); }
@Test public void singlePushNotification() throws Exception { app.clear(); String payload = getPayload(); Map<String, String> payloads = new HashMap<String, String>(1); payloads.put(notifier.getName(), payload); app.put("payloads", payloads); app.put("queued", System.currentTimeMillis()); app.put("debug",true); app.put("expire", System.currentTimeMillis() + 300000); // add 5 minutes to current time // post notification to service manager Entity e = app.testRequest(ServiceAction.POST, 1,"devices",device1.getUuid(), "notifications").getEntity(); app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid()); // ensure notification has expected name Notification notification = app.getEntityManager().get(e.getUuid(), Notification.class); assertEquals( notification.getPayloads().get(notifier.getName().toString()), payload); // perform push // notification = notificationWaitForComplete(notification); checkReceipts(notification, 1); }
public void removeInactiveDevices( Map<String,Date> inactiveDeviceMap ){ final String notfierPostFix = ApplicationQueueManagerImpl.NOTIFIER_ID_POSTFIX; if (inactiveDeviceMap != null && inactiveDeviceMap.size() > 0) { if (logger.isDebugEnabled()) { logger.debug("processing {} inactive devices", inactiveDeviceMap.size()); } Map<String, Object> clearPushtokenMap = new HashMap<String, Object>( 2); clearPushtokenMap.put(notifier.getName() + notfierPostFix, ""); clearPushtokenMap.put(notifier.getUuid() + notfierPostFix, ""); // todo: this could be done in a single query for (Map.Entry<String, Date> entry : inactiveDeviceMap.entrySet()) { try { // name Query query = Query.fromQL( notifier.getName() + notfierPostFix + " = '" + entry.getKey() + "'"); Results results = entityManager.searchCollection(entityManager.getApplication(), "devices", query); for (Entity e : results.getEntities()) { entityManager.updateProperties(e, clearPushtokenMap); } }catch (Exception e){ logger.error("failed to remove token",e); } } } } }