/// <summary> /// Pass the request's documents to the View Panel /// </summary> /// <param name="documents"></param> /// <param name="networkSetting"></param> public void SetRequestDocuments(Document[] documents) { this.documents = documents; FileListDataSource = documents; foreach (Lpp.Dns.DataMart.Model.Document document in documents) { Guid documentID = Guid.Parse(document.DocumentID); if (!document.IsViewable && string.Equals("application/json", document.MimeType, StringComparison.OrdinalIgnoreCase)) { //the request is using the QueryComposer model processor, attempt to deserialize the document to determine the viewing url try { using (var stream = new DocumentChunkStream(documentID, networkSetting)) using (var reader = new StreamReader(stream)) { string query = reader.ReadToEnd(); var jobj = Newtonsoft.Json.Linq.JObject.Parse(query); string viewUrl = (string)jobj["Header"]["ViewUrl"]; if (!string.IsNullOrEmpty(viewUrl)) { ShowMimeType = "application/URL"; string token = Convert.ToBase64String(Encoding.UTF8.GetBytes(Crypto.EncryptString(string.Format("{0}:{1}:{2}", networkSetting.Username, networkSetting.DecryptedPassword, DateTime.UtcNow.ToString("s"))))); string url = viewUrl + "&token=" + token; DataSource = url; return; } } } catch { //deserialization failed, not a document that we want } } if (document.IsViewable) { using (var documentStream = new DocumentChunkStream(documentID, networkSetting)) { ShowMimeType = document.MimeType; SetDataSourceStream(documentStream); } return; } } ShowView = DisplayType.FILELIST; }
string ProcessRequest(HubRequest request, IModelProcessor processor, NetWorkSetting networkSetting) { try { Document[] requestDocuments = request.Documents.Select(d => new Lpp.Dns.DataMart.Model.Document(d.ID.ToString("D"), d.Document.MimeType, d.Document.Name) { IsViewable = d.Document.IsViewable, Size = Convert.ToInt32(d.Document.Size), Kind = d.Document.Kind }).ToArray(); Document[] desiredDocuments; string requestId = request.Source.ID.ToString(); IDictionary <string, string> requestProperties; processor.Request(requestId, networkSetting.CreateInterfaceMetadata(), request.CreateInterfaceMetadata(), requestDocuments, out requestProperties, out desiredDocuments); _log.Info("Request posted: " + request.Source.Identifier + " (ID: " + requestId + ")"); _log.Info("Number of documents available: " + requestDocuments.Length); _log.Info("Number of documents desired: " + desiredDocuments.Length); if (requestProperties != null && requestProperties.Count > 0) { _log.Info("Properties: "); foreach (string key in requestProperties.Keys) { _log.Info("Key: " + key + "=" + requestProperties[key]); } } // TODO[ddee] Needs to update the requestProperties here, but do not have a proper status. // Temporarily using InProgress. // BMS: Don't report inprogress status until portal is fixed to display status in routings // DnsServiceManager.SetRequestStatus(request, DnsServiceManager.ConvertModelRequestStatus(processor.Status(requestId)), requestProperties, networkSetting); foreach (Lpp.Dns.DataMart.Model.Document requestDocument in desiredDocuments) { _log.Info("About to post desired document id: " + requestDocument.DocumentID); DocumentChunkStream requestDocumentStream = new DocumentChunkStream(Guid.Parse(requestDocument.DocumentID), networkSetting); processor.RequestDocument(requestId, requestDocument.DocumentID, requestDocumentStream); _log.Info("Posted desired document id: " + requestDocument.DocumentID); } _log.Info("Starting request with local request: " + request.Source.Identifier + " (ID: " + requestId + ")"); processor.Start(requestId); _log.Info("Start finished on request with local request: " + request.Source.Identifier + " (ID: " + requestId + ")"); return(requestId); } catch (Exception ex) { _log.Error("Unexpected exception in Util.ProcessRequest.", ex); throw; } }
private string ProcessRequest(HubRequest request, IModelProcessor processor) { try { Document[] requestDocuments = request.Documents.Select(d => new Lpp.Dns.DataMart.Model.Document(d.ID.ToString("D"), d.Document.MimeType, d.Document.Name) { IsViewable = d.Document.IsViewable, Size = Convert.ToInt32(d.Document.Size), Kind = d.Document.Kind }).ToArray(); Document[] desiredDocuments; string requestId = request.Source.ID.ToString(); IDictionary <string, string> requestProperties; processor.Request(requestId, _networkSetting.CreateInterfaceMetadata(), request.CreateInterfaceMetadata(), requestDocuments, out requestProperties, out desiredDocuments); Log.Info("Request posted: " + request.Source.Identifier + " (ID: " + requestId + ")"); Log.Info("Number of documents available: " + requestDocuments.Length); Log.Info("Number of documents desired: " + desiredDocuments.Length); if (requestProperties != null && requestProperties.Count > 0) { Log.Info("Properties: "); foreach (string key in requestProperties.Keys) { Log.Info("Key: " + key + "=" + requestProperties[key]); } } foreach (Lpp.Dns.DataMart.Model.Document requestDocument in desiredDocuments) { Log.Debug("Downloading document" + requestDocument.Filename + $" for Request: {request.Source.MSRequestID}, DataMart: { request.DataMartName }"); DocumentChunkStream requestDocumentStream = new DocumentChunkStream(Guid.Parse(requestDocument.DocumentID), _networkSetting); processor.RequestDocument(requestId, requestDocument.DocumentID, requestDocumentStream); Log.Debug("Successfully Downloaded document" + requestDocument.Filename + $" for Request: {request.Source.MSRequestID}, DataMart: { request.DataMartName }"); } Log.Info("Starting request with local request: " + request.Source.Identifier + " (ID: " + requestId + ")"); processor.Start(requestId); Log.Info("Start finished on request with local request: " + request.Source.Identifier + " (ID: " + requestId + ")"); return(requestId); } catch (Exception ex) { Log.Error("Unexpected exception in Util.ProcessRequest.", ex); throw; } }
/// <summary> /// Save Files attached to query /// </summary> /// <param name="doc">Document object </param> private void Savefile(Document doc) { saveFileDialog.FileName = Path.GetFileName(doc.Filename); if (doc.Filename.EndsWith("xml", StringComparison.OrdinalIgnoreCase)) { saveFileDialog.FilterIndex = 2; } else if (!doc.Filename.EndsWith("txt", StringComparison.OrdinalIgnoreCase)) { string fileExtension = Path.GetExtension(doc.Filename); if (fileExtension.NullOrEmpty()) { if (!doc.MimeType.NullOrEmpty() && doc.MimeType.IndexOf("/") >= 0) { fileExtension = "." + doc.MimeType.Substring(doc.MimeType.IndexOf('/') + 1); saveFileDialog.Filter = string.Format("{1} files (*{0})|*{0}|{2}", fileExtension, fileExtension.Substring(1, 1).ToUpper() + fileExtension.Substring(2), saveFileDialog.Filter); } } else { saveFileDialog.Filter = string.Format("{1} files (*{0})|*{0}|{2}", fileExtension, fileExtension.Substring(1, 1).ToUpper() + fileExtension.Substring(2), saveFileDialog.Filter); } } if (saveFileDialog.ShowDialog() != DialogResult.OK) { return; } var sSaveLocation = saveFileDialog.FileName; var progress = new ProgressForm("Saving File", "Transferring the File from the Portal...") { Indeteminate = doc.Size <= 0 }; byte[] buffer = new byte[0x100000]; Observable.Using(() => File.Create(sSaveLocation), outStream => Observable.Using(() => { Stream dcStream; if (isRequestView == true) { if (doc.MimeType != "application/vnd.pmn.lnk") { dcStream = new DocumentChunkStream(Guid.Parse(doc.DocumentID), networkSetting); } else { string lnk = string.Empty; using (var docStream = new DocumentChunkStream(Guid.Parse(doc.DocumentID), networkSetting)) { lnk = new StreamReader(docStream).ReadToEnd(); docStream.Close(); } if (networkSetting.SftpClient == null) { networkSetting.SftpClient = new SFTPClient(networkSetting.Host, networkSetting.Port, networkSetting.Username, networkSetting.DecryptedPassword); } dcStream = networkSetting.SftpClient.FileStream(lnk); } } else { processor.ResponseDocument(requestId, doc.DocumentID, out dcStream, 60000); } return(dcStream); }, inStream => Observable.Return(0, Scheduler.Default).Repeat() .Select(_ => inStream.Read(buffer, 0, buffer.Length)) .TakeWhile(bytesRead => bytesRead > 0) .Do(bytesRead => outStream.Write(buffer, 0, bytesRead)) .Scan(0, (totalTransferred, bytesRead) => totalTransferred + bytesRead) .ObserveOn(this) //.Do(totalTransferred => progress.Progress = totalTransferred * 100 / Math.Max(doc.Size, 1)) .Do(totalTransferred => progress.Progress = totalTransferred * 100 / Math.Max((int)inStream.Length, 1)) ) ) .SubscribeOn(Scheduler.Default) .TakeUntil(progress.ShowAndWaitForCancel(this)) .ObserveOn(this) .Finally(progress.Dispose) .LogExceptions(log.Error) .Do(_ => {}, ex => { MessageBox.Show(this, "There was an error saving the file: \r\n\r\n" + ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); }, () => { MessageBox.Show(this, "File downloaded successfully", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); }) .Catch() .Subscribe(); }