SoapServer.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\service;
12 
19 
20 /**
21  * SoapServer extends nusoap server to actually process
22  * requests inside the application context.
23  *
24  * @author ingo herwig <ingo@wemove.com>
25  */
26 class SoapServer extends \nusoap_server {
27 
28  const TNS = 'http://wcmf.sourceforge.net';
29 
30  private $_application = null;
31 
32  private static $_logger = null;
33 
34  /**
35  * Constructor
36  */
37  public function __construct() {
38  if (self::$_logger == null) {
39  self::$_logger = LogManager::getLogger(__CLASS__);
40  }
41  $scriptURL = URIUtil::getProtocolStr().$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
42  $endpoint = dirname($scriptURL).'/soap';
43  $this->configureWSDL('SOAPService', self::TNS, $endpoint, 'document');
44  $this->wsdl->schemaTargetNamespace = self::TNS;
45 
46  // register default complex types
47  $this->wsdl->addComplexType(
48  'OidList',
49  'complexType',
50  'array',
51  '',
52  'SOAP-ENC:Array',
53  array(),
54  array(
55  array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'xsd:string[]')
56  ),
57  'xsd:string'
58  );
59 
60  $this->wsdl->addComplexType(
61  'SearchResultList',
62  'complexType',
63  'array',
64  '',
65  'SOAP-ENC:Array',
66  array(),
67  array(
68  array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:SearchResultItem[]')
69  ),
70  'tns:SearchResultItem'
71  );
72 
73  $this->wsdl->addComplexType('SearchResultItem', 'complexType', 'struct', 'sequence', '',
74  array(
75  'type' => array('name' => 'type', 'type' => 'xsd:string'),
76  'oid' => array('name' => 'oid', 'type' => 'xsd:string'),
77  'displayValue' => array('name' => 'displayValue', 'type' => 'xsd:string'),
78  'summary' => array('name' => 'summary', 'type' => 'xsd:string')
79  )
80  );
81 
82  // initialize application
83  $this->_application = new Application();
84  try {
85  $this->_application->initialize();
86  }
87  catch (\Exception $ex) {
88  $this->handleException($ex);
89  }
90  }
91 
92  /**
93  * @see nusoap_server::service
94  */
95  public function service($data) {
96  if (self::$_logger->isDebugEnabled()) {
97  self::$_logger->debug($data);
98  }
99  try {
100  $oldErrorReporting = error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
101  parent::service($data);
102  error_reporting($oldErrorReporting);
103  }
104  catch (\Exception $ex) {
105  $this->handleException($ex);
106  }
107  }
108 
109  /**
110  * Get a dummy object id to be used in a request
111  * @param $type The entity type
112  * @return ObjectId
113  */
114  public function getDummyOid($type) {
115  return new ObjectId($type);
116  }
117 
118  /**
119  * Process a soap call
120  * @param $action The action
121  * @param $params The action parameters
122  * @return The Response instance from the executed Controller
123  */
124  public function doCall($action, $params) {
125  if (self::$_logger->isDebugEnabled()) {
126  self::$_logger->debug("SoapServer action: ".$action);
127  self::$_logger->debug($params);
128  }
129  $authHeader = $this->requestHeader['Security']['UsernameToken'];
130 
131  $request = ObjectFactory::getInstance('request');
132  $request->setAction('actionSet');
133  $request->setFormat('soap');
134  $request->setResponseFormat('null');
135  $request->setValues(array(
136  'data' => array(
137  'action1' => array(
138  'action' => 'login',
139  'params' => array(
140  'user' => $authHeader['Username'],
141  'password' => $authHeader['Password']['!']
142  )
143  ),
144  'action2' => array(
145  'action' => $action,
146  'params' => $params
147  ),
148  'action3' => array(
149  'action' => 'logout'
150  )
151  )
152  ));
153 
154  // run the application
155  $actionResponse = ObjectFactory::getInstance('response');
156  $actionResponse->setFinal();
157  try {
158  $response = $this->_application->run($request);
159  if ($response->hasErrors()) {
160  $errors = $response->getErrors();
161  $this->handleException(new ApplicationException($request, $response, $errors[0]));
162  }
163  else {
164  $responseData = $response->getValue('data');
165  $data = $responseData['action2'];
166  $actionResponse->setSender($data['controller']);
167  $actionResponse->setContext($data['context']);
168  $actionResponse->setAction($data['action']);
169  $actionResponse->setFormat('soap');
170  $actionResponse->setValues($data);
171  $formatter = ObjectFactory::getInstance('formatter');
172  $formatter->serialize($actionResponse);
173  if (self::$_logger->isDebugEnabled()) {
174  self::$_logger->debug($actionResponse->__toString());
175  }
176  }
177  }
178  catch (\Exception $ex) {
179  $this->handleException($ex);
180  }
181  return $actionResponse;
182  }
183 
184  /**
185  * Handle an exception
186  * @param $ex
187  */
188  private function handleException($ex) {
189  self::$_logger->error($ex->getMessage()."\n".$ex->getTraceAsString());
190  $this->fault('SOAP-ENV:SERVER', $ex->getMessage(), '', '');
191  }
192 }
193 ?>
Application is the main application class, that does all the initialization.
Definition: Application.php:23
ObjectId is the unique identifier of an object.
Definition: ObjectId.php:27
static getLogger($name)
Get the logger with the given name.
Definition: LogManager.php:35
static getInstance($name, $dynamicConfiguration=array())
static getProtocolStr()
Definition: URIUtil.php:156
Service related interfaces and classes.
Definition: namespaces.php:92
SoapServer extends nusoap server to actually process requests inside the application context...
Definition: SoapServer.php:26
__construct()
Constructor.
Definition: SoapServer.php:37
getDummyOid($type)
Get a dummy object id to be used in a request.
Definition: SoapServer.php:114
doCall($action, $params)
Process a soap call.
Definition: SoapServer.php:124
ApplicationException signals a general application exception.