示例#1
0
 /**
  * Matches the request's accept header againest supported mime types
  * and returns the supported type with highest priority found.
  *
  * @param Zend_Request_Abstract the request object
  *
  * @return string
  */
 private function _matchDocumentTypeRequest($request, array $supportedTypes = array())
 {
     return OntoWiki_Utils::matchMimetypeFromRequest($request, $supportedTypes);
 }
 /**
  * OntoWiki Sparql Endpoint
  *
  * Implements the SPARQL protocol according to {@link http://www.w3.org/TR/rdf-sparql-protocol/}.
  */
 public function sparqlAction()
 {
     // service controller needs no view renderer
     $this->_helper->viewRenderer->setNoRender();
     // disable layout for Ajax requests
     $this->_helper->layout()->disableLayout();
     $store = OntoWiki::getInstance()->erfurt->getStore();
     $response = $this->getResponse();
     // fetch params
     // TODO: support maxOccurs:unbound
     $queryString = $this->_request->getParam('query', '');
     if (get_magic_quotes_gpc()) {
         $queryString = stripslashes($queryString);
     }
     $defaultGraph = $this->_request->getParam('default-graph-uri', null);
     $namedGraph = $this->_request->getParam('named-graph-uri', null);
     if (!empty($queryString)) {
         $query = Erfurt_Sparql_SimpleQuery::initWithString($queryString);
         // overwrite query-specidfied dataset with protocoll-specified dataset
         if (null !== $defaultGraph) {
             $query->setFrom((array) $defaultGraph);
         }
         if (null !== $namedGraph) {
             $query->setFromNamed((array) $namedGraph);
         }
         // check graph availability
         $ac = Erfurt_App::getInstance()->getAc();
         foreach (array_merge($query->getFrom(), $query->getFromNamed()) as $graphUri) {
             if (!$ac->isModelAllowed('view', $graphUri)) {
                 if (Erfurt_App::getInstance()->getAuth()->getIdentity()->isAnonymousUser()) {
                     // In this case we allow the requesting party to authorize...
                     $response->setRawHeader('HTTP/1.1 401 Unauthorized')->setHeader('WWW-Authenticate', 'Basic realm="OntoWiki"')->setHttpResponseCode(401);
                     return;
                 } else {
                     $response->setRawHeader('HTTP/1.1 500 Internal Server Error')->setBody('QueryRequestRefused')->setHttpResponseCode(500);
                     return;
                 }
             }
         }
         $typeMapping = array('application/sparql-results+xml' => 'xml', 'application/json' => 'json', 'application/sparql-results+json' => 'json');
         try {
             $type = OntoWiki_Utils::matchMimetypeFromRequest($this->_request, array_keys($typeMapping));
         } catch (Exeption $e) {
             //
         }
         if (empty($type) && isset($this->_request->callback)) {
             // JSONp
             $type = 'application/sparql-results+json';
         } else {
             if (empty($type)) {
                 // default: XML
                 $type = 'application/sparql-results+xml';
             }
         }
         try {
             // get result for mimetype
             $result = $store->sparqlQuery($query, array('result_format' => $typeMapping[$type]));
         } catch (Exception $e) {
             $response->setRawHeader('HTTP/1.1 400 Bad Request')->setBody('MalformedQuery: ' . $e->getMessage())->setHttpResponseCode(400);
             return;
         }
         if (isset($this->_request->callback)) {
             // return jsonp
             $response->setHeader('Content-Type', 'application/javascript');
             $padding = $this->_request->getParam('callback', '');
             $response->setBody($padding . '(' . $result . ')');
         } else {
             // set header
             $response->setHeader('Content-Type', $type);
             // return normally
             $response->setBody($result);
         }
         $response->setHttpResponseCode(200);
         return;
     }
 }