@Post
  public MembershipResponse createMembership(MembershipRequest memberShip) {
    if (authenticate() == false) return null;

    Group group = getGroupFromRequest();

    if (memberShip.getUserId() == null) {
      throw new ActivitiIllegalArgumentException("UserId cannot be null.");
    }

    // Check if user is member of group since API doesn't return typed exception
    if (ActivitiUtil.getIdentityService()
            .createUserQuery()
            .memberOfGroup(group.getId())
            .userId(memberShip.getUserId())
            .count()
        > 0) {
      throw new ResourceException(
          Status.CLIENT_ERROR_CONFLICT.getCode(),
          "User '" + memberShip.getUserId() + "' is already part of group '" + group.getId() + "'.",
          null,
          null);
    }

    ActivitiUtil.getIdentityService().createMembership(memberShip.getUserId(), group.getId());
    setStatus(Status.SUCCESS_CREATED);

    return getApplication(ActivitiRestServicesApplication.class)
        .getRestResponseFactory()
        .createMembershipResponse(this, memberShip.getUserId(), group.getId());
  }
  protected void setVariable(
      Task task, String name, Object value, RestVariableScope scope, boolean isNew) {
    // Create can only be done on new variables. Existing variables should be updated using PUT
    boolean hasVariable = hasVariableOnScope(task, name, scope);
    if (isNew && hasVariable) {
      throw new ResourceException(
          new Status(
              Status.CLIENT_ERROR_CONFLICT.getCode(),
              "Variable '" + name + "' is already present on task '" + task.getId() + "'.",
              null,
              null));
    }

    if (!isNew && !hasVariable) {
      throw new ResourceException(
          new Status(
              Status.CLIENT_ERROR_NOT_FOUND.getCode(),
              "Task '" + task.getId() + "' doesn't have a variable with name: '" + name + "'.",
              null,
              null));
    }

    if (scope == RestVariableScope.LOCAL) {
      ActivitiUtil.getTaskService().setVariableLocal(task.getId(), name, value);
    } else {
      if (task.getExecutionId() != null) {
        // Explicitly set on execution, setting non-local variable on task will override
        // local-variable if exists
        ActivitiUtil.getRuntimeService().setVariable(task.getExecutionId(), name, value);
      } else {
        // Standalone task, no global variables possible
        throw new ActivitiIllegalArgumentException(
            "Cannot set global variable '"
                + name
                + "' on task '"
                + task.getId()
                + "', task is not part of process.");
      }
    }
  }