@POST
  @RequireApplicationAccess
  @Consumes(MediaType.APPLICATION_JSON)
  public JSONWithPadding executePost(
      @Context UriInfo ui,
      EntityHolder<Object> body,
      @QueryParam("callback") @DefaultValue("callback") String callback)
      throws Exception {

    logger.debug("ServiceResource.executePost");

    Object json = body.getEntity();

    ApiResponse response = createApiResponse();

    response.setAction("post");
    response.setApplication(services.getApplication());
    response.setParams(ui.getQueryParameters());

    ServicePayload payload = getPayload(json);

    executeServiceRequest(ui, response, ServiceAction.POST, payload);

    return new JSONWithPadding(response, callback);
  }
  @PUT
  @Path("password")
  public JSONWithPadding setUserPasswordPut(
      @Context UriInfo ui,
      Map<String, Object> json,
      @QueryParam("callback") @DefaultValue("callback") String callback)
      throws Exception {

    if (json == null) {
      return null;
    }

    String oldPassword = string(json.get("oldpassword"));
    String newPassword = string(json.get("newpassword"));

    if (isServiceAdmin()) {
      management.setAdminUserPassword(user.getUuid(), newPassword);
    } else {
      management.setAdminUserPassword(user.getUuid(), oldPassword, newPassword);
    }

    ApiResponse response = createApiResponse();
    response.setAction("set user password");

    return new JSONWithPadding(response, callback);
  }
  @GET
  public JSONWithPadding getOrganizationDetails(
      @Context UriInfo ui, @QueryParam("callback") @DefaultValue("callback") String callback)
      throws Exception {

    logger.info("Get details for organization: " + organization.getUuid());

    ApiResponse response = createApiResponse();
    response.setProperty("organization", management.getOrganizationData(organization));

    return new JSONWithPadding(response, callback);
  }
  public ServiceResults executeServiceRequest(
      UriInfo ui, ApiResponse response, ServiceAction action, ServicePayload payload)
      throws Exception {

    logger.debug("ServiceResource.executeServiceRequest");

    boolean tree = "true".equalsIgnoreCase(ui.getQueryParameters().getFirst("tree"));
    boolean collectionGet = false;
    if (action == ServiceAction.GET) {
      collectionGet =
          (getServiceParameters().size() == 1
                  && InflectionUtils.isPlural(getServiceParameters().get(0)))
              ? true
              : false;
    }
    addQueryParams(getServiceParameters(), ui);
    ServiceRequest r = services.newRequest(action, tree, getServiceParameters(), payload);
    response.setServiceRequest(r);
    ServiceResults results = r.execute();
    if (results != null) {
      if (results.hasData()) {
        response.setData(results.getData());
      }
      if (results.getServiceMetadata() != null) {
        response.setMetadata(results.getServiceMetadata());
      }
      Query query = r.getLastQuery();
      if (query != null) {
        query = new Query(query);
        query.setIdsOnly(false);
        if (query.hasSelectSubjects()) {
          response.setList(query.getSelectionResults(results));
          response.setCount(response.getList().size());
          response.setNext(results.getNextResult());
          response.setPath(results.getPath());
          return results;
        }
      }
      if (collectionGet) {
        response.setCount(results.size());
      }

      response.setResults(results);
    }

    httpServletRequest.setAttribute("applicationId", services.getApplicationId());

    return results;
  }
  @GET
  @Path("reactivate")
  public JSONWithPadding reactivate(
      @Context UriInfo ui, @QueryParam("callback") @DefaultValue("callback") String callback)
      throws Exception {

    logger.info("Send activation email for organization: " + organization.getUuid());

    ApiResponse response = createApiResponse();

    management.startOrganizationActivationFlow(organization);

    response.setAction("reactivate organization");
    return new JSONWithPadding(response, callback);
  }
  @RequireOrganizationAccess
  @GET
  @Path("feed")
  public JSONWithPadding getFeed(
      @Context UriInfo ui, @QueryParam("callback") @DefaultValue("callback") String callback)
      throws Exception {

    ApiResponse response = createApiResponse();
    response.setAction("get organization feed");

    ServiceResults results = management.getOrganizationActivity(organization);
    response.setEntities(results.getEntities());
    response.setSuccess();

    return new JSONWithPadding(response, callback);
  }
  @DELETE
  @RequireApplicationAccess
  public JSONWithPadding executeDelete(
      @Context UriInfo ui, @QueryParam("callback") @DefaultValue("callback") String callback)
      throws Exception {

    logger.debug("ServiceResource.executeDelete");

    ApiResponse response = createApiResponse();
    response.setAction("delete");
    response.setApplication(services.getApplication());
    response.setParams(ui.getQueryParameters());

    executeServiceRequest(ui, response, ServiceAction.DELETE, null);

    return new JSONWithPadding(response, callback);
  }
  @POST
  @Path("revoketokens")
  public JSONWithPadding revokeTokensPost(
      @Context UriInfo ui, @QueryParam("callback") @DefaultValue("callback") String callback)
      throws Exception {

    UUID adminId = user.getUuid();

    logger.info("Revoking user tokens for {}", adminId);

    ApiResponse response = createApiResponse();

    management.revokeAccessTokensForAdminUser(adminId);

    response.setAction("revoked user tokens");
    return new JSONWithPadding(response, callback);
  }
  @RequireOrganizationAccess
  @POST
  @Path("credentials")
  public JSONWithPadding generateCredentials(
      @Context UriInfo ui, @QueryParam("callback") @DefaultValue("callback") String callback)
      throws Exception {

    ApiResponse response = createApiResponse();
    response.setAction("generate organization client credentials");

    ClientCredentialsInfo credentials =
        new ClientCredentialsInfo(
            management.getClientIdForOrganization(organization.getUuid()),
            management.newClientSecretForOrganization(organization.getUuid()));

    response.setCredentials(credentials);
    return new JSONWithPadding(response, callback);
  }
  @RequireAdminUserAccess
  @GET
  public JSONWithPadding getUserData(
      @Context UriInfo ui,
      @QueryParam("ttl") long ttl,
      @QueryParam("callback") @DefaultValue("callback") String callback)
      throws Exception {

    ApiResponse response = createApiResponse();
    response.setAction("get admin user");

    String token = management.getAccessTokenForAdminUser(SubjectUtils.getUser().getUuid(), ttl);
    Map<String, Object> userOrganizationData =
        management.getAdminUserOrganizationData(user.getUuid());
    userOrganizationData.put("token", token);
    response.setData(userOrganizationData);
    response.setSuccess();

    return new JSONWithPadding(response, callback);
  }
  @PUT
  public JSONWithPadding setUserInfo(
      @Context UriInfo ui,
      Map<String, Object> json,
      @QueryParam("callback") @DefaultValue("callback") String callback)
      throws Exception {

    if (json == null) {
      return null;
    }

    setUserPasswordPut(ui, json, callback);

    String email = string(json.get("email"));
    String username = string(json.get("username"));
    String name = string(json.get("name"));

    management.updateAdminUser(user, username, name, email);

    ApiResponse response = createApiResponse();
    response.setAction("update user info");

    return new JSONWithPadding(response, callback);
  }