How to call Java Class/Method from CF

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bozzpaw
    New Member
    • Aug 2013
    • 1

    How to call Java Class/Method from CF

    I have the class file below that will get a PDF file and converts into 64 bit format.

    Code:
    package com.washgas.kubra.client;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import javax.xml.rpc.ServiceException;
    
    import org.apache.xerces.impl.dv.util.Base64;
    import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.DocumentHelper;
    
    import com.washgas.KubraBillPrintProxy;
    import com.washgas.KubraBillPrintProxyProxy;
    import com.washgas.KubraBillPrintProxyServiceLocator;
    
    public class KubraAPI {
    
    	/**
    	 * @param args
    	 */
    	private static KubraBillPrintProxyServiceLocator locator;
    	private static KubraBillPrintProxy soap;
    	private static String ADDRESS = "http://wgdevaix21.dev.washgas:9080/WSKubraProxy/services/KubraBillPrintProxy";
    	
    	public static void main(String[] args) 
    	{
    		/*
    		<?xml version="1.0" encoding="UTF-8"?>
    		<BILLPrint><account>0063062087</account><startdate/><enddate/><billsource/></BILLPrint>
    		
    		 Document document = DocumentHelper.createDocument();
    		 Element root = document.addElement( "BILLPrint" );
    		 root.addElement("account").addText("0063062087");
    		 root.addElement("startdate");
    		 root.addElement("enddate");
    		 root.addElement("billsource");
    		 
    		 System.out.println(document.asXML());
    		 	*/	
    		    
    		String filename = KubraAPI.getPDFFileName("0063062087", null, null, null,null);
    		System.out.println("File name: " + filename);
    	}
    	
    	public static String getPDFFileName(String account, Date startdate, Date enddate, String source, String filename)
    	{
    		if (filename == null)
    		filename = "C:/mockpdf.pdf";
    		try {
    			byte[] array = getPDFBytesArray(account, startdate, enddate, source);
    			
    			FileOutputStream fos1 = new FileOutputStream(filename);
    			int len = array.length;
    			System.out.println(new String(array));
    			fos1.write(array);
    			fos1.flush();
    			fos1.close();
    		} 
    		catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		return filename;
    	}
    	public static byte[] getPDFBytesArray(String account, Date startdate, Date enddate, String source) 
    	{
    		byte[] array = null;
    		String strstartdate = null;
    		String strenddate   = null;
    		
    		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    		
    		if (startdate != null)
    			strstartdate = formatter.format(startdate);
    		if (enddate != null)
    			strenddate = formatter.format(enddate);
    		
    		try 
    		{
    			//String datemft = formatter.format(date);
    			
    			 Document document = DocumentHelper.createDocument();
    			 Element root = document.addElement( "BILLPrint" );
    			 root.addElement("account").addText(account == null?"":account);
    			 root.addElement("startdate").addText(strstartdate == null?"":strstartdate);
    			 root.addElement("enddate").addText(strenddate == null?"":strstartdate);
    			 root.addElement("billsource").addText(source == null?"":source);
    			
    			locator = new KubraBillPrintProxyServiceLocator();
    			soap = locator.getKubraBillPrintProxy(new URL(ADDRESS)); // from configuration
    			
    			
    			String base64 =  soap.getKubraPDF(document.asXML());
    			array = base64Decode(base64);
    			FileOutputStream fos1 = new FileOutputStream("C:/Kubra/mockpdf.pdf");
    			int len = array.length;
    			System.out.println(new String(array));
    			fos1.write(array);
    			fos1.flush();
    			fos1.close();
    			
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} 
    		return array;
    	}
    	private static byte[] base64Decode(String base64) 
    	{
    		return Base64.decode(base64);
    	}
    	private static String base64Encode(byte[] array) 
    	{
    		return Base64.encode(array);
    	}
    
    }
    I need to call this getPDFFile method and set the result in a variable from my coldfusion. I am using the code below but receiving a null null error. I am having difficulties as my site is not telling me where the null error is. Any help will greatly appreciated.

    Code:
    <!--- create an object based on the EAI Kubra Web Service class  --->
    <cfset myObject = createObject( "java", "com.washgas.kubra.client.KubraAPI" )>
     
    <!--- call the getPDFFileName method and store the result in a variable. --->
    <cfset result = myObject.getPDFFileName("0063062087", "20130808", "20130808", "null", "C:\test.pdf")>
    Last edited by Rabbit; Aug 9 '13, 03:37 PM. Reason: Please use code tags when posting code or formatted data.
Working...