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;
  }
  private static String requestRegisterNotification(
      String username, String basicAuthenticationHeader) {

    BoundRequestBuilder reqBregister =
        asynSenderclient
            .preparePost(
                HttpResources.getNotifierBaseHost()
                    + HttpResources.getNotificationResource(username))
            .addHeader("Authorization", basicAuthenticationHeader);
    // -- Sync response management, no other thing can be done before this step is completed
    try {

      Response response = reqBregister.execute().get();
      if (response.getStatusCode() != 200) {
        log.error(
            "FAIL - Bad response to the notification request: "
                + response.getStatusCode()
                + " checking if it already came...");
      } else {
        return response.getResponseBody();
      }

    } catch (Exception e) {
      log.error("FAIL - Could not request the registration notification...");
      e.printStackTrace();
    }
    return null;
  }
  public static void sendIMAutomaticSession(
      String username, String basicAuthenticationHeader, String destUri, String message) {

    try {

      BoundRequestBuilder reqBsender =
          asynSenderclient
              .preparePost(
                  HttpResources.getBaseHost() + HttpResources.getChatResource(username, destUri))
              .addHeader("Authorization", basicAuthenticationHeader)
              .addHeader("Content-Type", "application/json; charset=utf-8");

      ChatMessage cm = new ChatMessage();
      cm.setText(message);
      log.info("OK - Send message request:\n" + cm.toString());
      reqBsender.setBody(cm.toString().getBytes("utf-8")).setBodyEncoding("utf-8");

      // -- Async response management, once the message is sent you can keep working, no need to
      // wait for the response from the API
      reqBsender.execute(
          new AsyncCompletionHandler<Response>() {
            @Override
            public Response onCompleted(Response response) throws Exception {
              if (!HttpErrorCodeEvaluator.isChatMessageSent(response.getStatusCode())) {
                log.error("FAIL -	Could not send message: " + response.getStatusCode());
              } else {
                log.info("OK -	Message sent: " + response.getStatusCode());
              }
              return null;
            }

            @Override
            public void onThrowable(Throwable t) {
              log.error("FAIL - ERROR: run - onThrowable: " + t.getCause());
            }
          });

    } catch (Exception e) {
      log.error(
          "FAIL - SENDIMMESSAGE ERROR: username="******", destination uri="
              + destUri
              + ", message="
              + message
              + ", cause: "
              + e.getCause());
    }
  }
  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;
  }