Clarity

 View Only

Calling External Rest API from Clarity Gel script

  • 1.  Calling External Rest API from Clarity Gel script

    Posted Jan 11, 2023 05:22 PM
    I am trying to call an external API from gel script in Clarity and I am getting the error
    Error: org.apache.commons.jelly.JellyTagException: null:23:63: <core:new> null
    11/01/23 13:16
    System Errors Start Gel to Access External API BPM-0545: An error occurred when executing custom action.
    java.lang.Exception
    at com.niku.bpm.services.ExecuteCustomAction.actionCompleted(ExecuteCustomAction.java:333)
    at com.niku.bpm.services.ExecuteCustomAction.run(ExecuteCustomAction.java:258)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)


    The code is shown below

    <gel:script
    xmlns:core="jelly:core"
    xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary">


    <core:catch var="restCatch">


    <core:set var="restResource" value="https://jsonplaceholder.typicode.com/todos/1" />
    <core:new var="restUrl" className="java.net.URL" >
    <core:arg type="java.lang.String" value="${restResource}" />
    </core:new>
    <core:invoke var="restConnection" on="${restUrl}" method="openConnection" />

    <!--Set and get Header information-->
    <core:expr value="${restConnection.setRequestMethod('GET')}" />
    <core:expr value="${restConnection.setRequestProperty('Content-Type', 'application/json')}" />

    <!--Check the response-->
    <core:choose>
    <core:when test="${restConnection.getResponseCode() == 200}" >
    <gel:log level="DEBUG">Result: 200 OK</gel:log>
    <core:invoke var="tapi" on="${restConnection}" method="getInputStream" />
    </core:when>
    <core:otherwise>
    <gel:log level="WARN">Result: ${restConnection.getResponseCode()}</gel:log>
    <core:invoke var="tapi" on="${restConnection}" method="getErrorStream" />
    </core:otherwise>
    </core:choose>

    <!--Read the response-->
    <core:new var="isr" className="java.io.InputStreamReader">
    <core:arg type="java.io.InputStream" value="${tapi}" />
    </core:new>
    <core:new var="br" className="java.io.BufferedReader">
    <core:arg type="java.io.Reader" value="${isr}" />
    </core:new>
    <core:new var="response" className="java.lang.StringBuffer" />
    <core:set var="nextLine" value="${br.readLine()}" />
    <core:while test="${nextLine != null}">
    <core:expr value="${response.append(nextLine)}" />
    <core:set var="nextLine" value="${br.readLine()}" />
    </core:while>
    <core:expr value="${br.close()}" />

    <!--Google GSON object -->
    <core:new var="jsonParser" className="com.google.gson.JsonParser" />
    <core:set var="jsonElement" value="${jsonParser.parse(response.toString())}" />
    <core:set var="catGson" value="${jsonElement.getAsJsonObject()}" />

    <!--Log the response-->
    <gel:log level="WARN">Did you know?! ${catGson.fact.getAsString()}</gel:log>


    </core:catch>

    <!-- Any exceptions? -->
    <core:if test="${!empty(restCatch)}">
    <gel:log level="ERROR">Error: ${restCatch}</gel:log>
    </core:if>

    <gel:log level="WARN">The error is ${catGson.fact.getAsString()}</gel:log>

    </gel:script>