From d6f1539a7bfd285a836111a4ddf20ad770134e3d Mon Sep 17 00:00:00 2001 From: ralph Date: Tue, 2 Oct 2012 10:27:57 -0400 Subject: [PATCH 1/3] BUG 9855 Rest Services for ProcessMaker. 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. --- .../engine/methods/services/Rest/CRUD.java | 200 ++++++++++++++++++ .../methods/services/Rest/CURLMessage.php | 116 ++++++++++ .../methods/services/Rest/FormatedMessage.php | 29 +++ .../methods/services/Rest/JsonMessage.php | 29 +++ .../methods/services/Rest/RestMessage.php | 33 +++ .../methods/services/Rest/SimpleMessage.php | 26 +++ .../methods/services/Rest/XmlMessage.php | 34 +++ .../engine/methods/services/Rest/testing.php | 38 ++++ 8 files changed, 505 insertions(+) create mode 100644 workflow/engine/methods/services/Rest/CRUD.java create mode 100644 workflow/engine/methods/services/Rest/CURLMessage.php create mode 100644 workflow/engine/methods/services/Rest/FormatedMessage.php create mode 100644 workflow/engine/methods/services/Rest/JsonMessage.php create mode 100644 workflow/engine/methods/services/Rest/RestMessage.php create mode 100644 workflow/engine/methods/services/Rest/SimpleMessage.php create mode 100644 workflow/engine/methods/services/Rest/XmlMessage.php create mode 100644 workflow/engine/methods/services/Rest/testing.php diff --git a/workflow/engine/methods/services/Rest/CRUD.java b/workflow/engine/methods/services/Rest/CRUD.java new file mode 100644 index 000000000..c9dea78f0 --- /dev/null +++ b/workflow/engine/methods/services/Rest/CRUD.java @@ -0,0 +1,200 @@ +//Archivo HolaMundo.java + +// Main class to sent differen kind of messages +import org.apache.http.impl.client.DefaultHttpClient; + +// Enter CRUD memebers +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.methods.HttpPost; +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 +import org.apache.http.HttpResponse; +import org.apache.http.HttpEntity; +import org.apache.http.util.EntityUtils; + + +public class CRUD +{ + private static void PostSample() + { + System.out.println("POST: Enter login params\n"); + + String loginParamsXML = "\n" + +"\n" + +"admin\n" + +"admin\n" + +""; + String URI = "http://ralph.pmos.colosa.net/rest/ralph/login/"; + + System.out.println( "Request: "+URI + "\n"+ loginParamsXML + "\n"); + + DefaultHttpClient httpClient = new DefaultHttpClient(); + HttpPost postRequest = new HttpPost(URI); + try + { + StringEntity input = new StringEntity( loginParamsXML); + input.setContentType("application/xml"); + postRequest.setEntity(input); + HttpResponse httpResponse = httpClient.execute(postRequest); + + HttpEntity responseEntity = httpResponse.getEntity(); + if( responseEntity != null) + { + String response = new String(); + response = EntityUtils.toString( responseEntity); + System.out.println( "Response: " + response + "\n"); + } + } + catch( java.io.IOException x) + { + throw new RuntimeException("I/O error" + x.toString()); + } + } + + private static void GetSample() + { + System.out.println("GET: Display TRANSLATION table row\n"); + + String URI = "http://ralph.pmos.colosa.net/rest/ralph/TRANSLATION/LABEL/LOGIN/en/"; + System.out.println( "Request: " + URI + "\n"); + + DefaultHttpClient httpClient = new DefaultHttpClient(); + HttpGet getRequest = new HttpGet(URI); + try + { + HttpResponse httpResponse = httpClient.execute(getRequest); + + HttpEntity responseEntity = httpResponse.getEntity(); + if( responseEntity != null) + { + String response = new String(); + response = EntityUtils.toString( responseEntity); + System.out.println( "Response: " + response + "\n"); + } + } + catch( java.io.IOException x) + { + throw new RuntimeException("I/O error" + x.toString()); + } + } + + private static void AnotherPostSample() + { + System.out.println("POST: Insert new row in TRANLATION\n"); + + String URI = "http://ralph.pmos.colosa.net/rest/ralph/TRANSLATION/"; + String newRow = "BUTTON/ESCAPE/en/sample/2012-05-05/"; + System.out.println( "Request: " + URI + " new row: " + newRow + "\n"); + URI = URI + newRow; + + DefaultHttpClient httpClient = new DefaultHttpClient(); + HttpPost postRequest = new HttpPost(URI); + try + { + HttpResponse httpResponse = httpClient.execute(postRequest); + + HttpEntity responseEntity = httpResponse.getEntity(); + if( responseEntity != null) + { + String response = new String(); + if(response.isEmpty()) + { + System.out.println( "Response: Status code: " + httpResponse.getStatusLine().getStatusCode()+ "\n"); + return; + } + response = EntityUtils.toString( responseEntity); + System.out.println( "Response: " + response + "\n"); + } + } + catch( java.io.IOException x) + { + throw new RuntimeException("I/O error" + x.toString()); + } + } + + private static void PutSample() + { + System.out.println("POST: Update a row in TRANLATION\n"); + + String URI = "http://ralph.pmos.colosa.net/rest/ralph/TRANSLATION/"; + String index = "BUTTON/ESCAPE/en/"; + String updateData = "changesample/2011-07-06/"; + + System.out.println( "Request: " + URI + " index: " + index + " updateData: " + updateData + "\n"); + URI = URI + index + updateData; + + DefaultHttpClient httpClient = new DefaultHttpClient(); + HttpPut putRequest = new HttpPut(URI); + try + { + HttpResponse httpResponse = httpClient.execute(putRequest); + + HttpEntity responseEntity = httpResponse.getEntity(); + if( responseEntity != null) + { + String response = new String(); + if(response.isEmpty()) + { + System.out.println( "Response: Status code: " + httpResponse.getStatusLine().getStatusCode()+ "\n"); + return; + } + response = EntityUtils.toString( responseEntity); + System.out.println( "Response: " + response + "\n"); + } + } + catch( java.io.IOException x) + { + throw new RuntimeException("I/O error" + x.toString()); + } + } + + private static void DeleteSample() + { + System.out.println("DELETE: Remove a row in TRANLATION\n"); + + String URI = "http://ralph.pmos.colosa.net/rest/ralph/TRANSLATION/"; + String index = "BUTTON/ESCAPE/en/"; + + System.out.println( "Request: " + URI + "index:" + index + "\n"); + URI = URI + index; + + DefaultHttpClient httpClient = new DefaultHttpClient(); + HttpDelete deleteRequest = new HttpDelete(URI); + try + { + HttpResponse httpResponse = httpClient.execute(deleteRequest); + + HttpEntity responseEntity = httpResponse.getEntity(); + if( responseEntity != null) + { + String response = new String(); + if(response.isEmpty()) + { + System.out.println( "Response: Status code: " + httpResponse.getStatusLine().getStatusCode()+ "\n"); + return; + } + response = EntityUtils.toString( responseEntity); + System.out.println( "Response: " + response + "\n"); + } + } + catch( java.io.IOException x) + { + throw new RuntimeException("I/O error" + x.toString()); + } + } + + public static void main(String args[]) + { + System.out.println("CRUD sample."); + PostSample(); + GetSample(); + AnotherPostSample(); + PutSample(); + DeleteSample(); + } +} \ No newline at end of file diff --git a/workflow/engine/methods/services/Rest/CURLMessage.php b/workflow/engine/methods/services/Rest/CURLMessage.php new file mode 100644 index 000000000..7ca2f992a --- /dev/null +++ b/workflow/engine/methods/services/Rest/CURLMessage.php @@ -0,0 +1,116 @@ +restServer = PROTOCOL_HTTP.COLON.PATH_SEP.PATH_SEP; + $this->restServer .= $_SERVER['SERVER_NAME'].PATH_SEP; + $this->restServer .= $this->serviceTechnic.PATH_SEP.$user.PATH_SEP; + + $this->ch = curl_init(); + curl_setopt( $this->ch, CURLOPT_TIMEOUT, 2); + curl_setopt( $this->ch, CURLOPT_POST, 1); + curl_setopt( $this->ch, CURLOPT_RETURNTRANSFER, 1); + } + //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. + abstract protected function setMoreProperties( $messageFormated); + + //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 + 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 + 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 + 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 + 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 + 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. + public function displayResponse() + { + $error = curl_error($this->ch); + $result = array( 'header' => '', + 'body' => '', + 'curl_error' => '', + 'http_code' => '', + 'last_url' => ''); + if ( $error != "" ) + { + $result['curl_error'] = $error; + return $result; + } + $response = $this->output; + $header_size = curl_getinfo($this->ch,CURLINFO_HEADER_SIZE); + $result['header'] = substr($response, 0, $header_size); + $result['body'] = substr( $response, $header_size ); + $result['http_code'] = curl_getinfo($this -> ch,CURLINFO_HTTP_CODE); + $result['last_url'] = curl_getinfo($this -> ch,CURLINFO_EFFECTIVE_URL); + + echo $this->type." Response: ".$response."
"; + foreach($result as $index => $data) + { + if( $data != "") + { + echo $index."=".$data."
"; + } + } + echo "
"; + } + //Close the Curl session using the Curl Handle set by curl_init() function. + public function close() + { + curl_close( $this->ch); + } +} + +?> diff --git a/workflow/engine/methods/services/Rest/FormatedMessage.php b/workflow/engine/methods/services/Rest/FormatedMessage.php new file mode 100644 index 000000000..7909acfbc --- /dev/null +++ b/workflow/engine/methods/services/Rest/FormatedMessage.php @@ -0,0 +1,29 @@ +server_method . PATH_SEP . $messageFormated."
"; + // + curl_setopt($this->ch, CURLOPT_URL, $this->server_method); + $contentSelected = $this->content . $this->type; + curl_setopt($this->ch, CURLOPT_HTTPHEADER, array($contentSelected)); + curl_setopt($this->ch, CURLOPT_POSTFIELDS, $messageFormated); + } +} +?> \ No newline at end of file diff --git a/workflow/engine/methods/services/Rest/JsonMessage.php b/workflow/engine/methods/services/Rest/JsonMessage.php new file mode 100644 index 000000000..535657aa8 --- /dev/null +++ b/workflow/engine/methods/services/Rest/JsonMessage.php @@ -0,0 +1,29 @@ +type = "json"; + } + //Format the array parameter to a json format. + protected function format( array $message) + { + if ( empty( $message) ) + { + return ; + } + if ( is_array( $message) ) + { + $jsonMessage = json_encode( $message); + } + return $jsonMessage; + } +} + +?> \ No newline at end of file diff --git a/workflow/engine/methods/services/Rest/RestMessage.php b/workflow/engine/methods/services/Rest/RestMessage.php new file mode 100644 index 000000000..a12fd8ee1 --- /dev/null +++ b/workflow/engine/methods/services/Rest/RestMessage.php @@ -0,0 +1,33 @@ +type = "rest"; + } + //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) ) + { + foreach( $message as $index => $data) + { + $rest .= "/" . $data; + } + $rest .= "/"; + } + } + return $rest; + } +} + +?> diff --git a/workflow/engine/methods/services/Rest/SimpleMessage.php b/workflow/engine/methods/services/Rest/SimpleMessage.php new file mode 100644 index 000000000..b9b26606b --- /dev/null +++ b/workflow/engine/methods/services/Rest/SimpleMessage.php @@ -0,0 +1,26 @@ +server_method . PATH_SEP . $messageFormated."
"; + // + curl_setopt($this->ch, CURLOPT_URL, $this->server_method . $messageFormated); + } +} +?> \ No newline at end of file diff --git a/workflow/engine/methods/services/Rest/XmlMessage.php b/workflow/engine/methods/services/Rest/XmlMessage.php new file mode 100644 index 000000000..fdc96ca3e --- /dev/null +++ b/workflow/engine/methods/services/Rest/XmlMessage.php @@ -0,0 +1,34 @@ +type = "xml"; + } + //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) ) + { + return ; + } + if ( is_array( $message) ) + { + $xml = ""; + foreach( $message as $index => $data) + { + $xml .= "<".$index.">".$data.""; + } + $xml .= ""; + } + return $xml; + } +} + +?> diff --git a/workflow/engine/methods/services/Rest/testing.php b/workflow/engine/methods/services/Rest/testing.php new file mode 100644 index 000000000..86e3d85a1 --- /dev/null +++ b/workflow/engine/methods/services/Rest/testing.php @@ -0,0 +1,38 @@ +'admin', 'password'=>'admin'); +$method = "login"; + +$jsonm = new JsonMessage(); +$jsonm->send( $method, $msg); +$jsonm->displayResponse(); + +$xmlm = new XmlMessage(); +$xmlm->send( $method, $msg); +$xmlm->displayResponse(); + +$msg = array("LABEL","LOGIN","en"); +$table = "TRANSLATION"; + +$rest = new RestMessage(); +$rest->sendGET( $table, $msg); +$rest->displayResponse(); + +$msg = array("HOUSE","PUSHIN","en","maullidin","2012-06-06" ); +$rest->sendPOST( $table, $msg); +$rest->displayResponse(); + +$msg = array("HOUSE","PUSHIN","en","wacheneger","2012-07-06" ); +$rest->sendPUT( $table, $msg); +$rest->displayResponse(); + +$msg = array("HOUSE","PUSHIN","en" ); +$rest->sendDELETE( $table, $msg); +$rest->displayResponse(); + + +?> From 6372bf808adc1deb7ccda5a9e39e4b36d314fe8b Mon Sep 17 00:00:00 2001 From: Ralph Asendeteufrer Date: Tue, 2 Oct 2012 13:13:19 -0400 Subject: [PATCH 2/3] 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. --- .../engine/methods/services/Rest/CRUD.java | 347 +++++++++--------- .../methods/services/Rest/CURLMessage.php | 215 ++++++----- .../methods/services/Rest/FormatedMessage.php | 59 +-- .../methods/services/Rest/JsonMessage.php | 58 +-- .../methods/services/Rest/RestMessage.php | 66 ++-- .../methods/services/Rest/SimpleMessage.php | 55 ++- .../methods/services/Rest/XmlMessage.php | 68 ++-- .../engine/methods/services/Rest/testing.php | 23 +- 8 files changed, 501 insertions(+), 390 deletions(-) diff --git a/workflow/engine/methods/services/Rest/CRUD.java b/workflow/engine/methods/services/Rest/CRUD.java index c9dea78f0..56262aef7 100644 --- a/workflow/engine/methods/services/Rest/CRUD.java +++ b/workflow/engine/methods/services/Rest/CRUD.java @@ -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,189 +10,194 @@ 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 void PostSample() - { - System.out.println("POST: Enter login params\n"); - + 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"); + String loginParamsXML = "\n" - +"\n" - +"admin\n" - +"admin\n" - +""; - String URI = "http://ralph.pmos.colosa.net/rest/ralph/login/"; - - System.out.println( "Request: "+URI + "\n"+ loginParamsXML + "\n"); - + +"\n" + +"admin\n" + +"admin\n" + +""; + String URI = "http://"+m_user+".pmos.colosa.net/rest/"+m_user+"/login/"; + + System.out.println( "Request: "+URI + "\n"+ loginParamsXML + "\n"); + DefaultHttpClient httpClient = new DefaultHttpClient(); - HttpPost postRequest = new HttpPost(URI); - try - { - StringEntity input = new StringEntity( loginParamsXML); - input.setContentType("application/xml"); - postRequest.setEntity(input); - HttpResponse httpResponse = httpClient.execute(postRequest); - - HttpEntity responseEntity = httpResponse.getEntity(); - if( responseEntity != null) - { - String response = new String(); - response = EntityUtils.toString( responseEntity); - System.out.println( "Response: " + response + "\n"); - } - } - catch( java.io.IOException x) - { - throw new RuntimeException("I/O error" + x.toString()); - } - } - - private static void GetSample() - { - System.out.println("GET: Display TRANSLATION table row\n"); - - String URI = "http://ralph.pmos.colosa.net/rest/ralph/TRANSLATION/LABEL/LOGIN/en/"; - System.out.println( "Request: " + URI + "\n"); - + HttpPost postRequest = new HttpPost(URI); + try + { + StringEntity input = new StringEntity( loginParamsXML); + input.setContentType("application/xml"); + postRequest.setEntity(input); + HttpResponse httpResponse = httpClient.execute(postRequest); + + HttpEntity responseEntity = httpResponse.getEntity(); + if( responseEntity != null) + { + String response = new String(); + response = EntityUtils.toString( responseEntity); + System.out.println( "Response: " + response + "\n"); + } + } + catch( java.io.IOException x) + { + throw new RuntimeException("I/O error: " + x.toString()); + } + } + + private static void GetSample() + { + System.out.println("GET: Display TRANSLATION table row\n"); + + 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(); - HttpGet getRequest = new HttpGet(URI); - try - { - HttpResponse httpResponse = httpClient.execute(getRequest); - - HttpEntity responseEntity = httpResponse.getEntity(); - if( responseEntity != null) - { - String response = new String(); - response = EntityUtils.toString( responseEntity); - System.out.println( "Response: " + response + "\n"); - } - } - catch( java.io.IOException x) - { - throw new RuntimeException("I/O error" + x.toString()); - } - } - - private static void AnotherPostSample() - { - System.out.println("POST: Insert new row in TRANLATION\n"); - - String URI = "http://ralph.pmos.colosa.net/rest/ralph/TRANSLATION/"; - String newRow = "BUTTON/ESCAPE/en/sample/2012-05-05/"; - System.out.println( "Request: " + URI + " new row: " + newRow + "\n"); - URI = URI + newRow; - + HttpGet getRequest = new HttpGet(URI); + try + { + HttpResponse httpResponse = httpClient.execute(getRequest); + + HttpEntity responseEntity = httpResponse.getEntity(); + if( responseEntity != null) + { + String response = new String(); + response = EntityUtils.toString( responseEntity); + System.out.println( "Response: " + response + "\n"); + } + } + catch( java.io.IOException x) + { + throw new RuntimeException("I/O error: " + x.toString()); + } + } + + private static void AnotherPostSample() + { + System.out.println("POST: Insert new row in TRANLATION\n"); + + 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; + DefaultHttpClient httpClient = new DefaultHttpClient(); - HttpPost postRequest = new HttpPost(URI); - try - { - HttpResponse httpResponse = httpClient.execute(postRequest); - - HttpEntity responseEntity = httpResponse.getEntity(); - if( responseEntity != null) - { - String response = new String(); - if(response.isEmpty()) - { - System.out.println( "Response: Status code: " + httpResponse.getStatusLine().getStatusCode()+ "\n"); - return; - } - response = EntityUtils.toString( responseEntity); - System.out.println( "Response: " + response + "\n"); - } - } - catch( java.io.IOException x) - { - throw new RuntimeException("I/O error" + x.toString()); - } - } - - private static void PutSample() - { - System.out.println("POST: Update a row in TRANLATION\n"); - - String URI = "http://ralph.pmos.colosa.net/rest/ralph/TRANSLATION/"; - String index = "BUTTON/ESCAPE/en/"; - String updateData = "changesample/2011-07-06/"; - - System.out.println( "Request: " + URI + " index: " + index + " updateData: " + updateData + "\n"); - URI = URI + index + updateData; - + HttpPost postRequest = new HttpPost(URI); + try + { + HttpResponse httpResponse = httpClient.execute(postRequest); + + HttpEntity responseEntity = httpResponse.getEntity(); + if( responseEntity != null) + { + String response = new String(); + if(response.isEmpty()) + { + System.out.println( "Response: Status code: " + httpResponse.getStatusLine().getStatusCode()+ "\n"); + return; + } + response = EntityUtils.toString( responseEntity); + System.out.println( "Response: " + response + "\n"); + } + } + catch( java.io.IOException x) + { + throw new RuntimeException("I/O error: " + x.toString()); + } + } + + private static void PutSample() + { + System.out.println("POST: Update a row in TRANLATION\n"); + + String URI = "http://"+m_user+".pmos.colosa.net/rest/"+m_user+"/TRANSLATION/"; + String index = "BUTTON/ESCAPE/en/"; + String updateData = "changesample/2011-07-06/"; + + System.out.println( "Request: " + URI + " index: " + index + " updateData: " + updateData + "\n"); + URI = URI + index + updateData; + DefaultHttpClient httpClient = new DefaultHttpClient(); - HttpPut putRequest = new HttpPut(URI); - try - { - HttpResponse httpResponse = httpClient.execute(putRequest); - - HttpEntity responseEntity = httpResponse.getEntity(); - if( responseEntity != null) - { - String response = new String(); - if(response.isEmpty()) - { - System.out.println( "Response: Status code: " + httpResponse.getStatusLine().getStatusCode()+ "\n"); - return; - } - response = EntityUtils.toString( responseEntity); - System.out.println( "Response: " + response + "\n"); - } - } - catch( java.io.IOException x) - { - throw new RuntimeException("I/O error" + x.toString()); - } - } - - private static void DeleteSample() - { - System.out.println("DELETE: Remove a row in TRANLATION\n"); - - String URI = "http://ralph.pmos.colosa.net/rest/ralph/TRANSLATION/"; - String index = "BUTTON/ESCAPE/en/"; - - System.out.println( "Request: " + URI + "index:" + index + "\n"); - URI = URI + index; - + HttpPut putRequest = new HttpPut(URI); + try + { + HttpResponse httpResponse = httpClient.execute(putRequest); + + HttpEntity responseEntity = httpResponse.getEntity(); + if( responseEntity != null) + { + String response = new String(); + if(response.isEmpty()) + { + System.out.println( "Response: Status code: " + httpResponse.getStatusLine().getStatusCode()+ "\n"); + return; + } + response = EntityUtils.toString( responseEntity); + System.out.println( "Response: " + response + "\n"); + } + } + catch( java.io.IOException x) + { + throw new RuntimeException("I/O error: " + x.toString()); + } + } + + private static void DeleteSample() + { + System.out.println("DELETE: Remove a row in TRANLATION\n"); + + 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"); + URI = URI + index; + DefaultHttpClient httpClient = new DefaultHttpClient(); - HttpDelete deleteRequest = new HttpDelete(URI); - try - { - HttpResponse httpResponse = httpClient.execute(deleteRequest); - - HttpEntity responseEntity = httpResponse.getEntity(); - if( responseEntity != null) - { - String response = new String(); - if(response.isEmpty()) - { - System.out.println( "Response: Status code: " + httpResponse.getStatusLine().getStatusCode()+ "\n"); - return; - } - response = EntityUtils.toString( responseEntity); - System.out.println( "Response: " + response + "\n"); - } - } - catch( java.io.IOException x) - { - throw new RuntimeException("I/O error" + x.toString()); - } - } + HttpDelete deleteRequest = new HttpDelete(URI); + try + { + HttpResponse httpResponse = httpClient.execute(deleteRequest); + + HttpEntity responseEntity = httpResponse.getEntity(); + if( responseEntity != null) + { + String response = new String(); + if(response.isEmpty()) + { + System.out.println( "Response: Status code: " + httpResponse.getStatusLine().getStatusCode()+ "\n"); + return; + } + response = EntityUtils.toString( responseEntity); + System.out.println( "Response: " + response + "\n"); + } + } + catch( java.io.IOException x) + { + throw new RuntimeException("I/O error: " + x.toString()); + } + } public static void main(String args[]) { - System.out.println("CRUD sample."); - PostSample(); - GetSample(); - AnotherPostSample(); - PutSample(); - DeleteSample(); + System.out.println("CRUD samples."); + PostSample(); + GetSample(); + AnotherPostSample(); + PutSample(); + DeleteSample(); + } } \ No newline at end of file diff --git a/workflow/engine/methods/services/Rest/CURLMessage.php b/workflow/engine/methods/services/Rest/CURLMessage.php index 7ca2f992a..d31fcc4c7 100644 --- a/workflow/engine/methods/services/Rest/CURLMessage.php +++ b/workflow/engine/methods/services/Rest/CURLMessage.php @@ -1,116 +1,149 @@ restServer = PROTOCOL_HTTP.COLON.PATH_SEP.PATH_SEP; - $this->restServer .= $_SERVER['SERVER_NAME'].PATH_SEP; - $this->restServer .= $this->serviceTechnic.PATH_SEP.$user.PATH_SEP; - - $this->ch = curl_init(); - curl_setopt( $this->ch, CURLOPT_TIMEOUT, 2); - curl_setopt( $this->ch, CURLOPT_POST, 1); - curl_setopt( $this->ch, CURLOPT_RETURNTRANSFER, 1); - } - //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. - abstract protected function setMoreProperties( $messageFormated); - - //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 - 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 - 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 - 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 - 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 - 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. - public function displayResponse() - { - $error = curl_error($this->ch); + $this->restServer = PROTOCOL_HTTP . COLON.PATH_SEP . PATH_SEP; + $this->restServer .= $_SERVER['SERVER_NAME'] . PATH_SEP; + $this->restServer .= $this->serviceTechnic . PATH_SEP . $user . PATH_SEP; + + $this->ch = curl_init(); + curl_setopt($this->ch,CURLOPT_TIMEOUT, 2); + curl_setopt($this->ch,CURLOPT_POST, 1); + curl_setopt($this->ch,CURLOPT_RETURNTRANSFER, 1); + } + /** + * 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. + */ + abstract protected function setMoreProperties($messageFormated); + /** + * 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 + **/ + 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 + */ + 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 + */ + 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 + */ + 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 + */ + 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. + */ + public function displayResponse() + { + $error = curl_error($this->ch); $result = array( 'header' => '', 'body' => '', 'curl_error' => '', 'http_code' => '', 'last_url' => ''); - if ( $error != "" ) - { + if ($error != ""){ $result['curl_error'] = $error; return $result; } $response = $this->output; $header_size = curl_getinfo($this->ch,CURLINFO_HEADER_SIZE); - $result['header'] = substr($response, 0, $header_size); - $result['body'] = substr( $response, $header_size ); + $result['header'] = substr($response,0,$header_size); + $result['body'] = substr($response,$header_size); $result['http_code'] = curl_getinfo($this -> ch,CURLINFO_HTTP_CODE); $result['last_url'] = curl_getinfo($this -> ch,CURLINFO_EFFECTIVE_URL); - - echo $this->type." Response: ".$response."
"; - foreach($result as $index => $data) - { - if( $data != "") - { - echo $index."=".$data."
"; - } - } - echo "
"; - } - //Close the Curl session using the Curl Handle set by curl_init() function. - public function close() - { - curl_close( $this->ch); - } + + echo $this->type." Response: ".$response."
"; + foreach($result as $index => $data) + { + if($data != ""){ + echo $index . "=" . $data . "
"; + } + } + echo "
"; + } + /** + * Close the Curl session using the Curl Handle set by curl_init() function. + */ + public function close() + { + curl_close($this->ch); + } } ?> diff --git a/workflow/engine/methods/services/Rest/FormatedMessage.php b/workflow/engine/methods/services/Rest/FormatedMessage.php index 7909acfbc..ba1914e3f 100644 --- a/workflow/engine/methods/services/Rest/FormatedMessage.php +++ b/workflow/engine/methods/services/Rest/FormatedMessage.php @@ -1,29 +1,46 @@ server_method . PATH_SEP . $messageFormated."
"; - // - curl_setopt($this->ch, CURLOPT_URL, $this->server_method); - $contentSelected = $this->content . $this->type; - curl_setopt($this->ch, CURLOPT_HTTPHEADER, array($contentSelected)); - curl_setopt($this->ch, CURLOPT_POSTFIELDS, $messageFormated); - } + { + parent::__construct(); + } + /** + * 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 + */ + protected function setMoreProperties($messageFormated) + { + //Please, remove this comment, is only for looging proposes. + // + echo "Request: " . $this->server_method . PATH_SEP . $messageFormated . "
"; + // + curl_setopt($this->ch,CURLOPT_URL,$this->server_method); + $contentSelected = $this->content . $this->type; + curl_setopt($this->ch,CURLOPT_HTTPHEADER,array($contentSelected)); + curl_setopt($this->ch,CURLOPT_POSTFIELDS,$messageFormated); + } } ?> \ No newline at end of file diff --git a/workflow/engine/methods/services/Rest/JsonMessage.php b/workflow/engine/methods/services/Rest/JsonMessage.php index 535657aa8..773fe0a37 100644 --- a/workflow/engine/methods/services/Rest/JsonMessage.php +++ b/workflow/engine/methods/services/Rest/JsonMessage.php @@ -1,29 +1,43 @@ type = "json"; - } - //Format the array parameter to a json format. - protected function format( array $message) - { - if ( empty( $message) ) - { - return ; - } - if ( is_array( $message) ) - { - $jsonMessage = json_encode( $message); - } - return $jsonMessage; - } + /** + * 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. + */ + protected function format(array $message) + { + if (empty($message)){ + return ; + } + if (is_array($message)){ + $jsonMessage = json_encode( $message); + } + return $jsonMessage; + } } ?> \ No newline at end of file diff --git a/workflow/engine/methods/services/Rest/RestMessage.php b/workflow/engine/methods/services/Rest/RestMessage.php index a12fd8ee1..c78fbc3b6 100644 --- a/workflow/engine/methods/services/Rest/RestMessage.php +++ b/workflow/engine/methods/services/Rest/RestMessage.php @@ -1,33 +1,47 @@ type = "rest"; - } - //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) ) - { - foreach( $message as $index => $data) - { - $rest .= "/" . $data; - } - $rest .= "/"; - } - } - return $rest; - } + /** + * 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 '/'. + */ + protected function format(array $message) + { + $rest = ""; + if (!empty($message)){ + if (is_array($message)){ + foreach($message as $index => $data) + { + $rest .= "/" . $data; + } + $rest .= "/"; + } + } + return $rest; + } } ?> diff --git a/workflow/engine/methods/services/Rest/SimpleMessage.php b/workflow/engine/methods/services/Rest/SimpleMessage.php index b9b26606b..7fe054951 100644 --- a/workflow/engine/methods/services/Rest/SimpleMessage.php +++ b/workflow/engine/methods/services/Rest/SimpleMessage.php @@ -1,26 +1,43 @@ server_method . PATH_SEP . $messageFormated."
"; - // - curl_setopt($this->ch, CURLOPT_URL, $this->server_method . $messageFormated); - } + public function SimpleMessage() + { + parent::__construct(); + } + /** + * 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. + */ + protected function setMoreProperties($messageFormated) + { + //Please, remove this comment, is only for looging proposes. + // + echo "Request: " . $this->server_method . PATH_SEP . $messageFormated . "
"; + // + curl_setopt($this->ch,CURLOPT_URL,$this->server_method.$messageFormated); + } } ?> \ No newline at end of file diff --git a/workflow/engine/methods/services/Rest/XmlMessage.php b/workflow/engine/methods/services/Rest/XmlMessage.php index fdc96ca3e..447a420ae 100644 --- a/workflow/engine/methods/services/Rest/XmlMessage.php +++ b/workflow/engine/methods/services/Rest/XmlMessage.php @@ -1,34 +1,48 @@ type = "xml"; - } - //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) ) - { - return ; - } - if ( is_array( $message) ) - { - $xml = ""; - foreach( $message as $index => $data) - { - $xml .= "<".$index.">".$data.""; - } - $xml .= ""; - } - return $xml; - } + /** + * 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. + */ + protected function format(array $message) + { + if (empty($message)){ + return ; + } + if (is_array($message)){ + $xml = ""; + foreach($message as $index => $data) + { + $xml .= "<" . $index . ">" . $data . ""; + } + $xml .= ""; + } + return $xml; + } } ?> diff --git a/workflow/engine/methods/services/Rest/testing.php b/workflow/engine/methods/services/Rest/testing.php index 86e3d85a1..3b93426e0 100644 --- a/workflow/engine/methods/services/Rest/testing.php +++ b/workflow/engine/methods/services/Rest/testing.php @@ -4,35 +4,34 @@ require_once("JsonMessage.php"); require_once("XmlMessage.php"); require_once("RestMessage.php"); -$msg = array( 'user'=>'admin', 'password'=>'admin'); +$msg = array( 'user'=>'admin' , 'password'=>'admin'); $method = "login"; $jsonm = new JsonMessage(); -$jsonm->send( $method, $msg); +$jsonm->send($method,$msg); $jsonm->displayResponse(); $xmlm = new XmlMessage(); -$xmlm->send( $method, $msg); +$xmlm->send($method, $msg); $xmlm->displayResponse(); -$msg = array("LABEL","LOGIN","en"); +$msg = array( "LABEL", "LOGIN", "en"); $table = "TRANSLATION"; $rest = new RestMessage(); -$rest->sendGET( $table, $msg); +$rest->sendGET($table,$msg); $rest->displayResponse(); -$msg = array("HOUSE","PUSHIN","en","maullidin","2012-06-06" ); -$rest->sendPOST( $table, $msg); +$msg = array( "HOUSE", "PUSHIN", "en", "sample", "2012-06-06" ); +$rest->sendPOST($table,$msg); $rest->displayResponse(); -$msg = array("HOUSE","PUSHIN","en","wacheneger","2012-07-06" ); -$rest->sendPUT( $table, $msg); +$msg = array( "HOUSE", "PUSHIN", "en", "samplemod", "2012-07-06" ); +$rest->sendPUT($table,$msg); $rest->displayResponse(); -$msg = array("HOUSE","PUSHIN","en" ); -$rest->sendDELETE( $table, $msg); +$msg = array( "HOUSE", "PUSHIN", "en"); +$rest->sendDELETE($table,$msg); $rest->displayResponse(); - ?> From 2a416c6e318d4ae7faae31382fd1c9b82c941693 Mon Sep 17 00:00:00 2001 From: Ralph Asendeteufrer Date: Tue, 2 Oct 2012 17:28:12 -0400 Subject: [PATCH 3/3] BUG 9855 REST services for ProcessMaker Solved PROBLEM there were not any documentation of Rest using Curl, and using rest with Java. SOLVED There were add classes to help to use Rest by Curl, and examples to use rest with Java were added. --- workflow/engine/methods/services/Rest/CRUD.java | 2 +- workflow/engine/methods/services/Rest/CURLMessage.php | 8 ++++---- workflow/engine/methods/services/Rest/FormatedMessage.php | 2 +- workflow/engine/methods/services/Rest/JsonMessage.php | 2 +- workflow/engine/methods/services/Rest/RestMessage.php | 2 +- workflow/engine/methods/services/Rest/SimpleMessage.php | 2 +- workflow/engine/methods/services/Rest/XmlMessage.php | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/workflow/engine/methods/services/Rest/CRUD.java b/workflow/engine/methods/services/Rest/CRUD.java index 56262aef7..d26b07971 100644 --- a/workflow/engine/methods/services/Rest/CRUD.java +++ b/workflow/engine/methods/services/Rest/CRUD.java @@ -20,7 +20,7 @@ import org.apache.http.util.EntityUtils; */ public class CRUD { - private static String m_user = "ralph"; // This member variable must be changed to its own dev username + private static String m_user = "workflow"; // This member variable must be changed to its own dev workspace private static void PostSample() { diff --git a/workflow/engine/methods/services/Rest/CURLMessage.php b/workflow/engine/methods/services/Rest/CURLMessage.php index d31fcc4c7..ae2ed0dbe 100644 --- a/workflow/engine/methods/services/Rest/CURLMessage.php +++ b/workflow/engine/methods/services/Rest/CURLMessage.php @@ -7,7 +7,7 @@ * @category Zend * @package ProcessMaker * @subpackage workflow - * @copyright Copyright (c) ProcessMaker Colosa S.A. + * @copyright Copyright (c) ProcessMaker Colosa Inc. * @version Release: @2.0.44@ * @since Class available since Release 2.0.44 */ @@ -22,7 +22,7 @@ define('PROTOCOL_HTTP', 'http'); */ abstract class CURLMessage { - protected $restServer; // e.g. "http://ralph.pmos.colosa.net/rest/ralph/"; host + technich dir + user dir + protected $restServer; // e.g. "http://ralph.pmos.colosa.net/rest/ralph/"; host + technich dir + workspace 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. @@ -37,11 +37,11 @@ abstract class CURLMessage { $serverDNS = explode(DOT,$_SERVER['SERVER_NAME']); $serverDNS = array_reverse($serverDNS); - $user = array_pop($serverDNS); //***aware this must contains user logged or enviroment name*** + $workspace = array_pop($serverDNS); //***aware this must contains the workspace name*** $this->restServer = PROTOCOL_HTTP . COLON.PATH_SEP . PATH_SEP; $this->restServer .= $_SERVER['SERVER_NAME'] . PATH_SEP; - $this->restServer .= $this->serviceTechnic . PATH_SEP . $user . PATH_SEP; + $this->restServer .= $this->serviceTechnic . PATH_SEP . $workspace . PATH_SEP; $this->ch = curl_init(); curl_setopt($this->ch,CURLOPT_TIMEOUT, 2); diff --git a/workflow/engine/methods/services/Rest/FormatedMessage.php b/workflow/engine/methods/services/Rest/FormatedMessage.php index ba1914e3f..191a693fe 100644 --- a/workflow/engine/methods/services/Rest/FormatedMessage.php +++ b/workflow/engine/methods/services/Rest/FormatedMessage.php @@ -6,7 +6,7 @@ * @category Zend * @package ProcessMaker * @subpackage workflow - * @copyright Copyright (c) ProcessMaker Colosa S.A. + * @copyright Copyright (c) ProcessMaker Colosa Inc. * @version Release: @2.0.44@ * @since Class available since Release 2.0.44 */ diff --git a/workflow/engine/methods/services/Rest/JsonMessage.php b/workflow/engine/methods/services/Rest/JsonMessage.php index 773fe0a37..2a8d8f332 100644 --- a/workflow/engine/methods/services/Rest/JsonMessage.php +++ b/workflow/engine/methods/services/Rest/JsonMessage.php @@ -6,7 +6,7 @@ * @category Zend * @package ProcessMaker * @subpackage workflow - * @copyright Copyright (c) ProcessMaker Colosa S.A. + * @copyright Copyright (c) ProcessMaker Colosa Inc. * @version Release: @2.0.44@ * @since Class available since Release 2.0.44 */ diff --git a/workflow/engine/methods/services/Rest/RestMessage.php b/workflow/engine/methods/services/Rest/RestMessage.php index c78fbc3b6..f6c7488b6 100644 --- a/workflow/engine/methods/services/Rest/RestMessage.php +++ b/workflow/engine/methods/services/Rest/RestMessage.php @@ -6,7 +6,7 @@ * @category Zend * @package ProcessMaker * @subpackage workflow - * @copyright Copyright (c) ProcessMaker Colosa S.A. + * @copyright Copyright (c) ProcessMaker Colosa Inc. * @version Release: @2.0.44@ * @since Class available since Release 2.0.44 */ diff --git a/workflow/engine/methods/services/Rest/SimpleMessage.php b/workflow/engine/methods/services/Rest/SimpleMessage.php index 7fe054951..94cfd3ec1 100644 --- a/workflow/engine/methods/services/Rest/SimpleMessage.php +++ b/workflow/engine/methods/services/Rest/SimpleMessage.php @@ -6,7 +6,7 @@ * @category Zend * @package ProcessMaker * @subpackage workflow - * @copyright Copyright (c) ProcessMaker Colosa S.A. + * @copyright Copyright (c) ProcessMaker Colosa Inc. * @version Release: @2.0.44@ * @since Class available since Release 2.0.44 */ diff --git a/workflow/engine/methods/services/Rest/XmlMessage.php b/workflow/engine/methods/services/Rest/XmlMessage.php index 447a420ae..9d33db4fd 100644 --- a/workflow/engine/methods/services/Rest/XmlMessage.php +++ b/workflow/engine/methods/services/Rest/XmlMessage.php @@ -6,7 +6,7 @@ * @category Zend * @package ProcessMaker * @subpackage workflow - * @copyright Copyright (c) ProcessMaker Colosa S.A. + * @copyright Copyright (c) ProcessMaker Colosa Inc. * @version Release: @2.0.44@ * @since Class available since Release 2.0.44 */