Permalink
Browse files

API CHANGE Moved SapphireSoapServer and SOAPModelAccess into its own …

  • Loading branch information...
1 parent cb8b118 commit 9e21fb8a4a0770ab753cf826d935d1d7e3cc169d @chillu chillu committed Jun 4, 2012
@@ -1,249 +0,0 @@
-<?php
-/**
- * Basic SOAP Server to access and modify DataObject instances.
- * You can enable SOAP access on a DataObject by setting {@link DataObject::$api_access} to true.
- * This means that you'll also enable a RESTful API through {@link RestfulServer}.
- *
- * Usage - Getting a record:
- * <code>
- * $c = new SoapClient('http://mysite.com/soap/v1/wsdl');
- * echo $c->getXML("MyClassName", 99); // gets record #99 as xml
- * </code>
- *
- * Usage - Updating a record:
- * <code>
- * $c = new SoapClient('http://mysite.com/soap/v1/wsdl');
- * $data = array('MyProperty' => 'MyUpdatedValue');
- * echo $c->putXML("MyClassName", 99, null, $data);
- * </code>
- *
- * Usage - Creating a record:
- * <code>
- * $c = new SoapClient('http://mysite.com/soap/v1/wsdl');
- * $data = array('MyProperty' => 'MyValue');
- * echo $c->putXML("MyClassName", null, null, $data);
- * </code>
- *
- * Usage - Creating a record:
- * <code>
- * $c = new SoapClient('http://mysite.com/soap/v1/wsdl');
- * echo $c->deleteXML("MyClassName");
- * </code>
- *
- * @todo Test relation methods
- *
- * @package framework
- * @subpackage api
- */
-class SOAPModelAccess extends SapphireSoapServer {
-
- public static $methods = array(
- 'getXML' => array(
- 'class' => 'string',
- 'id' => 'int',
- 'relation' => 'string',
- '_returns' => 'string',
- ),
- 'getJSON' => array(
- 'class' => 'string',
- 'id' => 'int',
- 'relation' => 'string',
- '_returns' => 'string',
- ),
- 'putXML' => array(
- 'class' => 'string',
- 'id' => 'int',
- 'relation' => 'string',
- 'data' => 'string',
- 'username' => 'string',
- 'password' => 'string',
- '_returns' => 'boolean',
- ),
- 'putJSON' => array(
- 'class' => 'string',
- 'id' => 'int',
- 'relation' => 'string',
- '_returns' => 'boolean',
- ),
- );
-
- function Link($action = null) {
- return Controller::join_links("soap/v1/", $action);
- }
-
- /**
- * Used to emulate RESTful GET requests with XML data.
- *
- * @param string $class
- * @param Number $id
- * @param string $relation Relation name
- * @return string
- */
- function getXML($class, $id, $relation = false, $username = null, $password = null) {
- $this->authenticate($username, $password);
-
- $response = Director::test(
- $this->buildRestfulURL($class, $id, $relation, 'xml'),
- null,
- null,
- 'GET'
- );
-
- return ($response->isError()) ? $this->getErrorMessage($response) : $response->getBody();
- }
-
- /**
- * Used to emulate RESTful GET requests with JSON data.
- *
- * @param string $class
- * @param Number $id
- * @param string $relation Relation name
- * @param string $username
- * @param string $password
- * @return string
- */
- function getJSON($class, $id, $relation = false, $username = null, $password = null) {
- $this->authenticate($username, $password);
-
- $response = Director::test(
- $this->buildRestfulURL($class, $id, $relation, 'json'),
- null,
- null,
- 'GET'
- );
-
- return ($response->isError()) ? $this->getErrorMessage($response) : $response->getBody();
- }
-
- /**
- * Used to emulate RESTful POST and PUT requests with XML data.
- *
- * @param string $class
- * @param Number $id
- * @param string $relation Relation name
- * @param array $data
- * @param string $username
- * @param string $password
- * @return string
- */
- function putXML($class, $id = false, $relation = false, $data, $username = null, $password = null) {
- $this->authenticate($username, $password);
-
- $response = Director::test(
- $this->buildRestfulURL($class, $id, $relation, 'xml'),
- array(),
- null,
- ($id) ? 'PUT' : 'POST',
- $data
- );
-
- return ($response->isError()) ? $this->getErrorMessage($response) : $response->getBody();
- }
-
- /**
- * Used to emulate RESTful POST and PUT requests with JSON data.
- *
- * @param string $class
- * @param Number $id
- * @param string $relation Relation name
- * @param array $data
- * @param string $username
- * @param string $password
- * @return string
- */
- function putJSON($class = false, $id = false, $relation = false, $data, $username = null, $password = null) {
- $this->authenticate($username, $password);
-
- $response = Director::test(
- $this->buildRestfulURL($class, $id, $relation, 'json'),
- array(),
- null,
- ($id) ? 'PUT' : 'POST',
- $data
- );
-
- return ($response->isError()) ? $this->getErrorMessage($response) : $response->getBody();
- }
-
- /**
- * Used to emulate RESTful DELETE requests.
- *
- * @param string $class
- * @param Number $id
- * @param string $relation Relation name
- * @param string $username
- * @param string $password
- * @return string
- */
- function deleteXML($class, $id, $relation = false, $username = null, $password = null) {
- $this->authenticate($username, $password);
-
- $response = Director::test(
- $this->buildRestfulURL($class, $id, $relation, 'xml'),
- null,
- null,
- 'DELETE'
- );
-
- return ($response->isError()) ? $this->getErrorMessage($response) : $response->getBody();
- }
-
- /**
- * Used to emulate RESTful DELETE requests.
- *
- * @param string $class
- * @param Number $id
- * @param string $relation Relation name
- * @param string $username
- * @param string $password
- * @return string
- */
- function deleteJSON($class, $id, $relation = false, $username = null, $password = null) {
- $this->authenticate($username, $password);
-
- $response = Director::test(
- $this->buildRestfulURL($class, $id, $relation, 'json'),
- null,
- null,
- 'DELETE'
- );
-
- return ($response->isError()) ? $this->getErrorMessage($response) : $response->getBody();
- }
-
- /**
- * Faking an HTTP Basicauth login in the PHP environment
- * that RestfulServer can pick up.
- *
- * @param string $username Username
- * @param string $password Plaintext password
- */
- protected function authenticate($username, $password) {
- if(is_string($username)) $_SERVER['PHP_AUTH_USER'] = $username;
- if(is_string($password)) $_SERVER['PHP_AUTH_PW'] = $password;
- }
-
- /**
- * @param string $class
- * @param Number $id
- * @param string $relation
- * @param string $extension
- * @return string
- */
- protected function buildRestfulURL($class, $id, $relation, $extension) {
- $url = "api/v1/{$class}";
- if($id) $url .= "/{$id}";
- if($relation) $url .= "/{$relation}";
- if($extension) $url .= "/.{$extension}";
- return $url;
- }
-
- /**
- * @param SS_HTTPResponse $response
- * @return string XML string containing the HTTP error message
- */
- protected function getErrorMessage($response) {
- return "<error type=\"authentication\" code=\"" . $response->getStatusCode() . "\">" . $response->getStatusDescription() . "</error>";
- }
-}
-
@@ -1,103 +0,0 @@
-<?php
-/**
- * Soap server class which auto-generates a WSDL
- * file to initialize PHPs integrated {@link SoapServer} class.
- *
- * See {@link SOAPModelAccess} for an auto-generated SOAP API for your models.
- *
- * @todo Improve documentation
- * @package framework
- * @subpackage integration
- */
-class SapphireSoapServer extends Controller {
-
- /**
- * @var array Map of method name to arguments.
- */
- static $methods = array();
-
- /**
- * @var array
- */
- static $xsd_types = array(
- 'int' => 'xsd:int',
- 'boolean' => 'xsd:boolean',
- 'string' => 'xsd:string',
- 'binary' => 'xsd:base64Binary',
- );
-
- static $allowed_actions = array(
- 'index',
- 'wsdl'
- );
-
- function wsdl() {
- $this->getResponse()->addHeader("Content-Type", "text/xml");
-
- return array();
- }
-
- /**
- * @return string
- */
- function getWSDLURL() {
- return Director::absoluteBaseURLWithAuth() . $this->Link() . "wsdl";
- }
-
- /**
- * @return SS_List Collection of ArrayData elements describing
- * the method (keys: 'Name', 'Arguments', 'ReturnType')
- */
- function Methods() {
- $methods = array();
-
- foreach($this->stat('methods') as $methodName => $arguments) {
- $returnType = $arguments['_returns'];
- unset($arguments['_returns']);
-
- $processedArguments = array();
- foreach($arguments as $argument => $type) {
- $processedArguments[] = new ArrayData(array(
- "Name" => $argument,
- "Type" => self::$xsd_types[$type],
- ));
-
- }
- $methods[] = new ArrayData(array(
- "Name" => $methodName,
- "Arguments" => new ArrayList($processedArguments),
- "ReturnType" => self::$xsd_types[$returnType],
- ));
- }
-
- return new ArrayList($methods);
- }
-
- /**
- * @return string
- */
- function TargetNamespace() {
- return Director::absoluteBaseURL();
- }
-
- /**
- * @return string
- */
- function ServiceURL() {
- return Director::absoluteBaseURLWithAuth() . $this->class . '/';
- }
-
- function index() {
- $wsdl = $this->getViewer('wsdl')->process($this);
- $wsdlFile = TEMP_FOLDER . '/sapphire-wsdl-' . $this->class;
- $fh = fopen($wsdlFile, 'w');
- fwrite($fh, $wsdl);
- fclose($fh);
-
- $s = new SoapServer($wsdlFile, array('cache_wsdl' => WSDL_CACHE_NONE));
- $s->setClass($this->class);
- $s->handle();
- }
-}
-
-
Oops, something went wrong.

0 comments on commit 9e21fb8

Please sign in to comment.