You are here

function restws_handle_request in RESTful Web Services 7.2

Same name and namespace in other branches
  1. 7 restws.module \restws_handle_request()

Handles a request.

Parameters

string $op: One of 'create', 'update', 'delete' or 'view'.

1 call to restws_handle_request()
restws_page_callback in ./restws.module
Menu page callback.

File

./restws.module, line 117
RESTful web services module.

Code

function restws_handle_request($op, $format, $resource_name, $id = NULL, $payload = NULL) {
  $message = $status_message = '';
  if ($resource = restws_resource_controller($resource_name)) {

    // Allow other modules to change the web service request or react upon it.
    $request = array(
      'op' => &$op,
      'format' => &$format,
      'resource' => &$resource,
      'id' => &$id,
      'payload' => &$payload,
    );
    drupal_alter('restws_request', $request);

    // Since there is no access callback for query we need to use view.
    $access_op = $op == 'query' ? 'view' : $op;
    if (user_access('access resource ' . $resource_name) && $resource
      ->access($access_op, $id)) {
      try {
        $method = $op . 'Resource';
        if ($op == 'create') {
          print $format
            ->{$method}($resource, $payload);
          $status_message = '201 Created';
        }
        elseif ($op == 'query') {
          if (!$resource instanceof RestWSQueryResourceControllerInterface) {
            throw new RestWSException('Querying not available for this resource', 501);
          }
          print $format
            ->{$method}($resource, $payload);
        }
        else {
          print $format
            ->{$method}($resource, $id, $payload);
        }
        drupal_add_http_header('Content-Type', $format
          ->mimeType());
      } catch (RestWSException $e) {
        $message = check_plain($e
          ->getHTTPError()) . ': ' . check_plain($e
          ->getMessage());
        $status_message = $e
          ->getHTTPError();
      }
    }
    else {
      $status_message = $message = '403 Forbidden';
      watchdog('access denied', check_plain($_GET['q']), NULL, WATCHDOG_WARNING);
    }
  }
  else {
    $status_message = $message = '404 Not Found';
  }
  restws_terminate_request($status_message, $message);
}