Clarity

 View Only
  • 1.  Calling Java Class in GEL

    Posted Apr 17, 2015 02:28 AM

    Hi,

     

    I want to use the HttpURLConnection for invoking a REST URL in GEL. How do I build the below statements in GEL?

     

              URL url = new URL ("http://www.test.com");
              HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    
    

     

    I can use the core:new tag for the first statement. Need help with the second statement.

    Thanks in advance,

     

    Leo.



  • 2.  Re: Calling Java Class in GEL

    Posted Apr 17, 2015 05:22 AM


  • 3.  Re: Calling Java Class in GEL

    Posted Apr 17, 2015 06:07 AM

    The answer mentioned in that post is incorrect. You cannot instantiate an object of HttpURLConnection class as it has a protected constructor.



  • 4.  Re: Calling Java Class in GEL

    Posted Apr 17, 2015 11:12 AM

    Could you live without the class cast?  If it is to communicate with a web service that requires basic authentication, that can be achieved without writing code that perfectly replicates the java equivalent with a cast: Re: Calling external SOAP from Clarity GEL



  • 5.  Re: Calling Java Class in GEL

    Posted Apr 17, 2015 02:18 PM

    Hi,

     

    If you create a new URL object and store it on the url var, you can try this:

     

    <core:invoke method="openConnection" on="${url}" var="connection"/>

     

    This means: var connection = url.openConnection();

     

    Regards,

    Roberto Barra



  • 6.  Re: Calling Java Class in GEL

    Posted Apr 17, 2015 04:57 PM

    Here is one I built to test if a URL was accessible using URL and openConnection.

     

     

    <?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()}" />
       
        <gel:out>Encoding Type</gel:out>
        <gel:out>${httpUrl.getResponseCode()}</gel:out>
        <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">
            <!-- /fs0/clarity1/share/UrlOutput.txt -->
            <core:arg type="java.lang.String" value="UrlOutput.txt"/>
        </core:new>
    
        <core:invoke method="println" on="${FileOne}">
            <core:arg value="${stringBuilder.toString()}" />
        </core:invoke>
    
    <!--    <gel:out>${stringBuilder.toString()}"</gel:out>-->
        <gel:out>End this Script</gel:out>
    
    </gel:script>
    

     

    Depending if you are looking for something simple (no auth) and have access to the util taglib.

     

    <?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: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



  • 7.  Re: Calling Java Class in GEL

    Posted Apr 19, 2015 02:27 AM

    Thanks for your replies.

     

    gcubed: I'm already leveraging the code you shared in earlier posts around invoking servicenow. I realized my problem is with maintaining sessions rather than the HTTPConnection object. In this third party application which I have, I need to first make a rest call to authenticate URL, get the session ID/token and send it as a parameter to the second rest call to object creation URL. When I use the session ID to create the object, it says the session ID is invalid. This is because it creates a new session every time I call the url.openConnection().

     

    I tried to use CookieManager to maintain the session and it works great in Java, but in GEL it doesn’t seem to work.

     

    <core:new className="java.net.CookieManager" var="cManager">

                    </core:new>

                    <core:expr value="${cManager.setCookiePolicy(java.net.CookiePolicy.ACCEPT_ALL)}"/>

                    <core:expr value="${java.net.CookieHandler.setDefault(cManager)}"/>

     

     

    Any ideas on how to manage HTTP sessions in GEL?

     

    Thanks,

     

    Leo.



  • 8.  Re: Calling Java Class in GEL
    Best Answer

    Posted Apr 19, 2015 06:08 PM

    I have had little success using the core:expr tag except on primitive data types.

     

    Try using the invoke, getStatic and invokeStatic tags to set up your java.net.CookieManager

     

    <core:new className="java.net.CookieManager" var="cManager" />
    
    <!-- <core:expr value="${cManager.setCookiePolicy(java.net.CookiePolicy.ACCEPT_ALL)}"/> -->
    <core:invoke method="setCookiePolicy" on="${cManager}" >
         <core:arg>
              <core:getStatic var="dummy" className="java.net.CookiePolicy" field="ACCEPT_ALL"/>
         </core:arg>
    </core:invoke>
    
    <!-- <core:expr value="${java.net.CookieHandler.setDefault(cManager)}"/> -->
    <core:invokeStatic className="java.net.CookieHandler" method="setDefault">
         <core:arg value="${cManager}" />
    </core:invokeStatic>
    

     

    V/r,

    Gene



  • 9.  Re: Calling Java Class in GEL

    Posted Apr 20, 2015 12:48 AM

    gcubed You are amazing! That worked perfectly..

     

    I had to make a one minor change of declaring the policy first and then using it in the invoke tag.

     

     <core:new className="java.net.CookieManager" var="cManager" />
    
     <core:getStatic var="cPolicy" className="java.net.CookiePolicy" field="ACCEPT_ALL"/>
    
     <core:invoke var="test" method="setCookiePolicy" on="${cManager}" >
       <core:arg value="${cPolicy}"></core:arg>
     </core:invoke>
    
     <core:invokeStatic className="java.net.CookieHandler" method="setDefault">
       <core:arg type="java.net.CookieHandler" value="${cManager}" />
     </core:invokeStatic>
    

     

    Thanks a ton for your help!

     

    Leo.