Clarity

  • 1.  convert Java code to GEL Script

    Posted May 02, 2011 04:28 AM
    Hi,

    I need help to convert following Java code sample to GEL script using new, invoke, set tags.
        try {
          //Create connection
          url = new URL(targetURL);
          connection = (HttpURLConnection)url.openConnection();
          connection.setRequestMethod("POST");
          ...
          BASE64Encoder enc = new sun.misc.BASE64Encoder();
          String userpassword = username + ":" + password;
          String encodedAuthorization = enc.encode( userpassword.getBytes() );
          connection.setRequestProperty("Authorization", "Basic "+
                encodedAuthorization);
          ...
          //Send post data
    My try:
    <core:new var="url" className="java.net.URL">
    
    
    <core:arg value="https://test.com/webservice/"/>
    
    </core:new>
    
    <core:new var="connection" className="HttpURLConnection">
    
    </core:new>
    
    <core:invoke method="openConnection" on="${url}" var="connection"/>
    In above code "connection" assignment is failing.

    Thanks,
    -NP.


  • 2.  RE: convert Java code to GEL Script

     
    Posted May 10, 2011 07:27 PM
    Hi All,

    Any ideas here for NP?

    Thanks!
    Chris


  • 3.  RE: convert Java code to GEL Script
    Best Answer

    Posted May 11, 2011 01:41 PM

    N.P wrote:

    Hi,

    I need help to convert following Java code sample to GEL script using new, invoke, set tags.
        try {
          //Create connection
          url = new URL(targetURL);
          connection = (HttpURLConnection)url.openConnection();
          connection.setRequestMethod("POST");
          ...
          BASE64Encoder enc = new sun.misc.BASE64Encoder();
          String userpassword = username + ":" + password;
          String encodedAuthorization = enc.encode( userpassword.getBytes() );
          connection.setRequestProperty("Authorization", "Basic "+
                encodedAuthorization);
          ...
          //Send post data
    My try:
    <core:new var="url" className="java.net.URL">
    
    
    <core:arg value="https://test.com/webservice/"/>
    
    </core:new>
    
    <core:new var="connection" className="HttpURLConnection">
    
    </core:new>
    
    <core:invoke method="openConnection" on="${url}" var="connection"/>
    In above code "connection" assignment is failing.

    Thanks,
    -NP.
    Hi N.P,

    In your GEL script version, you are trying to instantiate the HttpURLConnection class directly with a 0 argument constructor. This is not possible, because this class has only one contructor, and it has 1 argument.
    You should use something like
    <core:new var="connection" className="HttpURLConnection">
        <core:arg value="${url}"/>
    </core:new>
    Let me know if this works, I didn't test it.


  • 4.  Re: convert Java code to GEL Script

    Posted Apr 17, 2015 06:40 AM

    As per helloworld the answer is incorrect as -

     

     

      Re: Calling Java Class in GEL

       helloworld Member

     

     

      

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

     

    https://communities.ca.com/thread/241728688

     

    So, the correct answer needs to be provided if so is the case, I guess

     

    NJ