|
Mailing Lists
|
Home /
Groups /
Java
ColdFusion and Java
I have some java code which I want to kick in from a coldfusion pageAndrew Whone 04/13/07 10:48 A Close, but no cigar ;)Max Nyman 04/15/07 08:14 P I have some java code which I want to kick in from a coldfusion page e.g. <cfobject type="Java" class="IRMark" name="myObj"> <cfset ret=myObj.init("input.xml") It ought to be simple enough, pass the file name to the class and get the base64 number back. The Coldfusion documentation states: "Note: The init method is not a method of the object, but a ColdFusion identifier that calls the new function on the class constructor. So, if a Java object has an init method, a name conflict exists and you cannot call the object’s init method." and I suspect this is where my problem lies but my java is still basic. I would be very grateful if anyone could at least give me a few pointers to solve this problem. I have tried lots of things without any success. The Java Code: import java.io.*; import javax.xml.parsers.*; import java.security.*; import org.w3c.dom.*; import org.apache.xml.security.signature.*; import org.apache.xml.security.transforms.*; import org.apache.xml.security.Init; import org.bouncycastle.util.encoders.Base64; /** * This code generates an IRmark value for an input document. * The value is a base64 encoded SHA1 digest of a signature * transform over a certain style of document. The value has * to be placed inside documents to be signed by the XPE when * used in a EDS/IR deployment. * * The code has a number of jar dependencies:- * xmlsec.jar - The Apache XML Security Library * log4j-1.2.5.jar - The Apache Log utility * xalan.jar - Apache XSLT/XPath processor * xercesImpl.jar - Apache XML processor * bc-jce-jdk13-114.jar - Bouncy Castle JCE library * * The Bouncy Castle JCE provider is automatically downloaded * by the Apache XML sec library build so you may already have * that. */ public class IRMark { /** * Generate and print the IRmark. * * @param args - Pass the filename of the input document * @throws Exception */ public static void main(String args[]) throws Exception { // Init the Apache XML security library Init.init(); // Check we are given a file to work with if (args.length!=1) { System.out.println("Use: IRmark <file> "); return; } // Open the input file FileInputStream fis=null; try { fis=new FileInputStream(args[0]); } catch (FileNotFoundException e) { System.out.println("The file " + args[0] + " could not be opened."); return; } // Load file into a byte array byte[] data=null; try { int bytes=fis.available(); data=new byte[bytes]; fis.read(data); } catch (IOException e) { System.out.println("Error reading file."); e.printStackTrace(); } // First part is to run the a transform over the input to extract the // fragment to be digested. This is done by setting up a Transforms // object from a Template and then executing against the input document // The transforms to be performed are specified by using the template XML below. String transformStr = "<?xml version='1.0'?>\n" + "<dsig:Transforms xmlns:dsig='http://www.w3.org/2000/09/xmldsig#' xmlns:gt='http://www.govtalk.gov.uk/CM/envelope' xmlns:ir='http://www.govtalk.gov.uk/taxation/CISrequest'>\n" + "<dsig:Transform Algorithm='http://www.w3.org/TR/1999/REC-xpath-19991116'>\n" + "<dsig:XPath>\n" + "count(ancestor-or-self::node()|/gt:GovTalkMessage/gt:Body)=count(ancestor-or-self::node())\n" + " and count(self::ir:IRmark)=0 \n" + " and count(../self::ir:IRmark)=0 \n" + "</dsig:XPath>\n" + "</dsig:Transform>\n" + "<dsig:Transform Algorithm='http://www.w3.org/TR/2001/REC-xml-c14n-20010315#'/>\n" + "</dsig:Transforms>\n" ; // Parse the transform details to create a document DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db=dbf.newDocumentBuilder(); Document doc=db.parse(new ByteArrayInputStream(transformStr.getBytes())); // Construct a Apache security Transforms object from that document Transforms transforms = new Transforms(doc.getDocumentElement(), null); // Now perform the transform on the input to get the results. XMLSignatureInput input = new XMLSignatureInput(data); XMLSignatureInput result = transforms.performTransforms(input); // Uncomment this line to see transform output // System.out.println(new String(result.getBytes())); // Second part is to run output via SHA1 digest // This is done via the standard java.security API MessageDigest md = MessageDigest.getInstance("SHA"); md.update(result.getBytes()); byte[] digest=md.digest(); // And finally print a Base64 of the digest with // The help of the BouncyCastle JCE library System.out.println("IRmark: " + new String(Base64.encode(digest))); } } Thanks A+ -- No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.5.446 / Virus Database: 269.2.0/757 - Release Date: 11/04/2007 17:14 Close, but no cigar ;) The Java method "main" is called when you run the app from command line. In your case you want to make the call from CF so you need to "move" your code from the main method into the constructor (see below). I kept a main method in there for you, but the only thing it will really do is to call the constructor. I hope this helps. Cheers, Max <IRMark.java - START> import java.io.*; import javax.xml.parsers.*; import java.security.*; import org.w3c.dom.*; import org.apache.xml.security.signature.*; import org.apache.xml.security.transforms.*; import org.apache.xml.security.Init; import org.bouncycastle.util.encoders.Base64; /** * This code generates an IRmark value for an input document. * The value is a base64 encoded SHA1 digest of a signature * transform over a certain style of document. The value has * to be placed inside documents to be signed by the XPE when * used in a EDS/IR deployment. * * The code has a number of jar dependencies:- * xmlsec.jar - The Apache XML Security Library * log4j-1.2.5.jar - The Apache Log utility * xalan.jar - Apache XSLT/XPath processor * xercesImpl.jar - Apache XML processor * bc-jce-jdk13-114.jar - Bouncy Castle JCE library * * The Bouncy Castle JCE provider is automatically downloaded * by the Apache XML sec library build so you may already have * that. */ public class IRMark { /** * Generate and print the IRmark. * * @param fileName - Pass the filename of the input document * @throws Exception */ public IRMark(String fileName) throws Exception { // Init the Apache XML security library Init.init(); // Check we are given a file to work with if (fileName == null) { System.out.println("Use: IRMark <file> "); return; } // Open the input file FileInputStream fis=null; try { fis=new FileInputStream(fileName); } catch (FileNotFoundException e) { System.out.println("The file " + fileName + " could not be opened."); return; } // Load file into a byte array byte[] data=null; try { int bytes=fis.available(); data=new byte[bytes]; fis.read(data); } catch (IOException e) { System.out.println("Error reading file."); e.printStackTrace(); } // First part is to run the a transform over the input to extract the // fragment to be digested. This is done by setting up a Transforms // object from a Template and then executing against the input document // The transforms to be performed are specified by using the template XML below. String transformStr = "<?xml version='1.0'?>\n" + "<dsig:Transforms xmlns:dsig='http://www.w3.org/2000/09/xmldsig#' xmlns:gt='http://www.govtalk.gov.uk/CM/envelope' xmlns:ir='http://www.govtalk.gov.uk/taxation/CISrequest'>\n" + "<dsig:Transform Algorithm='http://www.w3.org/TR/1999/REC-xpath-19991116'>\n" + "<dsig:XPath>\n" + "count(ancestor-or-self::node()|/gt:GovTalkMessage/gt:Body)=count(ancestor-or-self::node())\n" + " and count(self::ir:IRmark)=0 \n" + " and count(../self::ir:IRmark)=0 \n" + "</dsig:XPath>\n" + "</dsig:Transform>\n" + "<dsig:Transform Algorithm='http://www.w3.org/TR/2001/REC-xml-c14n-20010315#'/>\n" + "</dsig:Transforms>\n" ; // Parse the transform details to create a document DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db=dbf.newDocumentBuilder(); Document doc=db.parse(new ByteArrayInputStream(transformStr.getBytes())); // Construct a Apache security Transforms object from that document Transforms transforms = new Transforms(doc.getDocumentElement(), null); // Now perform the transform on the input to get the results. XMLSignatureInput input = new XMLSignatureInput(data); XMLSignatureInput result = transforms.performTransforms(input); // Uncomment this line to see transform output // System.out.println(new String(result.getBytes())); // Second part is to run output via SHA1 digest // This is done via the standard java.security API MessageDigest md = MessageDigest.getInstance("SHA"); md.update(result.getBytes()); byte[] digest=md.digest(); // And finally print a Base64 of the digest with // The help of the BouncyCastle JCE library System.out.println("IRMark: " + new String(Base64.encode(digest))); } public static void main(String[] args) { // Check we are given a file to work with if (args.length!=1) { System.out.println("Use: IRMark <file> "); return; } new IRMark(args[0]); } } <IRMark.java - END> > I have some java code which I want to kick in from a coldfusion page > > > > e.g. <cfobject type="Java" class="IRMark" name="myObj"> > > <cfset ret=myObj.init("input.xml") > ----- Excess quoted text cut - see Original Post for more ----- > > > The Java Code: > ----- Excess quoted text cut - see Original Post for more ----- > > > import org.w3c.dom.*; > ----- Excess quoted text cut - see Original Post for more ----- > > > import org.bouncycastle.util.encoders.Base64; > > > > > > /** > > * This code generates an IRmark value for an input document. > > * The value is a base64 encoded SHA1 digest of a signature > > * transform over a certain style of document. The value has > > * to be placed inside documents to be signed by the XPE when > > * used in a EDS/IR deployment. > > * > > * The code has a number of jar dependencies:- > > * xmlsec.jar - The Apache XML Security Library > > * log4j-1.2.5.jar - The Apache Log utility > > * xalan.jar - Apache XSLT/XPath processor > > * xercesImpl.jar - Apache XML processor > > * bc-jce-jdk13-114.jar - Bouncy Castle JCE library > > * > > * The Bouncy Castle JCE provider is automatically downloaded > > * by the Apache XML sec library build so you may already have > > * that. > > */ > > > > > > public class IRMark { > > > > /** > > * Generate and print the IRmark. > > * > > * @param args - Pass the filename of the input document > > * @throws Exception > > */ > > public static void main(String args[]) throws Exception { > > > > // Init the Apache XML security library > > Init.init(); > > > > // Check we are given a file to work with > > if (args.length!=1) { > > System.out.println("Use: IRmark <file> "); > > return; > > } > > > > // Open the input file > > FileInputStream fis=null; > > try { > > fis=new FileInputStream(args[0]); > > } catch (FileNotFoundException e) { > > System.out.println("The file " + args[0] + " could not be opened."); > > return; > > } > > > > // Load file into a byte array > > byte[] data=null; > > try { > > int bytes=fis.available(); > > data=new byte[bytes]; > > fis.read(data); > > } catch (IOException e) { > > System.out.println("Error reading file."); > > e.printStackTrace(); > > } > > > > // First part is to run the a transform over the input to extract the > > // fragment to be digested. This is done by setting up a Transforms > > // object from a Template and then executing against the input > document > > > > // The transforms to be performed are specified by using the template > XML below. > > String transformStr = > > "<?xml version='1.0'?>\n" > > + "<dsig:Transforms xmlns:dsig='http://www.w3.org/2000/09/xmldsig#' > xmlns:gt='http://www.govtalk.gov.uk/CM/envelope' xmlns:ir='http://www. > govtalk.gov.uk/taxation/CISrequest'>\n" > > + "<dsig:Transform Algorithm='http://www.w3. > org/TR/1999/REC-xpath-19991116'>\n" > > + "<dsig:XPath>\n" > > + "count(ancestor-or-self::> node()|/gt:GovTalkMessage/gt:Body)=count(ancestor-or-self::node())\n" > > + " and count(self::ir:IRmark)=0 \n" > > + " and count(../self::ir:IRmark)=0 \n" > > + "</dsig:XPath>\n" > > + "</dsig:Transform>\n" > > + "<dsig:Transform Algorithm='http://www.w3. > org/TR/2001/REC-xml-c14n-20010315#'/>\n" > > + "</dsig:Transforms>\n" > > ; > > > > // Parse the transform details to create a document > > DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); > > dbf.setNamespaceAware(true); > > DocumentBuilder db=dbf.newDocumentBuilder(); > > Document doc=db.parse(new ByteArrayInputStream(transformStr. > getBytes())); > > > > // Construct a Apache security Transforms object from that document > > Transforms transforms = new Transforms(doc.getDocumentElement(), > null); > > > > // Now perform the transform on the input to get the results. > > XMLSignatureInput input = new XMLSignatureInput(data); > > XMLSignatureInput result = transforms.performTransforms(input); > > > > // Uncomment this line to see transform output > > // System.out.println(new String(result.getBytes())); > > > > // Second part is to run output via SHA1 digest > > // This is done via the standard java.security API > > MessageDigest md = MessageDigest.getInstance("SHA"); > > md.update(result.getBytes()); > > byte[] digest=md.digest(); > > > > // And finally print a Base64 of the digest with > > // The help of the BouncyCastle JCE library > > System.out.println("IRmark: " + new String(Base64.encode(digest))); > > } > > } > > Thanks A+ > ----- Excess quoted text cut - see Original Post for more -----
|
June 19, 2013
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||