public static boolean register(String username, String basicAuthenticationHeader) { try { BoundRequestBuilder reqBregister = asynSenderclient .preparePost( HttpResources.getBaseHost() + HttpResources.getRegisterResource(username)) .addHeader("Authorization", basicAuthenticationHeader); String registerNotification; // -- Sync response management, no other thing can be done before this step is completed Response response = reqBregister.execute().get(); if (!HttpErrorCodeEvaluator.isCorrectRegistrationOperation(response.getStatusCode())) { log.error("FAIL - Could not register " + response.getStatusCode()); return false; } else { registerNotification = requestRegisterNotification(username, basicAuthenticationHeader); if (registerNotification != null) { if (SimpleNotificationParser.extractRegistrationResult(registerNotification)) { log.info("OK - Registration requested successfully: " + response.getStatusCode()); return true; } } } } catch (Exception e) { log.error("FAIL - REGISTER ERROR: username="******", cause: " + e.getCause()); } return false; }
public static boolean unregister(String username, String basicAuthenticationHeader) { try { BoundRequestBuilder reqBunregister = asynSenderclient .prepareDelete( HttpResources.getBaseHost() + HttpResources.getRegisterResource(username)) .addHeader("Authorization", basicAuthenticationHeader); // -- Sync response management, no other thing can be done before this step is completed Response response = reqBunregister.execute().get(); if (!HttpErrorCodeEvaluator.isCorrectRegistrationOperation(response.getStatusCode())) { log.error("FAIL - Could not register " + response.getStatusCode()); return false; } else { // unregisterNotification = requestRegisterNotification(username, // basicAuthenticationHeader); if (ServiceExample .isUnregisterNotificationArrived()) { // You got the registration notification log.info("OK - Unregistered successfully: " + response.getStatusCode()); return true; } else { // The only possibility is that it was caught on the very last notification request log.info("OK - Waiting for the unregister notification to come..."); Thread.sleep(21000); return ServiceExample.isUnregisterNotificationArrived(); } } } catch (Exception e) { log.error("FAIL - UNREGISTER ERROR: username="******", cause: " + e.getCause()); } return false; }
public static boolean createChannelAndSubscriptions( String username, String basicAuthenticationHeader) { try { // Channel creation BoundRequestBuilder reqSubscribe = asynSenderclient .preparePost( HttpResources.getBaseHost() + HttpResources.getNotificationChannelResource(username)) .addHeader("Authorization", basicAuthenticationHeader); NotificationChannelSubscription ncs = new NotificationChannelSubscription("12345", "myService", 50, "LongPolling", 3600); reqSubscribe.setBody(ncs.getText()); // -- Sync response management, no other thing can be done before this step is completed Response response = reqSubscribe.execute().get(); notificationChannelURL = getChannelURL(response.getResponseBody()); if ((!HttpErrorCodeEvaluator.isChannelCorrectlyCreated(response.getStatusCode())) || (notificationChannelURL == null)) { log.error("FAIL - Channel not created " + response.getStatusCode()); return false; } else { log.info("OK - Channel correctly created, HTTP " + response.getStatusCode()); } NotificationSubscription ns = new NotificationSubscription( "sessionSubscription", "sessionSubscription", notificationChannelURL, 3600); // Subscription creation // -- Sync response management, no other thing can be done before this step is completed // Session subscription reqSubscribe = asynSenderclient .preparePost( HttpResources.getBaseHost() + HttpResources.getSubscriptionsResource(username, "register")) .addHeader("Authorization", basicAuthenticationHeader); reqSubscribe.setBody(ns.getText()); response = reqSubscribe.execute().get(); if (!HttpErrorCodeEvaluator.isSessionSubscribeCorrect(response.getStatusCode())) { log.error( "FAIL - Session notification subscription could not be created: " + response.getStatusCode()); return false; } else { log.info("OK - Session notification subscription created: " + response.getStatusCode()); } // Chat subscription ns = new NotificationSubscription( "chatNotificationSubscription", "chatSubscription1", notificationChannelURL, 3600); reqSubscribe = asynSenderclient .preparePost( HttpResources.getBaseHost() + HttpResources.getSubscriptionsResource(username, "chat")) .addHeader("Authorization", basicAuthenticationHeader); reqSubscribe.setBody(ns.getText()); response = reqSubscribe.execute().get(); if (!HttpErrorCodeEvaluator.isChatSubscribeCorrect(response.getStatusCode())) { log.error( "FAIL - Chat notification subscription could not be created: " + response.getStatusCode()); return false; } else { log.info("OK - Chat notification subscription created: " + response.getStatusCode()); } } catch (Exception e) { log.error( "FAIL - NOTIFICATION CHANNEL ERROR: username="******", cause: " + e.getCause()); return false; } return true; }