BUG 9855 REST services for ProcessMaker Solved

PROBLEM There were no documentation for use of Rest by Curl and Rest with Java use.
SOLVED Adding classes to help to use Rest by Curl and Adding samples to use Rest with Java.
This commit is contained in:
Ralph Asendeteufrer
2012-10-02 13:13:19 -04:00
parent 19a75a730c
commit 6372bf808a
8 changed files with 501 additions and 390 deletions

View File

@@ -1,6 +1,4 @@
//Archivo HolaMundo.java
// Main class to sent differen kind of messages
// Main class to sent differen kind of messages to the http server
import org.apache.http.impl.client.DefaultHttpClient;
// Enter CRUD memebers
@@ -12,14 +10,18 @@ import org.apache.http.client.methods.HttpDelete;
// Used to set JSON or XML messages request
import org.apache.http.entity.StringEntity;
// Needed for response goal
// Needed for response fetch goal
import org.apache.http.HttpResponse;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
/**
* Single class containing functions to show how to use GET,POST,PUT,DELETE methods.
*/
public class CRUD
{
private static String m_user = "ralph"; // This member variable must be changed to its own dev username
private static void PostSample()
{
System.out.println("POST: Enter login params\n");
@@ -29,7 +31,7 @@ public class CRUD
+"<user>admin</user>\n"
+"<password>admin</password>\n"
+"</request>";
String URI = "http://ralph.pmos.colosa.net/rest/ralph/login/";
String URI = "http://"+m_user+".pmos.colosa.net/rest/"+m_user+"/login/";
System.out.println( "Request: "+URI + "\n"+ loginParamsXML + "\n");
@@ -52,7 +54,7 @@ public class CRUD
}
catch( java.io.IOException x)
{
throw new RuntimeException("I/O error" + x.toString());
throw new RuntimeException("I/O error: " + x.toString());
}
}
@@ -60,7 +62,7 @@ public class CRUD
{
System.out.println("GET: Display TRANSLATION table row\n");
String URI = "http://ralph.pmos.colosa.net/rest/ralph/TRANSLATION/LABEL/LOGIN/en/";
String URI = "http://"+m_user+".pmos.colosa.net/rest/"+m_user+"/TRANSLATION/LABEL/LOGIN/en/";
System.out.println( "Request: " + URI + "\n");
DefaultHttpClient httpClient = new DefaultHttpClient();
@@ -79,7 +81,7 @@ public class CRUD
}
catch( java.io.IOException x)
{
throw new RuntimeException("I/O error" + x.toString());
throw new RuntimeException("I/O error: " + x.toString());
}
}
@@ -87,7 +89,7 @@ public class CRUD
{
System.out.println("POST: Insert new row in TRANLATION\n");
String URI = "http://ralph.pmos.colosa.net/rest/ralph/TRANSLATION/";
String URI = "http://"+m_user+".pmos.colosa.net/rest/"+m_user+"/TRANSLATION/";
String newRow = "BUTTON/ESCAPE/en/sample/2012-05-05/";
System.out.println( "Request: " + URI + " new row: " + newRow + "\n");
URI = URI + newRow;
@@ -113,7 +115,7 @@ public class CRUD
}
catch( java.io.IOException x)
{
throw new RuntimeException("I/O error" + x.toString());
throw new RuntimeException("I/O error: " + x.toString());
}
}
@@ -121,7 +123,7 @@ public class CRUD
{
System.out.println("POST: Update a row in TRANLATION\n");
String URI = "http://ralph.pmos.colosa.net/rest/ralph/TRANSLATION/";
String URI = "http://"+m_user+".pmos.colosa.net/rest/"+m_user+"/TRANSLATION/";
String index = "BUTTON/ESCAPE/en/";
String updateData = "changesample/2011-07-06/";
@@ -149,7 +151,7 @@ public class CRUD
}
catch( java.io.IOException x)
{
throw new RuntimeException("I/O error" + x.toString());
throw new RuntimeException("I/O error: " + x.toString());
}
}
@@ -157,7 +159,7 @@ public class CRUD
{
System.out.println("DELETE: Remove a row in TRANLATION\n");
String URI = "http://ralph.pmos.colosa.net/rest/ralph/TRANSLATION/";
String URI = "http://"+m_user+".pmos.colosa.net/rest/"+m_user+"/TRANSLATION/";
String index = "BUTTON/ESCAPE/en/";
System.out.println( "Request: " + URI + "index:" + index + "\n");
@@ -184,17 +186,18 @@ public class CRUD
}
catch( java.io.IOException x)
{
throw new RuntimeException("I/O error" + x.toString());
throw new RuntimeException("I/O error: " + x.toString());
}
}
public static void main(String args[])
{
System.out.println("CRUD sample.");
System.out.println("CRUD samples.");
PostSample();
GetSample();
AnotherPostSample();
PutSample();
DeleteSample();
}
}

View File

@@ -1,21 +1,38 @@
<?php
/**
* Abstract class containing the CURL functionality, used to handle GET, POST, PUT and DELETE http methods.
*
* This class uses many different Curl functions, it would be great if this one gorws to allow the use of alll of them.
*
* @category Zend
* @package ProcessMaker
* @subpackage workflow
* @copyright Copyright (c) ProcessMaker Colosa S.A.
* @version Release: @2.0.44@
* @since Class available since Release 2.0.44
*/
define('PATH_SEP', '/');
define('COLON', ':');
define('DOT', '.');
define('PROTOCOL_HTTP', 'http');
// Abstract class, containing the CURL functionality, used to handle GET, POST, PUT and DELETE http methods.
/**
* Abstract class, containing the CURL functionality, used to handle GET, POST, PUT and DELETE http methods.
*/
abstract class CURLMessage
{
var $restServer; // e.g. "http://ralph.pmos.colosa.net/rest/ralph/"; host + technich dir + user dir
var $content = "Content-Type: application/"; //set the string used to attach next the kind of message to be handle.
var $serviceTechnic = "rest";// name of the current durectory where the rest methods are.
var $server_method; // used to set the name of the remote host and the Rest method to be called.
var $type; // contains the type of the message.
var $ch; //curl handle instance.
var $output; // contains the output returned by the curl_exec function.
protected $restServer; // e.g. "http://ralph.pmos.colosa.net/rest/ralph/"; host + technich dir + user dir
protected $content = "Content-Type: application/"; //set the string used to attach next the kind of message to be handle.
protected $serviceTechnic = "rest";// name of the current durectory where the rest methods are.
protected $server_method; // used to set the name of the remote host and the Rest method to be called.
protected $type; // contains the type of the message.
protected $ch; //curl handle instance.
protected $output; // contains the output returned by the curl_exec function.
/**
* Setting the remote host and init the Curl handle options
*/
function __construct()
{
$serverDNS = explode(DOT,$_SERVER['SERVER_NAME']);
@@ -31,51 +48,67 @@ abstract class CURLMessage
curl_setopt($this->ch,CURLOPT_POST, 1);
curl_setopt($this->ch,CURLOPT_RETURNTRANSFER, 1);
}
//set the message in order to follow the message format
/**
* set the message in order to follow the message format
*/
abstract protected function format(array $message);
//Set properties used in a simpleMessage Class like a set in a URI, or formatted as a JSon msg.
/**
* Set properties used in a simpleMessage Class like a set in a URI, or formatted as a JSon msg.
*/
abstract protected function setMoreProperties($messageFormated);
//attach the method to the restServer path, set the type of the message, and the message itself.
/**
* Attach the method to the restServer path, set the type of the message, and the message itself.
*/
protected function setMessageProperties($method,array $message)
{
$messageFormated = $this->format($message);
$this->server_method = $this->restServer . $method;
$this->setMoreProperties($messageFormated);
}
//Send or execute(curl notation) the message using a rest method
/**
* Send or execute(curl notation) the message using a rest method
**/
public function send($method,array $message)
{
self::setMessageProperties($method,$message);
$this->output = curl_exec($this->ch);
return $this->output;
}
//set the message to GET method type
/**
* Set the message to GET method type
*/
public function sendGET($method,array $message)
{
curl_setopt($this->ch, CURLOPT_HTTPGET,true);
return $this->send($method,$message);
}
//set the message to POST method type
/**
* Set the message to POST method type
*/
public function sendPOST($method,array $message)
{
curl_setopt($this->ch,CURLOPT_POST,true);
return $this->send($method,$message);
}
//set the message to PUT method type
/**
* Set the message to PUT method type
*/
public function sendPUT($method,array $message)
{
curl_setopt($this->ch,CURLOPT_PUT,true);
return $this->send($method,$message);
}
//set the message to DELETE method type
/**
* Set the message to DELETE method type
*/
public function sendDELETE($method,array $message)
{
curl_setopt($this->ch,CURLOPT_CUSTOMREQUEST,"DELETE");
return $this->send($method,$message);
}
//Display all the data that the response could got.
/**
* Display all the data that the response could got.
*/
public function displayResponse()
{
$error = curl_error($this->ch);
@@ -84,8 +117,7 @@ abstract class CURLMessage
'curl_error' => '',
'http_code' => '',
'last_url' => '');
if ( $error != "" )
{
if ($error != ""){
$result['curl_error'] = $error;
return $result;
}
@@ -99,14 +131,15 @@ abstract class CURLMessage
echo $this->type." Response: ".$response."<BR>";
foreach($result as $index => $data)
{
if( $data != "")
{
if($data != ""){
echo $index . "=" . $data . "<BR>";
}
}
echo "<BR>";
}
//Close the Curl session using the Curl Handle set by curl_init() function.
/**
* Close the Curl session using the Curl Handle set by curl_init() function.
*/
public function close()
{
curl_close($this->ch);

View File

@@ -1,19 +1,36 @@
<?php
/**
* Class defined to set all the configuration that XML, Json or other needs.
*
*
* @category Zend
* @package ProcessMaker
* @subpackage workflow
* @copyright Copyright (c) ProcessMaker Colosa S.A.
* @version Release: @2.0.44@
* @since Class available since Release 2.0.44
*/
require_once("CURLMessage.php");
//Class defined to set all the configuration that XML, Json or other needs.
/**
* Class defined to set all the configuration that XML, Json or other needs.
*/
class FormatedMessage extends CURLMessage
{
public function FormatedMessage()
{
parent::__construct();
}
//set the message in order to follow the message format, empty caused this class should not be instanced
/**
* Set the message in order to follow the message format, empty caused this class should not be instanced
*/
protected function format(array $message)
{
}
//Setting CURL Url, Content and the message to be send
/**
* Setting CURL Url, Content and the message to be send
*/
protected function setMoreProperties($messageFormated)
{
//Please, remove this comment, is only for looging proposes.

View File

@@ -1,25 +1,39 @@
<?php
/**
* Class defined to be instanced and handle Json messages.
*
*
* @category Zend
* @package ProcessMaker
* @subpackage workflow
* @copyright Copyright (c) ProcessMaker Colosa S.A.
* @version Release: @2.0.44@
* @since Class available since Release 2.0.44
*/
require_once("FormatedMessage.php");
//Class defined to be instanced and handle Json messages
/**
* Class defined to be instanced and handle Json messages
*/
class JsonMessage extends FormatedMessage
{
//Call the parent Curl initialization and set the type of the message
/**
* Call the parent Curl initialization and set the type of the message
*/
public function JsonMessage()
{
parent::__construct();
$this->type = "json";
}
//Format the array parameter to a json format.
/**
* Format the array parameter to a json format.
*/
protected function format(array $message)
{
if ( empty( $message) )
{
if (empty($message)){
return ;
}
if ( is_array( $message) )
{
if (is_array($message)){
$jsonMessage = json_encode( $message);
}
return $jsonMessage;

View File

@@ -1,24 +1,38 @@
<?php
/**
* Class defined to be instanced and handle rest single parameters.
*
*
* @category Zend
* @package ProcessMaker
* @subpackage workflow
* @copyright Copyright (c) ProcessMaker Colosa S.A.
* @version Release: @2.0.44@
* @since Class available since Release 2.0.44
*/
require_once("SimpleMessage.php");
//Class defined to be instanced and handle rest single parameters
/**
* Class defined to be instanced and handle rest single parameters
*/
class RestMessage extends SimpleMessage
{
//call the parent Curl initialization and set the type of the message
/**
* Call the parent Curl initialization and set the type of the message
*/
public function RestMessage()
{
parent::__construct();
$this->type = "rest";
}
//Format the array parameter to a single rest line format separed by '/'.
/**
* Format the array parameter to a single rest line format separed by '/'.
*/
protected function format(array $message)
{
$rest = "";
if ( !empty( $message) )
{
if ( is_array( $message) )
{
if (!empty($message)){
if (is_array($message)){
foreach($message as $index => $data)
{
$rest .= "/" . $data;

View File

@@ -1,19 +1,36 @@
<?php
/**
* Class defined to set enough curl configuration.
*
*
* @category Zend
* @package ProcessMaker
* @subpackage workflow
* @copyright Copyright (c) ProcessMaker Colosa S.A.
* @version Release: @2.0.44@
* @since Class available since Release 2.0.44
*/
require_once("CURLMessage.php");
//Class defined to set enough curl configuration
/**
* Class defined to set enough curl configuration
*/
class SimpleMessage extends CURLMessage
{
public function SimpleMessage()
{
parent::__construct();
}
//set the message in order to follow the message format, empty caused this class should not be instanced
/**
* Set the message in order to follow the message format, empty caused this class should not be instanced
*/
protected function format(array $message)
{
}
//Setting CURL Url, enough to attach a message.
/**
* Setting CURL Url, enough to attach a message.
*/
protected function setMoreProperties($messageFormated)
{
//Please, remove this comment, is only for looging proposes.

View File

@@ -1,25 +1,39 @@
<?php
/**
* Class defined to be instanced and handle XML format messages.
*
*
* @category Zend
* @package ProcessMaker
* @subpackage workflow
* @copyright Copyright (c) ProcessMaker Colosa S.A.
* @version Release: @2.0.44@
* @since Class available since Release 2.0.44
*/
require_once("FormatedMessage.php");
//Class defined to be instanced and handle XML format messages.
/**
* Class defined to be instanced and handle XML format messages.
*/
class XmlMessage extends FormatedMessage
{
//call the parent Curl initialization and set the type of the message
/**
* Call the parent Curl initialization and set the type of the message
*/
public function XmlMessage()
{
parent::__construct();
$this->type = "xml";
}
//Format the array parameter to a xml valid format. TODO: Need to find out a better way to do it.
/**
* Format the array parameter to a xml valid format. TODO: Need to find out a better way to do it.
*/
protected function format(array $message)
{
if ( empty( $message) )
{
if (empty($message)){
return ;
}
if ( is_array( $message) )
{
if (is_array($message)){
$xml = "<?xml version='1.0'?><request>";
foreach($message as $index => $data)
{

View File

@@ -22,11 +22,11 @@ $rest = new RestMessage();
$rest->sendGET($table,$msg);
$rest->displayResponse();
$msg = array("HOUSE","PUSHIN","en","maullidin","2012-06-06" );
$msg = array( "HOUSE", "PUSHIN", "en", "sample", "2012-06-06" );
$rest->sendPOST($table,$msg);
$rest->displayResponse();
$msg = array("HOUSE","PUSHIN","en","wacheneger","2012-07-06" );
$msg = array( "HOUSE", "PUSHIN", "en", "samplemod", "2012-07-06" );
$rest->sendPUT($table,$msg);
$rest->displayResponse();
@@ -34,5 +34,4 @@ $msg = array("HOUSE","PUSHIN","en" );
$rest->sendDELETE($table,$msg);
$rest->displayResponse();
?>