using System; using TestFramework.ApplicationAPI; using TestFramework.AuthenticateAPI; using TestFramework; using System.Net; using System.IO; using System.Web; using System.Web.Services.Protocols; namespace Tests.pool { /// <summary> /// Summary description for PoolDoc. /// /// This sample loads a pool from the RPM /// repository then it creates a document and assigns /// it to the pool. It uploads an attachment to the /// document and then downloads it. /// </summary> public class PoolDoc { // constructor public PoolDoc(){} //All these variables are used in this sample public static String poolName = "IBMPOOL_123"; public static String docName = "IBMDOCUMENT"; public static String docFName= "IBMDOCFOLDER"; public static String dEleName= "IBMDOCELEMENT"; public static String contextName = "Pool"; public static String uploadAction = "UPLOADZIPPED"; public static String dwnldAction = "DOWNLOADZIPPED"; public static String filePath = "C:\\RPM Initial Hypothesis " + "v3.zip"; public static String filePathOut = "C:\\bucket\\RPM " + "Initial Hypothesis v3.zip"; private static int BUFFER_SIZE = 2048; private static int MAX_SIZE = 2147483647; // Session ID is use for all transaction and is get from the // Authenticate.login function. The session ID will replace // the User password while transacting public static String sessionid = ""; public void sampleTest() { // start the communication with the api sessionid = APISetup.SetUp(); // Declare the pool wich we are going to use Pool pool = null; // create a poolscope to keep track of the relation // between the pool and its attribute PoolScope pScope = new PoolScope(); // adjust the pool scope to save the document pScope.documentFolder = new DocumentScope(); // declare a save result object SaveResult pSave = null; // create a new document Document doc = new Document(); // retrieve the pool in RPM using XPath. pool = (Pool)APISetup.application.loadFromXpath( sessionid, "/Pool[name='" + poolName + "']" ,pScope).rpmObjectList[0]; // initialize the document doc.name = docName; doc.parent = pool.documentFolder; // save the document in the RPM repository // use the sessionid acquired in the setUp pSave = new SaveResult(); pSave = APISetup.application.save(sessionid, doc, null, ReloadType.ReloadResult); // method showing the detail of any error in the save APISetup.checkForErrors( pSave ); // load the doc in RPM using XPath. doc = (Document)pSave.rpmObject; /* * Upload a file */ // create a fileStream and initialize it with // the file we wish to upload FileStream f = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read); // addres to wich we will upload the file String adresse = "http://localhost:8080/rpm/servlet/DocumentTransfer"; // create a HttpWebRequest object HttpWebRequest myReq = (HttpWebRequest)WebRequest. Create(adresse); // initialize request // method POST for upload myReq.Method = "POST"; myReq.AllowWriteStreamBuffering = true; // add information in the headers // must contain mandatory information on the server part // plus information about the file myReq.Headers.Add("ACTION", uploadAction); myReq.Headers.Add("DOCUMENT_ID", doc.ID); myReq.Headers.Add("CONTEXT_NAME", contextName); myReq.Headers.Add("SESSION_ID", sessionid); myReq.Headers.Add("FILE", filePath); myReq.Headers.Add("CONTENT_LENGTH", ((long)f.Length).ToString()); // assign the file length to the request content length myReq.ContentLength = f.Length; // create a buffered stream object BufferedStream bos = null; // verifies if file is empty or oversized if (f.Length > 0 && f.Length < MAX_SIZE) { // get request stream in wich we will transfer data bos = new BufferedStream(myReq.GetRequestStream()); // create a file stream with the file to upload FileStream fis = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read); // create a byte array with size // equal to chunk size we wish to send byte [] data = new byte[BUFFER_SIZE]; int total = 0; int count; // loop for each chunk of data while file is not over while ((count = fis.Read(data, 0, BUFFER_SIZE)) != 0) { // write data into request stream bos.Write(data, 0, count); total += count; } // clean all streams bos.Flush(); bos.Close(); fis.Close(); } // get response for request HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse(); // if request executes normally status is OK if (myResp.StatusCode != System.Net.HttpStatusCode.OK) { System.Console.Out.WriteLine("Upload failed"); } // clean response and request // not closing the HttpWebResponse object // might cause time-out errors in the futur myResp.Close(); myResp = null; myReq = null; // assign the docFolder to the pool // pool does not have a documentElements // yet cause it is not implemented pool.documentFolder.documentElements = new DocumentElement[] {doc}; // save the pool in the RPM repository // use the sessionid acquired in the setUp // a scope is necessary because we save // the pool with the document pSave = new SaveResult(); pSave = APISetup.application.save(sessionid, pool, pScope, ReloadType.SavedResult ); // method showing the detail of any error in the save APISetup.checkForErrors( pSave ); /* * Download a file */ // download the document just saved doc = (Document)APISetup.application. loadFromXpath(sessionid, "/Document[name='" + docName + "']", null).rpmObjectList[0]; // create request URL with parameters String getURL = adresse + "?ACTION=" + dwnldAction + "&DOCUMENT_ID=" + doc.ID + "&CONTEXT_NAME=" + contextName + "&SESSION_ID=" + sessionid; // create HttpWebRequest object myReq = (HttpWebRequest)WebRequest.Create(getURL); // method for request is GET since we download a file myReq.Method = "GET"; // allow stream buffering = true // we will use streams to transfert data myReq.AllowWriteStreamBuffering = true; // get response to web request myResp = (HttpWebResponse)myReq.GetResponse(); // get response stream, data downloaded Stream stream = myResp.GetResponseStream(); // get headers from request WebHeaderCollection headers = myResp.Headers; // get content disposition from headers String contentDisposition = myResp. GetResponseHeader("Content-Disposition"); // get content type from response String contentType = myResp.ContentType; // get encoding from response String contentEnc = myResp.ContentEncoding; // get data length from response long contentLen = myResp.ContentLength; if (contentLen > 0) { // get file name String delimiter = "filename=\""; int start = delimiter.Length + contentDisposition.IndexOf(delimiter); int end = contentDisposition.LastIndexOf('\"') - start; String filename = contentDisposition. Substring(start, end); // create a buffered stream from the response stream BufferedStream bis = new BufferedStream(stream); // create a file stream in wich we will // write data from response stream FileStream fStream = new FileStream(filePathOut, System.IO.FileMode.OpenOrCreate); // create a buffered stream from file stream bos = new BufferedStream(fStream); // create a byte array with size equal // to chunk size we wish to send byte[] data = new byte[BUFFER_SIZE]; int total = 0; int count; // loop for each chunk of data while file is not over while ((count = bis.Read(data, 0, BUFFER_SIZE)) != 0) { // write data into file stream bos.Write(data, 0, count); total += count; } // clean all streams bos.Close(); bis.Close(); } // clean response and request // not closing the HttpWebResponse object // might cause time-out errors in the futur myResp.Close(); myResp = null; myReq = null; // end the communication with the api. APISetup.CleanUp(sessionid); } } }