Angelos PaaS4SaaS Blog

Wednesday, 3 June 2015

Base64 and URL Encoding with Groovy and Java Cloud SX (PaaS)

There are times when you want to execute some code within Groovy which Oracle Sales Cloud's groovy doesn't like. A very common example is URLEncode and Base64Encoding, however there are many others..


Native Groovy supports both base64 encoding/decoding and URL Encoding/Decoding


e.g.



String encoded = s.bytes.encodeBase64.toString()


Alas the groovy interpreter within Sales Cloud doesn't support either the base64 encoding/decoding classes or the URLEncoding classes. Thankfully there is a an easy workaround, Sales Cloud does support the ability to call a SOAP Service from Sales Cloud and given that many SalesCloud installations will have a Java Cloud SX instance available to them its quite easy to create a Java SOAP Service, deploy it to JCSSX and then call this from Sales Cloud to do the stuff that Sales Cloud’s groovy wont allow you to do.

https://4.bp.blogspot.com/-W_PVvNpvJ_Y/VW7a8Ow9DpI/AAAAAAAAMq8/FOvtRAKulGM/s400/base64image.jpg


Steps to recreate this
  1. Create a new Project within your favourite IDE (I use JDeveloper11g for Sales Cloud Development, Netbeans for other stuff)
  2. Ensure your project has support for JAX-WS WebServices, within JDeveloper  create a JEE project.
  3. Within your project create a new Java class, I’ve called PTSEncoder
  4. Now cut and paste the following code into this class, obviously rename the Class name if you havent used the same name as I have
package oracle.pts.encoder;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.bind.DatatypeConverter;
@WebService
public class PTSEncoder {
   public PTSEncoder() {
       super();
   }

   /**
    *
    * @param s - String to be translated
    * @return
    */
   @WebMethod(operationName = "encode")
   public String utf8encode(String s) {
       String result = "";
       try {

           result = URLEncoder.encode(s, "UTF-8");
           System.out.println("Encoded URL " + result);

       } catch (UnsupportedEncodingException e) {
           System.err.println(e);
       }
       return result;
   }

   /**
    *
    * @param s - String to be translated
    * @param enc - The name of a supported character encoding
    * @return
    */
   @WebMethod(operationName = "encodeWithEncType")
   public String ptsEncodeWithEncType(String s, String enc) {
       String result = "";
       try {
           if (enc == null || enc.length() <= 0) {
               enc = "UTF-8";
           }
           result = URLEncoder.encode(s, enc);
           System.out.println("Encoded URL " + result);

       } catch (UnsupportedEncodingException e) {
           System.err.println(e);

       }
       return result;
   }

   /**
    *
    * @param s - String to be translated
    * @return
    */
   @WebMethod(operationName = "decode")
   public String ptsDecode(String s) {
       String result = "";
       try {

           result = URLDecoder.decode(s, "UTF-8");
           System.out.println("Decoded URL " + result);

       } catch (UnsupportedEncodingException e) {
           System.err.println(e);
       }
       return result;
   }

   /**
    *
    * @param s - String to be translated
    * @param enc - The name of a supported character encoding
    * @return
    */
   @WebMethod(operationName = "decodeWithEncType")
   public String ptsDecodeWithEncType(String s, String enc) {
       String result = "";
       try {
           if (enc == null || enc.length() <= 0) {
               enc = "UTF-8";
           }
           result = URLDecoder.decode(s, enc);
           System.out.println("Decoded URL " + result);

           // String decodedUrl = URLDecoder.decode(encodedUrl, "UTF-8");
           //System.out.println("Dncoded URL " + decodedUrl);

       } catch (UnsupportedEncodingException e) {
           System.err.println(e);

       }
       return result;
   }

   /**
    * @param s
    * @return
    * @throws IOException
    */
   @WebMethod(operationName = "encodebase64")
   public String ptsEncodeBase64(String s) throws IOException {        
       return DatatypeConverter.printBase64Binary(s.getBytes());
   }

   /**
    * @param s
    * @return
    * @throws IOException
    */
   @WebMethod(operationName = "decodebase64")
   public String ptsDecodeBase64(String s) throws IOException {    
       String result = new String(DatatypeConverter.parseBase64Binary(s));
       
       return result;
   }
 // Simple tester
   @WebMethod(exclude = true)
   public static void main(String[] args) {
       PTSEncoder pTSEncode = new PTSEncoder();
       pTSEncode.utf8encode("Angelo Woz here");
       pTSEncode.ptsEncodeWithEncType("Angelo Woz Here", "UTF-8");
       pTSEncode.utf8encode("------------");
       pTSEncode.ptsDecode("Jo was here");
       pTSEncode.ptsDecodeWithEncType("Jo Was here", "UTF-8");

       try {
           System.out.println("Encode Angelo = "+pTSEncode.ptsEncodeBase64("Encode Angelo"));
       } catch (IOException e) {
           e.printStackTrace();
       }
   }
}
For interest I created this class by first creating the methods and then using J Developers wizard to convert a class+methods into a SOAP WebService. This class uses Java annotations which tell at JEE server that most (not all) of these methods are WebService calls. This is done using server side injection at deployment time.
  1. If within JDeveloper you created your project as a web/jee project you can simply deploy it as is to your JCSSX, or local WLS Application Server
    1. Right Mouse Click on the Project, deploy to your deployment profile
    2. Deploy to Application Server
    3. Choose your application server and deploy
    4. Check the deployment has completed
You can now test the SOAP Service using a SOAP testing tool like Http Analyzer or SOAPUI The WSDL of the service would be the contextRoot+WebService Name. For JDeveloper this can be found if you right-click on the Webservice Class,Java WebServices Editor and look at the generation options



So in my instance the WSDL will be available at

https://<JCSSXServer>.java.us2.oraclecloudapps.com/PTSEncoder/PTSEncoderService?wsdl


  1. You can put this into SOAPUI or Http Analyzer and test away
  2. Now last you can register it in Sales Cloud as a web service and use it from Groovy
    1. Activate a Sandbox,  that way you can undo changes if oyu want
    2. Navigate to Application Composer
    3. Navigate to the application you will be using the SOAP WebService from (Common,Sales etc.)
    4. Select WebServices
    5. Enter a name for the WebService, this name becomes the groovy package name
    6. Security None (for testing only)
    7. Then finally use the SoapService from any groovy script you desire, remember the Palette helps you find different services registered on you system


Sample Groovy Code
def base64result = adf.webServices.PTSBase64.encodebase64("Angelo Woz Here")


Final footnote
This example shows how to execute a base64 encoding externally using Java Cloud ServiceSaaS eXtensions (JCSSX), the example could easily have used Java Cloud Service, or some other Cloud service. More importantly you can code extensions using Java Cloud Service and call them from SalesCloud. Given that most JCSSX instances are going to be co-located within the same data-centre this makes the operation quick, efficient and very flexible!
Lastly, the service I deployed didn't contain any security because it’s a stateless service and ok for anyone to call, that said in a production environment I would still add a modicum of security to the service just to make sure someone doesn't try and abuse it.

Angelo
Enjoy!

3 comments:

  1. If you own the magnificent LG G6 smartphone, then you must be wandering how to protect this expensive phone with a cover that would look chic as

    well as provide protection to accidental drops and other hazards. Well you have to look no further as I have a well comprised list of some of the

    best LG G6 phone covers. Just have a look at my site here for all the information Best LG G6 Case

    ReplyDelete
  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete
  3. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well.

    Oracle Fusion Financials Online Training

    ReplyDelete