/**
   * Creates the underlying backend policies.
   *
   * <p>NOTE: if the creation of the underlying policies fails, any successfully created underlying
   * policies will be attempted to be deleted but if the deletion fails, then the underlying
   * policies may be in an inconsistent state.
   *
   * @param context The request context.
   * @param policies The underlying policies to create.
   * @return A promise containing the list of created underlying policies or a {@code
   *     ResourceException} if the creation fails.
   */
  public Promise<List<Resource>, ResourceException> createPolicies(
      ServerContext context, Set<JsonValue> policies) {

    final List<String> policyIds = new ArrayList<String>();
    List<Promise<Resource, ResourceException>> promises =
        new ArrayList<Promise<Resource, ResourceException>>();
    for (JsonValue policy : policies) {
      promises.add(
          policyResource
              .handleCreate(context, Requests.newCreateRequest("", policy))
              .thenOnResult(
                  new ResultHandler<Resource>() {
                    @Override
                    public void handleResult(Resource result) {
                      // Save ids of created policies, in case a latter policy fails to be created,
                      // so we can roll back.
                      policyIds.add(result.getId());
                    }
                  }));
    }
    return Promises.when(promises)
        .thenAsync(
            new AsyncFunction<List<Resource>, List<Resource>, ResourceException>() {
              @Override
              public Promise<List<Resource>, ResourceException> apply(List<Resource> value) {
                return Promises.newResultPromise(value);
              }
            },
            new UmaPolicyCreateFailureHandler(context, policyIds));
  }
 /**
  * Deletes the underlying backend policies.
  *
  * <p>NOTE: if the deletion of the underlying policies fails, the underlying policies may be in an
  * inconsistent state.
  *
  * @param context The request context.
  * @param policyIds The list of ids of the underlying backend policies to delete.
  * @return A promise containing the list of underlying policies that were deleted or a {@code
  *     ResourceException} if the policies were failed to be deleted.
  */
 public Promise<List<Resource>, ResourceException> deletePolicies(
     ServerContext context, Collection<String> policyIds) {
   List<Promise<Resource, ResourceException>> promises =
       new ArrayList<Promise<Resource, ResourceException>>();
   for (String policyId : policyIds) {
     promises.add(policyResource.handleDelete(context, Requests.newDeleteRequest(policyId)));
   }
   return Promises.when(promises);
 }
 /**
  * Updates the underlying backend policies.
  *
  * <p>NOTE: if the update of the underlying policies fails, the underlying policies may be in an
  * inconsistent state.
  *
  * @param context The request context.
  * @param policies The updated underlying policies to update.
  * @return A promise containing the list of updated underlying policies or a {@code
  *     ResourceException} if the update failed.
  */
 public Promise<List<Resource>, ResourceException> updatePolicies(
     ServerContext context, Set<JsonValue> policies) {
   List<Promise<Resource, ResourceException>> promises =
       new ArrayList<Promise<Resource, ResourceException>>();
   for (JsonValue policy : policies) {
     String policyName = policy.get("name").asString();
     promises.add(
         policyResource.handleUpdate(context, Requests.newUpdateRequest(policyName, policy)));
   }
   return Promises.when(promises);
 }
 /**
  * Queries the underlying backend policies.
  *
  * @param context The request context.
  * @param request The query request to execute on the backend policies.
  * @param resultHandler The handler for results.
  * @return A promise contain the {@code QueryResult} and list of underlying policies or a {@code
  *     ResourceException} if the query failed.
  */
 public Promise<QueryResult, ResourceException> queryPolicies(
     ServerContext context, QueryRequest request, QueryResultHandler resultHandler) {
   return policyResource.handleQuery(context, request, resultHandler);
 }
 /**
  * Queries the underlying backend policies.
  *
  * @param context The request context.
  * @param request The query request to execute on the backend policies.
  * @return A promise contain the {@code QueryResult} and list of underlying policies or a {@code
  *     ResourceException} if the query failed.
  */
 public Promise<Pair<QueryResult, List<Resource>>, ResourceException> queryPolicies(
     ServerContext context, QueryRequest request) {
   return policyResource.handleQuery(context, request);
 }