public static void SendToLoiess(SortMainObject sort) { var funding = sort.Funding.Where(n => n.FundingTypeId == 2 && !string.IsNullOrWhiteSpace(n.TrackingNumber)).ToList(); if (funding != null && funding.Count > 0) { foreach (var f in funding) { var stims = new StimsData(); stims.SourceId = sort.SortMainId.Value; stims.TrackingNumber = f.TrackingNumber; stims.StimsNumber = sort.TitleStr; stims.StimsType = sort.ProductTypeDisplayName; stims.Title = sort.PublishTitle; stims.JournalName = sort.JournalName; stims.PublicationDate = sort.PublicationDate; stims.JournalVolume = sort.JournalVolume; stims.JournalNumber = sort.JournalSerial; stims.OstiId = sort.OstiId; stims.OstiSaveDate = sort.OstiDate.Value; stims.DoiNum = sort.DoiNum; stims.AuthorNames = string.Join("|", sort.Authors.Select(n => n.FullName)); stims.FirstInlAuthor = sort.Authors.FirstOrDefault(n => n.IsPrimary)?.FullName; UploadDataToLoiEss(stims); } } }
public static ExportResponse UploadDataToLoiEss(StimsData data) { ExportResponse rtn = new ExportResponse(); try { using (var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true })) { client.BaseAddress = new Uri(Config.LoiessUrl); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.PostAsJsonAsync("api/Stims/", data).Result; if (response.IsSuccessStatusCode) { var formatters = new List <MediaTypeFormatter>() { new JsonMediaTypeFormatter(), new XmlMediaTypeFormatter() }; rtn.Success = response.Content.ReadAsAsync <bool>(formatters).Result; } else { rtn.Success = false; rtn.ErrorMessage = $"Status Returned from API was : {response.StatusCode}"; } } } catch (Exception ex) { rtn.Success = false; rtn.ErrorMessage = ex.Message; rtn.StackTrace = ex.StackTrace; } return(rtn); }
public static bool UploadToOsti(SortMainObject sort) { string xml = string.Empty; try { // upload to OSTI HttpWebRequest webreq = null; HttpWebResponse webres = null; string res = string.Empty; xml = sort.GetXml(); ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; webreq = WebRequest.Create(Config.OstiUrl) as HttpWebRequest; webreq.Credentials = new NetworkCredential(Config.OstiUserName, Config.OstiPassword); webreq.PreAuthenticate = true; webreq.ContentType = "application/xml"; webreq.Method = "POST"; webreq.Timeout = 10000000; Stream postData = null; byte[] buffer = Encoding.UTF8.GetBytes(xml); try { postData = webreq.GetRequestStream(); } catch (Exception ex) { ErrorLogObject.LogError("OstiUploadObject::UploadToOsti1", ex); sort.OstiStatusMsg = $"Exception Caught while connecting to OSTI API: {ex.Message}"; AddLogRecord(sort.SortMainId.Value, xml, sort.OstiStatusMsg, "FAILED - Exception Caught while connecting to OSTI API"); return(false); } if (postData != null && buffer != null) { postData.Write(buffer, 0, buffer.Length); postData.Close(); } WebResponse xres = null; try { xres = webreq.GetResponse(); } catch (WebException ex) { ErrorLogObject.LogError("OstiUploadObject::UploadToOsti", ex); if (ex.Response != null) { using (var stream = ex.Response.GetResponseStream()) using (var reader = new StreamReader(stream)) { res = reader.ReadToEnd(); } } else { sort.OstiStatusMsg = $"Error returned:{ex.Message} => Could be that the password for OSTI site has expired."; AddLogRecord(sort.SortMainId.Value, xml, sort.OstiStatusMsg, "FAILED - webreq.GetResponse"); return(false); } } if (xres is HttpWebResponse) { if (((HttpWebResponse)xres).StatusCode != HttpStatusCode.OK) { sort.OstiStatusMsg = "Status Code returned was: " + ((HttpWebResponse)xres).StatusDescription; AddLogRecord(sort.SortMainId.Value, xml, sort.OstiStatusMsg, "FAILED - StatusCode"); return(false); } using (webres = xres as HttpWebResponse) { StreamReader reader = new StreamReader(webres.GetResponseStream()); res = reader.ReadToEnd(); } xres.Close(); } if (!string.IsNullOrWhiteSpace(res)) { try { OstiRecords record = null; if (!string.IsNullOrWhiteSpace(res)) { using (TextReader reader = new StringReader(res)) { XmlSerializer serializer = new XmlSerializer(typeof(OstiRecords)); record = (OstiRecords)serializer.Deserialize(reader); } } if (record != null && record.Record != null && record.Record.Length > 0) { OstiRecord rec = record.Record[0]; sort.OstiId = rec.Id.HasValue ? (rec.Id.Value > 0 ? rec.Id.Value.ToString() : sort.OstiId) : sort.OstiId; sort.OstiStatus = record.Record[0].Status; sort.OstiStatusMsg = record.Record[0].StatusMessage; sort.OstiDate = DateTime.Now; } } catch (Exception ex) { ErrorLogObject.LogError("OstiUploadObject::UploadToOsti2", ex); using (XmlReader rdr = XmlReader.Create(new StringReader(res))) { rdr.ReadToFollowing(Extensions.GetPropertyShortName(() => sort.OstiId)); sort.OstiId = rdr.ReadElementContentAsString(); rdr.ReadToFollowing(Extensions.GetPropertyShortName(() => sort.OstiStatus)); sort.OstiStatus = rdr.ReadElementContentAsString(); rdr.ReadToFollowing(Extensions.GetPropertyShortName(() => sort.OstiStatusMsg)); if (rdr.NodeType == XmlNodeType.Element) { sort.OstiStatusMsg = rdr.ReadElementContentAsString(); } sort.OstiDate = DateTime.Now; } } AddLogRecord(sort.SortMainId.Value, xml, res, sort.OstiStatus); if (!string.IsNullOrWhiteSpace(sort.OstiId) && sort.OstiStatus.Equals("success", StringComparison.OrdinalIgnoreCase)) { if (!sort.PublicRelease) { string error = string.Empty; if (!UploadFilesFtpS(sort.SortMainId.Value, sort.OstiId, ref error)) { sort.OstiStatusMsg = $"FAILED - MetaData Uploaded, but FTPS Failed to upload the document to OSTI: {error}"; sort.OstiStatus = "FAILED"; AddLogRecord(sort.SortMainId.Value, xml, sort.OstiStatusMsg, "FAILED - FTPS Exception Caught"); return(false); } } else { // Update Digital Library for file that was uploaded to them. string jsonStr = Config.DigitalLibraryManager.GetDigitalLibraryJson(sort); Config.DigitalLibraryManager.ReportData(sort.StiSpId.Value, jsonStr); } //Report all LDRD Funding to LOI-ESS if (sort.SharePointId.HasValue) // SharePoint ID is also the LRS ID { try { StimsData.SendToLoiess(sort); } catch (Exception ex) { ErrorLogObject.LogError("OstiUploadObject::UploadToOsti3", ex); } } } else { return(false); } } else { sort.OstiStatusMsg = "No data returned from OSTI, Unable to complete the transfer."; AddLogRecord(sort.SortMainId.Value, xml, sort.OstiStatusMsg, "FAILED - Return Data"); return(false); } } catch (Exception ex) { ErrorLogObject.LogError("OstiUploadObject::UploadToOsti4", ex); sort.OstiStatusMsg = ex.Message; AddLogRecord(sort.SortMainId.Value, xml, sort.OstiStatusMsg, "FAILED - Exception Caught"); return(false); } return(true); }