WR Vishnu

Blog for Coldfusion , Mobile & .Net related technology

Archive for the ‘Coldfusion’ Category

This is for all Coldfusion article and sample related to Coldfusion

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/&#8221;)>

<!— 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&gt;.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 »

Suggest Coldfusion .Net integration

Posted by wrvishnu on October 18, 2007

Hi All

I would like you to suggest some samples that you required  using  Coldfusion and .Net integration.

Accessing the .Net UI controls  will not be accessible in Coldfusion any example using .Net ui cannot be done in coldfusion

Why wait post you suggestion in comment section….

Posted in Coldfusion | 16 Comments »

Downlaod EventViewer Log and Save in Database

Posted by wrvishnu on October 6, 2007

This Article describe how to get the event viewer log and clear the data from .Net and Save the event viewer into database in Coldfusion

To do this we need to create a class in .Net which will get the event viewer log and clear the entry and return the data as datatable which can be used from coldfusion and save the same in databasse

Create the table using the query below

CREATE TABLE EVENTVIEWERDATA ( EVENTID INTEGER, ENTRYTYPE VARCHAR2 (200), MESSAGE VARCHAR2 (4000),TIME VARCHAR2 (800))

.Net Code


using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;
public class EvtViewerSample
{
public DataTable getEventviewer(String EventLogName)
{
// Create the object for EventLog
EventLog ev = new EventLog(EventLogName, System.Environment.MachineName, "MyAppName");

//Create the columns for the datatable
DataTable CSDT = new DataTable();
CSDT.Columns.Add("EventId");
CSDT.Columns.Add("Message");
CSDT.Columns.Add("EntryType");
CSDT.Columns.Add("Time");

try
{

// Loop through the data and add a row
foreach (EventLogEntry entry in ev.Entries)
{

DataRow drdummy = CSDT.NewRow();
drdummy["EventId"] = entry.InstanceId.ToString();
drdummy["Message"] = entry.Message.ToString();
drdummy["EntryType"] = entry.EntryType.ToString();
drdummy["Time"] = entry.TimeGenerated.ToString();
CSDT.Rows.Add(drdummy);

}
ev.Clear(); // Clear the log information
ev.Close(); // Close the connection

return CSDT;
}
catch (Exception ex)
{
throw ex;
}

}

}

CFM Code
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Eventviewer Demo</title>
</head>
<body> <!—CAll the .Net method by passing the event log name like Application or Security or SystemNote : Current code will clear the Log file from event Viewer Please dont try this in Production database

—->

<cfobject
type = “dotnet”
name = “EvtDemo”
class = “EvtViewerSample”
assembly =”d:/Vishnu/EvtViewerSample.dll”>

<cftry>

<strong>Note: All Event log entried are cleared and saved in the database </strong>
<cfset Qrydata = #EvtDemo.getEventviewer(“Security”)#>
<cfloop query=”Qrydata”>
<cfquery name=”InsData” datasource=”CWtestsrv”>
Insert into EVENTVIEWERDATA (EVENTID, ENTRYTYPE,MESSAGE,TIME)
Values (
‘#Qrydata.EVENTID#’,
‘#Qrydata.ENTRYTYPE#’,
‘#Qrydata.MESSAGE#’,
‘#Qrydata.TIME#’)
</cfquery>
</cfloop>

<cfdump var=”#Qrydata#”>

<cfcatch type=”Any” >

<h3>Error Occured</b></h3>
<cfoutput>
<!— and the diagnostic message from the ColdFusion server —>
<p>#cfcatch.message#</p>
<p>Caught an exception, type = #CFCATCH.TYPE# </p>
<p>The contents of the tag stack are:</p>
<cfloop index = i from = 1
to = #ArrayLen(CFCATCH.TAGCONTEXT)#>
<cfset sCurrent = #CFCATCH.TAGCONTEXT[i]#>
<br>#i# #sCurrent["ID"]#
(#sCurrent["LINE"]#,#sCurrent["COLUMN"]#)
#sCurrent["TEMPLATE"]#
</cfloop>
</cfoutput>
</cfcatch>
</cftry>

Code Samples avilable  in the link

https://share.adobe.com/adc/document.do?docid=91bcf306-7405-11dc-b75f-151d3f6d9313

Posted in Coldfusion | 11 Comments »

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 | 7 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”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<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 | 4 Comments »

Coldfusion & .Net Integeration

Posted by wrvishnu on September 12, 2007

This is a sample code snippet for Coldfusion and .Net intgeration

To access the .net methods first we need to create an object for the .Net  Class , after creating the object we can access the methods in that class . Following is a sample code snippet for accessing the .net methods from coldfusion

Steps to work on the samples
Step 1 Copy the Access MDB file into the “D:\LEarning\CFDotNet\Northwind.mdb”

Step 2 Create the .Net Class Library in Visual studio 2005 , below is the code snippet

// This Code is developed in .net 2005
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
public class CfSample{
public CfSample()
{
}
public int SampAdding(int a, int b)
{
int c;
c = a + b;
return c;
}
public int SampSubtract(int a, int b)
{
int c;
c = a - b;
return c;
}
public int SampMultiply(int a, int b)
{
int c;
c = a * b;
return c;
}
public DataTable ACCdatasetMethod()
{
//conn string
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\LEarning\\CFDotNet\\Northwind.mdb" ;
//ConnectionString='
//connection
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand cmd = new OleDbCommand(@"SELECT [EmployeeID], [LastName], [FirstName], [Title], [City], [Address], [HireDate], [BirthDate], [TitleOfCourtesy], [Region], [PostalCode], [Country], [HomePhone] FROM [Employees]", connection);
connection.Open();
OleDbDataReader reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
return dt;
}
}
}

Step 3 Compile the Visual studio solution

Step 4 Copy the generated DLL file in any path or can use the same path where it got generated

Step 5 Call the .Net method in Coldfusion

<cfobject
type = “dotnet”
name = “MDBDTable”
class = “CfSample”
assembly = “D:/vishnu/CfSample.dll”>
<cfset query11 = MDBDTable.ACCdatasetMethod()>
<cfdump var=”#query11#”>

Please get the sample files from the location http://www.drivehq.com/folder/p2544729.aspx

Posted in Coldfusion | 28 Comments »

Welcome to my Blog!

Posted by wrvishnu on September 12, 2007

Welcome to my Blog , this is just start , wish to post as many useful information for Coldfusion Developer

Posted in Coldfusion | 1 Comment »

 
Follow

Get every new post delivered to your Inbox.