Author: krispopat
Date: Tue Mar  9 18:37:54 2010
New Revision: 921050

URL: http://svn.apache.org/viewvc?rev=921050&view=rev
Log:
Added:

Basic connection testing
add/get/delete participant
add/get/delete properties

Lot's of other code work.

See TestWookieService.php 

Thanks to Raido Kuli

Added:
    incubator/wookie/trunk/connector/php/WidgetProperties.php   (with props)
Modified:
    incubator/wookie/trunk/connector/php/HTTP_Response.php
    incubator/wookie/trunk/connector/php/TestWookieService.php
    incubator/wookie/trunk/connector/php/Widget.php
    incubator/wookie/trunk/connector/php/WidgetInstance.php
    incubator/wookie/trunk/connector/php/WidgetInstances.php
    incubator/wookie/trunk/connector/php/WookieConnectorExceptions.php
    incubator/wookie/trunk/connector/php/WookieConnectorService.php
    incubator/wookie/trunk/connector/php/WookieServerConnection.php

Modified: incubator/wookie/trunk/connector/php/HTTP_Response.php
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/connector/php/HTTP_Response.php?rev=921050&r1=921049&r2=921050&view=diff
==============================================================================
--- incubator/wookie/trunk/connector/php/HTTP_Response.php (original)
+++ incubator/wookie/trunk/connector/php/HTTP_Response.php Tue Mar  9 18:37:54 
2010
@@ -1,5 +1,6 @@
 <?php
-/**
+
+/*
  *  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
@@ -12,7 +13,7 @@
  *  See the License for the specific language governing permissions and
  * limitations under the License.
  */
- 
+
 class HTTP_Response {
        private $statusCode;
        private $responseText;

Modified: incubator/wookie/trunk/connector/php/TestWookieService.php
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/connector/php/TestWookieService.php?rev=921050&r1=921049&r2=921050&view=diff
==============================================================================
--- incubator/wookie/trunk/connector/php/TestWookieService.php (original)
+++ incubator/wookie/trunk/connector/php/TestWookieService.php Tue Mar  9 
18:37:54 2010
@@ -1,5 +1,5 @@
 <?php
-/**
+/*
  *  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
@@ -17,15 +17,18 @@
 ini_set('display_errors', 1);
 error_reporting(E_ALL &~ E_NOTICE);
 
-require("WookieConnectorService.php");
+require_once("WookieConnectorService.php");
 
-$test = new WookieConnectorService("http://127.0.0.1:8080/wookie/";, "TEST", 
"localhost_test", 'demo_2');
+$test = new WookieConnectorService("http://localhost:8080/wookie/";, "TEST", 
"localhost_test", 'demo_2');
 //setup different userName
 $test->getUser()->setLoginName("demo_1");
 //get all available widgets
 $availableWidgets = $test->getAvailableWidgets();
 //print_r($availableWidgets);
 
+//check connection
+echo $test->getConnection()->Test().'<br />';
+
 //create select menus
 echo '<pre>';
 echo '<form action="" method="GET">';
@@ -66,20 +69,45 @@ if($_GET['widget_id'] != '') {
        //setup different userName for current user
        $test->getUser()->setLoginName("demo_2");
        $widget = $test->getOrCreateInstance($_GET['widget_id']);
-       echo '<iframe src="'.$widget->getUrl().'" 
width="'.$widget->getWidth().'" height="'.$widget->getHeight().'"></iframe><br 
/>';
-       
+       if($widget) {
+               echo '<iframe src="'.$widget->getUrl().'" 
width="'.$widget->getWidth().'" height="'.$widget->getHeight().'"></iframe><br 
/>';
+       }
        //add participant
        $testUser = new User('demo_2', 'demo_2');
        $test->addParticipant($widget, $testUser);
        print_r($test->getUsers($widget));
+       
+       //delete participant DOES NOT WORK
+       $testUser = new User('demo_2', 'demo_2');
+    $test->deleteParticipant($widget, $testUser);
+       $test->deleteParticipant($widget, $testUser);
+       echo 'Users after delete <br />';
+       print_r($test->getUsers($widget));
+       
+       
+       //add property
+       $newProperty = new Property('demo_property', 'demo_value');
+       $result = $test->setProperty($widget, $newProperty);
+       print_r($result);
+       
+       //get property from server
+       $newProperty = new Property('demo_property');
+       print_r($test->getProperty($widget, $newProperty)); // you can use 
property without value for get -> new Property('proovikas');
+       
+       //finally delete it from server 
+       $newProperty = new Property('proovikas');
+       echo 'Properties after delete<br />';
+       print_r($test->deleteProperty($widget, $newProperty)); // you can use 
property without value for get -> new Property('proovikas');
+       echo '<br />';
 }
 
 if($_GET['widget_id2'] != '') {
        //setup different userName for current user
        $test->getUser()->setLoginName("demo_3");
        $widget2 = $test->getOrCreateInstance($_GET['widget_id2']);
-       echo '<iframe src="'.$widget2->getUrl().'" 
width="'.$widget2->getWidth().'" 
height="'.$widget2->getHeight().'"></iframe><br />';
-       
+       if($widget2) {
+               echo '<iframe src="'.$widget2->getUrl().'" 
width="'.$widget2->getWidth().'" 
height="'.$widget2->getHeight().'"></iframe><br />';
+       }
        //add participant
        $testUser = new User('demo_3', 'demo_3');
        $test->addParticipant($widget2, $testUser);

Modified: incubator/wookie/trunk/connector/php/Widget.php
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/connector/php/Widget.php?rev=921050&r1=921049&r2=921050&view=diff
==============================================================================
--- incubator/wookie/trunk/connector/php/Widget.php (original)
+++ incubator/wookie/trunk/connector/php/Widget.php Tue Mar  9 18:37:54 2010
@@ -1,5 +1,6 @@
 <?php
-/**
+
+/*
  *  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
@@ -12,8 +13,7 @@
  *  See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
-
+ 
 /**
  * A client side representation of a widget. 
  * 

Modified: incubator/wookie/trunk/connector/php/WidgetInstance.php
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/connector/php/WidgetInstance.php?rev=921050&r1=921049&r2=921050&view=diff
==============================================================================
--- incubator/wookie/trunk/connector/php/WidgetInstance.php (original)
+++ incubator/wookie/trunk/connector/php/WidgetInstance.php Tue Mar  9 18:37:54 
2010
@@ -1,4 +1,5 @@
 <?php
+
 /*
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.

Modified: incubator/wookie/trunk/connector/php/WidgetInstances.php
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/connector/php/WidgetInstances.php?rev=921050&r1=921049&r2=921050&view=diff
==============================================================================
--- incubator/wookie/trunk/connector/php/WidgetInstances.php (original)
+++ incubator/wookie/trunk/connector/php/WidgetInstances.php Tue Mar  9 
18:37:54 2010
@@ -1,4 +1,5 @@
 <?php
+
 /*
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.

Added: incubator/wookie/trunk/connector/php/WidgetProperties.php
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/connector/php/WidgetProperties.php?rev=921050&view=auto
==============================================================================
--- incubator/wookie/trunk/connector/php/WidgetProperties.php (added)
+++ incubator/wookie/trunk/connector/php/WidgetProperties.php Tue Mar  9 
18:37:54 2010
@@ -0,0 +1,52 @@
+<?php
+
+/*
+ *  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.
+ */
+
+ class Property {
+       private $propertyName = '';
+       private $propertyValue = '';
+       private $isPublic = 'false';
+       
+       function __construct($propertyName, $propertyValue = null, $isPublic = 
'false') {
+               $this->propertyName = (string) $propertyName;
+               $this->propertyValue = (string) $propertyValue;
+               $this->isPublic = (string) $isPublic;
+       }
+       
+       public function getValue() {
+               return $this->propertyValue;
+       }
+       
+       public function getName() {
+               return $this->propertyName;
+       }
+       public function isPublic() {
+               return $this->isPublic;
+       }
+       
+       public function setValue($value) {
+               $this->propertyValue = (string) $value;
+       }
+       
+       public function setName($propertyName) {
+               $this->propertyName = (string) $propertyName;
+       }
+       public function set_isPublic($isPublic) {
+               $this->isPublic = (string) $isPublic;
+       }
+       
+ }
+
+?>
\ No newline at end of file

Propchange: incubator/wookie/trunk/connector/php/WidgetProperties.php
------------------------------------------------------------------------------
    svn:executable = *

Modified: incubator/wookie/trunk/connector/php/WookieConnectorExceptions.php
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/connector/php/WookieConnectorExceptions.php?rev=921050&r1=921049&r2=921050&view=diff
==============================================================================
--- incubator/wookie/trunk/connector/php/WookieConnectorExceptions.php 
(original)
+++ incubator/wookie/trunk/connector/php/WookieConnectorExceptions.php Tue Mar  
9 18:37:54 2010
@@ -1,4 +1,5 @@
 <?php
+
 /*
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.

Modified: incubator/wookie/trunk/connector/php/WookieConnectorService.php
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/connector/php/WookieConnectorService.php?rev=921050&r1=921049&r2=921050&view=diff
==============================================================================
--- incubator/wookie/trunk/connector/php/WookieConnectorService.php (original)
+++ incubator/wookie/trunk/connector/php/WookieConnectorService.php Tue Mar  9 
18:37:54 2010
@@ -1,4 +1,5 @@
 <?php
+
 /*
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -18,168 +19,182 @@ require("WookieServerConnection.php");
 require("WidgetInstances.php");
 require("Widget.php");
 require("WidgetInstance.php");
+require("WidgetProperties.php");
 require("User.php");
 require("HTTP_Response.php");
 
 
 class WookieConnectorService {
-  private $conn;
-  public  $WidgetInstances;
-  private $user;
-  
-  function __construct($url, $apiKey, $sharedDataKey, $loginName, $screenName 
= null) {
-    $this->setConnection(new WookieServerConnection($url, $apiKey, 
$sharedDataKey));
-       $this->setWidgetInstancesHolder();
-       $this->setUser($loginName, $screenName);
-  }
-  
-  public function setConnection($newConn) {
-    $this->conn = $newConn;
-  }
-  
-  public function getConnection() {
-    return $this->conn;
-  }
-  
-  public function setWidgetInstancesHolder() {
-       $this->WidgetInstances = new WidgetInstances();
-  }
-  
-  public function setUser($loginName, $screenName = null) {
-       if($screenName == null) {
-               $screenName = $loginName;
-       }
-       $this->user = new User($loginName, $screenName);
-  }
-  
-  public function getUser() {
-       return $this->user;
-  }
-  
-  /* Do POST request
-  /* @return new HTTP_Response instance */
-  
-  private function do_post_request($url, $data, $optional_headers = null)
-  {
-       if(is_array($data)) {
-                // convert variables array to string:
-               $_data = array();    
-               while(list($n,$v) = each($data)){
-                       $_data[] = urlencode($n)."=".urlencode($v);
-               }    
-               $data = implode('&', $_data);
+       private $conn;
+       public  $WidgetInstances;
+       private $user;
+       private $httpStreamCtx;
+
+       function __construct($url, $apiKey, $sharedDataKey, $loginName, 
$screenName = null) {
+               $this->setConnection(new WookieServerConnection($url, $apiKey, 
$sharedDataKey));
+               $this->setWidgetInstancesHolder();
+               $this->setUser($loginName, $screenName);
+               $this->setHttpStreamContext(array('http' => array('timeout' => 
15)));
+       }
+
+       public function setConnection($newConn) {
+               $this->conn = $newConn;
+       }
+
+       public function getConnection() {
+               return $this->conn;
+       }
+
+       public function setWidgetInstancesHolder() {
+               $this->WidgetInstances = new WidgetInstances();
+       }
+
+       public function setUser($loginName, $screenName = null) {
+               if($screenName == null) {
+                       $screenName = $loginName;
+               }
+               $this->user = new User($loginName, $screenName);
+       }
+
+       public function getUser() {
+               return $this->user;
        }
        
-     $params = array('http' => array(
-                  'method' => 'POST',
-                  'content' => $data
-               ));
+       private function setHttpStreamContext($params = null) {
+               $this->httpStreamCtx = @stream_context_create($params);
+       }
        
-     if ($optional_headers !== null) {
-        $params['http']['header'] = $optional_headers;
-     }
-     $ctx = @stream_context_create($params);
-    // $fp = @@file_get_contents($url, 'rb', false, $ctx);
-     $response = @file_get_contents($url, false, $ctx);
-
-     return new HTTP_Response($response, $http_response_header);
-  }
-  
-  
-  /**
-   * Get or create an instance of a widget.
-   * 
-   * @param widget
-   * @return the ID of the widget instance
-   * @throws IOException
-   * @throws SimalRepositoryException
-   */
-
-  public function getOrCreateInstance($Widget_or_GUID = null) {
-    try {
-               if(is_object($Widget_or_GUID)) {
-                       $guid = $Widget_or_GUID->getIdentifier();
-               } else {
-                       $guid = $Widget_or_GUID;
-               }
-               if($guid == '') {
-                       throw new WookieConnectorException("No GUID nor Widget 
object");
-               }
-               $requestUrl = 
$this->getConnection()->getURL().'/widgetinstances';
-               $request.= '&api_key='.$this->getConnection()->getApiKey();
-               $request.= '&servicetype=';
-               $request.= '&userid='.$this->getUser()->getLoginName();
-               $request.= 
'&shareddatakey='.$this->getConnection()->getSharedDataKey();
-               $request.= '&widgetid='.$guid;
-               
-               if(!$this->checkURL($requestUrl)) {
-                       throw new WookieConnectorException("URL for supplied 
Wookie Server is malformed: ".$requestUrl);
-               }       
-               $response = $this->do_post_request($requestUrl, $request);
+       private function getHttpStreamContext() {
+               return $this->httpStreamCtx;
+       }
+
+       /* Do HTTP request
+        /* @return new HTTP_Response instance */
+
+       private function do_request($url, $data, $method = 'POST')
+       {
+               if(is_array($data)) {
+                // convert variables array to string:
+                       $_data = array();
+                       while(list($n,$v) = each($data)){
+                               $_data[] = urlencode($n)."=".urlencode($v);
+                       }
+                       $data = implode('&', $_data);
+               }
+
+               $params = array('http' => array(
+                  'method' => $method,
+                  'content' => $data,
+                         'timeout' => 15
+               ));
+               $this->setHttpStreamContext($params);
+               $response = @file_get_contents($url, false, 
$this->getHttpStreamContext());
                
-               //if instance was created, perform second request to get widget 
instance
-               if($response->getStatusCode() == 201) {
-                       $response = $this->do_post_request($requestUrl, 
$request);
-               }
-               $instance = $this->parseInstance($guid, 
$response->getResponseText());
-               $this->WidgetInstances->put($instance);
-               return $instance;
-       } catch (WookieConnectorException $e) {
-               echo $e->errorMessage();
+               //revert back to default value for other requests
+               $this->setHttpStreamContext(array('http' => array('timeout' => 
15)));
+
+               return new HTTP_Response($response, $http_response_header);
        }
+
+
+       /**
+        * Get or create an instance of a widget.
+        *
+        * @param widget
+        * @return the ID of the widget instance
+        * @throws IOException
+        * @throws SimalRepositoryException
+        */
+
+       public function getOrCreateInstance($Widget_or_GUID = null) {
+               try {
+                       if(is_object($Widget_or_GUID)) {
+                               $guid = $Widget_or_GUID->getIdentifier();
+                       } else {
+                               $guid = $Widget_or_GUID;
+                       }
+                       if($guid == '') {
+                               throw new WookieConnectorException("No GUID nor 
Widget object");
+                       }
+                       $requestUrl = 
$this->getConnection()->getURL().'widgetinstances';
+                       $request.= 
'&api_key='.$this->getConnection()->getApiKey();
+                       $request.= '&servicetype=';
+                       $request.= '&userid='.$this->getUser()->getLoginName();
+                       $request.= 
'&shareddatakey='.$this->getConnection()->getSharedDataKey();
+                       $request.= '&widgetid='.$guid;
+
+                       if(!$this->checkURL($requestUrl)) {
+                               throw new WookieConnectorException("URL for 
supplied Wookie Server is malformed: ".$requestUrl);
+                       }
+                       $response = $this->do_request($requestUrl, $request);
+
+                       //if instance was created, perform second request to 
get widget instance
+                       if($response->getStatusCode() == 201) {
+                               $response = $this->do_request($requestUrl, 
$request);
+                       }
+                       if($response->getStatusCode() == 401) { throw new 
WookieConnectorException("Invalid API key"); }
+
+                       $instance = $this->parseInstance($guid, 
$response->getResponseText());
+                       $this->WidgetInstances->put($instance);
+                       return $instance;
+               } catch (WookieConnectorException $e) {
+                       echo $e->errorMessage();
+               }
                return false;
-  }
-  
-  
-    /**
-   * Record an instance of the given widget.
-   * 
-   * @param xml description of the instance as returned by the widget server 
when the widget was instantiated.
-   * @return new Widget instance
-   */
-  private function parseInstance($widgetGuid, $xml) {
-       $xmlWidgetData = @simplexml_load_string($xml);
-       if(is_object($xmlWidgetData)) {
-               $url = (string) $xmlWidgetData->url;
-               $title = (string) $xmlWidgetData->title;
-               $height = (string) $xmlWidgetData->height;
-               $width = (string) $xmlWidgetData->width;
-               $maximize = (string) $xmlWidgetData->maximize;
-               $instance = new WidgetInstance($url, $widgetGuid, $title, 
$height, $width, $maximize);
-               return $instance;
-       } 
-       return false;
-  }
-  
-   /**
-   * Check if URL is parsable.
-   * 
-   * @param url
-   * @return boolean
-   */
-  
-  private function checkURL($url) {
-       $UrlCheck = parse_url($url);
-       if($UrlCheck['scheme'] != 'http' || $UrlCheck['host'] == null || 
$UrlCheck['path'] == null) {
+       }
+
+
+       /**
+        * Record an instance of the given widget.
+        *
+        * @param xml description of the instance as returned by the widget 
server when the widget was instantiated.
+        * @return new Widget instance
+        */
+       private function parseInstance($widgetGuid, $xml) {
+               $xmlWidgetData = @simplexml_load_string($xml);
+               if(is_object($xmlWidgetData)) {
+                       //print_r($xmlWidgetData);
+                       $url = (string) $xmlWidgetData->url;
+                       $title = (string) $xmlWidgetData->title;
+                       $height = (string) $xmlWidgetData->height;
+                       $width = (string) $xmlWidgetData->width;
+                       $maximize = (string) $xmlWidgetData->maximize;
+                       $instance = new WidgetInstance($url, $widgetGuid, 
$title, $height, $width, $maximize);
+                       return $instance;
+               }
                return false;
        }
-       return true;
-  }
-  
-   /**
-   * @refactor At time of writing the REST API for adding a participant is 
broken so we are
-   * using the non-REST approach. The code for REST API is commented out and 
should be used
-   * in the future.
-   */
-   
-  public function addParticipant($widgetInstance, $User)  {
-       $Url = $this->getConnection()->getURL().'/participants';
-       
-       try {
-               if(!is_object($widgetInstance)) throw new 
WookieWidgetInstanceException('No Widget instance');
-               if(!is_object($User)) throw new WookieConnectorException('No 
User object');
-               
-               $data = array(
+
+       /**
+        * Check if URL is parsable.
+        *
+        * @param url
+        * @return boolean
+        */
+
+       private function checkURL($url) {
+               $UrlCheck = @parse_url($url);
+               if($UrlCheck['scheme'] != 'http' || $UrlCheck['host'] == null 
|| $UrlCheck['path'] == null) {
+                       return false;
+               }
+               return true;
+       }
+
+       /**
+        * @refactor At time of writing the REST API for adding a participant 
is broken so we are
+        * using the non-REST approach. The code for REST API is commented out 
and should be used
+        * in the future.
+        * @return boolean true - if added/exists - false if some error
+        */
+
+       public function addParticipant($widgetInstance, $User)  {
+               $Url = $this->getConnection()->getURL().'participants';
+
+               try {
+                       if(!is_object($widgetInstance)) throw new 
WookieWidgetInstanceException('No Widget instance');
+                       if(!is_object($User)) throw new 
WookieConnectorException('No User object');
+
+                       $data = array(
                                'api_key' => 
$this->getConnection()->getApiKey(),
                                'shareddatakey' => 
$this->getConnection()->getSharedDataKey(),
                                'userid' => $this->getUser()->getLoginName(),
@@ -187,111 +202,157 @@ class WookieConnectorService {
                                'participant_id' => 
$this->getUser()->getLoginName(),
                                'participant_display_name' => 
$User->getScreenName(),
                                'participant_thumbnail_url' => 
$User->getThumbnailUrl(),
-               );      
-               
-               if(!$this->checkURL($Url)) {
-                       throw new WookieConnectorException("Participants rest 
URL is incorrect: ".$Url);
-               }       
-               
-               $response = $this->do_post_request($Url, $data);
-               $statusCode = $response->getStatusCode();
-               
-               switch($statusCode) {
+                       );
+
+                       if(!$this->checkURL($Url)) {
+                               throw new 
WookieConnectorException("Participants rest URL is incorrect: ".$Url);
+                       }
+
+                       $response = $this->do_request($Url, $data);
+                       $statusCode = $response->getStatusCode();
+
+                       switch($statusCode) {
                  case 200: //participant already exists
-                       return true;
-                       break;
+                       return true;
+                       break;
                  case 201:
-                       return true; //new participant added
-                       break;
+                       return true; //new participant added
+                       break;
                  case ($statusCode > 201):
-                       throw new 
WookieConnectorException($response->headerToString().'<br 
/>'.$response->getResponseText());
-                       break;
-                 default:
-                       return false;
+                       throw new 
WookieConnectorException($response->headerToString().'<br 
/>'.$response->getResponseText());
+                       break;
+                       }
+
+               } catch (WookieConnectorException $e) {
+                       echo $e->errorMessage();
+               } catch (WookieWidgetInstanceException $e) {
+                       echo '<b>function.addParticipant:</b> 
'.$e->getMessage().'<br />';
                }
+       return false;
+       }
+
+       /**
+        * @refactor Delete participant
+        * @param WidgetInstance $widgetInstance
+        * @param UserInstance $User
+        * @return boolean true - if deleted, false - if not found
+        */
+
+       public function deleteParticipant($widgetInstance, $User)  {
+               $Url = $this->getConnection()->getURL().'participants';
 
-       } catch (WookieConnectorException $e) {
-               echo $e->errorMessage();
-       } catch (WookieWidgetInstanceException $e) {
-               echo '<b>function.addParticipant:</b> '.$e->getMessage().'<br 
/>';
-       }
-    
-  }
-  
-   /**
-   * Get the array of users for a widget instance
-   * @param instance
-   * @return an array of users
-   * @throws WookieConnectorException
-   */
-  public function getUsers($widgetInstance) {
-       $Url = $this->getConnection()->getURL().'/participants';
-       $Users = array();
                try {
                        if(!is_object($widgetInstance)) throw new 
WookieWidgetInstanceException('No Widget instance');
+                       if(!is_object($User)) throw new 
WookieConnectorException('No User object');
+
                        $request = 
'?api_key='.$this->getConnection()->getApiKey();
                        $request .= 
'&shareddatakey='.$this->getConnection()->getSharedDataKey();
                        $request .= '&userid='.$this->getUser()->getLoginName();
                        $request .= 
'&widgetid='.$widgetInstance->getIdentifier();
-                       
+                       $request .= 
'&participant_id='.$this->getUser()->getLoginName();
+
+
                        if(!$this->checkURL($Url)) {
                                throw new 
WookieConnectorException("Participants rest URL is incorrect: ".$Url);
                        }
-                       
-                       $response = new 
HTTP_Response(@file_get_contents($Url.$request), $http_response_header);
+
+                       $response = $this->do_request($Url.$request, false, 
'DELETE');
+                       $statusCode = $response->getStatusCode();
+
+               switch($statusCode) {
+                 case 200: //participant deleted
+                       return true;
+                       break;
+                 case 404:
+                       return false; //participant not found
+                       break;
+                 case ($statusCode > 201):
+                       throw new 
WookieConnectorException($response->headerToString().'<br 
/>'.$response->getResponseText());
+                       break;
+               }
+
+               } catch (WookieConnectorException $e) {
+                       echo $e->errorMessage();
+               } catch (WookieWidgetInstanceException $e) {
+                       echo '<b>function.deleteParticipant:</b> 
'.$e->getMessage().'<br />';
+               }
+               return false;
+       }
+
+       /**
+        * Get the array of users for a widget instance
+        * @param instance
+        * @return an array of users
+        * @throws WookieConnectorException
+        */
+       public function getUsers($widgetInstance) {
+               $Url = $this->getConnection()->getURL().'participants';
+               $Users = array();
+               try {
+                       if(!is_object($widgetInstance)) throw new 
WookieWidgetInstanceException('No Widget instance');
+                       $request = 
'?api_key='.$this->getConnection()->getApiKey();
+                       $request .= 
'&shareddatakey='.$this->getConnection()->getSharedDataKey();
+                       $request .= '&userid='.$this->getUser()->getLoginName();
+                       $request .= 
'&widgetid='.$widgetInstance->getIdentifier();
+
+                       if(!$this->checkURL($Url)) {
+                               throw new 
WookieConnectorException("Participants rest URL is incorrect: ".$Url);
+                       }
+
+                       $response = new 
HTTP_Response(@file_get_contents($Url.$request, false, 
$this->getHttpStreamContext()), $http_response_header);
                        if($response->getStatusCode() > 200) throw new 
WookieConnectorException($response->headerToString().'<br 
/>'.$response->getResponseText());
-                       
+
                        $xmlObj = 
@simplexml_load_string($response->getResponseText());
-                       
+
                        if(is_object($xmlObj)) {
                                foreach($xmlObj->children() as $participant) {
-                                 $participantAttr = $participant->attributes();
-                                 
-                                 $id = (string) $participantAttr->id;
-                                 $name = (string) 
$participantAttr->display_name;
-                                 $thumbnail_url = (string) 
$participantAttr->thumbnail_url;
-                                 
-                                 $newUser = new User($id, $name, 
$thumbnail_url);
-                                 array_push($Users, $newUser);
+                                       $participantAttr = 
$participant->attributes();
+
+                                       $id = (string) $participantAttr->id;
+                                       $name = (string) 
$participantAttr->display_name;
+                                       $thumbnail_url = (string) 
$participantAttr->thumbnail_url;
+
+                                       $newUser = new User($id, $name, 
$thumbnail_url);
+                                       array_push($Users, $newUser);
                                }
                        } else {
                                throw new WookieConnectorException('Problem 
getting participants');
                        }
-                       
-                       return $Users;
+
                } catch (WookieWidgetInstanceException $e) {
                        echo '<b>function.getUsers:</b> '.$e->getMessage().'<br 
/>';
                } catch (WookieConnectorException $e) {
                        echo $e->errorMessage();
                }
-  }
+               return $Users;
+       }
 
- 
 
-  /**
-   * Get a set of all the available widgets in the server. If there is an error
-   * communicating with the server return an empty set, or the set received so
-   * far in order to allow the application to proceed. The application should
-   * display an appropriate message in this case.
-   * 
-   * @return array of available widgets
-   * @throws WookieConnectorException
-   */
-   
-  public function getAvailableWidgets() {
-       $widgets = array();
-       try {
-               $request = $this->getConnection()->getURL().'/widgets?all=true';
-               
-               if(!$this->checkURL($request)) {
-                       throw new WookieConnectorException("URL for Wookie is 
malformed");
-               }
-               
-               $response = new HTTP_Response(@file_get_contents($request), 
$http_response_header);
-               $xmlObj = @simplexml_load_string($response->getResponseText());
-               
-               if(is_object($xmlObj)) {
-                       foreach($xmlObj->children() as $widget) {
+
+       /**
+        * Get a set of all the available widgets in the server. If there is an 
error
+        * communicating with the server return an empty set, or the set 
received so
+        * far in order to allow the application to proceed. The application 
should
+        * display an appropriate message in this case.
+        *
+        * @return array of available widgets
+        * @throws WookieConnectorException
+        */
+
+       public function getAvailableWidgets() {
+               $widgets = array();
+               try {
+                       $request = 
$this->getConnection()->getURL().'widgets?all=true';
+
+                       if(!$this->checkURL($request)) {
+                               throw new WookieConnectorException("URL for 
Wookie is malformed");
+                       }
+
+                       $response = new 
HTTP_Response(@file_get_contents($request, false, 
$this->getHttpStreamContext()), $http_response_header);
+                       $xmlObj = 
@simplexml_load_string($response->getResponseText());
+
+                       if(is_object($xmlObj)) {
+                               foreach($xmlObj->children() as $widget) {
                                 $id = (string) 
$widget->attributes()->identifier;
                                 $title = (string) $widget->title;
                                 $description = (string) $widget->description;
@@ -299,18 +360,152 @@ class WookieConnectorService {
                                 if($iconURL == '') {
                                                $iconURL = (string) 
'http://www.oss-watch.ac.uk/images/logo2.gif';
                                        }
-                               $Widget = new Widget($id, $title, $description, 
$iconURL);
-                               $widgets[$id] = $Widget;
-                       }
-               } else {
+                                       $Widget = new Widget($id, $title, 
$description, $iconURL);
+                                       $widgets[$id] = $Widget;
+                               }
+                       } else {
                                throw new WookieConnectorException('Problem 
getting available widgets');
                        }
-                       
+
         } catch(WookieConnectorException $e) {
                        echo $e->errorMessage();
                }
-    return $widgets;
-  }
-    
+               return $widgets;
+       }
+
+       /**
+        * Set property for Widget instance
+        *
+        * @return new Property instance
+        * @throws WookieConnectorException, WookieWidgetInstanceException
+        */
+
+       public function setProperty($widgetInstance = null, $propertyInstance = 
null) {
+               $Url = $this->getConnection()->getURL().'properties';
+
+               try {
+                       if(!is_object($widgetInstance)) throw new 
WookieWidgetInstanceException('No Widget instance');
+                       if(!is_object($propertyInstance)) throw new 
WookieConnectorException('No properties instance');
+
+                       $data = array(
+                               'api_key' => 
$this->getConnection()->getApiKey(),
+                               'shareddatakey' => 
$this->getConnection()->getSharedDataKey(),
+                               'userid' => $this->getUser()->getLoginName(),
+                               'widgetid' => $widgetInstance->getIdentifier(),
+                               'propertyname' => $propertyInstance->getName(),
+                               'propertyvalue' => 
$propertyInstance->getValue(),
+                               'is_public' => $propertyInstance->isPublic(),
+                       );
+
+                       if(!$this->checkURL($Url)) {
+                               throw new WookieConnectorException("Properties 
rest URL is incorrect: ".$Url);
+                       }
+
+                       $response = $this->do_request($Url, $data);
+                       $statusCode = $response->getStatusCode();
+
+               switch($statusCode) {
+                 case 201:
+                       return $propertyInstance; //new property added, let's 
return initial Property instance
+                       break;
+                 case ($statusCode != 201):
+                       throw new 
WookieConnectorException($response->headerToString().'<br 
/>'.$response->getResponseText());
+                       break;
+                       }
+
+               } catch (WookieConnectorException $e) {
+                       echo $e->errorMessage();
+               } catch (WookieWidgetInstanceException $e) {
+                       echo '<b>function.setProperty:</b> 
'.$e->getMessage().'<br />';
+               }
+               return false;
+       }
+
+       /**
+        * Get property for Widget instance
+        *
+        * @return new Property(), if request fails, return false;
+        * @throws WookieConnectorException, WookieWidgetInstanceException
+        */
+
+       public function getProperty($widgetInstance = null, $propertyInstance = 
null) {
+               $Url = $this->getConnection()->getURL().'properties';
+
+               try {
+                       if(!is_object($widgetInstance)) throw new 
WookieWidgetInstanceException('No Widget instance');
+                       if(!is_object($propertyInstance)) throw new 
WookieConnectorException('No properties instance');
+
+                       $data = array(
+                               'api_key' => 
$this->getConnection()->getApiKey(),
+                               'shareddatakey' => 
$this->getConnection()->getSharedDataKey(),
+                               'userid' => $this->getUser()->getLoginName(),
+                               'widgetid' => $widgetInstance->getIdentifier(),
+                               'propertyname' => $propertyInstance->getName()
+                       );
+                       $request = @http_build_query($data);
+
+                       if(!$this->checkURL($Url)) {
+                               throw new WookieConnectorException("Properties 
rest URL is incorrect: ".$Url);
+                       }
+
+                       $response = new 
HTTP_Response(@file_get_contents($Url.'?'.$request, false, 
$this->getHttpStreamContext()), $http_response_header);
+                       $statusCode = $response->getStatusCode();
+                       if($statusCode != 200) {
+                               throw new 
WookieConnectorException($response->headerToString().'<br 
/>'.$response->getResponseText());
+                       }
+                       return new Property($propertyInstance->getName(), 
$response->getResponseText());
+
+               } catch (WookieConnectorException $e) {
+                       echo $e->errorMessage();
+               } catch (WookieWidgetInstanceException $e) {
+                       echo '<b>function.getProperty:</b> 
'.$e->getMessage().'<br />';
+               }
+               return false;
+       }
+
+       /**
+        * Delete property for Widget instance
+        *
+        * @access Public
+        * @return boolean true/false -- true if deleted, false if doesnt exist
+        * @throws WookieConnectorException, WookieWidgetInstanceException
+        */
+
+       public function deleteProperty($widgetInstance = null, 
$propertyInstance = null) {
+               $Url = $this->getConnection()->getURL().'properties';
+
+               try {
+                       if(!is_object($widgetInstance)) throw new 
WookieWidgetInstanceException('No Widget instance');
+                       if(!is_object($propertyInstance)) throw new 
WookieConnectorException('No properties instance');
+
+                       $request = 
'?api_key='.$this->getConnection()->getApiKey();
+                       $request .= 
'&shareddatakey='.$this->getConnection()->getSharedDataKey();
+                       $request .= '&userid='.$this->getUser()->getLoginName();
+                       $request .= 
'&widgetid='.$widgetInstance->getIdentifier();
+                       $request .= 
'&propertyname='.$propertyInstance->getName();
+
+                       if(!$this->checkURL($Url)) {
+                               throw new WookieConnectorException("Properties 
rest URL is incorrect: ".$Url);
+                       }
+
+                       $response = $this->do_request($Url.$request, false, 
'DELETE');
+                       $statusCode = $response->getStatusCode();
+                       
+                       if($statusCode != 200 && $statusCode != 404) {
+                               throw new 
WookieConnectorException($response->headerToString().'<br 
/>'.$response->getResponseText());
+                       }
+                       if($statusCode == 404) {
+                               return false;
+                       }
+                       return true;
+
+               } catch (WookieConnectorException $e) {
+                       echo $e->errorMessage();
+               } catch (WookieWidgetInstanceException $e) {
+                       echo '<b>function.getProperty:</b> 
'.$e->getMessage().'<br />';
+               }
+               return false;
+       }
+
 }
 ?>
\ No newline at end of file

Modified: incubator/wookie/trunk/connector/php/WookieServerConnection.php
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/connector/php/WookieServerConnection.php?rev=921050&r1=921049&r2=921050&view=diff
==============================================================================
--- incubator/wookie/trunk/connector/php/WookieServerConnection.php (original)
+++ incubator/wookie/trunk/connector/php/WookieServerConnection.php Tue Mar  9 
18:37:54 2010
@@ -1,4 +1,5 @@
 <?php
+
 /*
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -17,89 +18,112 @@
  * A connection to a Wookie server. This maintains the necessary data for
  * connecting to the server and provides utility methods for making common 
calls
  * via the Wookie REST API.
- * 
+ *
  */
 
- 
+
 class WookieServerConnection {
-  private $url;
-  private $apiKey = "TEST";
-  private $sharedDataKey = "mysharedkey";
-
-  /**
-   * Create a connection to a Wookie server at a given URL.
-   * @param url the URL of the wookie server
-   * @param apiKey the API key for the server
-   * @param sharedDataKey the sharedDataKey for the server connection 
-   * 
-   * @throws WookieConnectorException if there is a problem setting up this 
connection.
-   */
-  function __construct($url, $apiKey, $sharedDataKey) {
-    $this->setURL($url);
-    $this->setApiKey($apiKey);
-    $this->setSharedDataKey($sharedDataKey);
-  }
-
-  /**
-   * Get the URL of the wookie server.
-   * 
-   * @return
-   * @throws WookieConnectionException
-   */
-  public function getURL() {
-    return $this->url;
-  }
-  
-  /**
-   * Set the URL of the wookie server.
-   * 
-   * @throws WookieConnectionException
-   */
-  public function setURL($newUrl) {
-    $this->url = $newUrl;
-  }
-  
-  /**
-   * Get the API key for this server.
-   * 
-   * @return
-   */
-  public function getApiKey() {
-    return $this->apiKey;
-  }
-
-  /**
-   * Set the API key for this server.
-   * 
-   */
-  public function setApiKey($newApiKey) {
-    $this->apiKey = $newApiKey;
-  }
-
-  /**
-   * Get the shared data key for this server.
-   * 
-   * @return
-   */
-  public function getSharedDataKey() {
-    return $this->sharedDataKey;
-  }
-
-  /**
-   * Set the shared data key for this server.
-   *
-   */
-  public function setSharedDataKey($newKey) {
-    $this->sharedDataKey = $newKey;
-  }
-  
-  public function toString() {
-    $str = "Wookie Server Connection - ";
-    $str .= "URL: ".$this->getURL();
-    $str .= "API Key: ".$this->getApiKey();
-    $str .= "Shared Data Key: ".$this->getSharedDataKey();
-    return $str;
-  }
+       private $url;
+       private $apiKey = "TEST";
+       private $sharedDataKey = "mysharedkey";
+
+       /**
+        * Create a connection to a Wookie server at a given URL.
+        * @param url the URL of the wookie server
+        * @param apiKey the API key for the server
+        * @param sharedDataKey the sharedDataKey for the server connection
+        *
+        * @throws WookieConnectorException if there is a problem setting up 
this connection.
+        */
+       function __construct($url, $apiKey, $sharedDataKey) {
+               $this->setURL($url);
+               $this->setApiKey($apiKey);
+               $this->setSharedDataKey($sharedDataKey);
+       }
+
+       /**
+        * Get the URL of the wookie server.
+        *
+        * @return
+        * @throws WookieConnectionException
+        */
+       public function getURL() {
+               return $this->url;
+       }
+
+       /**
+        * Set the URL of the wookie server.
+        *
+        * @throws WookieConnectionException
+        */
+       public function setURL($newUrl) {
+               //parse url, if host == localhost, replace it with 127.0.0.1
+               // Bug causes Apache crash, while using file_get_contents 
function
+               // with 'localhost'
+               $urlParts = @parse_url($newUrl);
+               if($urlParts['host'] == 'localhost') {
+                       $newUrl = str_replace('localhost', '127.0.0.1', 
$newUrl);
+               }
+               $this->url = strrchr($newUrl, '/') != '/'?$newUrl.'/':$newUrl;
+       }
+
+       /**
+        * Get the API key for this server.
+        *
+        * @return
+        */
+       public function getApiKey() {
+               return $this->apiKey;
+       }
+
+       /**
+        * Set the API key for this server.
+        *
+        */
+       public function setApiKey($newApiKey) {
+               $this->apiKey = (string) $newApiKey;
+       }
+
+       /**
+        * Get the shared data key for this server.
+        *
+        * @return
+        */
+       public function getSharedDataKey() {
+               return $this->sharedDataKey;
+       }
+
+       /**
+        * Set the shared data key for this server.
+        *
+        */
+       public function setSharedDataKey($newKey) {
+               $this->sharedDataKey = $newKey;
+       }
+
+       public function toString() {
+               $str = "Wookie Server Connection - ";
+               $str .= "URL: ".$this->getURL();
+               $str .= "API Key: ".$this->getApiKey();
+               $str .= "Shared Data Key: ".$this->getSharedDataKey();
+               return $str;
+       }
+
+       /* Test Wookie server connection
+        *  @return boolean - true if success, otherwise false
+        */
+
+       public function Test() {
+               $ctx = @stream_context_create(array('http' => array('timeout' 
=> 15)));
+               $response = new 
HTTP_Response(@file_get_contents($this->getURL().'advertise?all=true', false, 
$ctx), $http_response_header);
+               if($response->getStatusCode() == 200) {
+                       $xmlDoc = 
@simplexml_load_string($response->getResponseText());
+                       if(is_object($xmlDoc) && $xmlDoc->getName() == 
'widgets') {
+                               return true;
+                       }
+               }
+               return false;
+       }
 }
 
 


Reply via email to