Esempio n. 1
0
 public function head()
 {
     if (method_exists($this, 'get')) {
         call_user_func_array(array($this, 'get'), func_get_args());
     } else {
         Http::setHttpResponseCode(405);
     }
     return '';
 }
 public function get($resourceId = null)
 {
     // call the method that populate resultGraph
     $this->linkData();
     $hasNextPage = $this->detectIfHasNextPage();
     // add metadata and weblinks to single-page resource
     if ($this->context->isSinglePageResource()) {
         // provide web links as required by LDP specifications
         $this->weblinks[] = WebLink::factory('http://www.w3.org/ns/ldp-paging#Page')->rel('type');
         // LDP servers may provide a first page link when responding to requests with any single-page
         //  resource as the Request-URI.
         $this->weblinks[] = WebLink::factory($this->context->firstPageUri())->rel('first');
         // LDP servers may provide a last page link in responses to GET requests with any single-page
         //  resource as the Request-URI.
         // LDP servers must provide a next page link in responses to GET requests with any single-page
         //  resource other than the final page as the Request-URI. This is the mechanism by which clients c
         //  an discover the URL of the next page.
         // LDP servers must not provide a next page link in responses to GET requests with the final
         //  single-page resource as the Request-URI. This is the mechanism by which clients can discover
         //  the end of the page sequence as currently known by the server.
         if ($hasNextPage) {
             $this->weblinks[] = WebLink::factory($this->context->nextPageUri())->rel('next');
         }
         // LDP servers may provide a previous page link in responses to GET requests with any
         //  single-page resource other than the first page as the Request-URI.
         //  This is one mechanism by which clients can discover the URL of the previous page.
         // LDP servers must not provide a previous page link in responses to GET requests with the
         //  First single-page resource as the Request-URI. This is one mechanism by which clients
         //  can discover the beginning of the page sequence as currently known by the server.
         if ($this->context->getPageNum() > 0) {
             $this->weblinks[] = WebLink::factory($this->context->prevPageUri())->rel('prev');
         }
         // add metadata about linked Data Single page resource
         $metadata = $this->templateEngine->setTemplate('
             @base <{requestedUri}> .
             @prefix ldp-paging: <http://www.w3.org/ns/ldp-paging#> .
             <> a ldp-paging:Page; ldp-paging:pageOf <{pagedResourceUri}>.
         ')->render();
         $this->resultGraph->parse($metadata, 'turtle');
     } elseif ($hasNextPage) {
         // Server side initiated page management:
         // WARNING THI IMPLEMENTATION USES A LDP SPECIFICATIO FEATURE AT RISK:
         // LDP servers should respond with HTTP status code 333 (Returning Related) to successful
         //   GET requests with any paged resource as the Request-URI, although any appropriate code may be used.
         Http::setHttpResponseCode(333);
         header('Location: ' . $this->context->firstPageUri());
         // N.B.  333 code is not yet standarized..it is threathed as 302 by many agents
         // In that case you coud return withoud any other payload setup.
         // but if 333 will be standarized the payload is ready to serve first page
     }
     // add to result graph the metadata about Linked Data paged resource in first page
     // Note that this apply both for Single-Page resource and for Paged Resource
     if ($this->context->getPageNum() == 0) {
         $this->linkMetadata();
     }
     return $this->stateTransfer($this->resultGraph, $this->weblinks);
 }
Esempio n. 3
0
 /**
  * This routine drive http caching process
  * Not sure to have well understand caching :-(
  * 
  * Note that this routine is called by stateTransfer and may be called also from
  * End-Point routine after the controller exit.
  *  
  * @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
  */
 public static function processor($data, $sec = 0)
 {
     // ensure not negative caching age!
     if ($sec < 0) {
         $sec = 0;
     }
     $etag = Http::setETagHeader($data);
     $lastModifiedOn = Http::setLastModifiedHeader($data, new \DateTime());
     // this is the caching algorithm: only return data if necessary..
     if (self::isNotModified($etag, $lastModifiedOn)) {
         // Return http status 304 Not Modified and stop data flow processing
         Http::setHttpResponseCode(304);
         return '';
         // return empty data
     }
     // advertise caching policy
     self::setCacheControl($sec);
     return $data;
 }
Esempio n. 4
0
 /**
  * Redefine run taking into account mountend end points
  */
 public function run(Request $request = null)
 {
     $endPointName = $this->getEndPointName();
     $applicationPath = $this->getEndPointPath();
     $routerClass = $endPointName && isset($this->endPointCatalog[$endPointName]) ? $this->endPointCatalog[$endPointName] : '';
     $virtualhost = empty($endPointName) ? $applicationPath : $applicationPath . '/' . $endPointName;
     if (!$routerClass) {
         $result = parent::run($request);
         //fall back to local application routing
     } else {
         // now we test that $routerclass is a valid end_point
         $myClass = get_class();
         if ($routerClass == $myClass || is_subclass_of($routerClass, $myClass)) {
             // Create new end-point
             $endpoint = new $routerClass($virtualhost);
             $result = $endpoint->run();
         } else {
             throw new HttpErrorException(HttpProblem::factory(500, 'Invalid endpoint', $routerClass . ' end point class is not a subClass of ' . $myClass));
         }
     }
     //now prepare an error report for humans when something went wrong
     //Warning this trigger is called only in php >5.4. Otherwhise just empty content is printed
     //(but original status is preserved)
     if (empty($result) && ($errorCode = Http::getHttpResponseCode()) >= 400) {
         $result = ErrorManager::getInstance()->serializeHttpProblem(new HttpProblem($errorCode));
     }
     return $result;
 }
 public function render(Exception $e)
 {
     // build error model from Exception
     if ($e instanceof HttpErrorException) {
         $problem = $e->getHttpProblem();
     } else {
         // guess http status code
         $errorCode = $e->getCode();
         $isHttpErrorCode = $errorCode >= 400 && array_key_exists($errorCode, Http::$STATUS_CODES);
         $statusCode = $isHttpErrorCode ? $errorCode : 500;
         $phpErrorURI = 'http://php.net/manual/errorfunc.constants';
         $problem = new HttpProblem($statusCode, null, (string) $e, $isHttpErrorCode ? null : $phpErrorURI . '#' . $errorCode, $isHttpErrorCode ? null : $phpErrorURI);
     }
     // Set header and rendering payload
     Http::setHttpResponseCode($problem->httpStatus);
     return $this->serializeHttpProblem($problem);
 }
 /**
  * if $useCustomFields use an xml representation for fields.. Not yet standarized in html5
  * 
  * This render use html5 semantic tagging.
  */
 public static function htmlSerializer($data, $meta = null, $title = null, $header = null, $footer = null, $dataIsHtmlFragment = null)
 {
     //set defaults
     if (is_null($meta)) {
         $meta = array();
     }
     if (is_null($title)) {
         $title = is_object($data) ? get_class($data) : gettype($data);
     }
     if (is_null($header)) {
         $header = "<h1>{$title}</h1>";
     }
     if (is_null($footer)) {
         $footer = '';
     }
     if (is_null($dataIsHtmlFragment)) {
         $dataIsHtmlFragment = false;
     }
     // initilalize metadata
     $metadata = array();
     if (is_string($meta) && ($meta = trim($meta))) {
         $metadata[] = "<link rel='stylesheet' type='text/css' href='{$meta}'/>";
     } elseif (is_array($metadata)) {
         $metadata = $meta;
     }
     // prepare hyperlinks
     $links = Http::getResponseLinks();
     foreach ($links as $link) {
         $metadata[] = self::htmlWebLinkSerializer($link);
     }
     $navLinks = empty($links) ? '' : static::htmlWebLinks($links);
     // prepare head
     $head = implode("\n", $metadata);
     // Prepare data content
     $text = is_scalar($data) ? (string) $data : var_export($data, true);
     $htmlDataRepresentation = $dataIsHtmlFragment ? $text : htmlspecialchars($text);
     return is_null($meta) ? $htmlDataRepresentation : "<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset='utf-8'>\n        <title>{$title}</title>\n        {$head}\n    </head>\n    <body>\n        <header>\n            {$header}\n            <nav>\n               <div id=botkHyperlinks>\n                {$navLinks}\n               </div>\n            </nav>\n        </header>\n<!-- main tag contains a php html encoded representation of public data --> \n<!-- the class property contains the name of the data model from whom public data are extracted -->       \n{$htmlDataRepresentation}\n        <footer>\n            {$footer}\n        </footer>\n    </body>\n</html>\n";
 }