private static ICredentials RebuildCredentials(ICredentials credentials, string redirectUrl) { CredentialCache cc = credentials as CredentialCache; if (cc != null) { IEnumerator iterate = cc.GetEnumerator(); while (iterate.MoveNext()) { NetworkCredential c = iterate.Current as NetworkCredential; if (c != null) { // we just take the first one to recreate string domainUser = c.Domain; if (!string.IsNullOrEmpty(domainUser)) { domainUser = domainUser + @"\"; } domainUser = String.Concat(domainUser, c.UserName); return(FeedSource.CreateCredentialsFrom(redirectUrl, domainUser, c.Password)); } } } // give up/forward original credentials: return(credentials); }
/// <summary> /// Download application state either as a ZIP file or as a SIAM file from a remote location. /// </summary> /// <param name="syncFormat">The synchronization format to use</param> public void RunDownload(SynchronizationFormat syncFormat) { try { Stream importStream = null; string tempFileName = null; switch (remoteProtocol) { case RemoteStorageProtocolType.UNC: // Fetch from a UNC path importStream = File.Open( Path.Combine(Environment.ExpandEnvironmentVariables(remoteLocation), remoteFileName), FileMode.Open); break; //case RemoteStorageProtocolType.dasBlog: // // Fetch from the dasBlog web service // // Connect to the web service and request an update // ConfigEditingService remoteStore = new ConfigEditingService(); // remoteStore.Url = remoteLocation; // remoteStore.authenticationHeaderValue = new authenticationHeader(); // remoteStore.authenticationHeaderValue.userName = credentialUser; // remoteStore.authenticationHeaderValue.password = credentialPassword; // remoteStore.Proxy = rssBanditApp.Proxy; // importStream = new MemoryStream(); // XmlElement xml = remoteStore.GetBlogroll("blogroll"); // ".opml" is appended by the service! // XmlDocument doc = new XmlDocument(); // doc.LoadXml(xml.OuterXml); // doc.Save(importStream); // importStream.Position = 0; // break; case RemoteStorageProtocolType.FTP: // Fetch from FTP Uri remoteUri = new Uri(remoteLocation); UriBuilder builder = new UriBuilder(remoteUri); builder.Path += builder.Path.EndsWith("/") ? remoteFileName : "/" + remoteFileName; /* set up the FTP connection */ FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(builder.Uri); ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; ftpRequest.KeepAlive = false; ftpRequest.UseBinary = true; ftpRequest.UsePassive = settings.GetProperty(Ps.FtpConnectionModePassive, true); ftpRequest.Credentials = new NetworkCredential(credentialUser, credentialPassword); // Create a file stream and get the file into that tempFileName = Path.GetTempFileName(); Stream fileStream = File.Create(tempFileName); /* perform download */ try { // The buffer size is set to 2kb const int buffLength = 2048; byte[] buff = new byte[buffLength]; var response = (FtpWebResponse)ftpRequest.GetResponse(); // Stream to which the file to be upload is written Stream strm = response.GetResponseStream(); if (strm != null) { // Read from the FTP stream 2kb at a time int contentLen = strm.Read(buff, 0, buffLength); // Till Stream content ends while (contentLen != 0) { // Write Content from the file stream to the // FTP Upload Stream fileStream.Write(buff, 0, contentLen); contentLen = strm.Read(buff, 0, buffLength); } // Close the Response Stream strm.Close(); } } catch (Exception ex) { //ToDO: Add support for switching between active and passive mode this.p_operationException = ex; _log.Error("FTP Download Error", ex); } fileStream.Close(); // save new setting: //settings.SetProperty("RemoteFeedlist/Ftp.ConnectionMode.Passive", connectionMode_Passive); // Open the temporary file so we can import it if (p_operationException == null) { importStream = File.OpenRead(tempFileName); } break; case RemoteStorageProtocolType.WebDAV: remoteUri = new Uri(remoteLocation.EndsWith("/") ? remoteLocation + remoteFileName : remoteLocation + "/" + remoteFileName); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(remoteUri); request.Method = "GET"; request.AllowAutoRedirect = true; request.UserAgent = RssBanditApplication.UserAgent; request.Proxy = rssBanditApp.Proxy; if (!string.IsNullOrEmpty(credentialUser)) { NetworkCredential nc = FeedSource.CreateCredentialsFrom(credentialUser, credentialPassword); CredentialCache cc = new CredentialCache(); cc.Add(remoteUri, "Basic", nc); cc.Add(remoteUri, "Digest", nc); cc.Add(remoteUri, "NTLM", nc); request.Credentials = cc; } importStream = request.GetResponse().GetResponseStream(); break; default: Debug.Assert(false, "unknown remote protocol: '" + remoteProtocol + "' in RemoteFeedlistThreadHandler"); break; } // Now import the downloaded feed list if (importStream != null) { try { if ((remoteProtocol == RemoteStorageProtocolType.dasBlog) || (remoteProtocol == RemoteStorageProtocolType.dasBlog_1_3)) { rssBanditApp.BanditFeedSource.ImportFeedlist(importStream); } else { Synchronize(importStream, syncFormat); } } catch (Exception ex) { p_operationException = ex; } finally { importStream.Close(); } } if (tempFileName != null) { File.Delete(tempFileName); } } catch (ThreadAbortException) { // eat up } catch (Exception ex) { p_operationException = ex; _log.Error("RunDownload(" + syncFormat + ") Exception", ex); } finally { WorkDone.Set(); } }
/// <summary> /// Upload application state either as a ZIP file or as a SIAM file /// </summary> /// <param name="syncFormat">The synchronization format to use</param> public void RunUpload(SynchronizationFormat syncFormat) { List <string> files = new List <string>(new[] { RssBanditApplication.GetFeedSourcesFileName(), RssBanditApplication.GetFlagItemsFileName(), RssBanditApplication.GetSearchFolderFileName(), RssBanditApplication.GetSentItemsFileName(), RssBanditApplication.GetWatchedItemsFileName() }); // add files managed by Bandit data storage (column layout and user identities): IUserRoamingDataService dataService = IoC.Resolve <IUserRoamingDataService>(); if (dataService != null) { files.AddRange(dataService.GetUserDataFileNames()); } // add files managed by NewComponents feed source data storage (e.g. Nntp Server Definitions filename) : rssBanditApp.FeedSources.ForEach(f => { files.AddRange(f.GetDataServiceFiles()); files.Add(f.SubscriptionLocation.Location); }); try { rssBanditApp.SaveApplicationState(); // Older versions support: FeedSourceEntry entry = rssBanditApp.FeedSources.Sources.FirstOrDefault( fs => fs.SourceType == FeedSourceType.DirectAccess); if (entry != null) { //copy BanditFeedSource feed list to subscriptions.xml (backward versions compatibility support) File.Copy(entry.Source.SubscriptionLocation.Location, RssBanditApplication.OldVersionSupport.GetSubscriptionsFileName(), true); //convert subscriptions.xml to feedlist.xml then save to temp folder using (Stream xsltStream = Resource.GetStream("Resources.feedlist2subscriptions.xslt")) { XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load(new XmlTextReader(xsltStream)); xslt.Transform( entry.Source.SubscriptionLocation.Location, RssBanditApplication.OldVersionSupport.GetFeedListFileName()); } files.AddRange(new[] { RssBanditApplication.OldVersionSupport.GetSubscriptionsFileName(), RssBanditApplication.OldVersionSupport.GetFeedListFileName() }); } using (MemoryStream tempStream = new MemoryStream()) { //ZipOutputStream zos; switch (remoteProtocol) { case RemoteStorageProtocolType.UNC: using (FileStream fs = FileHelper.OpenForWrite( Path.Combine(Environment.ExpandEnvironmentVariables(remoteLocation), remoteFileName))) { var zos = new ZipOutputStream(fs); FileHelper.ZipFiles(files, zos); } break; //case RemoteStorageProtocolType.dasBlog: // //save direct access feed list as OPML: // rssBanditApp.BanditFeedSource.SaveFeedList(tempStream, FeedListFormat.OPML); // tempStream.Position = 0; // XmlDocument doc = new XmlDocument(); // doc.Load(tempStream); // // Send it to the web service // ConfigEditingService remoteStore = new ConfigEditingService(); // remoteStore.Url = remoteLocation; // remoteStore.authenticationHeaderValue = new authenticationHeader(); // remoteStore.authenticationHeaderValue.userName = credentialUser; // remoteStore.authenticationHeaderValue.password = credentialPassword; // remoteStore.Proxy = rssBanditApp.Proxy; // //TODO: figure out, when we have to use Credentials???? // // remoteStore.Credentials = FeedSource.CreateCredentialsFrom(credentialUser, credentialPassword); // remoteStore.PostBlogroll("blogroll", doc.DocumentElement); // // ".opml" is appended by the service! // break; case RemoteStorageProtocolType.FTP: // Send to FTP server var zosFtp = new ZipOutputStream(tempStream); FileHelper.ZipFiles(files, zosFtp); tempStream.Position = 0; var ftpUri = new Uri(remoteLocation); UriBuilder builder = new UriBuilder(ftpUri); builder.Path += builder.Path.EndsWith("/") ? remoteFileName : "/" + remoteFileName; /* set up the FTP connection */ FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(builder.Uri); ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; ftpRequest.KeepAlive = false; ftpRequest.UseBinary = true; ftpRequest.UsePassive = settings.GetProperty(Ps.FtpConnectionModePassive, true); ftpRequest.Credentials = new NetworkCredential(credentialUser, credentialPassword); ftpRequest.ContentLength = tempStream.Length; /* perform upload */ try { // The buffer size is set to 2kb const int buffLength = 2048; byte[] buff = new byte[buffLength]; // Stream to which the file to be upload is written using (Stream strm = ftpRequest.GetRequestStream()) { // Read from the file stream 2kb at a time int contentLen = tempStream.Read(buff, 0, buffLength); // Till Stream content ends while (contentLen != 0) { // Write Content from the file stream to the // FTP Upload Stream strm.Write(buff, 0, contentLen); contentLen = tempStream.Read(buff, 0, buffLength); } } } catch (Exception ex) { //ToDO: Add support for switching between active and passive mode p_operationException = ex; _log.Error("FTP Upload Error", ex); } break; case RemoteStorageProtocolType.WebDAV: var zosDav = new ZipOutputStream(tempStream); FileHelper.ZipFiles(files, zosDav); var remoteUri = new Uri(remoteLocation.EndsWith("/") ? remoteLocation + remoteFileName : remoteLocation + "/" + remoteFileName); tempStream.Position = 0; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(remoteUri); request.Method = "PUT"; request.ContentType = "application/zip"; request.AllowAutoRedirect = true; request.UserAgent = RssBanditApplication.UserAgent; request.Proxy = rssBanditApp.Proxy; if (!string.IsNullOrEmpty(credentialUser)) { NetworkCredential nc = FeedSource.CreateCredentialsFrom(credentialUser, credentialPassword); CredentialCache cc = new CredentialCache(); cc.Add(remoteUri, "Basic", nc); cc.Add(remoteUri, "Digest", nc); cc.Add(remoteUri, "NTLM", nc); request.Credentials = cc; } byte[] bytes = new byte[tempStream.Length]; tempStream.Read(bytes, 0, bytes.Length); request.ContentLength = bytes.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(bytes, 0, bytes.Length); requestStream.Close(); break; default: Debug.Assert(false, "unknown remote protocol: '" + remoteProtocol + "' in RemoteFeedlistThreadHandler"); break; } } // Cool, we made it } catch (ThreadAbortException) { // eat up } catch (Exception ex) { p_operationException = ex; _log.Error("RunUpload(" + syncFormat + ") Exception", ex); } finally { WorkDone.Set(); } }