You are here

function restws_handle_request in RESTful Web Services 7

Same name and namespace in other branches
  1. 7.2 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 111
RESTful web services module.

Code

function restws_handle_request($op, $format, $resource_name, $id = NULL, $payload = NULL) {
  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);
    if (user_access('access resource ' . $resource_name) && $resource
      ->access($op, $id)) {
      try {
        $method = $op . 'Resource';
        if ($op == 'create') {
          print $format
            ->{$method}($resource, $payload);
          drupal_add_http_header('Status', '201 Created');
        }
        else {
          print $format
            ->{$method}($resource, $id, $payload);
        }
        drupal_add_http_header('Content-Type', $format
          ->mimeType());
      } catch (RestWSException $e) {
        echo check_plain($e
          ->getHTTPError()) . ': ' . check_plain($e
          ->getMessage());
        drupal_add_http_header('Status', $e
          ->getHTTPError());
      }
    }
    else {
      echo '403 Forbidden';
      drupal_add_http_header('Status', '403 Forbidden');
      watchdog('access denied', check_plain($_GET['q']), NULL, WATCHDOG_WARNING);
    }
  }
  else {
    echo '404 Not Found';
    drupal_add_http_header('Status', '404 Not Found');
  }
  drupal_page_footer();
  exit;
}