Author: dimuthu Date: Mon Feb 25 04:48:48 2008 New Revision: 14181 Log:
Adding PHP Atom Client Added: trunk/registry/clients/ trunk/registry/clients/php/ trunk/registry/clients/php/Comment.php trunk/registry/clients/php/RESTClient.php trunk/registry/clients/php/RemoteRegistry.php trunk/registry/clients/php/Resource.php Added: trunk/registry/clients/php/Comment.php ============================================================================== --- (empty file) +++ trunk/registry/clients/php/Comment.php Mon Feb 25 04:48:48 2008 @@ -0,0 +1,61 @@ +<?php +/* + * Copyright (c) 2005-2008 WSO2, Inc. http://wso2.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Represent Comment + * + */ +class Comment +{ + /** + * resource path + * + * @var string + */ + public $resource_path; + + /** + * comment path + * + * @var string + */ + public $comment_path; + + /** + * Comment text + * + * @var string + */ + public $text; + + /** + * Commenting User + * + * @var string + */ + public $user; + + /** + * DateTime of the comment + * + * @var string + */ + public $date_time; +} +?> + Added: trunk/registry/clients/php/RESTClient.php ============================================================================== --- (empty file) +++ trunk/registry/clients/php/RESTClient.php Mon Feb 25 04:48:48 2008 @@ -0,0 +1,309 @@ +<?php +/* + * Copyright (c) 2005-2008 WSO2, Inc. http://wso2.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * RestClient interface for PHP + * + */ +class RESTClient +{ + private $with_curl; + + const USER_AGENT = 'RESTClient PHP Library Class'; + + private $username; + private $password; + + private $status; + + /** + * Constructor of the RESTClient + */ + public function __construct($username = NULL, $passoword = NULL) + { + if(function_exists("curl_init")) + { + $this->with_curl = TRUE; + } + else + { + $this->with_curl = FALSE; + } + $this->username = $username; + $this->password = $passoword; + + } + + /** + * Call the HTTP 'GET' method + * @param string $url URL of the service.. + * @param array $params request parameters, hash of (key,value) pairs + * @return response string + */ + public function get($url, $params = NULL) + { + if($params !== NULL) + { + $params_str = "?"; + if(is_array($params)) + { + foreach($params as $key=> $value) + { + $params_str .= urlencode($key)."=".urlencode($value)."&"; + } + } + else + { + $params_str .= $params; + } + + $url .= $params_str; + } + + $result = ""; + + if($this->with_curl) + { + $curl = curl_init(); + + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_HTTPGET, TRUE); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($curl, CURLOPT_USERAGENT, RESTClient::USER_AGENT); + if($this->username !== NULL) + { + curl_setopt($curl, CURLOPT_USERPWD, "{$this->username}:{$this->password}"); + } + $result = curl_exec($curl); + + $this->status = curl_getinfo($curl,CURLINFO_HTTP_CODE); + + curl_close($curl); + } + else + { + $username_str = "{$this->username}:{$this->password}"; + $auth_string = "Basic " + base64_encode($username_str); + + $opts = array( + 'http'=>array( + 'method'=>"GET", + 'header'=>"User-Agent: ".RESTClient::USER_AGENT."\r\n". + "Authorization: ".$auth_string."\r\n" + ) + ); + + $context = stream_context_create($opts); + + $fp = fopen($url, 'r', false, $context); + $result = fpassthru($fp); + fclose($fp); + } + return $result; + } + + /** + * Call the HTTP 'POST' method + * @param string $url URL of the service.. + * @param string $data request data + * @param array $content_type the http content type + * @return response string + */ + public function post($url, $data, $content_type = "application/atom+xml;type=entr") + { + $result = ""; + + if($this->with_curl) + { + $curl = curl_init(); + curl_setopt($curl, CURLOPT_HTTPHEADER, Array("Content-Type: ".$content_type)); + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_POST, TRUE); + curl_setopt($curl, CURLOPT_POSTFIELDS, $data); + if($this->username !== NULL) + { + curl_setopt($curl, CURLOPT_USERPWD, "{$this->username}:{$this->password}"); + } + curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($curl, CURLOPT_USERAGENT, RESTClient::USER_AGENT); + $result = curl_exec($curl); + $this->status = curl_getinfo($curl,CURLINFO_HTTP_CODE); + curl_close($curl); + } + else + { + $username_str = "{$this->username}:{$this->password}"; + $auth_string = "Basic " + base64_encode($username_str); + + $opts = array( + 'http'=>array( + 'method'=>"POST", + 'header'=>"User-Agent: ".RESTClient::USER_AGENT."\r\n". + "Authorization: ".$auth_string."\r\n". + "Content-Type: ".$content_type."\r\n". + "Content-length: " . strlen($data)."\r\n", + 'content' => $data + ) + ); + + $context = stream_context_create($opts); + + $fp = fopen($url, 'r', false, $context); + $result = fpassthru($fp); + fclose($fp); + } + + return $result; + } + + /** + * Call the HTTP 'PUT' method + * @param string $url URL of the service.. + * @param string $data request data + * @return response string + */ + public function put($url, $data) + { + $result = ""; + + if($this->with_curl) + { + + $fh = fopen('php://memory', 'rw'); + fwrite($fh, $data); + rewind($fh); + + $curl = curl_init(); + + + if($this->username !== NULL) + { + curl_setopt($curl, CURLOPT_USERPWD, "{$this->username}:{$this->password}"); + } + curl_setopt($curl, CURLOPT_USERAGENT, RESTClient::USER_AGENT); + curl_setopt($curl, CURLOPT_INFILE, $fh); + curl_setopt($curl, CURLOPT_INFILESIZE, strlen($data)); + curl_setopt($curl, CURLOPT_TIMEOUT, 10); + curl_setopt($curl, CURLOPT_PUT, 1); + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); + //curl_setopt($curl, CURLOPT_HTTPHEADER, array("Expect: ")); + $result = curl_exec($curl); + + $this->status = curl_getinfo($curl,CURLINFO_HTTP_CODE); + curl_close($curl); + + fclose($fh); + } + else + { + /* This executes when only curl is not installed, + This was not working with 'PUT' */ + + $username_str = "{$this->username}:{$this->password}"; + $auth_string = "Basic " + base64_encode($username_str); + + $opts = array( + 'http'=>array( + 'header'=>"User-Agent: ".RESTClient::USER_AGENT."\r\n". + "Authorization: ".$auth_string."\r\n" + ) + ); + + $context = stream_context_create($opts); + + $fp = fopen($url, 'w', false, $context); + $result = fwrite($fp, $data); + fclose($fp); + } + + return $result; + } + + /** + * Call the HTTP 'DELETE' method + * @param string $url URL of the service.. + * @param array $params request parameters, hash of (key,value) pairs + */ + public function delete($url, $params = NULL) + { + if($params !== NULL) + { + $params_str = "?"; + if(is_array($params)) + { + foreach($params as $key=> $value) + { + $params_str .= urlencode($key)."=".urlencode($value)."&"; + } + } + else + { + $params_str .= $params; + } + + $url .= $params_str; + } + + $result = ""; + + if($this->with_curl) + { + $curl = curl_init(); + if($this->username !== NULL) + { + curl_setopt($curl, CURLOPT_USERPWD, "{$this->username}:{$this->password}"); + } + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($curl, CURLOPT_USERAGENT, RESTClient::USER_AGENT); + $result = curl_exec($curl); + + $this->status = curl_getinfo($curl,CURLINFO_HTTP_CODE); + curl_close($curl); + } + else + { + /* This executes when only curl is not installed, + This was not working with 'DELETE' */ + $username_str = "{$this->username}:{$this->password}"; + $auth_string = "Basic " + base64_encode($username_str); + + $opts = array( + 'http'=>array( + 'header'=>"User-Agent: ".RESTClient::USER_AGENT."\r\n". + "Authorization: ".$auth_string."\r\n" + ) + ); + + $context = stream_context_create($opts); + + unlink($url, $context); + } + + } + + public function getLastResponseStatus() + { + return $this->status; + } + +} +?> + Added: trunk/registry/clients/php/RemoteRegistry.php ============================================================================== --- (empty file) +++ trunk/registry/clients/php/RemoteRegistry.php Mon Feb 25 04:48:48 2008 @@ -0,0 +1,513 @@ +<?php +/* + * Copyright (c) 2005-2008 WSO2, Inc. http://wso2.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +require_once "RESTClient.php"; +require_once "Resource.php"; +require_once "Comment.php"; + +/** + * RemoteRegistry, facilitate access to the Registry interface + * + */ +class RemoteRegistry +{ + private $authorization_str = NULL; + private $rest_client = NULL; + private $registry_url = NULL; + private $last_feed_xml; + + const COMMENT_PARAMTER = "comments"; + const RATING_PARAMTER = "ratings"; + const TAG_PARAMTER = "tags"; + const TAGPATHS_PARAMTER = "tagpaths"; + const ATOM_NS = "http://www.w3.org/2005/Atom"; + const WSO2_TAG_NS = "tag:wso2.org,2008:foo"; + + /** + * Construct the RemoteRegistry + * + * @param string $registry_url url of the registry instance + * @param string $username username of the registry account, default to NULL + * @param string $password password of the registry accound, default to NULL + */ + public function __construct($registry_url, $username = NULL, $password = NULL) + { + if(substr($registry_url, -1) == '/') + { + $registry_url = substr($registry_url, 0, strlen($registry_url)-1); + } + + $this->rest_client = new RESTClient($username, $password); + $this->registry_url = $registry_url; + } + + /** + * Get resources of an perticular registry path + * + * @param string $path + * @param array $params + * @return string retrieved resource + */ + public function get($path, array $params = array()) + { + $response = $this->rest_client->get($this->registry_url.$path, $params); + + if($this->rest_client->getLastResponseStatus() == "200") + { + $resouce = $this->createResourceFromFeed($response); + } + + return $resouce; + } + + /** + * Delete resource + * + * @param string $path + * @return 0 on success, -1 otherwise + */ + public function delete($path) + { + $this->rest_client->delete($this->registry_url.$path); + if($this->rest_client->getLastResponseStatus() == "204") + { + return 0; + } + return -1; + } + + /** + * Put resource in to a perticular path + * + * @param string $path + * @param string $resource + * @return void + */ + public function put($path, $resource) + { + $dom = new DOMDocument(); + $root = $dom->createElementNS(RemoteRegistry::ATOM_NS, "entry"); + $dom->appendChild($root); + if($resource->description !== NULL) + { + $summery = $dom->createElementNS(RemoteRegistry::ATOM_NS, "summery", $resource->description); + $summery->setAttribute("type", "text"); + $root->appendChild($summery); + } + if($resource->author_username !== NULL) + { + $author = $dom->createElementNS(RemoteRegistry::ATOM_NS, "author"); + $name = $dom->createElementNS(RemoteRegistry::ATOM_NS, "name", $resource->author_username); + $author->appendChild($name); + $root->appendChild($author); + } + + $properties = $dom->createElementNS(RemoteRegistry::WSO2_TAG_NS, "properties"); + if($resource->properties !== NULL && is_array($resource->properties)) + { + foreach($resource->properties as $key => $value) + { + $property = $dom->createElementNS(RemoteRegistry::WSO2_TAG_NS, "property"); + + $name = $dom->createElementNS(RemoteRegistry::WSO2_TAG_NS, "name", $key); + $property->appendChild($name); + $value_ele = $dom->createElementNS(RemoteRegistry::WSO2_TAG_NS, "value", $value); + $property->appendChild($value_ele); + + $properties->appendChild($property); + } + } + $root->appendChild($properties); + + if($resource->content != NULL) + { + $content = $dom->createElementNS(RemoteRegistry::ATOM_NS, "content", $resource->content); + $content->setAttribute("type", $resource->media_type); + $root->appendChild($content); + } + + if($resource->is_directory) + { + $dir = $dom->createElementNS("", "directory", "true"); + } + else + { + $dir = $dom->createElementNS("", "directory", "false"); + } + $root->appendChild($dir); + + if($resource->content_modified) + { + $modified = $dom->createElementNS("", "contentModified", "true"); + } + else + { + $modified = $dom->createElementNS("", "contentModified", "false"); + } + + $root->appendChild($modified); + + $data = $dom->saveXML($dom->documentElement); + $response = $this->rest_client->post($this->registry_url.$path, $data); + + return $response; + } + + /** + * addComment to a perticular resource + * + * @param string $path + * @param Comment $comment + * @return void + */ + public function addComment($path, $comment) + { + $dom = new DOMDocument(); + $root = $dom->createElementNS(RemoteRegistry::ATOM_NS, "entry"); + $dom->appendChild($root); + if($comment->user !== NULL) + { + $author = $dom->createElementNS(RemoteRegistry::ATOM_NS, "author"); + $name = $dom->createElementNS(RemoteRegistry::ATOM_NS, "name", $comment->user); + $author->appendChild($name); + $root->appendChild($author); + } + + if($comment->text !== NULL) + { + $content = $dom->createElementNS(RemoteRegistry::ATOM_NS, "content", $comment->text); + $content->setAttribute("type", "text"); + $root->appendChild($content); + } + + $data = $dom->saveXML($dom->documentElement); + + $response = $this->rest_client->post($this->registry_url.$path.";".RemoteRegistry::COMMENT_PARAMTER, $data); + + return $response; + } + + /** + * retrieve comments for a perticulr resource + * + * @param string $path + * @return array of Comment comments + */ + public function getComments($path) + { + $response = $this->rest_client->get($this->registry_url.$path.";". + RemoteRegistry::COMMENT_PARAMTER); + + $comments = array(); + + if($this->rest_client->getLastResponseStatus() == "200") + { + $resouce = $this->createResourceFromFeed($response); + $feed_xml = $this->last_feed_xml; + if($feed_xml->entry !== NULL) + { + foreach($feed_xml->entry as $entry) + { + $comment = new Comment(); + if($entry->updated !== NULL) + { + $comment->date_time = $entry->updated.""; + } + + if($entry->authour !== NULL && $entry->author->name !== NULL) + { + $comment->user = $entry->author->name.""; + } + + if($entry->content !== NULL) + { + $comment->text = $entry->content.""; + } + $comments[count($comments)] = $comment; + } + } + } + + return $comments; + } + + /** + * rate a resource + * + * @param string $path resource path + * @param int $rateValue + * @return void + */ + public function rateResource($path, $rateValue) + { + $dom = new DOMDocument(); + $root = $dom->createElementNS(RemoteRegistry::ATOM_NS, "entry"); + $dom->appendChild($root); + + $content = $dom->createElementNS(RemoteRegistry::ATOM_NS, "content", $rateValue); + $content->setAttribute("type", "text"); + $root->appendChild($content); + + $summery = $dom->createElementNS(RemoteRegistry::ATOM_NS, "summery", RemoteRegistry::RATING_PARAMTER); + $summery->setAttribute("type", "text"); + $root->appendChild($summery); + + $data = $dom->saveXML($dom->documentElement); + + $response = $this->rest_client->post($this->registry_url.$path.";".RemoteRegistry::RATING_PARAMTER, $data); + + return $this->rest_client->getLastResponseStatus(); + } + + /** + * Get Average Rating of a resource + * + * @param string $path + * @return int the avarage rating + */ + public function getAverageRating($path) + { + $response = $this->rest_client->get($this->registry_url.$path.";".RemoteRegistry::RATING_PARAMTER); + + if($this->rest_client->getLastResponseStatus() == "200") + { + $resouce = $this->createResourceFromFeed($response); + $feed_xml = $this->last_feed_xml; + $wso2_ns = $feed_xml->children("http://wso2.org/registry"); + if($wso2_ns->AverageRating !== NULL) + { + return (float)($wso2_ns->AverageRating.""); + } + } + + return 0; + } + + /** + * Get user rating + * + * @param string $path + * @param string $username + * @return int + */ + public function getUserRating($path, $username) + { + $response = $this->rest_client->get($this->registry_url.$path.";". + RemoteRegistry::RATING_PARAMTER.":". + $username); + + if($this->rest_client->getLastResponseStatus() == "200") + { + $resouce = $this->createResourceFromFeed($response); + $feed_xml = $this->last_feed_xml; + if($feed_xml->entry !== NULL && $feed_xml->entry->content !== NULL) + { + return (int)($feed_xml->entry->content); + } + } + + return 0; + } + + /** + * apply a tag + * + * @param string $path + * @param string $tag + * @return void + */ + public function applyTag($path, $tag) + { + $dom = new DOMDocument(); + $root = $dom->createElementNS(RemoteRegistry::ATOM_NS, "entry"); + $dom->appendChild($root); + + + $content = $dom->createElementNS(RemoteRegistry::ATOM_NS, "content", $tag); + $content->setAttribute("type", "text"); + $root->appendChild($content); + + $data = $dom->saveXML($dom->documentElement); + + $response = $this->rest_client->post($this->registry_url.$path.";".RemoteRegistry::TAG_PARAMTER, $data); + + return $response; + } + + /** + * get tags + * + * @param string $path + * @return array of string + */ + public function getTags($path) + { + $response = $this->rest_client->get($this->registry_url.$path.";". + RemoteRegistry::TAG_PARAMTER); + + $tags = array(); + + if($this->rest_client->getLastResponseStatus() == "200") + { + $resouce = $this->createResourceFromFeed($response); + $feed_xml = $this->last_feed_xml; + if($feed_xml->entry !== NULL) + { + foreach($feed_xml->entry as $entry) + { + $taggings = $entry->taggings.""; + if($taggings == "1") + { + $tag = $entry->title.""; + $tags[count($tags)] = $tag; + } + } + } + } + + return $tags; + } + + /** + * get resouce paths for a given tag + * + * @param string $tag + * @return Resource which contains + */ + public function getResourcePathsWithTag($tag) + { + + $response = $this->rest_client->get($this->registry_url."/". ";". + RemoteRegistry::TAGPATHS_PARAMTER.":".$tag); + + $resource = NULL; + if($this->rest_client->getLastResponseStatus() == "200") + { + $resource = $this->createResourceFromFeed($response, TRUE); + } + + return $resource; + } + + + /** + * get versions + * + * @param string $path + */ + public function getVersions($path) + { + // TODO: + } + + /** + * restore versions + * + * @param string $version_path + */ + public function restoreVersion($version_path) + { + // TODO: + } + + private function createResourceFromFeed($feed, $no_content = FALSE) + { + $index = strpos($feed, "?>"); + if($index === false) + { + $xml_str = $feed; + } + else + { + $xml_str = substr($feed, $index+2); + } + $feed_xml = new SimpleXMLElement($xml_str); + $this->last_feed_xml = $feed_xml; + + $resource = new Resource(); + + /* assign author */ + if($feed_xml->author !== NULL) + { + $resource->author_username = $feed_xml->author->name.""; + } + + /* assign properties */ + $foo_ns = $feed_xml->children(RemoteRegistry::WSO2_TAG_NS); + if($foo_ns->properties !== NULL && $foo_ns->properties->property !== NULL) + { + $i = 0; + foreach($foo_ns->properties->property as $property) + { + $name = $property->name.""; + $value = $property->value.""; + + + $resource->properties[$name] = $value; + + } + } + + + $resource->id = $feed_xml->id.""; + $resource->subtitle = $feed_xml->subtitle.""; + $resource->title = $feed_xml->title.""; + $resource->updated = $feed_xml->updated.""; + + + $empty_ns = $feed_xml->children(""); + + $resource->media_type = $empty_ns->mediaType.""; + $resource->parent_path = $empty_ns->parentPath.""; + $resource->last_updated_username = $empty_ns->lastUpdatedUser.""; + $resource->directory = $empty_ns->directory.""; + $resource->created_time = $empty_ns->createdTime.""; + + $i = 0; + if($feed_xml->entry !== NULL) + { + foreach($feed_xml->entry as $entry) + { + $title = $entry->title.""; + $href = ""; + foreach($entry->link as $link) + { + $href = $link["href"].""; + } + $resource->entries[$title] = $href; + } + } + + if($resource->directory !== NULL && $resource->directory."" == "true") + { + + } + else + { + //echo "href". $href."\n"; + if($no_content == FALSE) + { + $resource->content = $this->rest_client->get($href, array()); + } + } + + return $resource; + } +} +?> + Added: trunk/registry/clients/php/Resource.php ============================================================================== --- (empty file) +++ trunk/registry/clients/php/Resource.php Mon Feb 25 04:48:48 2008 @@ -0,0 +1,76 @@ +<?php +/* + * Copyright (c) 2005-2008 WSO2, Inc. http://wso2.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Represent Resources + * + */ +class Resource +{ + public $id; + public $author_username; + public $created_time; + public $last_updated_username; + public $updated; //timestamp + public $description; + public $path; + public $media_type; + public $parent_path; + + /** + * Used to detect whether the resource content is modified after it is retrieved from the + * Registry. If this is set to true at the time of adding the resource back to the Registry, new + * version will be created. + */ + public $content_modified; //boolean + + /** + * Normal resources have the state RegistryConstants.ACTIVE_STATE (100) Deleted resources have + * the state RegistryConstants.DELETED_STATE (101) + */ + public $state; + + /* hash map of properties */ + public $properties; + + public $depends_on; //String[] + public $depended_on_by; + + //To store any kind of object + public $content; + + public $is_directory; //Bool + + public $subtitle; + + public $title; + + public $directory; + + public $entries; + + public function __construct() + { + $this->properties = array(); + $this->entries = array(); + $this->is_directory = false; + + } +} +?> + _______________________________________________ Registry-dev mailing list [email protected] http://wso2.org/cgi-bin/mailman/listinfo/registry-dev
