Example #1
0
 public function invalidrestresourceAction()
 {
     $this->_helper->layout->disableLayout();
     $this->_helper->viewRenderer->setNoRender();
     $entry = RestAPIHelper::wrapResponse("", null, null, -1, null, null, RestErrorEnum::RE_INVALID_RESOURCE, null);
     $this->getResponse()->clearAllHeaders();
     header('Content-type: text/xml');
     header("HTTP/1.0 400 Bad Response");
     header("Status: 400 Bad Response");
     header("X-AppDB-REST-Resource-Invalid: 1");
     echo $entry;
 }
Example #2
0
 public function get()
 {
     if (parent::get() !== false) {
         $s = file_get_contents(APPLICATION_PATH . "/apiroutes.xml");
         $s = str_replace("/:version/", "/" . RestAPIHelper::VERSION . "/", $s);
         $s = str_replace("\t", "", $s);
         $s = preg_replace('/>\\s+/', '>', $s);
         $s = preg_replace('/<\\s+/', '<', $s);
         $xslt = RestAPIHelper::getFolder(RestFolderEnum::FE_XSL_FOLDER) . "/apiroutes.xsl";
         $xsl = new DOMDocument();
         $xsl->load($xslt);
         $xml = new DOMDocument();
         $xml->loadXML($s, LIBXML_NSCLEAN | LIBXML_COMPACT);
         $proc = new XSLTProcessor();
         $proc->registerPHPFunctions();
         $proc->importStylesheet($xsl);
         $s = $proc->transformToXml($xml);
         $xml = new SimpleXMLElement($s);
         $xml->registerXPathNamespace('appdb', 'http://appdb.egi.eu/api/' . RestAPIHelper::VERSION . '/appdb');
         $x = $xml->xpath("//appdb:resource");
         $s = array();
         while (list(, $node) = each($x)) {
             $s[] = trim($node->asXML());
         }
         $this->_pageOffset = 0;
         $this->_pageLength = count($s);
         $this->_total = count($s);
         $s = RestAPIHelper::wrapResponse($s, "resource", "list", count($s));
         return new XMLRestResponse($s, $this);
     } else {
         return false;
     }
 }
Example #3
0
 public function finalize()
 {
     return RestAPIHelper::wrapResponse($this);
 }
Example #4
0
 private function putpost($method)
 {
     if ($this->_res === null) {
         return false;
     }
     $data = $this->getData();
     if ($method === RestMethodEnum::RM_POST) {
         try {
             $xml = new SimpleXMLElement($data);
         } catch (Exception $e) {
             $this->setError(RestErrorEnum::RE_INVALID_REPRESENTATION);
             return false;
         }
         $xp = $xml->xpath("publication:publication");
         if (count($xp) > 0) {
             $id = strval($xp[0]->attributes()->id);
         }
         if ($id == '') {
             $this->setError(RestErrorEnum::RE_INVALID_REPRESENTATION);
             return false;
         }
         try {
             $xml = new DOMDocument();
             $xml->loadXML(strval(RestAPIHelper::wrapResponse(strval($this->get()))));
             $xpath = new DOMXPath($xml);
             $xpres = $xpath->query('//publication:publication[@id="' . $id . '"]');
         } catch (Exception $e) {
             $this->setError(RestErrorEnum::RE_INVALID_REPRESENTATION);
             return false;
         }
         if ($xpres !== false) {
             $xnode = $xpres->item(0);
             $xparent = $xnode->parentNode;
             $xparent->removeChild($xpres->item(0));
             try {
                 $xml = new SimpleXMLElement($xml->saveXML());
             } catch (Exception $e) {
                 $this->setError(RestErrorEnum::RE_INVALID_REPRESENTATION);
                 return false;
             }
         } else {
             $this->setError(RestErrorEnum::RE_ITEM_NOT_FOUND);
             return false;
         }
     } else {
         try {
             $xml = new SimpleXMLElement(strval(RestAPIHelper::wrapResponse($this->get())));
         } catch (Exception $e) {
             $this->setError(RestErrorEnum::RE_INVALID_REPRESENTATION);
             return false;
         }
     }
     try {
         $data = new SimpleXMLElement($data);
         $data = $data->xpath("//publication:publication");
     } catch (Exception $e) {
         $this->setError(RestErrorEnum::RE_INVALID_REPRESENTATION);
         return false;
     }
     if (count($data) > 0) {
         $data = $data[0]->asXML();
     } else {
         $this->setError(RestErrorEnum::RE_INVALID_REPRESENTATION);
         return false;
     }
     $data = array($data);
     $xp = $xml->xpath("publication:publication");
     foreach ($xp as $x) {
         $data[] = $x->asXML();
     }
     $data = '<application:application id="' . $this->_res->id . '">' . implode($data) . '</application:application>';
     $data = strval(RestAPIHelper::wrapResponse($data));
     $this->_pars['data'] = $data;
     $res = new RestAppList($this->_pars);
     $ret = $res->post();
     $ret = new SimpleXMLElement(strval($ret->finalize()));
     if ($method === RestMethodEnum::RM_POST) {
         $xp = $ret->xpath('//publication:publication[@id="' . $id . '"]');
     } else {
         // try to find the publication with the max id attribute, which should be the newest
         $xp = $ret->xpath('//publication:publication[not(@id <= preceding-sibling::publication:publication/@id) and not(@id <= following-sibling::publication:publication/@id)]');
         // if this fails, try to find the last publication, which still should be the newest,
         // since results are returned in DB (natural) order
         if (count($xp) == 0 || !is_array($xp)) {
             $xp = $ret->xpath('//publication:publication[last()]');
         }
     }
     $ret = array();
     foreach ($xp as $x) {
         $ret[] = $x->asXML();
     }
     return new XMLFragmentRestResponse($ret);
 }
Example #5
0
 private function handleResource($res)
 {
     $ok = true;
     if (class_exists($res)) {
         $r = new $res($this->pars);
         $r->startLogging(APPLICATION_PATH . '/appdbapilog.xml');
     } else {
         $ok = false;
     }
     if ($ok) {
         $method = strtolower($_SERVER['REQUEST_METHOD']);
         if (method_exists($r, $method)) {
             $this->entry = $r->{$method}();
         } else {
             $this->entry = $r->unknown();
         }
         if ($this->entry === false) {
             $this->entry = new XMLFragmentRestResponse("", $r);
         }
         $this->Error = RestErrorEnum::toString($r->error);
         if ($r->error != "" && $r->extError != "") {
             $this->Error = $this->Error . ". " . $r->extError;
         }
         $this->total = $r->total;
         $this->dataType = $r->dataType;
         $this->length = $r->pageLength;
         $this->offset = $r->pageOffset;
         $this->authenticated = $r->authenticate();
     } else {
         $this->Error = RestErrorEnum::toString(RestErrorEnum::RE_INVALID_RESOURCE);
         $this->total = -1;
         $this->entry = RestAPIHelper::wrapResponse("", null, null, $this->total, null, null, RestErrorEnum::RE_INVALID_RESOURCE, null);
         $this->authenticated = false;
         header('HTTP/1.0 400 Bad Request');
     }
 }