#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    17
    Rep Power
    0

    CF6: Calling 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 = "(URL address blocked: See forum rules):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")>
  2. #2
  3. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,264
    Rep Power
    968
    I'm not going to go through all the Java code (I don't have the time), but it may be because your method call is passing date strings and "null". Those are strings, not real Java nulls or Date objects. You'd need to use JavaCast to convert your values into the proper Java types. However, CF didn't add null support to JavaCast until CF 7. So for the null value, I'm not sure what you want to do is possible with CF 6.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    17
    Rep Power
    0
    Originally Posted by kiteless
    I'm not going to go through all the Java code (I don't have the time), but it may be because your method call is passing date strings and "null". Those are strings, not real Java nulls or Date objects. You'd need to use JavaCast to convert your values into the proper Java types. However, CF didn't add null support to JavaCast until CF 7. So for the null value, I'm not sure what you want to do is possible with CF 6.
    Any suggestions beside upgrading coldfusion?
  6. #4
  7. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,264
    Rep Power
    968
    The only other option might be to create your own JAR, and use your own Java code to pass the null into the original Java method.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    131
    Rep Power
    7
    but receiving a null null error. I am having difficulties as my site is not telling me where the null error is.
    First off, check the logs for more details about the error. What does the stack trace message report? Check both the main \logs directory and runtime\logs.

    public static String getPDFFileName(String account, Date startdate, Date enddate, String source, String filename)

    Second, ignoring the lack of null's for a moment, there are other syntax problems. The method signatures require java.util.Date objects, but you are passing in strings ie "20130808". You should be passing in date/time objects instead. IIRC, CF datetime objects are automatically converted to java.util.Date's.

    Third, I only skimmed your code, but it looks like the method just substitutes an empty string when the source is null. So it is possible you do not need a real null anyway. Try passing in "" instead.
    Last edited by cfSearching; August 9th, 2013 at 07:05 PM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    131
    Rep Power
    7
    Originally Posted by bozzpaw
    get a PDF file and converts into 64 bit format.
    Hm.. do you mean base64? If so, why not just read the file as binary and use toBase64?

IMN logo majestic logo threadwatch logo seochat tools logo