helper.cs 12.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
using System;
using System.Text;
using System.Net;
using System.IO;
using System.Collections;
using System.Data;
using System.Data.Odbc;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Services;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Web.Services.Protocols;
using System.Reflection;
using System.Web;
using System.Xml;
using System.Web.Services;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Web.Services.Description;
using System.Web.Services.Discovery;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Threading;
using System.Web.Services.Protocols;

namespace MonoTests.KnowledgeTree
{



	[System.Web.Services.WebServiceBinding(Name="KnowledgeTreePort", Namespace="urn:KnowledgeTree")]
	public class KTWebService : KnowledgeTreeService
 	{
		public KTWebService() : base()
		{
			this.Url = Environment.GetEnvironmentVariable("KT_ROOT_URL") + "/ktwebservice/webservice.php";
		}
 	}

	public class MySoapHttpClientProtocol : SoapHttpClientProtocol
	{
		public MySoapHttpClientProtocol() : base() {}

		public  object [] ReceiveResponse (WebResponse response, SoapClientMessage message, SoapExtension[] extensions)
		{

			StreamReader sr = new StreamReader(response.GetResponseStream());
			String content = sr.ReadToEnd();
			System.Console.WriteLine(content);

			return null;
		}
	}

	public class KTTest
    	{
		protected KTWebService 	_kt;
		protected String 			_session;
		protected bool	_verbose;


		public KTTest()
		{
			this._kt = new KTWebService();
			kt_response response = this._kt.login("admin","admin","127.0.0.1");
			this._session = response.message;
			this._verbose = false;
			this.setupDb();

			//System.Web.Services.Protocols.SoapHttpClientProtocol.ReceiveResponse
		}

		void setupDb()
		{

			String connectionString = "DSN=ktdms;" + "UID=root;" + "PWD=";
			try
	  		{
       				IDbConnection dbcon = new OdbcConnection(connectionString);
       				if (dbcon == null)
				{
					System.Console.WriteLine("Cannot create connection");
       				}
				dbcon.Open();
       				IDbCommand dbcmd = dbcon.CreateCommand();
         			if (dbcmd == null)
				{
					System.Console.WriteLine("Cannot create command");
				}
       				dbcmd.CommandText = "DELETE FROM folders WHERE id > 1";
       				dbcmd.CommandType = CommandType.Text;
        			dbcmd.ExecuteNonQuery();
       				dbcmd.CommandText = "DELETE FROM documents";
       				dbcmd.CommandType = CommandType.Text;
        			dbcmd.ExecuteNonQuery();
				dbcmd.CommandText = "DELETE FROM document_types_lookup WHERE name = 'NewType'";
        			dbcmd.ExecuteNonQuery();
				dbcmd.CommandText = "INSERT INTO document_types_lookup(id,name) VALUES(2,'NewType')";
        			dbcmd.ExecuteNonQuery();
				dbcmd.Dispose();
       				dbcmd = null;
       				dbcon.Close();
       				dbcon = null;
       			}
       			catch(Exception ex)
       			{
       				System.Console.WriteLine(ex.Message);
       			}
		}

		~KTTest()
		{
	   		this._kt.logout(this._session);
		}
	}



	public class FileUploader
	{
		private String boundary;
		private String uri;
		public String filename;


		public FileUploader(String uri)
		{
			this.uri = uri;
			System.Console.WriteLine("Using upload URL: " + uri);
			this.boundary = "----" + DateTime.Now.Ticks.ToString("x");
		}

		public FileUploader() : this(Environment.GetEnvironmentVariable("KT_ROOT_URL") + "/ktwebservice/upload.php")
		{
		}


		public String getFilename()
		{
			return this.filename;
		}


		public void upload(String sessionid, String filename)
		{
			String displayname = Path.GetFileName(filename);
			StringBuilder header = new StringBuilder();

			header.Append("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"session_id\"\r\n\r\n" + sessionid + "\r\n");
			header.Append("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"action\"\r\n\r\nA\r\n");
			header.Append("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"output\"\r\n\r\nxml\r\n");

			header.Append("--" + boundary + "\r\n");
			header.Append("Content-Disposition: form-data; name=\"name\";");
			header.Append("filename=\"" + displayname + "\"\r\nContent-Type: application/octet-stream\r\n\r\n");


			HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(this.uri);
			webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
			webrequest.Method = "POST";


			byte[] headerArray = Encoding.UTF8.GetBytes(header.ToString());
			byte[] boundaryArray = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

			FileStream file = new FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);

			long filesize = file.Length;
			webrequest.ContentLength = headerArray.Length + filesize + boundaryArray.Length;

			Stream requestStream = webrequest.GetRequestStream();
			requestStream.Write(headerArray, 0, headerArray.Length);

			byte[] buffer = new byte[10240];

			int read = 0;

			while ((read = file.Read(buffer, 0, buffer.Length)) > 0)
			{

				requestStream.Write(buffer, 0, read);
			}

			requestStream.Write(boundaryArray, 0, boundaryArray.Length);


			WebResponse response = webrequest.GetResponse();
			StreamReader sr = new StreamReader(response.GetResponseStream());
			String xml = sr.ReadToEnd();
			//System.Console.WriteLine("xml: " + xml);



			if (xml.IndexOf("<status_code>0</status_code>") != -1)
			{
				long tmp = this.tokenInt("filesize",xml);
				if (tmp != filesize)
				{
					throw new Exception("Filesize should be " + filesize + " but appears to be "+ tmp);
				}

				tmp = this.tokenInt("error",xml);
				if (tmp > 0)
				{
					throw new Exception("The server reported error code " + tmp + " for the file upload");
				}
				this.filename = this.tokenString("filename",xml);
				// yay, all is good!
				return;
			}

			String msg = this.tokenString("msg", xml);
			long error = this.tokenInt("error", xml);
			if (error > 0)
			{
				throw new Exception("The server reported error code " + error + " for the file upload");
			}

			throw new Exception("Upload error: " + msg);

		}

		private String tokenString(String token, String xml)
		{
			int tokStart = xml.IndexOf("<"+token+">") + token.Length+2;
			int tokEnd = xml.IndexOf("</"+token+">");
			if (tokEnd == -1) return "";

			String value = xml.Substring(tokStart, tokEnd-tokStart);
			//System.Console.WriteLine(token + ": " + value);
			return value;
		}

		private long tokenInt(String token, String xml)
		{
			String value = this.tokenString(token, xml);
			if (value.Equals(""))
			{
				return -1;
			}
			return long.Parse(value);
		}

	}



	public class Document
	{
		public String title;
		public String filename;
		public String realFilename;
		public String content;
		public int docId;
		public String 			session;
		public KnowledgeTreeService 	kt;
		public long filesize;
		public bool verbose;
		public bool local;

		public Document(int offset, String session, KnowledgeTreeService kt, bool verbose, bool local)
		{
			this.title = "kt unit test" + offset;
			this.realFilename =  "kt_unit_test" + offset + ".txt";
			this.filename = (Helper.isUnix()?("/tmp/"):("c:\\")) + this.realFilename;
			this.content = "Hello World!\nThis is a test! And more!\n\n\r\n";
			this.docId = 0;
			this.session = session;
			this.kt = kt;
			this.verbose =verbose;
			this.local = local;
		}

		public kt_document_detail createFile(int folderId)
		{
			Helper.writeFile(this.filename, this.content);
			this.filesize = this.content.Length;

			if (this.local)
			{
				return null;
			}
			kt_document_detail d1 = this.kt.get_document_detail_by_title(this.session, folderId, this.title, "");
			if (d1.status_code == 1)
			{
				this.docId = d1.document_id;
				this.deleteFile();
			}

			kt_document_detail response1 = this.kt.add_base64_document(this.session, folderId, this.title, this.filename, "Default", Helper.ConvertFileToBase64Encoding(this.filename));

			if (response1.status_code == 0)
			{
				this.docId = response1.document_id;
			}

			if (this.verbose)
			{
				if (response1.status_code == 0)
				{
					System.Console.WriteLine("docid: " + this.docId + " filename: " + this.filename);
				}
				else
				{
					System.Console.WriteLine("Could not create file: " + this.filename);
				}
			}

			return response1;

		}

		public kt_document_detail createFileWithMetadata(int folderId, kt_metadata_fieldset[] metadata, kt_sysdata_item[] sysdata)
		{
			Helper.writeFile(this.filename, this.content);

			this.filesize = this.content.Length;

			if (this.local)
			{
				return null;
			}

			kt_document_detail d1 = this.kt.get_document_detail_by_title(this.session, folderId, this.title, "");
			if (d1.status_code == 1)
			{
				this.docId = d1.document_id;
				this.deleteFile();
			}

			kt_document_detail response1 = this.kt.add_base64_document_with_metadata(this.session, folderId, this.title, this.filename, "Default", Helper.ConvertFileToBase64Encoding(this.filename), metadata, sysdata);

			if (response1.status_code == 0)
			{
				this.docId = response1.document_id;
			}

			if (this.verbose)
			{
				if (response1.status_code == 0)
				{
					System.Console.WriteLine("docid: " + this.docId + " filename: " + this.filename);
				}
				else
				{
					System.Console.WriteLine("Could not create file: " + this.filename);
				}
			}

			return 	response1;
		}

		public kt_document_detail checkinFileWithMetadata(int folderId, kt_metadata_fieldset[] metadata, kt_sysdata_item[] sysdata)
		{
			Helper.writeFile(this.filename, this.content);
			this.filesize = this.content.Length;

			if (this.local)
			{
				return null;
			}

			kt_document_detail d1 = this.kt.get_document_detail_by_title(this.session, folderId, this.title, "");
			if (d1.status_code == 1)
			{
				this.docId = d1.document_id;
				this.deleteFile();
			}

			kt_document_detail response1 = this.kt.checkin_base64_document_with_metadata(this.session, this.docId, this.filename, "checkin reason", Helper.ConvertFileToBase64Encoding(this.filename), false, metadata, sysdata);

			if (response1.status_code == 0)
			{
				this.docId = response1.document_id;
			}

			if (this.verbose)
			{
				if (response1.status_code == 0)
				{
					System.Console.WriteLine("docid: " + this.docId + " filename: " + this.filename);
				}
				else
				{
					System.Console.WriteLine("Could not create file: " + this.filename);
				}
			}

			return 	response1;
		}



		public void deleteFile()
		{
			Helper.deleteFile(this.filename);

			if (this.local)
			{
				return;
			 }

			if (this.docId > 0)
			{

				kt_response response = this.kt.delete_document(this.session, this.docId, "Delete - cleaning up");
				if (this.verbose && response.status_code != 0)
				{
					System.Console.WriteLine("Could not delete file: " + this.filename);
				}
			}
		}
	}


	public class Helper
    	{
		public static bool isUnix()
		{
			// found reference on: http://www.mono-project.com/FAQ:_Technical
			int platform = (int) Environment.OSVersion.Platform;
			return (platform == 4) || (platform == 128);
		}

		public static void writeFile(String filename, String text)
		{
			try
			{
		 		TextWriter tw = new StreamWriter(filename);
			 	tw.WriteLine(text );
			 	tw.Close();
			}
			catch (System.Exception exp)
			{
				System.Console.WriteLine("{0}", exp.Message);
				throw;
			}
		}

		public static String readFile(String filename)
		{
			String text = null;
			try
			{
				FileStream inFile = new FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
				StreamReader sr = new StreamReader(inFile);
				text = sr.ReadToEnd();
				inFile.Close();
			}
			catch (System.Exception exp)
			{
				System.Console.WriteLine("{0}", exp.Message);
				throw;
			}

			return text;
		}

		public static void deleteFile(string filename)
		{
			try
			{
				File.Delete(filename);
			}
			catch(System.Exception)
			{
				// we are using this to cleanup, so don't handle
			}
		}

		public static string ConvertFileToBase64Encoding(string filename)
		{
			System.IO.FileStream inFile;

			byte[] binaryData;
			string base64String = "";

			try
			{
				inFile = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
				binaryData = new Byte[inFile.Length];
				inFile.Read(binaryData, 0, (int)inFile.Length);
				inFile.Close();

				base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
			}
			catch (System.Exception exp)
			{
				System.Console.WriteLine("{0}", exp.Message);
				throw;
			}

			return base64String;
		}

		public static long ConvertBase64EncodingToFile(String encoding, string filename)
		{
			System.IO.FileStream inFile;

			byte[] binaryData;

			try
			{
				binaryData = Convert.FromBase64String (encoding);

				inFile = new System.IO.FileStream(filename, System.IO.FileMode.Create);

				inFile.Write(binaryData, 0, (int)binaryData.Length);
				inFile.Close();
			}
			catch (System.Exception exp)
			{
				System.Console.WriteLine("{0}", exp.Message);
				throw;
			}
			return binaryData.Length;
		}

	}
}