Skip to content
This repository has been archived by the owner on Mar 14, 2023. It is now read-only.

Commit

Permalink
MINOR Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chillu committed Jun 4, 2012
0 parents commit 929f888
Show file tree
Hide file tree
Showing 9 changed files with 675 additions and 0 deletions.
24 changes: 24 additions & 0 deletions LICENSE
@@ -0,0 +1,24 @@
* Copyright (c) 2012, Silverstripe Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Silverstripe Ltd. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Silverstripe Ltd. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 changes: 48 additions & 0 deletions README.md
@@ -0,0 +1,48 @@
# SilverStripe RestfulServer Module

## Overview

SOAP server class which auto-generates a WSDL file to initialize PHPs integrated `SoapServer` functionality.
Extended by `SOAPModelAccess` to scaffold WSDL for a specific class.

**This module is just a wrapper for the "[restfulserver](https://github.com/silverstripe/silverstripe-restfulserver)" module,
internally all SOAP calls are rewritten as RESTful calls**

## Requirements

* SilverStripe 3.0 or newer
* "[restfulserver](https://github.com/silverstripe/silverstripe-restfulserver)" module

## Configuration

Example DataObject with simple api access, giving full access to all object properties and relations,
unless explicitly controlled through model permissions.

class Article extends DataObject {
static $db = array('Title'=>'Text','Published'=>'Boolean');
static $api_access = true;
}

## Usage

Getting a record:

$c = new SoapClient('http://mysite.com/soap/v1/wsdl');
echo $c->getXML("MyClassName", 99); // gets record #99 as xml

Updating a record:

$c = new SoapClient('http://mysite.com/soap/v1/wsdl');
$data = array('MyProperty' => 'MyUpdatedValue');
echo $c->putXML("MyClassName", 99, null, $data);

Creating a record:

$c = new SoapClient('http://mysite.com/soap/v1/wsdl');
$data = array('MyProperty' => 'MyValue');
echo $c->putXML("MyClassName", null, null, $data);

Creating a record:

$c = new SoapClient('http://mysite.com/soap/v1/wsdl');
echo $c->deleteXML("MyClassName");
Empty file added _config.php
Empty file.
223 changes: 223 additions & 0 deletions code/SOAPModelAccess.php
@@ -0,0 +1,223 @@
<?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}.
*
* @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>";
}
}

0 comments on commit 929f888

Please sign in to comment.