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.wbs { /// <summary> /// Summary description for WBSDoc. /// /// This sample loads a project from the RPM /// repository then it creates a document and assigns /// it to the project. It uploads an attachment to the /// document and then downloads it. /// </summary> public class WBSDoc { public WBSDoc(){} //All these variables are used in this sample public static String projectName = "IBM_GENERIC_PROJECT_ABC"; public static String contextName = "WBS"; public static String docName = "Hypothesis"; public static String uploadAction = "UPLOADZIPPED"; public static String dwnldAction = "DOWNLOADZIPPED"; public static String filePath = "C:\\Documents and Settings \\username\\My Documents\\RPM Initial Hypothesis v3.zip"; public static String filePathOut = "C:\\Documents and Settings \\username\\My Documents\\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 resource wich we are going to use Project project = null; // declare a save result object SaveResult pSave = null; // create a resourceScope to keep track of the relation between // the resource and the documents WorkElementScope pScope = new WorkElementScope(); pScope.documentFolder = new DocumentScope(); // Find the resource in RPM using XPath. project = (Project)APISetup.application.loadFromXpath( sessionid, "/Project[name='" + projectName + "']", pScope ).rpmObjectList[0]; // create a new document Document wNewDoc = new Document(); // initialize the document wNewDoc.name = docName; // assign the resource to the document as the parent wNewDoc.parent = project.documentFolder; // save the document into RPM repository // a scope isn't necessary // use a reload type that enables you to work with the document object afterward pSave = new SaveResult(); pSave = APISetup.application.save(sessionid, wNewDoc, null, ReloadType.ReloadResult); APISetup.checkForErrors( pSave ); // use the SaveResult object to load the document just saved wNewDoc = (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"; // allow stream buffering = true // we will use streams to transfert data 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", wNewDoc.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 new document in the resource project.documentFolder.documentElements = new DocumentElement[] {wNewDoc}; // save the resource in the RPM repository // use the sessionid acquired in the setUp // a scope is necessary because we save the link with document pSave = new SaveResult(); pSave = APISetup.application.save (sessionid, project, pScope, ReloadType.ReloadResult ); // method showing the detail of any error in the save APISetup.checkForErrors( pSave ); /* * Download a file */ // download the document just saved wNewDoc = (Document)APISetup.application.loadFromXpath (sessionid, "/Document[name='" + docName + "']", null).rpmObjectList[0]; // create request URL with parameters String getURL = adresse + "?ACTION=" + dwnldAction + "DOCUMENT_ID=" + wNewDoc.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); } } }