Improving and Refactoring some task to dispatch multipart request on REST API Dispatching routine

This commit is contained in:
Erik Amaru Ortiz
2014-07-08 09:57:47 -04:00
parent 4326ea3e84
commit 8cfa67ca6d

View File

@@ -5,24 +5,52 @@ use Maveriks\Util;
use ProcessMaker\Services; use ProcessMaker\Services;
use ProcessMaker\Services\Api; use ProcessMaker\Services\Api;
use Luracast\Restler\RestException; use Luracast\Restler\RestException;
/**
* Web application bootstrap
*
* @author Erik Amaru Ortiz <aortiz.erik@gmail.com>
*/
class WebApplication class WebApplication
{ {
protected $rootDir = "";
protected $workflowDir = "";
protected $workspaceDir = "";
protected $workspaceCacheDir = "";
protected $requestUri = "";
protected $responseMultipart = array();
const RUNNING_DEFAULT = "default.running"; const RUNNING_DEFAULT = "default.running";
const RUNNING_INDEX = "index.running"; const RUNNING_INDEX = "index.running";
const RUNNING_WORKFLOW = "workflow.running"; const RUNNING_WORKFLOW = "workflow.running";
const RUNNING_API = "api.running"; const RUNNING_API = "api.running";
const SERVICE_API = "service.api"; const SERVICE_API = "service.api";
const REDIRECT_DEFAULT = "redirect.default"; const REDIRECT_DEFAULT = "redirect.default";
/**
* @var string application root directory
*/
protected $rootDir = "";
/**
* @var string workflow directory
*/
protected $workflowDir = "";
/**
* @var string workspace directory located into shared directory
*/
protected $workspaceDir = "";
/**
* @var string workspace cache directory
*/
protected $workspaceCacheDir = "";
/**
* @var string request location uri
*/
protected $requestUri = "";
/**
* @var array holds multiple request response
*/
protected $responseMultipart = array();
/**
* @var \Maveriks\Extension\Restler main REST dispatcher object
*/
protected $rest;
/**
* class constructor
*/
public function __construct() public function __construct()
{ {
defined("DS") || define("DS", DIRECTORY_SEPARATOR); defined("DS") || define("DS", DIRECTORY_SEPARATOR);
@@ -61,6 +89,10 @@ class WebApplication
return $this->requestUri; return $this->requestUri;
} }
/**
* Routes the request to dispatch
* @return string
*/
public function route() public function route()
{ {
if ($this->requestUri === "/") { if ($this->requestUri === "/") {
@@ -78,6 +110,10 @@ class WebApplication
} }
} }
/**
* Run application
* @param string $type the request type to run and dispatch, by now only self::SERVICE_API is accepted
*/
public function run($type = "") public function run($type = "")
{ {
switch ($type) { switch ($type) {
@@ -86,8 +122,9 @@ class WebApplication
$this->loadEnvironment($request["workspace"]); $this->loadEnvironment($request["workspace"]);
Util\Logger::log("API::Dispatching ".$_SERVER["REQUEST_METHOD"]." ".$request["uri"]); Util\Logger::log("API::Dispatching ".$_SERVER["REQUEST_METHOD"]." ".$request["uri"]);
if (isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtoupper($_SERVER["HTTP_X_REQUESTED_WITH"]) == 'MULTIPART') { if (isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtoupper($_SERVER["HTTP_X_REQUESTED_WITH"]) == 'MULTIPART') {
$this->multipart($request["uri"], $request["version"]); $this->dispatchMultipleApiRequest($request["uri"], $request["version"]);
} else { } else {
$this->dispatchApiRequest($request["uri"], $request["version"]); $this->dispatchApiRequest($request["uri"], $request["version"]);
} }
@@ -97,19 +134,16 @@ class WebApplication
} }
/** /**
* This method performs the functionality of multipart * Dispatch multiple api request
* *
* @param string $version. Version Api * @param string $uri the request uri
* * @param string $version version of api
* @access public
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com> * @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
* @copyright Colosa - Bolivia
*
* @return void
*/ */
public function multipart($uri, $version = "1.0") public function dispatchMultipleApiRequest($uri, $version = "1.0")
{ {
$stringInput = file_get_contents('php://input'); $stringInput = file_get_contents('php://input');
if (empty($stringInput)) { if (empty($stringInput)) {
$rest = new \Maveriks\Extension\Restler(); $rest = new \Maveriks\Extension\Restler();
$rest->setMessage(new RestException(Api::STAT_APP_EXCEPTION, "Invalid Request, multipart without body.")); $rest->setMessage(new RestException(Api::STAT_APP_EXCEPTION, "Invalid Request, multipart without body."));
@@ -124,22 +158,25 @@ class WebApplication
} }
$baseUrl = (empty($input->base_url)) ? $uri : $input->base_url; $baseUrl = (empty($input->base_url)) ? $uri : $input->base_url;
foreach($input->calls as $value) { foreach($input->calls as $value) {
$_SERVER["REQUEST_METHOD"] = (empty($value->method)) ? 'GET' : $value->method; $_SERVER["REQUEST_METHOD"] = empty($value->method) ? 'GET' : $value->method;
$uriTemp = trim($baseUrl) . trim($value->url); $uriTemp = trim($baseUrl) . trim($value->url);
if (strpos($uriTemp, '?') !== false) { if (strpos($uriTemp, '?') !== false) {
$dataGet = explode('?', $uriTemp); $dataGet = explode('?', $uriTemp);
parse_str($dataGet[1], $_GET); parse_str($dataGet[1], $_GET);
} }
$inputExecute = (empty($value->data)) ? '' : json_encode($value->data);
$inputExecute = empty($value->data) ? '' : json_encode($value->data);
$this->responseMultipart[] = $this->dispatchApiRequest($uriTemp, $version, true, $inputExecute); $this->responseMultipart[] = $this->dispatchApiRequest($uriTemp, $version, true, $inputExecute);
} }
echo json_encode($this->responseMultipart); echo json_encode($this->responseMultipart);
} }
/** /**
* This method dispatch rest/api service * This method dispatch rest/api service
*
* @author Erik Amaru Ortiz <erik@colosa.com> * @author Erik Amaru Ortiz <erik@colosa.com>
*/ */
public function dispatchApiRequest($uri, $version = "1.0", $multipart = false, $inputExecute = '') public function dispatchApiRequest($uri, $version = "1.0", $multipart = false, $inputExecute = '')
@@ -161,6 +198,22 @@ class WebApplication
*/ */
header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Origin: *');
if (is_null($this->rest)) {
$this->initRest($uri, $version, $multipart, $inputExecute);
}
$this->rest->handle();
if ($this->rest->flagMultipart === true) {
return $this->rest->responseMultipart;
}
}
/**
* create a new instance of local $rest Restler object
*/
protected function initRest($uri, $version, $multipart = false, $inputExecute = '')
{
require_once $this->rootDir . "/framework/src/Maveriks/Extension/Restler/UploadFormat.php"; require_once $this->rootDir . "/framework/src/Maveriks/Extension/Restler/UploadFormat.php";
// $servicesDir contains directory where Services Classes are allocated // $servicesDir contains directory where Services Classes are allocated
@@ -209,14 +262,14 @@ class WebApplication
// create a new Restler instance // create a new Restler instance
//$rest = new \Luracast\Restler\Restler(); //$rest = new \Luracast\Restler\Restler();
$rest = new \Maveriks\Extension\Restler($productionMode); $this->rest = new \Maveriks\Extension\Restler($productionMode);
// setting flag for multipart to Restler // setting flag for multipart to Restler
$rest->setFlagMultipart($multipart); $this->rest->setFlagMultipart($multipart);
$rest->inputExecute = $inputExecute; $this->rest->inputExecute = $inputExecute;
// setting api version to Restler // setting api version to Restler
$rest->setAPIVersion($version); $this->rest->setAPIVersion($version);
// adding $authenticationClass to Restler // adding $authenticationClass to Restler
$rest->addAuthenticationClass($authenticationClass, ''); $this->rest->addAuthenticationClass($authenticationClass, '');
// Setting database connection source // Setting database connection source
list($host, $port) = strpos(DB_HOST, ':') !== false ? explode(':', DB_HOST) : array(DB_HOST, ''); list($host, $port) = strpos(DB_HOST, ':') !== false ? explode(':', DB_HOST) : array(DB_HOST, '');
@@ -226,7 +279,7 @@ class WebApplication
// Setting default OAuth Client id, for local PM Web Designer // Setting default OAuth Client id, for local PM Web Designer
Services\OAuth2\Server::setPmClientId($pmOauthClientId); Services\OAuth2\Server::setPmClientId($pmOauthClientId);
$rest->setOverridingFormats('JsonFormat', 'UploadFormat'); $this->rest->setOverridingFormats('JsonFormat', 'UploadFormat');
$isPluginRequest = strpos($uri, '/plugin-') !== false ? true : false; $isPluginRequest = strpos($uri, '/plugin-') !== false ? true : false;
@@ -248,13 +301,14 @@ class WebApplication
foreach ($classesList as $classFile) { foreach ($classesList as $classFile) {
if (pathinfo($classFile, PATHINFO_EXTENSION) === 'php') { if (pathinfo($classFile, PATHINFO_EXTENSION) === 'php') {
require_once $classFile; $relClassPath = str_replace('.php', '', str_replace($servicesDir, '', $classFile));
$namespace = '\\ProcessMaker\\Services\\' . str_replace( $namespace = '\\ProcessMaker\\Services\\' . str_replace(DS, '\\', $relClassPath);
DIRECTORY_SEPARATOR,
'\\', if (! class_exists($namespace)) {
str_replace('.php', '', str_replace($servicesDir, '', $classFile)) require_once $classFile;
); }
$rest->addAPIClass($namespace);
$this->rest->addAPIClass($namespace);
} }
} }
@@ -264,7 +318,7 @@ class WebApplication
if (is_array($aliasData)) { if (is_array($aliasData)) {
foreach ($aliasData as $label => $namespace) { foreach ($aliasData as $label => $namespace) {
$namespace = '\\' . ltrim($namespace, '\\'); $namespace = '\\' . ltrim($namespace, '\\');
$rest->addAPIClass($namespace, $alias); $this->rest->addAPIClass($namespace, $alias);
} }
} }
} }
@@ -282,12 +336,6 @@ class WebApplication
// } // }
// } // }
} }
$rest->handle();
if ($rest->flagMultipart === true) {
return $rest->responseMultipart;
}
} }
public function parseApiRequestUri() public function parseApiRequestUri()