protected static function getExceptionMessage($status, $message)
 {
     try {
         throw Rest_Exception::factory($status, $message);
     } catch (HTTP_Exception $e) {
         return $e->getMessage();
     }
 }
Beispiel #2
0
 public static function filter($route, $params, $request)
 {
     Rest_Config::product_id($params['product']);
     $version = strtoupper($params['version']);
     $resource = str_replace(' ', '_', ucwords(trim(str_replace(array('/', '_'), ' ', strtolower($params['resource'])))));
     $params['controller'] = 'Resources_' . $version . '_' . $resource;
     $params['action'] = strtolower($request->method());
     $methods = get_class_methods('Controller_' . $params['controller']);
     if (empty($methods)) {
         throw Rest_Exception::factory(404, 'resource_not_found');
     }
     return $params;
 }
Beispiel #3
0
 /**
  * Converts the sort parameter
  *
  * @param   string    $sort             sort parameter from request
  * @param   array     $sortable_fields  list of sortable parameters
  * @return  array     parsed
  */
 public function parse_sort($sort, $sortable_fields)
 {
     $result = array();
     $sort_fields = explode(',', trim($sort));
     foreach ($sort_fields as $sort) {
         $method = substr($sort, 0, 1);
         $field = substr($sort, 1);
         if (!in_array($method, array_keys($this->_sort_method))) {
             throw Rest_Exception::factory(400, 'resource_card_fund_transfer_sort_method_invalid');
         }
         if (!in_array($field, $sortable_fields)) {
             throw Rest_Exception::factory(400, 'resource_card_fund_transfer_sort_fields_invalid', array(':fields' => implode(',', $sortable_fields)));
         }
         array_push($result, array('field' => $field, 'method' => $this->_sort_method[$method]));
     }
     return $result;
 }
Beispiel #4
0
 public function encode($encoding)
 {
     if (empty($encoding)) {
         return false;
     }
     $status = Rest_Response_Format::parse_accept($encoding, $matches);
     if (!empty($status)) {
         foreach ($matches as $match) {
             $class = Rest_Response_Encoding::factory($match[1]);
             if (!empty($class)) {
                 // Perform only one type of encoding.
                 $this->_data = $class->encode($this->_data, Arr::get($match, 6, null));
                 return $class;
             }
         }
     }
     throw Rest_Exception::factory(406, 'request_header_accept_encoding_invalid', array(':content_encoding' => $encoding));
 }
Beispiel #5
0
 protected function _render_as_pages(&$data, $callback)
 {
     if (empty($callback)) {
         return false;
     }
     $url = parse_url($_SERVER['REQUEST_URI']);
     parse_str($url['query'], $query);
     $count = clone $data;
     $count = $this->measure_runtime(array($count, 'count_all'));
     $offset = Arr::get($query, 'offset', 0);
     $limit = Arr::get($query, 'limit', RESOURCE_QUERY_LIMIT_DEFAULT);
     if ($limit > RESOURCE_QUERY_LIMIT_MAX) {
         throw Rest_Exception::factory(400, 'resource_query_limit_exceeded', array(':max' => RESOURCE_QUERY_LIMIT_MAX));
     }
     $limit = $this->_get_query_option($data, 'limit', $limit);
     $offset = $this->_get_query_option($data, 'offset', $offset);
     $records = $this->measure_runtime('Rest_Response_Format::get_object_fields', $data, $callback);
     $class_name = get_class($this);
     $data = array(strtolower(substr($class_name, strrpos($class_name, '_') + 1)) => $records, 'count' => array('total' => $count), 'link' => $this->_get_page_links($count, $offset, $limit));
 }
Beispiel #6
0
 public function execute()
 {
     $namespace = 'action_';
     $time = microtime(true);
     $this->before();
     $action = $namespace . $this->request->action();
     if (!method_exists($this, $action)) {
         $methods = array(Controller_Resources::METHOD_GET, Controller_Resources::METHOD_POST, Controller_Resources::METHOD_PUT, Controller_Resources::METHOD_DELETE);
         for ($i = 0, $count = count($methods); $i < $count; $i++) {
             if (!method_exists($this, $namespace . strtolower($methods[$i]))) {
                 unset($methods[$i]);
             }
         }
         $methods = array_merge($methods);
         $exception = Rest_Exception::factory(405, 'method_not_supported', array(':method' => strtoupper($this->request->method())));
         throw $exception->headers('allow', implode(',', $methods));
     }
     $this->{$action}();
     $this->after();
     $time = (microtime(true) - $time) * 1000;
     ORM::factory('Rest_Metric')->millisec(get_class($this) . '/' . $this->request->method(), ceil($time));
     return $this->response;
 }
Beispiel #7
0
 /**
  * Exception handler, logs the exception and generates a Response object
  * for display.
  * To support ReSTful, exception messages are added to the standard http status response.
  *
  * @uses    Kohana_Exception::response
  * @param   Exception  $e
  * @return  void
  */
 public static function handler(Exception $e)
 {
     Rest_Exception::_handler($e);
     exit(1);
 }