/** * Set an error result to the MvcEvent and return the result. * * @param MvcEvent $event * @param Exception $error */ protected function getErrorResult(MvcEvent $event, \Exception $error) { $response = new Response(); $response->setStatus(Response::ERROR); $result = new ApiJsonModel($response, $this->getViewOptions()); $result->setException($error); $event->setResult($result); return $result; }
/** * Execute an API request. * * @param Request $request * @return Response */ public function execute(Request $request) { try { $t = $this->getTranslator(); // Validate the request. if (!$request->isValidOperation($request->getOperation())) { throw new Exception\BadRequestException(sprintf($t->translate('The API does not support the "%1$s" request operation.'), $request->getOperation())); } if (!is_array($request->getContent())) { throw new Exception\BadRequestException(sprintf($t->translate('The API request content must be a JSON object (for HTTP) or PHP array. "%1$s" given.'), gettype($request->getContent()))); } // Get the adapter. try { $adapter = $this->getServiceLocator()->get('Omeka\\ApiAdapterManager')->get($request->getResource()); } catch (ServiceNotFoundException $e) { throw new Exception\BadRequestException(sprintf($t->translate('The API does not support the "%1$s" resource.'), $request->getResource())); } // Verify that the current user has general access to this resource. $acl = $this->getServiceLocator()->get('Omeka\\Acl'); if (!$acl->userIsAllowed($adapter, $request->getOperation())) { throw new Exception\PermissionDeniedException(sprintf($t->translate('Permission denied for the current user to %1$s the %2$s resource.'), $request->getOperation(), $adapter->getResourceId())); } // Trigger the api.execute.pre event. $event = new Event(Event::API_EXECUTE_PRE, $adapter, ['services' => $this->getServiceLocator(), 'request' => $request]); $adapter->getEventManager()->trigger($event); // Trigger the api.{operation}.pre event. $event = new Event('api.' . $request->getOperation() . '.pre', $adapter, ['services' => $this->getServiceLocator(), 'request' => $request]); $adapter->getEventManager()->trigger($event); switch ($request->getOperation()) { case Request::SEARCH: $response = $adapter->search($request); break; case Request::CREATE: $response = $adapter->create($request); break; case Request::BATCH_CREATE: $response = $this->executeBatchCreate($request, $adapter); break; case Request::READ: $response = $adapter->read($request); break; case Request::UPDATE: $response = $adapter->update($request); break; case Request::DELETE: $response = $adapter->delete($request); break; default: throw new Exception\BadRequestException(sprintf($t->translate('The API does not support the "%1$s" request operation.'), $request->getOperation())); } // Validate the response. if (!$response instanceof Response) { throw new Exception\BadResponseException(sprintf($t->translate('The "%1$s" operation for the "%2$s" adapter did not return a valid response.'), $request->getOperation(), $request->getResource())); } if (!$response->isValidStatus($response->getStatus())) { throw new Exception\BadResponseException(sprintf($t->translate('The "%1$s" operation for the "%2$s" adapter did not return a valid response status.'), $request->getOperation(), $request->getResource())); } if (!$this->isValidResponseContent($response)) { throw new Exception\BadResponseException(sprintf($t->translate('The "%1$s" operation for the "%2$s" adapter did not return valid response content.'), $request->getOperation(), $request->getResource())); } // Trigger the api.{operation}.post event. $event = new Event('api.' . $request->getOperation() . '.post', $adapter, ['services' => $this->getServiceLocator(), 'request' => $request, 'response' => $response]); $adapter->getEventManager()->trigger($event); // Trigger the api.execute.post event. $event = new Event(Event::API_EXECUTE_POST, $adapter, ['services' => $this->getServiceLocator(), 'request' => $request, 'response' => $response]); $adapter->getEventManager()->trigger($event); } catch (Exception\ValidationException $e) { $this->getServiceLocator()->get('Omeka\\Logger')->err((string) $e); $response = new Response(); $response->setStatus(Response::ERROR_VALIDATION); $response->mergeErrors($e->getErrorStore()); } $response->setRequest($request); return $response; }