Clarity

Expand all | Collapse all

Calling a URL using GEL script

  • 1.  Calling a URL using GEL script

    Posted Aug 21, 2013 09:43 PM
    Hi,

    We have a URL that will perform certain action when it is called ( paste in the browser and hit enter).

    I want to call this URL using a Gel-script code so that it can be automated. Any idea how we can do this?

    Regards,
    Pankaj


  • 2.  RE: Calling a URL using GEL script

    Posted Aug 22, 2013 01:00 AM
    If your URL points to an XML source then you could do it like this.
    <?xml version="1.0" encoding="utf-8"?>
    <gel:script
    
    xmlns:core="jelly:core"
    
    xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
    
    xmlns:x="jelly:org.apache.commons.jelly.tags.xml.XMLTagLibrary">
    
    
    <x:parse var="xmlSource" xml="http://www.webservicex.net/stockquote.asmx?WSDL" />
    
    <gel:out>${xmlSource.asXML()}</gel:out>
    
    </gel:script>
    Or the hard way is like this which works with any style URL supported by java.net.URL.
    <?xml version="1.0" encoding="utf-8"?>
    <gel:script
    
    xmlns:core="jelly:core"
    
    xmlns:log="jelly:log"
    
    xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
    
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
    
    <!-- StringBuilder for the output -->
    
    <core:new className="java.lang.StringBuilder" var="stringBuilder" />
    
    
    <!-- URL for the web url -->
    
    <core:new className="java.net.URL" var="url">
    
    
    <core:arg type="java.lang.String" value="http://www.ca.com/us/default.aspx"/>
    
    </core:new>
    
    
    <!-- Open and read the response stream -->
    
    <!-- Open and read the response stream -->
    
    <core:set var="httpUrl" value="${url.openConnection()}" />
    
    <core:new className="java.io.InputStreamReader" var="inputStreamReader">
    
    
    <core:arg type="java.io.InputStream" value="${httpUrl.getInputStream()}"/>
    
    </core:new>
    
    <core:new className="java.io.BufferedReader" var="bufferedReader">
    
    
    <core:arg type="java.io.InputStreamReader" value="${inputStreamReader}"/>
    
    </core:new>
    
    
    <!-- loop through the response appending each line into the stringbuilder -->
    
    <core:set var="line" value="" />
    
    <core:while test="${line != null}">
    
    
    <core:set var="line" value="${bufferedReader.readLine()}" />
    
    
    <core:if test="${line != null}">
    
    
    
    <core:invoke method="append" on="${stringBuilder}">
    
    
    
    
    <core:arg value="${line}" />
    
    
    
    </core:invoke>
    
    
    </core:if>
    
    </core:while>
    
    
    <!-- Close the bufferReader -->
    
    <core:if test="${bufferedReader != null}">
    
    
    <core:invoke method="close" on="${bufferedReader}" />
    
    </core:if>
    
    
    <gel:out>${stringBuilder.toString()}</gel:out>
    
    <gel:out>End this Script </gel:out>
    
    </gel:script>
    V/r,
    Gene


  • 3.  RE: Calling a URL using GEL script

    Posted Aug 23, 2013 12:03 AM
    Hi Gene - Thanks for the script, I am getting this error when I run it using a Clarity process. Sorry, I am not too technical with Java else would have debugged it myself.

    BPM-0704: An error occurred while executing custom script: org.apache.commons.jelly.JellyTagException: null:18:77: <core:new> null at org.apache.commons.jelly.tags.core.NewTag.doTag(NewTag.java:107) at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:247) at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:95) at org.apache.commons.jelly.TagSupport.invokeBody(TagSupport.java:186) at com.niku.union.gel.tags.ScriptTag.doTag(ScriptTag.java:20) at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:247) at com.niku.union.gel.GELScript.run(GELScript.java:80) at com.niku.union.gel.GELController.invoke(GELController.java:20) at com.niku.bpm.services.ExecuteCustomAction.run(ExecuteCustomAction.java:180) at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:727) at java.lang.Thread.run(Thread.java:619) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at org.apache.commons.beanutils.ConstructorUtils.invokeConstructor(ConstructorUtils.java:155) at org.apache.commons.jelly.tags.core.NewTag.doTag(NewTag.java:84) ... 10 more Caused by: java.lang.NullPointerException at java.io.Reader.<init>(Reader.java:61) at java.io.InputStreamReader.<init>(InputStreamReader.java:55) ... 16 more Root cause java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at org.apache.commons.beanutils.ConstructorUtils.invokeConstructor(ConstructorUtils.java:155) at org.apache.commons.jelly.tags.core.NewTag.doTag(NewTag.java:84) at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:247) at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:95) at org.apache.commons.jelly.TagSupport.invokeBody(TagSupport.java:186) at com.niku.union.gel.tags.ScriptTag.doTag(ScriptTag.java:20) at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:247) at com.niku.union.gel.GELScript.run(GELScript.java:80) at com.niku.union.gel.GELController.invoke(GELController.java:20) at com.niku.bpm.services.ExecuteCustomAction.run(ExecuteCustomAction.java:180) at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:727) at java.lang.Thread.run(Thread.java:619) Caused by: java.lang.NullPointerException at java.io.Reader.<init>(Reader.java:61) at java.io.InputStreamReader.<init>(InputStreamReader.java:55) ... 16 more


  • 4.  RE: Calling a URL using GEL script

    Posted Aug 23, 2013 01:46 AM
    BPM-0704: An error occurred while executing custom script: org.apache.commons.jelly.JellyTagException: null:18:77:

    Can you check line 18 of the script ? Please provide the gel you have written.

    NJ


  • 5.  RE: Calling a URL using GEL script

    Posted Aug 23, 2013 02:27 AM
    Not sure of your error. I updated this to run via the process engine (gel:out to gel:log). I also added a step to save the URL response to a file.

    It ran in our On Demand 13.1 environment via the process engine. If you are not on On Demand, you will want to change the file location as "/fs0/clarity1/share" is most likely not available.

    <?xml version="1.0" encoding="utf-8"?>
    <gel:script
    
    xmlns:core="jelly:core"
    
    xmlns:log="jelly:log"
    
    xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
    
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
    
    <!-- StringBuilder for the output -->
    
    <core:new className="java.lang.StringBuilder" var="stringBuilder" />
    
    
    <!-- URL for the web url -->
    
    <core:new className="java.net.URL" var="url">
    
    
    <core:arg type="java.lang.String" value="http://www.ca.com/us/default.aspx"/>
    
    </core:new>
    
    
    <!-- Open and read the response stream -->
    
    <core:set var="httpUrl" value="${url.openConnection()}" />
    
    <core:new className="java.io.InputStreamReader" var="inputStreamReader">
    
    
    <core:arg type="java.io.InputStream" value="${httpUrl.getInputStream()}"/>
    
    </core:new>
    
    <core:new className="java.io.BufferedReader" var="bufferedReader">
    
    
    <core:arg type="java.io.InputStreamReader" value="${inputStreamReader}"/>
    
    </core:new>
    
    
    <!-- l anoop through the response appending each line into the stringbuilder -->
    
    <core:set var="line" value="" />
    
    <core:while test="${line != null}">
    
    
    <core:set var="line" value="${bufferedReader.readLine()}" />
    
    
    <core:if test="${line != null}">
    
    
    
    <core:invoke method="append" on="${stringBuilder}">
    
    
    
    
    <core:arg value="${line}" />
    
    
    
    </core:invoke>
    
    
    </core:if>
    
    </core:while>
    
    
    <!-- Close the bufferReader -->
    
    <core:if test="${bufferedReader != null}">
    
    
    <core:invoke method="close" on="${bufferedReader}" />
    
    </core:if>
    
    
    <core:new className="java.io.PrintWriter" var="FileOne">
    
    
    <core:arg type="java.lang.String" value="/fs0/clarity1/share/UrlOutput.txt"/>
    
    </core:new>
    
    
    <core:invoke method="println" on="${FileOne}">
    
    
    <core:arg value="${stringBuilder.toString()}" />
    
    </core:invoke>
    
    
    <gel:log level="Info" message="${stringBuilder.toString()}" />
    
    <gel:log level="Info" message="End this Script" />
    
    </gel:script>
    V/r,
    Gene


  • 6.  RE: Calling a URL using GEL script

    Posted Aug 23, 2013 09:20 PM
    The error starts appearing from this line

    <core:new className="java.io.InputStreamReader" var="inputStreamReader">
    <core:arg type="java.io.InputStream" value="${httpUrl.getInputStream()}"/>
    </core:new>

    before that where I am opening the connection, that works fine. I am even able to print the httpurl variable. but beyond that where the above i/o operation starts, it fails


  • 7.  RE: Calling a URL using GEL script

    Posted Aug 24, 2013 03:49 AM
    Place this or log it:


    <gel:out>${httpUrl.getContentType()}</gel:out>

    just above:


    <core:new className="java.io.InputStreamReader" var="inputStreamReader">

    Depending on your URL you should see something like:

    text/html; charset=utf-8

    This will validate that you have an URLConnection object in httpUrl.

    Another thing that will toss an exception if you are not in an On Demand environment.

    Change



    <core:arg type="java.lang.String" value="/fs0/clarity1/share/UrlOutput.txt"/>
    to



    <core:arg type="java.lang.String" value="UrlOutput.txt"/>

    V/r,
    Gene


  • 8.  RE: Calling a URL using GEL script

    Posted Aug 25, 2013 09:43 PM
    Thanks Gene, I tried printing the output of the various methods that can be called from the httpUrl variable and here's the summary.

    <gel:log category="INFO" level="INFO">HTTP URL = ${httpUrl}</gel:log>

    <gel:log category="INFO" level="INFO">getContent = ${httpUrl.getContent()}</gel:log>

    <gel:log category="INFO" level="INFO">getHeaderField = ${httpUrl.getHeaderField}</gel:log>

    <gel:log category="INFO" level="INFO">getInputStream = ${httpUrl.getInputStream()}</gel:log>

    <gel:log category="INFO" level="INFO">getOutputStream= ${httpUrl.getOutputStream()}</gel:log>

    <gel:log category="INFO" level="INFO">getDate = ${httpUrl.getDate()}</gel:log>

    <gel:log category="INFO" level="INFO">getLastModifed = ${httpUrl.getLastModifed()}</gel:log>

    httpUrl gives me this value "sun.net.www.protocol.http.HttpURLConnection:http://www.google.com"

    getContent ,getContentType,getLastModifed,getOutputStream,getInputStream,getHeaderField all give me a BLANK. getDate gives a 0.

    This means surely there is some problem with httpUrl variable although it is not null itself but any method it calls gives me a null as per the above. Here is the exact gel script
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


    <gel:script xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:core="jelly:core" xmlns:email="jelly:email"
    xmlns:file="jelly:com.niku.union.gel.FileTagLibrary"
    xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
    xmlns:soap="jelly:com.niku.union.gel.SOAPTagLibrary"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:sql="jelly:sql" xmlns:util="jelly:util"

    xmlns:log="jelly:log"

    xmlns:xog="http://www.niku.com/xog"

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

    <!-- StringBuilder for the output -->
    <core:new className="java.lang.StringBuilder" var="stringBuilder" />


    <!-- URL for the web url -->
    <core:new className="java.net.URL" var="url">
    <core:arg type="java.lang.String" value="http://www.google.com"/>
    </core:new>



    <!--
    Define a URLConnection variable httpUrl

    -->

    <core:set var="httpUrl" value="${url.openConnection()}" />


    <gel:log category="INFO" level="INFO">URL = $url</gel:log>


    <gel:log category="INFO" level="INFO">HTTP URL = ${httpUrl}</gel:log>

    <gel:log category="INFO" level="INFO">getContent = ${httpUrl.getContent()}</gel:log>

    <gel:log category="INFO" level="INFO">getHeaderField = ${httpUrl.getHeaderField}</gel:log>

    <gel:log category="INFO" level="INFO">getInputStream = ${httpUrl.getInputStream()}</gel:log>

    <gel:log category="INFO" level="INFO">getOutputStream= ${httpUrl.getOutputStream()}</gel:log>

    <gel:log category="INFO" level="INFO">getDate = ${httpUrl.getDate()}</gel:log>

    <gel:log category="INFO" level="INFO">getLastModifed = ${httpUrl.getLastModifed()}</gel:log>

    </gel:script>


  • 9.  RE: Calling a URL using GEL script

    Posted Aug 26, 2013 01:59 AM
      |   view attached
    I ran your script on my On Demand server and this is what I get:

    Make sure that your url "www.google.com" doesn't redirect. I don't believe that java.net.URL can handle URL redirects.

    I added this to your script to see the content type.


    <gel:log category="INFO" level="INFO">getContent = ${httpUrl.getContentType()}</gel:log>

    V/r,
    Gene


  • 10.  RE: Calling a URL using GEL script

    Posted Aug 26, 2013 02:05 AM
    The other thing to consider is if you need a proxy to connect to www.google.com. If so you will need to deal with that:

    http://stackoverflow.com/questions/15878720/how-to-use-java-net-url-urlconnection-with-a-proxy-chain

    V/r,
    Gene


  • 11.  RE: Calling a URL using GEL script

    Posted Aug 26, 2013 12:15 PM
    I found this also works.
    <?xml version="1.0" encoding="utf-8"?>
    <gel:script
    
    xmlns:core="jelly:core"
    
    xmlns:log="jelly:log"
    
    xmlns:util="jelly:util"
    
    xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
    
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
    
    <gel:out>Start this Script</gel:out>
    
    
    
    <util:available uri="http://www.ca.com/us/default.aspx">
    
    
    <gel:out>This URL is available</gel:out>
    
    </util:available>
    
    
    
    <gel:out>End this Script</gel:out>
    
    </gel:script>
    But is also has the same issues with redirects / proxies and you don't get any of the response.

    Another option if you have access to the server is to drop the HTTP Tag Library into your directory that contains the other tag jars.

    The HTTP Tag library support redirects.

    V/r,
    Gene


  • 12.  Re: Calling a URL using GEL script

    Posted Oct 09, 2017 10:59 AM

    Hi Genegcubed,

     

    May I ask regarding this old post, how should I proceed when would like to redirect to another URL by a process? 

    Simply we just need to get to the default Project page (which URL I can construct), however the redirection is a problem. I found you advised download HTTP tag library which I downloaded from http://commons.apache.org  (commons-jelly.jar file). However what we should to do next?

     

    Thanks,

    Matej



  • 13.  Re: Calling a URL using GEL script

    Posted Oct 09, 2017 07:48 PM

    Just drop the jar in the lib directory and you should be able to use it in your Gel script.

     

    To do a get:

     

    <http:get followRedirects='true' uri='http://server/path' var='pageReturned' />

     

    V/r,

    Gene



  • 14.  Re: Calling a URL using GEL script

    Posted Oct 10, 2017 03:29 AM

    Hi Gene, 

     

    Thank for sharing this. I've just added it to "your" script but nothing happened. Furthermore I had to define <http> tag with extra line in definition because got an error like <http:get> is not known....

     

    Process script code:

     

    Process is auto-started when change one value in project, ran successfully, however without any log info details (this is very weird for me). Nothing happened as I said.

     

     

    Do you know the reason? 

     

    Thanks a lot!

    Matej



  • 15.  Re: Calling a URL using GEL script

    Posted Oct 10, 2017 01:35 PM

    So instead of the http taglib.  How about just getting the url contents to a string.

     

    <?xml version="1.0" encoding="utf-8"?>
    <gel:script
         xmlns:core="jelly:core"
         xmlns:log="jelly:log"
         xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">


         <!-- URL for the web url -->
         <core:new className="java.net.URL" var="url">
              <core:arg type="java.lang.String" value="https://www.ca.com/us/default.aspx"/>
         </core:new>
         
         <!-- Open and read the response stream -->
         <core:set var="httpUrl" value="${url.openConnection()}" />
         
         <!-- get the contents into a string -->
         <core:invokeStatic var="urlContents" className="org.apache.commons.io.IOUtils" method="toString" >
              <core:arg type="java.io.InputStream" value="${httpUrl.getInputStream()}" />
         </core:invokeStatic>

         <gel:log>${urlContents}"</gel:log>
         <gel:log>End this Script</gel:log>

    </gel:script>

     

    The https://www.ca.com/us/default.aspx page does a 301 redirect. The key thing with java.net.URL is that it will not redirect from http to https so you will need to be in the right schema.

     

    V/r,

    Gene



  • 16.  Re: Calling a URL using GEL script

    Posted Oct 11, 2017 02:25 AM

    Hi Gene, 

     

    Thanks for this another code. I used "http" site as our DEV environment is running with "http" protocol. 

    However once process is initiated, it stays in "Running" status with 0% of progress...

    That's weird for me this process works for you but not for me...

     

    Furthermore, As I said, I've downloaded commons jelly library as advised from apache.org site. - Only underlined one I was able to download. Others are OOTB libs:

    Is there any missing lib? 

     

    Matej



  • 17.  Re: Calling a URL using GEL script

    Posted Oct 12, 2017 11:22 PM

    The commons-jelly-1.0.1.jar is the wrong jar.

     

    You need the commons-jelly-tags-http-1.0.jar and commons-httpclient-3.1.jar.

     

     

    Then you use it as follows:

     

    <?xml version="1.0" encoding="utf-8"?>
    <gel:script
         xmlns:core="jelly:core"
         xmlns:log="jelly:log"
         xmlns:util="jelly:util"
         xmlns:http="jelly:http"
         xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">


         <gel:out>Start this Script</gel:out>

         <http:get followRedirects='true' uri='http://www.ca.com/us/default.aspx' var='responseVar' />
         <core:set var="body" value="${responseVar.getResponseBodyAsString()}" />

         
         <util:available uri="http://www.ca.com/us/default.aspx">
              <gel:out>This URL is available</gel:out>
              <util:loadText uri="http://www.ca.com/us/default.aspx" var="urlText" />
         </util:available>

         <gel:log>Dump Context Variables</gel:log>
         <core:set var="entries" value="${context.getVariables().entrySet().toArray()}" />     
         <core:forEach var="entry" items="${entries}">
              <core:if test="${!entry.getKey().equalsIgnoreCase('systemScope')}" >
                   <gel:log>${entry.getKey()} = ${entry.getValue()} |  ${entry.getValue().getClass().getName()} </gel:log>
              </core:if>
         </core:forEach>

         <gel:out>End this Script</gel:out>

    </gel:script>

     

     

    V/r,

    Gene



  • 18.  Re: Calling a URL using GEL script

    Posted Oct 13, 2017 04:55 AM

    Hi Gene, 

     

    Aaaah, that's important info for me, thanks. Your script with CA page thrown "Timeout error" when executed, but never mind...I changed URL then. 

     

    Our aim of all this staff, after saving one Subpage, is redirection user to the Default page of project.

    After changed URL to redirect it to Default Page of my project, process finished successfully then, however nothing happened, I still stayed at the mentioned subpage without any redirection...

     

    Is this redirection works for you so you get on the CA page? 

    Matej