WR Vishnu Coldfusion Weblog

Blog for Coldfusion and Coldfusion related technologies….

Archive for the ‘Coldfusion Blogs’ Category

Using SOAP Attachment from Coldfusion

Posted by wrvishnu on August 22, 2008

I came accros the issue on accessing the java web service which will return SOAP attachment . Tried goole to check whether there are any implementation using coldfusion but we could’t find . So we decided to use SAAJ API to get the soap attachment

Follwoing are the steps i used

Get these saaj-api.jar , saaj-impl.jar jar file  can be get from https://saaj.dev.java.net/

copied the jar file in coldfusion lib directory

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>

<html>
<head>
<title>Soap Atachment</title>
</head>

<body>

<cfobject action=”create” type=”java” class=”javax.xml.soap.AttachmentPart” name=”Apart”>
<cfobject action=”create” type=”java” class=”java.net.URL” name=”URL”>
<cfobject action=”create” type=”java”  class=”javax.xml.soap.MessageFactory”name=”MF”>
<cfobject action=”create” type=”java”   class=”javax.xml.soap.Name” name=”N”>
<cfobject action=”create” type=”java”  class=”javax.xml.soap.SOAPBody” name=”SB”>
<cfobject action=”create” type=”java”  class=”javax.xml.soap.SOAPBodyElement” name=”SBE”>
<cfobject action=”create” type=”java”  class=”javax.xml.soap.SOAPConnection” name=”SPC”>
<cfobject action=”create” type=”java”  class=”javax.xml.soap.SOAPConnectionFactory” name=”SCPF”>
<cfobject action=”create” type=”java”  class=”javax.xml.soap.SOAPElement” name=”SEL”>
<cfobject action=”create” type=”java”  class=”javax.xml.soap.SOAPEnvelope” name=”ENV”>
<cfobject action=”create” type=”java” class=”javax.xml.soap.SOAPMessage” name=”SM”>
<cfobject action=”create” type=”java”  class=”javax.xml.soap.SOAPPart” name=”SP”>

<!— Following are the information that required by the web service as a parameter ” Starts here —>
<cfset dm_lib = “<Sometext>”>
<cfset  doc_ver = “”>
<cfset  documentId = “<Number>”>
<cfset  profile_form = “EKRIS_LAD_CPD_PF”>
<cfset app_user_id = “userid”>
<cfset user_id = “userid”>
<!— Following are the information that required by the web service as a parameter ” Ends here —>

<!— Calling methods of SAAJ api to create a the connection made the reques  —>
<cfset scFactory = SCPF.newInstance()>
<cfset con= scFactory.createConnection()>
<cfset factory = MF.newInstance()>
<cfset Message =  factory.createMessage()>
<cfset soapPart = message.getSOAPPart()>
<cfset envelope = soapPart.getEnvelope()>
<cfset body = envelope.getBody()>

<!— here mentioned the methodname—>
<cfset bodyName = envelope.createName(“RetrieveDocument”, “”, “http://www.openuri.org/”)>

<!— Create the element for Sopa which will be part of Soap Request Starts here —>
<cfset gltp = body.addBodyElement(bodyName)>
<cfset elementName = envelope.createName(“DMlib”)>
<cfset element = gltp.addChildElement(elementName)>
<cfset element.addTextNode(dm_lib)>
<cfset elementName = envelope.createName(“DocumentNumber”)>
<cfset element = gltp.addChildElement(elementName)>
<cfset element.addTextNode(documentId)>
<cfset elementName = envelope.createName(“VersionNo”)>
<cfset element = gltp.addChildElement(elementName)>
<cfset element.addTextNode(“”)>

<cfset elementName = envelope.createName(“ProfileForm”)>
<cfset element = gltp.addChildElement(elementName)>
<cfset element.addTextNode(profile_form)>

<cfset     elementName = envelope.createName(“appUserID”)>
<cfset  element = gltp.addChildElement(elementName)>
<cfset     element.addTextNode(app_user_id)>

<cfset elementName = envelope.createName(“userID”)>
<cfset element = gltp.addChildElement(elementName)>
<cfset element.addTextNode(user_id)>
<!— Create the element for Sopa which will be part of Soap Request ends here —>

<!— Calling the WS starts here—->
<cfset strUrl =”http://Domainname/webservices/jws/<webservicename>.jws”>

<cfset jUrl = CreateObject( “java”, “java.net.URL” ).Init(strUrl) />
<cfset response = con.call(message, jUrl)>
<!— Calling the WS ends here—->

<!— getting the attachment part starts here—->
<cfset retrievedAttachments = response.getAttachments()>
<cfset AttachmentPart = retrievedAttachments.next()>
<cfset dataHandler = AttachmentPart.getDataHandler()>
<!— This will have the attachment part ends here—->

<!— Write the file from the attachment to the mentioned location starts  here—->

<cfscript>
//… other code

saveToFileName = “C:\Vishnu123.jpg”;
inputStream =  dataHandler.getInputStream();
outStream = createObject(“java”,”java.io.ByteArrayOutputStream”).init();
// create byte array. read up to the first
// 1024 bytes (or however many) into the array
byteClass = createObject(“java”, “java.lang.Byte”).TYPE;
byteArray = createObject(“java”,”java.lang.reflect.Array”).newInstance(byteClass, javacast(“int”, 1024));
length = inputStream.read(byteArray);

// if there is any data to read
offset = 0;
while ( length GT 0) {
outStream.write( byteArray, offset, length );
length = inputStream.read( byteArray );
}

outStream.close();
inputStream.close();

FileWrite( saveToFileName, outStream.toByteArray() );
</cfscript>
<!— Write the file from the attachment to the mentioned location starts  here—->

</body>
</html>

Using this we saved the SOAP attachment to the file server .

If anyone think about the alternative way or any suggetion for improvment are welcome

Posted in Coldfusion, Coldfusion Blogs | Leave a Comment »

Getting System/OS information in Coldfusion

Posted by wrvishnu on October 5, 2007

This Article describe how to get the follwoing system information in Coldfusion using .Net OS Information

  • Hard Disk Information
  • IP and Computer Name
  • Mac Address
  • Computer information

Steps to follow

Create a Class Library in .Net and compile and get the dll file generated from .Net

Call the dll from coldfusion and access the methods from dll

.Net Code Snippets

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections;
using System.Data;
using System.Net;
using System.Text;
using System.Management;
public class OSinformation
{
// Method to get the HardDisk Informationpublic DataTable GetDriveInfo()
{
ManagementObjectSearcher query1 = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
ManagementObjectCollection queryCollection1 = query1.Get();
DataTable DT = new DataTable();
// Add the Columns headers to the table
DT.Columns.Add("DeviceID");
DT.Columns.Add("Description");
DT.Columns.Add("Freespace");
foreach (ManagementObject mo in queryCollection1)
{
// Adding the rwos to the table
DataRow drdummy= DT.NewRow();
drdummy["DeviceID"] = mo["DeviceID"].ToString();
drdummy["Description"] = mo["Description"].ToString();
drdummy["Freespace"] = Convert.ToInt64(mo["Freespace"])/ 1024/1024 + " MB";
DT.Rows.Add(drdummy);
}
return DT;
}
// Method to get the OS Information
public DataTable GetOsinfo()
{
ManagementObjectSearcher query2 = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
ManagementObjectCollection queryCollection2 = query2.Get();
DataTable OSDT = new DataTable();
// Add the Columns headers to the table
OSDT.Columns.Add("name");
OSDT.Columns.Add("version");
OSDT.Columns.Add("csname");
OSDT.Columns.Add("windowsdirectory");
foreach (ManagementObject mo in queryCollection2)
{
DataRow drdummy = OSDT.NewRow();
drdummy["name"] = mo["name"].ToString();
drdummy["version"] = mo["version"].ToString();
drdummy["csname"] = mo["csname"].ToString();
drdummy["windowsdirectory"] = mo["windowsdirectory"].ToString();

OSDT.Rows.Add(drdummy);

}
return OSDT;
}

// Method to get the Computer Information
public DataTable GetCSinfo()
{
ManagementObjectSearcher query3 = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem");
ManagementObjectCollection queryCollection3 = query3.Get();
DataTable CSDT = new DataTable();
CSDT.Columns.Add("manufacturer");
CSDT.Columns.Add("model");
CSDT.Columns.Add("systemtype");
CSDT.Columns.Add("Memory");

foreach (ManagementObject mo in queryCollection3)
{

DataRow drdummy = CSDT.NewRow();
drdummy["manufacturer"] = mo["manufacturer"].ToString();
drdummy["model"] = mo["model"].ToString();
drdummy["systemtype"] = mo["systemtype"].ToString();
drdummy["Memory"] = mo["totalphysicalmemory"].ToString();

CSDT.Rows.Add(drdummy);

}
return CSDT;
}

public String GetIPAndCompName()
{
String S22;
String ComputerName;
String IP;
ComputerName = Dns.GetHostName();
IPHostEntry ipEntry = Dns.GetHostEntry(ComputerName);
IP = ipEntry.AddressList[0].ToString();
S22 = IP + "," + ComputerName;
return S22;
}
///
/// Returns MAC Address from first Network Card in Computer
///
/// [string] MAC Address
public string GetMACAddress()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
string MACAddress = String.Empty;
foreach (ManagementObject mo in moc)
{
if (MACAddress == String.Empty) // only return MAC Address from first card
{
if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
}
mo.Dispose();
}
MACAddress = MACAddress.Replace(":", "");
return MACAddress;
}

///
/// Return processorId from first CPU in machine
///
/// [string] ProcessorId
public string GetCPUId()
{
string cpuInfo = String.Empty;
string temp = String.Empty;
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (cpuInfo == String.Empty)
{// only return cpuInfo from first CPU
cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
}
}
return cpuInfo;
}

}

CFM Code

<cfobject
type = "dotnet"
name = "MDBDTable"
class = "OSinformation"
assembly ="d:/Vishnu/OSinformation.dll">

<cfset dummy = MDBDTable.GetCSinfo()>
<cfdump var="#dummy#"><br>

<cfset dummy = MDBDTable.GetDriveInfo()>
<cfdump var="#dummy#"><br>

<cfset dummy = MDBDTable.GetOsinfo()>
<cfdump var="#dummy#"><br>

<cfset dummy = MDBDTable.GetIPAndCompName()>
<cfset IP = listgetat(dummy,1)>
<cfset domain = listgetat(dummy,2)>
<cfoutput>
IP is #IP# <br>
Domain is #domain#<br>

<cfset dummy = MDBDTable.GetMACAddress()>
Macaddress is : #dummy#
</cfoutput>

Source fot this sample can be get from the url https://share.adobe.com/adc/document.do?docid=4298a1c1-7327-11dc-b75f-151d3f6d9313

Posted in Coldfusion, Coldfusion Blogs | 4 Comments »

access .Net Hashtable in Coldfusion

Posted by wrvishnu on September 28, 2007

.Net hashtable can be used as a Structure in coldfusion, below snippets explains the steps  

This Sample is for accessing the .Net Hashtable in Coldfusion

 Steps to be Followed

Create a Class Library which generate  Hashtable (collection or structure)

Call the .Net method from Coldfusion

.Net Code

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
public class HashTableDemo
{
public HashTableDemo()
{
}
public Hashtable getInfo()
{
Hashtable HTdemo = new Hashtable();
HTdemo.Add("Name","Vishnuprasad" );
HTdemo.Add("Age", 29);
HTdemo.Add("Sex", "Male");
HTdemo.Add("Mobile", 9884077182);
HTdemo.Add("Email", "demo@demo.com");
HTdemo.Add("SkillSet", "Coldfusion, .Net , SQL Server, ORacle , Business Objects");
return HTdemo;

}

}

Above code will generate the DLL which needs to be used in Coldfusion as mentioned below with the path
CFM Code

<cfobject
type = "dotnet"
name = "Htdemo"
class = "HashTableDemo"
assembly ="d:/Vishnu/HashTableDemo.dll">
<cfset HCollection = #Htdemo.getInfo()#><cfoutput>
<table cellpadding = "2 " cellspacing = "2 ">
<!--- In cfloop, use item to create a variable
called person to hold value of key as loop runs --->
<cfloop collection = #HCollection# item = "Value">
<TR>
<TD><strong>#Value#</strong></TD>
<TD>#StructFind(HCollection, Value)#</TD>
</TR>
</cfloop>
</table>
</cfoutput>

Posted in Coldfusion, Coldfusion Blogs | Leave a Comment »

Create Windows User in Local Machine

Posted by wrvishnu on September 27, 2007

Creating windows users in Local Machine using coldfusion 8 & .Net

Steps to be Followed :

Create a .Net Class Library to Create Windows NT Userccount in local machine using VB as Language

Write a Coldfuusion Entry form to Enter the necessary information to Create the user account

Write a Coldfuusion Action page to call the .Net Method to create the user account in local windows machine

Code Snippets

CFM Code

Entry form Code

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Entry Form</title>
</head>
<!—
Password needs to be given as Pasword policy set in that machine
Paramenter to be passed
First Parameter : Username
Second Parameter : Password
Third Parameter : Description
Fourth Parameter : FullName
Fifth Parameter : Machine name like WinNT://machinenameNote : As this is Just a sample there is no Validation or exception Handling
This Example will work only in Windows OS ....
---><cfform name="Adsample" method="post" action="Act_ADSample.cfm" >
<strong>Username </strong><cfinput required="yes" message="Username Required" name="LclUsername" size="10"><br />
<strong>Password</strong><cfinput required="yes" message="Password Required" name="LclPassword" size="10"><br />
<strong>Description </strong><cfinput required="yes" message="Description Required" name="LclDescription" size="10"><br />
<strong>Full Name</strong><cfinput required="yes" message="Full Name Required" name="LclFullName" size="20"><br />
<strong>Machine name </strong><cfinput required="yes" message="Machine Name Required" name="LclPcName" size="10"><br />
<cfinput type="submit" name="Createuser" value="Create USer">
</cfform>
<body>
</body>
</html>
Actionpage Code
em><cfobject
</cfobject
type = "dotnet"
name = "MDBDTable"
class = "ADintegration.ADintegration"
assembly ="d:/Vishnu/ADintegration.dll">#Dummy1#

.Net Code

Imports System.DirectoryServices
Public Class ADintegration
  Dim outputval As String
  Public Function ADuser(ByVal uname As String, ByVal pass As String, ByVal desc As String, ByVal fullname As String, ByVal PCname As String) As String
'First(Parameter) : Username
  'Second(Parameter) : Password
  'Third(Parameter) : Description
  'Fourth(Parameter) : fullname
  'Fourth(Parameter) : PC name : eg: Winnt://Machinename
Dim lblDMBase As String
  lblDMBase = PCname
  REM opens a connection to the local machine. It does not necessarily need to be
  REM the machine the web page is running on.
  Dim adsComputer As New DirectoryEntry(lblDMBase)
  Dim adsUser As DirectoryEntry
  REM Open a connection to the Group
  Dim adsGroup As New DirectoryEntry(lblDMBase & "/Users")
  REM You can also open the object by:
  'Dim adsGroup As DirectoryEntry
  'adsGroup.Path = lblDMBase & "/Users"
  Try
  REM Add a user to the defined computer object
  adsUser = adsComputer.Children.Add(uname, "User")
  REM Populate the FullName and Description Properties
  adsUser.Properties("FullName").Add(fullname)
  adsUser.Properties("Description").Add(desc)
  REM Set the password (a random password function would be good here).
  adsUser.Invoke("SetPassword", pass)
  REM Identical to .SetInfo
  adsUser.CommitChanges()
REM Add the User to the Group
  adsGroup.Invoke("Add", New Object() {adsUser.Path.ToString()})
  adsGroup.CommitChanges()
outputval = "User Created"
  Catch ex As Exception
  outputval = ex.Message.ToString()
  End Try
  Return outputval
  End Function
End Class

.Net and Coldfusion Templates used in this sample can be get from

http://www.drivehq.com/folder/p2613077.aspx

Posted in Coldfusion, Coldfusion Blogs | Leave a Comment »