hello@pemari.com 0844 736 2500

GEL Script consuming CA PPM REST API

22 Jun, 2017
Spread the love ...

Introduction of the REST API has opened doors for more powerful and effective ways to achieve integration with different applications. We have already gone through the CA PPM REST API  overview in a previous Blog. In this Blog we will try to consume RESTful URL’s via GEL scripts, so let’s start with a basic Project Read and extracting some basic information.

We have already used Postman (or similar tool) for testing and gathering information which we might need.

One Project Read from Gel

                      URL /projects/{project_internal_id}

                      Method GET

                      Response Document Type application/json

Please follow comments and logs in script for learning gel to call HTTP service, creating connection, fetching response via input stream and posting request via output stream.

First we need to get all Parameters related to CA PPM REST like basic authentication and URL.

1) GEL Script#1: For setting Basic Parameters.

<gel:script xmlns=http://www.w3.org/2001/XMLSchema
xmlns:core=“jelly:core”
xmlns:gel=“jelly:com.niku.union.gel.GELTagLibrary”
xmlns:sql=“jelly:sql” xmlns:xog=http://www.niku.com/xog
xmlns:xsd=http://www.w3.org/2001/XMLSchema
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns:util=“jelly:util”>
<!– ***************************************************************************** –>
<!– Program:       Community Blog                                                        –>
<!– Object:        NA                                                                               –>
<!– Step:          Start Step                                                                      –>
<!– Action:        Set Clarity Parameter                                                  –>
<!– Author:        Prashank Singh,(Pemari Technology Consultant)      –>
<!– Version:       1.0.00                                                                          –>
<!– Created On:    4th June 2017                                                         –>
<!– Modified On:   5th June 2016, Modified By: Prashank Singh         –>
<!–                                                                                                         –>
<!– Modification History:   Version 1.1.03                                             –>
<!– Dependencies:   NA                                                                        –>
<!– ***************************************************************************** –>
<!–*****************************************************************************  –>
<!– Set Clarity Parameters –>
<!–*******************************************************************************–>
<core:invokeStatic className=“java.lang.System”   method=“currentTimeMillis” var=“milStart00”/>

<!– *** Fetch the clarity’s parameter from Property File ***   –>
<core:invokeStatic className=“com.niku.union.config.ConfigurationManager” method=“getInstance” var=“thisClarityConfig”/><!– *** Fetch the clarity’s Version ***    –>
<core:set value=“${thisClarityConfig.getVersionProperty(‘version’)}” var=“ClarityVersion”/><!– *** Fetch the clarity’s Application URL ***    –>
<core:set value=“${thisClarityConfig.getProperties().getWebServer().getWebServerInstance(0).getEntryUrl()}” var=“AppEntryUrl”/>
<core:set value=“${thisClarityConfig.getProperties().getWebServer().getWebServerInstance(0).getContext()}” var=“AppContext”/>
<!– *** no method for rest context, using gel parameter*** –>
<!–  <core:set value=”${thisClarityConfig.getProperties().getWebServer().getWebServerInstance(0).getrestContext()}” var=”AppRestContext”/> –><gel:parameter default=“/ppm” var=“AppRestContext”/>
<core:set value=“${AppEntryUrl}${AppContext}” var=“ClarityBaseUrl”/>
<core:set value=“${AppEntryUrl}${AppRestContext}/rest/v1” var=“ClarityRestUrl”/>

<!– *** Fetch the sender’s mail address *** –>
<core:invoke method=“getProperties” on=“${thisClarityConfig}” var=“property”/>
<core:invoke method=“getMailServer” on=“${property}” var=“mailserver”/>
<core:invoke method=“getUsername” on=“${mailserver}” var=“mailsender”/>

<!– *** Set integration user and password *** –>

<gel:parameter default=“true” var=“DebugParameter”/>
<gel:parameter default=“admin” var=“cl_UserName”/>
<gel:parameter default=“${thisClarityConfig.getProperties().getApplicationServer().getAdminPassword()}” secure=“true” var=“cl_UserPassword”/>
<core:if test=“${cl_UserPassword==null}”><gel:log level=“ERROR”>No Password set for user ${cl_UserName}</gel:log></core:if>    
<!– *** Encoding basic Authorization with Base 64 *** –> 
<core:invokeStatic var=“base64” className=“com.niku.union.utility.Base64” method=“encode”> 
    <core:arg type=“java.lang.String” value=“${cl_UserName}:${cl_UserPassword}” /> 
</core:invokeStatic> 
<core:set var=“basicAuth” value=“Basic ${base64}”/>
<!–*****************************************************************–>
<!– Persist the parameters through the process –>
<!–*****************************************************************–>
<gel:persist scope=“INSTANCE” var=“ClarityBaseUrl”>${ClarityBaseUrl}</gel:persist>
<gel:persist scope=“INSTANCE” var=“ClarityRestUrl”>${ClarityRestUrl}</gel:persist>
<gel:persist scope=“INSTANCE” var=“cl_UserName”>${cl_UserName}</gel:persist>
<gel:persist scope=“INSTANCE” var=“cl_UserPassword”>${cl_UserPassword}</gel:persist>
<gel:persist scope=“INSTANCE” var=“mailsender”>${mailsender}</gel:persist>
<gel:persist scope=“INSTANCE” var=“ClarityVersion”>${ClarityVersion}</gel:persist>
<gel:persist scope=“INSTANCE” var=“basicAuth”>${basicAuth}</gel:persist>
<core:if test=“${DebugParameter}”>
      <gel:log level=“INFO”>Clarity Parameter’s </gel:log>
      <gel:log level=“INFO”>ClarityBaseUrl        : ${ClarityBaseUrl} </gel:log>
      <gel:log level=“INFO”>ClarityRestUrl        : ${ClarityRestUrl}</gel:log>
      <gel:log level=“INFO”>cl_UserName           : ${cl_UserName}</gel:log>
      <gel:log level=“INFO”>mailsender            : ${mailsender}</gel:log>
      <gel:log level=“INFO”>ClarityVersion        : ${ClarityVersion}</gel:log>  
      <gel:log level=“INFO”>basicAuth             : ${basicAuth}</gel:log>   
</core:if> 
    <core:invokeStatic className=“java.lang.System”   method=“currentTimeMillis” var=“milEnd00”/>
    <core:set var=“elapsedTime”   value=“${(milEnd00 – milStart00)}”/>
<core:if test=“${DebugTimings}”><gel:log><![CDATA[Time taken by script= ${elapsedTime/1000} sec ]]></gel:log></core:if> 
</gel:script>

2) GEL Script#2: GET Project Name and Objective field from CA PPM via REST API.

<gel:script xmlns=http://www.w3.org/2001/XMLSchema

xmlns:core=“jelly:core”

xmlns:gel=“jelly:com.niku.union.gel.GELTagLibrary”

xmlns:sql=“jelly:sql” xmlns:xog=http://www.niku.com/xog

xmlns:xsd=http://www.w3.org/2001/XMLSchema xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance

xmlns:util=“jelly:util”>

    <!– ******************************************************************************  –>

    <!– Program:       Community Blog                                                          –>

    <!– Object:        NA                                                                                 –>

    <!– Step:          Start Step                                                                       –>

    <!– Action:        GET CA PPM Project                                                    –>

    <!– Author:        Prashank Singh,(Pemari Technology Consultant)       –>

    <!– Version:       1.0.00                                                                            –>

    <!– Created On:    5th June 2017                                                           –>

    <!– Modified On:   5th June 2016, Modified By: Prashank Singh          –>

    <!–                                                                                                           –>

    <!– Modification History:   Version 1.0.00                                               –>

    <!– Dependencies:   NA                                                                         –>

    <!– ****************************************************************************** –>

   

    <!–******************************************************************************–>

    <!– Reading Project Infromation –>

    <!–******************************************************************************–>

   

<!– *** Please Save Gel Parameter for proejct to fetch *** –>

<gel:parameter default=“1” var=“cl_ProjectInternalId”/>

<gel:parameter default=“true” var=“DebugClarity”/>

 

<core:catch var=“SuperException”>      

<core:invokeStatic className=“java.lang.System”   method=“currentTimeMillis” var=“milStart00”/>

<gel:log><![CDATA[************ Clarity Project Information Extraction started  *********]]></gel:log>

<core:if test=“${DebugClarity}”><gel:log/><gel:log>Basic Auth Encoded 64: ${basicAuth}</gel:log></core:if>

 

<!– *** Setting REST URL for Getting Project Name and Objective ***    –>

<core:set var=“httpRESTURL”>${ClarityRestUrl}/projects/${cl_ProjectInternalId}/?fields=objective,name</core:set>

    <core:if test=“${DebugClarity}”><gel:log/><gel:log>httpRESTURL: ${httpRESTURL}</gel:log></core:if>

<core:new className=“java.net.URL” var=“remoteURL” ><core:arg type=“java.lang.String” value=“${httpRESTURL}” /></core:new>

   

<!– *** Setting Connection Details *** –>

<core:set var=“ClarityConnection” value=“${remoteURL.openConnection()}”/>  

<core:expr value=“${ClarityConnection.setDoOutput(true)}” /> 

<core:expr value=“${ClarityConnection.setDoInput(true)}” />

<core:expr value=“${ClarityConnection.setConnectTimeout(180000)}” />

<core:expr value=“${ClarityConnection.setReadTimeout(180000)}” />

<core:expr value=‘${ClarityConnection.setRequestMethod(“GET”)}’/> 

<core:expr value=‘${ClarityConnection.setRequestProperty(“Content-type”, “application/json”)}’/>

<core:expr value=‘${ClarityConnection.setRequestProperty(“Accept”, “application/json”)}’/>         

<core:expr value=‘${ClarityConnection.setRequestProperty(“cache-control”, “no-cache”)}’/>

<core:expr value=‘${ClarityConnection.setRequestProperty(“Authorization”,  basicAuth)}’/> 

<!– *** Sending Request for Fetching Details from Connection ***   –>         

<core:set var=“void” value=“${ClarityConnection.connect()}”/>

 

<!– *** Fetching Responce from Connection ***  –>                

<core:set var=“CL_isAuthenticated” value=“${ClarityConnection.getHeaderField(0)}” />

<core:set var=“response_msg” value=“${ClarityConnection.getResponseMessage()}” />

<core:if test=“${DebugClarity}”><gel:log>Clarity isAuthenticated: ${CL_isAuthenticated}</gel:log></core:if>

    <core:if test=“${CL_isAuthenticated==’HTTP/1.1 200′}”>

 

        <!– *** Reading Input Stream form Connection ***   –>

        <core:set var=“httpInputStream1” value=“${ClarityConnection.getInputStream()}” />

        <core:new className=“java.io.InputStreamReader” var=“v_Input1”>

            <core:arg type=“java.io.InputStream” value=“${httpInputStream1}”/>

        </core:new>

        <core:new className=“java.io.BufferedReader” var=“v_InputData1”>

            <core:arg type=“java.io.InputStreamReader” value=“${v_Input1}”/>

        </core:new>

       

        <!– *** Responce Saved in Variable for Reading/Modification ***    –>

        <core:set value=“${v_InputData1.readLine()}” var=“ClarityProjectInfo”/>

        <core:if test=“${DebugClarity}”><gel:log>httpData: ${ClarityProjectInfo}</gel:log></core:if>

        

        <!– *** Closing Input Stream ***   –>

        <core:set var=“void” value=“${httpInputStream1.close()}” />

       

        <!– Get JSONObject from response string –>

        <core:new className=“org.json.JSONObject” var=“J_ClarityPrjInfo” >

            <core:arg type=“java.lang.String” value=“${ClarityProjectInfo}” />

        </core:new>

       

        <!– Get the values from response –>

        <core:set var=“CL_name”       value=“${J_ClarityPrjInfo.get(‘name’)}”/>

        <core:set var=“CL_objective”  value=“${J_ClarityPrjInfo.get(‘objective’)}”/>

        <core:if test=“${DebugClarity}”><gel:log>Clarity Project name: ${CL_name}</gel:log></core:if>

        <core:if test=“${DebugClarity}”><gel:log>Clarity Project Objective: ${CL_objective}</gel:log></core:if>

</core:if> 

    <core:if test=“${CL_isAuthenticated!=’HTTP/1.1 200′}”>

        <gel:log><![CDATA[************ Error while reading Project Information *********]]></gel:log>                           

    </core:if> 

        <core:invokeStatic className=“java.lang.System”   method=“currentTimeMillis” var=“milEnd02”/>

            <core:set var=“elapsedTime”   value=“${(milEnd02 – milStart00)}”/>

            <gel:log><![CDATA[************ Clarity Project Information Extracted *********]]></gel:log>                            

        <core:if test=“${DebugTimings}”><gel:log><![CDATA[Time taken to extract Information= ${elapsedTime/1000} sec ]]></gel:log></core:if>    

 </core:catch>

<core:if test=“${SuperException!=null}”><gel:log>Super Exception — ${SuperException}</gel:log></core:if>

    <core:invokeStatic className=“java.lang.System”   method=“currentTimeMillis” var=“milEnd00”/>

    <core:set var=“elapsedTime”   value=“${(milEnd00 – milStart00)}”/>

    <core:if test=“${DebugTimings}”><gel:log><![CDATA[Time taken by script= ${elapsedTime/1000} sec ]]></gel:log></core:if> 

</gel:script>

 

Screenshot of GET Method response in GEL Script as below:

Now lets try to create a Project in CA PPM using Restful URL and POST Method. Creating a New Project information we might need will be similar as below:

URL /projects

Method POST

Response Document Type application/json

Request Document Type application/json

Response Payload  {  “code”: “P0000001”,  “name”: “ProjectNameChange” }

3) GEL Script#3: POSTing Project in CA PPM

<gel:script xmlns=http://www.w3.org/2001/XMLSchema

xmlns:core=“jelly:core”

xmlns:gel=“jelly:com.niku.union.gel.GELTagLibrary”

xmlns:sql=“jelly:sql” xmlns:xog=http://www.niku.com/xog

xmlns:xsd=http://www.w3.org/2001/XMLSchema xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance

xmlns:util=“jelly:util”>

    <!– ******************************************************************************  –>

    <!– Program:       Community Blog                                                          –>

    <!– Object:        NA                                                                                 –>

    <!– Step:          Start Step                                                                       –>

    <!– Action:        POST CA PPM Project                                                    –>

    <!– Author:        Prashank Singh,(Pemari Technology Consultant)       –>

    <!– Version:       1.0.00                                                                            –>

    <!– Created On:    5th June 2017                                                           –>

    <!– Modified On:   5th June 2016, Modified By: Prashank Singh          –>

    <!–                                                                                                           –>

    <!– Modification History:   Version 1.0.00                                               –>

    <!– Dependencies:   NA                                                                         –>

    <!– ****************************************************************************** –>

      <!–******************************************************************************–>

    <!– Creating Project Infromation –>

    <!–******************************************************************************–>

  <gel:parameter default=”true” var=”DebugClarity”/>

<core:catch var=“SuperException”>     

<core:invokeStatic className=“java.lang.System”   method=“currentTimeMillis” var=“milStart00”/>

<gel:log><![CDATA[************ Clarity Project creation started  *********]]></gel:log>

<core:if test=“${DebugClarity}”><gel:log/><gel:log>Basic Auth Encoded 64: ${basicAuth}</gel:log></core:if>

 <!– *** Setting REST URL for POSTing Project  ***    –>

<core:set var=“httpRESTURL”>${ClarityRestUrl}/projects</core:set>

    <core:if test=“${DebugClarity}”><gel:log/><gel:log>httpRESTURL: ${httpRESTURL}</gel:log></core:if>

<core:new className=“java.net.URL” var=“remoteURL” ><core:arg type=“java.lang.String” value=“${httpRESTURL}” /></core:new>

 <!– *** Setting Connection Details *** –>

<core:set var=“ClarityConnection” value=“${remoteURL.openConnection()}”/> 

<core:expr value=“${ClarityConnection.setDoOutput(true)}” />

<core:expr value=“${ClarityConnection.setDoInput(true)}” />

<core:expr value=“${ClarityConnection.setConnectTimeout(180000)}” />

<core:expr value=“${ClarityConnection.setReadTimeout(180000)}” />

<core:expr value=‘${ClarityConnection.setRequestMethod(“POST”)}’/>

<core:expr value=‘${ClarityConnection.setRequestProperty(“Content-type”, “application/json”)}’/>

<core:expr value=‘${ClarityConnection.setRequestProperty(“Accept”, “application/json”)}’/>        

<core:expr value=‘${ClarityConnection.setRequestProperty(“cache-control”, “no-cache”)}’/>

<core:expr value=‘${ClarityConnection.setRequestProperty(“Authorization”,  basicAuth)}’/>

<!– *** Sending Request for psodting details from Connection ***   –>        

<core:set var=“void” value=“${ClarityConnection.connect()}”/>

 

<!– *** Setting Project creation variable ***   –>    

 <core:set var=“CreateProject” escapeText=“false”>

<![CDATA[{“code”: “P0000001”,  “name”: “ProjectNameChange”}]]>

</core:set>

    <core:new className=“java.io.OutputStreamWriter” var=“wr1”>

      <core:arg type=“java.io.OutputStream” value=“${ClarityConnection.getOutputStream()}”/>

    </core:new>

        <core:set var=“void” value=“${wr1.write(CreateProject)}”/>

        <core:set var=“void” value=“${wr1.flush()}”/>

        <core:set var=“void” value=“${wr1.close()}”/>

 <!– *** Fetching Responce from Connection ***  –>               

<core:set var=“CL_isAuthenticated” value=“${ClarityConnection.getHeaderField(0)}” />

<core:set var=“response_msg” value=“${ClarityConnection.getResponseMessage()}” />

<core:if test=“${DebugClarity}”>

    <gel:log>isAuthenticated: ${CL_isAuthenticated}</gel:log>

    <gel:log>response_msg:  ${response_msg}</gel:log>

</core:if>

    <core:if test=“${CL_isAuthenticated==’HTTP/1.1 200′}”>

         <gel:log><![CDATA[************ Project Created Successfully *********]]></gel:log>                          

    </core:if>

    <core:if test=“${CL_isAuthenticated!=’HTTP/1.1 200′}”>

        <gel:log><![CDATA[************ Error while reading Project Information *********]]></gel:log>                          

    </core:if>

        <core:invokeStatic className=“java.lang.System”   method=“currentTimeMillis” var=“milEnd02”/>

            <core:set var=“elapsedTime”   value=“${(milEnd02 – milStart00)}”/>

    <core:if test=“${DebugTimings}”><gel:log><![CDATA[Time taken to extract Information= ${elapsedTime/1000} sec ]]></gel:log></core:if>   

 </core:catch>

<core:if test=“${SuperException!=null}”><gel:log>Super Exception — ${SuperException}</gel:log></core:if>

    <core:invokeStatic className=“java.lang.System”   method=“currentTimeMillis” var=“milEnd00”/>

    <core:set var=“elapsedTime”   value=“${(milEnd00 – milStart00)}”/>

    <core:if test=“${DebugTimings}”><gel:log><![CDATA[Time taken by script= ${elapsedTime/1000} sec ]]></gel:log></core:if>

</gel:script>

 

Similarly we can call other methods for operations like Create, Read, Full object Update, Partial Update. Please provide your feedback/comment/concerns over CA PPM REST and GEL Script Consuming these RESTful URLs for healthy discussion.

Prashank Singh

Senior CA PPM Consultant


Spread the love ...
«
»