using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Text;
using System.Xml;
using System.IO;
using System.Diagnostics;
namespace FindPeople
{
///
/// Summary description for Form1.
///
///
public class DSMLProcess
{
protected Uri site = new Uri("http://localhost/dsml");
protected string inputFile=null;
protected string outputFile=null;
protected string templateFile=String.Empty;
protected string searchFilter=String.Empty;
public DSMLProcess( Uri uri, String filter, String output, String template)
{
site = uri;
inputFile = @"apps\query.xml";
outputFile = output;
templateFile=template;
searchFilter = filter;
}
public void DoIt()
{
try
{
// Create the request instance.
WebRequest wReq = WebRequest.Create(site);
// Set it to default credentatials
wReq.Credentials = CredentialCache.DefaultCredentials;
//////////////////////////////////
// Set the HTTP-specific UserAgent property
HttpWebRequest httpReq = (HttpWebRequest)wReq;
httpReq.Headers.Add("SOAPAction", "\"#batchRequest\"");
httpReq.Method = "POST";
// Write the DMSL content to HTTP
Stream reqStream = httpReq.GetRequestStream();
StreamWriter writer = new StreamWriter(reqStream);
//Read the query doc
XmlDocument dom = new XmlDocument();
dom.Load(inputFile);
// Replace the query with query specified by the user
XmlNamespaceManager ns = new XmlNamespaceManager(dom.NameTable);
ns.AddNamespace("se", "http://schemas.xmlsoap.org/soap/envelope/");
ns.AddNamespace("dsml","urn:oasis:names:tc:DSML:2:0:core");
XmlNode node = dom.SelectSingleNode("se:Envelope/se:Body/dsml:batchRequest/dsml:searchRequest/dsml:filter/dsml:and/dsml:substrings/dsml:initial", ns);
XmlNode searchNode = node.FirstChild;
searchNode.Value = searchFilter;
//ns.DefaultNamespace;
writer.Write(dom.InnerXml);
writer.Flush();
writer.Close();
//////////////////////////////
// Response----
WebResponse resp = httpReq.GetResponse();
Stream respStream = resp.GetResponseStream();
StreamReader reader = new StreamReader(respStream);
XmlTextWriter wrt;
if (outputFile == null )
{
wrt= new XmlTextWriter(System.Console.Out);
}
else
{
wrt = new XmlTextWriter(outputFile,System.Text.Encoding.Unicode);
}
//Write the ProcessingInstruction node
if (templateFile.Length > 0 )
{
String PItext="type='text/xsl' href='" + templateFile + "'";
wrt.WriteProcessingInstruction("xml-stylesheet", PItext);
}
wrt.Formatting = System.Xml.Formatting.Indented;
dom.Load(reader);
dom.WriteTo(wrt);
reader.Close();
wrt.Flush();
wrt.Close();
}
catch(Exception exc)
{
Console.WriteLine(exc.Message);
return;
}
}
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtSearch;
private System.Windows.Forms.Button cmdGo;
private System.Windows.Forms.ComboBox cbReport;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.txtSearch = new System.Windows.Forms.TextBox();
this.cmdGo = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.cbReport = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Search: ";
//
// txtSearch
//
this.txtSearch.Location = new System.Drawing.Point(136, 32);
this.txtSearch.Name = "txtSearch";
this.txtSearch.Size = new System.Drawing.Size(224, 20);
this.txtSearch.TabIndex = 1;
this.txtSearch.Text = "";
//
// cmdGo
//
this.cmdGo.Location = new System.Drawing.Point(376, 32);
this.cmdGo.Name = "cmdGo";
this.cmdGo.Size = new System.Drawing.Size(56, 24);
this.cmdGo.TabIndex = 2;
this.cmdGo.Text = "Go";
this.cmdGo.Click += new System.EventHandler(this.cmdGo_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 72);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 16);
this.label2.TabIndex = 3;
this.label2.Text = "Report Template:";
//
// cbReport
//
this.cbReport.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbReport.Location = new System.Drawing.Point(136, 72);
this.cbReport.Name = "cbReport";
this.cbReport.Size = new System.Drawing.Size(232, 21);
this.cbReport.TabIndex = 4;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(488, 126);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.cbReport,
this.label2,
this.cmdGo,
this.txtSearch,
this.label1});
this.Name = "Form1";
this.Text = "Find People via DSML Services";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
cbReport.Items.Add("Raw");
cbReport.Items.Add("Table");
cbReport.Items.Add("Form");
cbReport.SelectedIndex = 1;
}
private void cmdGo_Click(object sender, System.EventArgs e)
{
String serverName = "localhost";
String virtualDir = "dsml";
String url = "http://" + serverName + "/" + virtualDir + "/adssoap.dll";
System.Uri uri = new Uri(url);
String outFile = @"apps\t.xml";
String sTemplate = String.Empty;
String[] templates = { "", "table.xsl", "form.xsl" };
int idx = cbReport.SelectedIndex;
if ( idx == -1) // not selected == Raw
{
idx=0;
}
DSMLProcess dsml = new DSMLProcess(uri,txtSearch.Text,outFile, templates[idx]);
dsml.DoIt();
Process.Start(outFile);
}
}
}
}