class RequestResourceProvider extends AbstractControllerResourceProvider {

  @Override
  public RequestStatus createResources(Request request)
      throws SystemException, UnsupportedPropertyException, NoSuchParentResourceException, ResourceAlreadyExistsException {
    if (request.getProperties().size() > 1) {
      throw new UnsupportedOperationException("Multiple actions/commands cannot be executed at the same time.");
    }
    final ExecuteActionRequest actionRequest = getActionRequest(request);
    final Map<String, String> requestInfoProperties = request.getRequestInfoProperties();

    return getRequestStatus(createResources(new Command<RequestStatusResponse>() {
      @Override
      public RequestStatusResponse invoke() throws AmbariException, AuthorizationException {

        String clusterName = actionRequest.getClusterName();

        ResourceType resourceType;
        Long resourceId;

        if (StringUtils.isEmpty(clusterName)) {
          resourceType = ResourceType.AMBARI;
          resourceId = null;
        } else {
          resourceType = ResourceType.CLUSTER;
          resourceId = getClusterResourceId(clusterName);
        }

        if (actionRequest.isCommand()) {
          String commandName = actionRequest.getCommandName();

          if (StringUtils.isEmpty(commandName)) {
            commandName = "_unknown_command_";
          }

          if (commandName.endsWith("_SERVICE_CHECK")) {
            if (!AuthorizationHelper.isAuthorized(resourceType, resourceId, RoleAuthorization.SERVICE_RUN_SERVICE_CHECK)) {
              throw new AuthorizationException("The authenticated user is not authorized to execute service checks.");
            }
          } else if (commandName.equals("DECOMMISSION")) {
            if (!AuthorizationHelper.isAuthorized(resourceType, resourceId, RoleAuthorization.SERVICE_DECOMMISSION_RECOMMISSION)) {
              throw new AuthorizationException("The authenticated user is not authorized to decommission services.");
            }
          } else {
            if (!AuthorizationHelper.isAuthorized(resourceType, resourceId, RoleAuthorization.SERVICE_RUN_CUSTOM_COMMAND)) {
              throw new AuthorizationException(String.format("The authenticated user is not authorized to execute the command, %s.",
                  commandName));
            }
          }
        } else {
          String actionName = actionRequest.getActionName();

          if (StringUtils.isEmpty(actionName)) {
            actionName = "_unknown_action_";
          }

          if (actionName.contains("SERVICE_CHECK")) {
            if (!AuthorizationHelper.isAuthorized(resourceType, resourceId, RoleAuthorization.SERVICE_RUN_SERVICE_CHECK)) {
              throw new AuthorizationException("The authenticated user is not authorized to execute service checks.");
            }
          } else {
            // A custom action has been requested
            ActionDefinition actionDefinition = (actionName == null)
                ? null
                : getManagementController().getAmbariMetaInfo().getActionDefinition(actionName);

            Set<RoleAuthorization> permissions = (actionDefinition == null)
                ? null
                : actionDefinition.getPermissions();

            // here goes ResourceType handling for some specific custom actions
            ResourceType customActionResourceType = resourceType;
            if (actionName.contains("check_host")) { // check_host custom action
              customActionResourceType = ResourceType.CLUSTER;
            }

            if (!AuthorizationHelper.isAuthorized(customActionResourceType, resourceId, permissions)) {
              throw new AuthorizationException(String.format("The authenticated user is not authorized to execute the action %s.", actionName));
            }
          }
        }

        return getManagementController().createAction(actionRequest, requestInfoProperties);
      }
    }));
  }

    RequestResourceProvider(AmbariManagementController managementController);

}