/**
   * Updates a registration
   *
   * @param registration The registration to update
   * @return The updated registration
   * @throws Exception
   */
  private Registration upsertRegistrationInternal(Registration registration) throws Exception {
    Connection conn = new Connection(mConnectionString);

    String resource = registration.getURI();
    String content = registration.toXml();

    String response = conn.executeRequest(resource, content, XML_CONTENT_TYPE, "PUT");

    Registration result;
    if (PnsSpecificRegistrationFactory.getInstance().isTemplateRegistration(response)) {
      result =
          PnsSpecificRegistrationFactory.getInstance()
              .createTemplateRegistration(mNotificationHubPath);
    } else {
      result =
          PnsSpecificRegistrationFactory.getInstance()
              .createNativeRegistration(mNotificationHubPath);
    }

    result.loadXml(response, mNotificationHubPath);

    storeRegistrationId(result.getName(), result.getRegistrationId(), registration.getPNSHandle());

    return result;
  }