/// <summary> /// Method to get the cab list for the event and event type name. /// </summary> /// <param name="loginObject">Object containing the login credentials.</param> /// <returns>Collection of Cab objects.</returns> public CabCollection GetCabs(ref Login loginObject) { // // return the cab list using the static method of the cab class. // return(Cab.GetCabs(this.ID, this.EventTypeName, ref loginObject)); }
/// <summary> /// Method to save the cab to a folder with a specified file name. /// </summary> /// <param name="folderPath">Path to the folder to save the cab at.</param> /// <param name="fileName">Name of the file to save the cab file as.</param> /// <param name="overWrite">Flag to overwrite the existing cab file</param> /// <param name="loginObject">Object containing the login credentials.</param> /// <returns>true if the cab was saved successfully, else false.</returns> private bool SaveCab(string folderPath, string fileName, bool overWrite, ref Login loginObject) { // // make sure folder path is ok. // if (Directory.Exists(folderPath) == false) { // // TODO: throw directory not accessible exception. // } // check if cab already exists in the folder path before download starts // if (File.Exists(Path.Combine(folderPath, fileName)) == true && !overWrite) { return(false); } // // get the response from the server for the cab collection url. // HttpWebResponse cabResponse = Cab.GetFeedResponseObject(this.CabUrl, ref loginObject); Stream responseStream = cabResponse.GetResponseStream(); //Stream responseStream = this.GetCabStream(ref loginObject); FileStream fileStream = new FileStream(Path.Combine(folderPath, fileName), FileMode.OpenOrCreate, FileAccess.Write); int bufferLength = 256; Byte[] buffer = new byte[bufferLength]; int bytesRead = responseStream.Read(buffer, 0, bufferLength); while (bytesRead > 0) { fileStream.Write(buffer, 0, bytesRead); bytesRead = responseStream.Read(buffer, 0, bufferLength); } // // Cleanup the streams and the response. // responseStream.Close(); cabResponse.Close(); fileStream.Close(); return(true); }
/// <summary> /// Static method to get the list of cabs /// </summary> /// <param name="eventID">ID of the event associated with the cab.</param> /// <param name="eventTypeName">Event type of the event associated with the cab.</param> /// <param name="loginObject">Object containing the login credentials.</param> /// <returns>CabCollection object contaning cab objects.</returns> public static CabCollection GetCabs(int eventID, string eventTypeName, ref Login loginObject) { // // throw argument exception if event id is less than 1 // if (eventID < 1) { throw new ArgumentOutOfRangeException("eventID", "eventID cannot be less than 1."); } // // throw null reference exception if null or empty event type name is passed in. // if (string.IsNullOrEmpty(eventTypeName) == true) { throw new NullReferenceException("eventTypeName cannot be null or empty."); } // // get the response from the server for the cab collection url. // string responseFromServer = Cab.GetFeedResponse( new Uri( string.Format( "{0}cabs.aspx?eventid={1}&eventtypename={2}" , BASE_URL , eventID , eventTypeName ) ) , ref loginObject ); XmlNamespaceManager namespaceMgr; // // get the entry nodes from the XML // XmlNodeList entryNodes = Event.GetEntryNodes(responseFromServer, out namespaceMgr); // // instantiate the cab collection object. // CabCollection cabCollection = new CabCollection(); // // parse the entry elements and load the cab objects // foreach (XmlNode entryNode in entryNodes) { Cab cab = new Cab(); // id - cab id cab.ID = int.Parse(entryNode.SelectSingleNode("atom:id", namespaceMgr).InnerText); // updated - product date modified cab.DateModifiedLocal = DateTime.Parse(entryNode.SelectSingleNode("atom:updated", namespaceMgr).InnerText).ToLocalTime(); // published - product date created cab.DateCreatedLocal = DateTime.Parse(entryNode.SelectSingleNode("atom:published", namespaceMgr).InnerText).ToLocalTime(); // wer:cabSize - cab size cab.SizeInBytes = long.Parse(entryNode.SelectSingleNode("wer:cabSize", namespaceMgr).InnerText); // wer:eventid = event id cab.EventID = int.Parse(entryNode.SelectSingleNode("wer:eventID", namespaceMgr).InnerText); // wer:eventTypeName = event type name cab.EventTypeName = entryNode.SelectSingleNode("wer:eventTypeName", namespaceMgr).InnerText; // wer:cabFileName = cab file name cab.CabFileName = entryNode.SelectSingleNode("wer:cabFileName", namespaceMgr).InnerText; // link - cab url cab.CabUrl = new Uri(entryNode.SelectSingleNode("atom:link[@rel='enclosure']", namespaceMgr).Attributes["href"].Value); // // add the cab object to the cab collection // cabCollection.Add(cab); } // // return the cab collection // return(cabCollection); }