Exemplo n.º 1
0
 public RequestCache(NetWorkSetting ns)
 {
     Network = ns;
 }
Exemplo n.º 2
0
 public RxQueryTests()
 {
     log4net.Config.XmlConfigurator.Configure();
     _logger         = log4net.LogManager.GetLogger(typeof(RxQueryTests));
     _networkSetting = Configuration.Instance.GetNetworkSetting(1);
 }
Exemplo n.º 3
0
 public static RequestCache ForNetwork(NetWorkSetting ns)
 {
     return(_perNetwork.GetOrAdd(ns.NetworkId, _ => new RequestCache(ns)));
 }
Exemplo n.º 4
0
 public static Lpp.Dns.DTO.DataMartClient.RequestTypeIdentifier GetRequestTypeIdentifier(NetWorkSetting ns, Guid modelID, Guid processorID)
 {
     try
     {
         using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
         {
             return(Task.Run(() => web.GetRequestTypeIdentifier(modelID, processorID)).Result);
         }
     }
     catch (Exception ex)
     {
         _log.Error(string.Format("Unable to get requests identifier for Network: {0}.", ns.NetworkName), ex);
         throw new GetRequestTypeIdentifierException(ex);
     }
 }
Exemplo n.º 5
0
        public static IObservable <RequestList> GetRequestList(string queryDescription, NetWorkSetting ns, int startIndex, int count, RequestFilter filter, RequestSortColumn?sortColumn, bool?sortAscending)
        {
            var nullReqs = new RequestList();
            var noError  = (Exception)null;

            if (count < 0)
            {
                count = int.MaxValue;
            }

            return
                (Observable.Generate(new
            {
                reqs = nullReqs,
                nextIndex = startIndex,
                pageSize = 100,
                count,
                finish = false,
                error = noError
            },
                                     st => !st.finish,
                                     st =>
            {
                try
                {
                    if (st.error != null || st.count == 0)
                    {
                        return new { st.reqs, st.nextIndex, st.pageSize, st.count, finish = true, st.error };
                    }

                    Lpp.Dns.DTO.DataMartClient.RequestList list = null;
                    using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
                    {
                        list = Task.Run(() => web.GetRequestList(queryDescription, filter.EffectiveFromDate, filter.EffectiveToDate, filter.DataMartIds, filter.Statuses, (Lpp.Dns.DTO.DataMartClient.RequestSortColumn?)(int?) sortColumn, sortAscending, st.nextIndex, Math.Min(st.count, st.pageSize))).Result;
                    }

                    RequestList reqs = null;
                    if (list == null)
                    {
                        reqs = new RequestList
                        {
                            Segment = Enumerable.Empty <RequestListRow>().ToArray(),
                            SortedAscending = true,
                            SortedByColumn = RequestSortColumn.RequestTime,
                            StartIndex = 0,
                            TotalCount = 0
                        };
                    }
                    else
                    {
                        reqs = new RequestList
                        {
                            Segment = list.Segment,
                            SortedAscending = list.SortedAscending,
                            SortedByColumn = list.SortedByColumn,
                            StartIndex = list.StartIndex,
                            TotalCount = list.TotalCount
                        };
                    }

                    _lastSucessfulPageSize = st.pageSize;


                    return new
                    {
                        reqs,
                        nextIndex = st.nextIndex + reqs.Segment.Count(),
                        st.pageSize,
                        count = Math.Min(st.count, reqs.TotalCount - reqs.StartIndex) - reqs.Segment.Count(),
                        finish = false,
                        error = noError
                    };
                }
                catch (CommunicationException ex)
                {
                    if (ex.Message.ToLower().Contains("size quota"))
                    {
                        return st.pageSize > 1 ?
                        new { reqs = nullReqs, st.nextIndex, pageSize = st.pageSize / 2, st.count, finish = false, error = noError } :
                        new { reqs = nullReqs, st.nextIndex, st.pageSize, st.count, finish = false, error = (Exception)ex };
                    }

                    throw;
                }
            },
                                     st => st.error == null ? Observable.Return(st.reqs) : Observable.Throw <RequestList>(st.error),
                                     System.Reactive.Concurrency.Scheduler.Default
                                     )
                 .Catch(( Exception ex ) =>
            {
                _log.Error(string.Format("Unable to get requests for Network: {0}.", ns.NetworkName), ex);
                return Observable.Throw <IObservable <RequestList> >(new GetRequestsFailed(ex));
            })
                 .SelectMany(ls => ls)
                 .Skip(1)
                 .Aggregate((l1, l2) => new RequestList
            {
                Segment = l1.Segment.EmptyIfNull().Concat(l2.Segment.EmptyIfNull()).ToArray(),
                SortedAscending = l1.SortedAscending,
                SortedByColumn = l1.SortedByColumn,
                StartIndex = l1.StartIndex,
                TotalCount = l1.TotalCount
            }));
        }
Exemplo n.º 6
0
 public static void SetRequestStatus(HubRequest request, HubRequestStatus status, IDictionary <string, string> requestProperties, NetWorkSetting ns)
 {
     try
     {
         using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
         {
             Task.Run(() => web.SetRequestStatus(request.Source.ID, request.DataMartId, status.Code, status.Message, requestProperties.EmptyIfNull().Select(p => new DTO.DataMartClient.RoutingProperty {
                 Name = p.Key, Value = p.Value
             }).ToArray())).Wait();
         }
     }
     catch (Exception ex)
     {
         _log.Error(string.Format("Unable to set request status for requestID: {0}.", request.Source.ID), ex);
         throw new SetRequestStatusFailed(ex);
     }
 }
Exemplo n.º 7
0
 public static bool CheckUserRight(HubRequest request, HubRequestRights rights, NetWorkSetting ns)
 {
     return((request.Rights & rights) > 0);
 }
Exemplo n.º 8
0
        public static Guid[] PostResponseDocuments(string uploadIdentifier, string requestId, Guid dataMartId, Lpp.Dns.DataMart.Model.Document[] documents, NetWorkSetting ns)
        {
            string docString = string.Join(", ", documents.Select(x => string.Format("'{0:D}'", x.Filename)));

            try
            {
                using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
                {
                    _log.Debug($"{uploadIdentifier} - Posting metadata for the following documents to API: {docString} for RequestID: {requestId}, DataMartID: {dataMartId.ToString("D")}");

                    var result = Task.Run(() => web.PostResponseDocuments(new Guid(requestId), dataMartId, documents.Select(d => new Lpp.Dns.DTO.DataMartClient.Document {
                        Name = d.Filename, MimeType = d.MimeType, Size = d.Size, IsViewable = d.IsViewable, Kind = d.Kind
                    }).ToArray())).Result;

                    _log.Debug($"{uploadIdentifier} - Posting metadata complete for the following documents to API: {docString} for RequestID: {requestId}, DataMartID: {dataMartId.ToString("D")}");
                    return(result.ToArray());
                }
            }
            catch (Exception ex)
            {
                _log.Error(string.Format("{2} - Unable to post document metadata for the following documents: {1} for RequestID: {0}.", requestId, docString, uploadIdentifier), ex);
                throw new PostResponseDocumentsFailed(ex);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Starts sending the document data to the portal and returns a stream of "bytes written so far" values
        /// TODO: Replace IObservable[int] with a specific type that would reflect the semantics
        /// </summary>
        public static IObservable <int> PostResponseDocumentContent(string uploadIdentifier, string requestID, Guid dataMartID, Guid documentId, string documentName, Stream stream, NetWorkSetting ns)
        {
            _log.Debug($"{uploadIdentifier} - Posting document content to API for: {documentName} (ID: {documentId.ToString("D") }); RequestID: {requestID}, DataMartID: {dataMartID.ToString("D")}");

            byte[] buffer = new byte[0x400000];
            return(Observable.Generate(
                       new { offset = 0, finish = false },
                       st => !st.finish,
                       st =>
            {
                int bytesRead = stream.Read(buffer, 0, buffer.Length);
                if (bytesRead == 0)
                {
                    _log.Debug($"{uploadIdentifier} - Finished posting document content to API for: {documentName} (ID: {documentId.ToString("D") }); RequestID: {requestID}, DataMartID: {dataMartID.ToString("D")}");
                    return new { st.offset, finish = true };
                }

                byte[] data = buffer;
                if (bytesRead < data.Length)
                {
                    data = new byte[bytesRead];
                    Buffer.BlockCopy(buffer, 0, data, 0, bytesRead);
                }

                try
                {
                    using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
                    {
                        Task.Run(() => web.PostResponseDocumentChunk(documentId, data)).Wait();
                    }
                }
                catch (Exception ex)
                {
                    _log.Error(string.Format("Unable to post Repsonse Doucument Content for Document: {0}.", documentId), ex);
                    throw new PostResponseDocumentContentFailed(ex);
                }

                return new { offset = st.offset + bytesRead, finish = false };
            },
                       st => st.offset
                       ));
        }
Exemplo n.º 10
0
 public static bool LogIn(NetWorkSetting ns)
 {
     try
     {
         try
         {
             using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
             {
                 var profile = Task.Run(() => web.GetProfile()).Result;
                 ns.Profile = new Profile
                 {
                     Email            = profile.Email,
                     FullName         = profile.FullName,
                     OrganizationName = profile.OrganizationName,
                     Username         = profile.Username
                 };
             }
             ns.IsAuthenticated = true;
             ns.NetworkStatus   = Util.ConnectionOKStatus;
             ns.NetworkMessage  = string.Empty;
             return(true);
         }
         catch (System.Net.Http.HttpRequestException rex)
         {
             //Unwrap the raised exception to the original exception - it gets wrapped in generic http exceptions.
             throw UnwrapException(rex);
         }
         catch (AggregateException aex)
         {
             //Unwrap the raised exception to the original exception - it gets wrapped in generic Aggregate exceptions.
             throw UnwrapException(aex);
         }
     }
     catch (System.Net.Http.HttpRequestException rex)
     {
         ns.NetworkStatus   = Util.ConnectionFailedStatus;
         ns.IsAuthenticated = false;
         ns.NetworkMessage  = rex.Message;
         if (string.Equals(ns.NetworkMessage, "Response status code does not indicate success: 401 (Unauthorized).", StringComparison.OrdinalIgnoreCase))
         {
             ns.NetworkMessage = "Authentication failed, please check your credentials are correct.";
         }
     }
     catch (System.Net.Sockets.SocketException sockEx)
     {
         ns.NetworkStatus   = Util.ConnectionFailedStatus;
         ns.IsAuthenticated = false;
         if (sockEx.ErrorCode == 10061)
         {
             ns.NetworkMessage = string.Format("Unable to connect, please confirm service url:{1}{1}{0}{1}{1}If the url is correct, and the problem persists please contact your network administrator.", ns.HubWebServiceUrl, Environment.NewLine);
         }
         else
         {
             ns.NetworkMessage = sockEx.Message;
         }
     }
     catch (Exception e)
     {
         ns.NetworkStatus   = Util.ConnectionFailedStatus;
         ns.IsAuthenticated = false;
         ns.NetworkMessage  = e.Message;
     }
     return(false);
 }
Exemplo n.º 11
0
        public static IObservable <Request> GetRequests(NetWorkSetting ns, Guid[] ids, Guid dmID)
        {
            if (ids.NullOrEmpty())
            {
                return(Observable.Empty <Request>());
            }

            var nullReqs = (Request[])null;
            var noError  = (Exception)null;

            return
                (Observable.Generate(
                     new { reqs = nullReqs, idss = new[] { ids }.AsEnumerable(), DataMartID = dmID, finish = false, error = noError },
                     st => !st.finish,
                     st =>
            {
                try
                {
                    if (st.error != null)
                    {
                        return new { st.reqs, st.idss, st.DataMartID, finish = true, st.error }
                    }
                    ;

                    var i = st.idss.FirstOrDefault();
                    var dm = st.DataMartID;
                    if (i == null)
                    {
                        return new { st.reqs, idss = st.idss, DataMartID = st.DataMartID, finish = true, error = noError }
                    }
                    ;

                    Request[] reqs = null;
                    using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
                    {
                        var requests = AsyncHelpers.RunSync <IQueryable <DTO.DataMartClient.Request> >(() => web.GetRequests(i.Select(x => x).ToArray(), dm));

                        reqs = requests.ToArray();
                    }

                    return new { reqs, idss = st.idss.Skip(1), DataMartID = st.DataMartID, finish = false, error = noError };
                }
                catch (CommunicationException ex)
                {
                    if (ex.Message.ToLower().Contains("size quota"))
                    {
                        return st.idss.First().Length > 1         // When the next segment can be subdivided further, do it
                                    ? new
                        {
                            reqs = nullReqs,
                            idss = st.idss
                                   .SelectMany(i => new[] { i.Take(i.Length / 2).ToArray(), i.Skip(i.Length / 2).ToArray() })
                                   .Where(i => i.Any()),
                            DataMartID = st.DataMartID,
                            finish = false,
                            error = noError
                        }
                        // Otherwise, return error
                                    : new { reqs = nullReqs, idss = st.idss, DataMartID = st.DataMartID, finish = false, error = (Exception)ex };
                    }

                    throw;
                }
            },
                     st => st.error == null ? st.reqs.EmptyIfNull().ToObservable() : Observable.Throw <Request>(st.error),
                     System.Reactive.Concurrency.Scheduler.Default
                     )
                 .Catch(( Exception ex ) =>
            {
                _log.Error(string.Format("Unable to get requests for Network: {0}.", ns.NetworkName), ex);
                return Observable.Throw <IObservable <Request> >(new GetRequestsFailed(ex));
            })
                 .SelectMany(rs => rs));
        }
Exemplo n.º 12
0
 public static Lpp.Dns.DTO.DataMartClient.RequestTypeIdentifier GetRequestTypeIdentifier(NetWorkSetting ns, Guid modelID, Guid processorID)
 {
     try
     {
         using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
         {
             return(AsyncHelpers.RunSync <Lpp.Dns.DTO.DataMartClient.RequestTypeIdentifier>(() => web.GetRequestTypeIdentifier(modelID, processorID)));
         }
     }
     catch (Exception ex)
     {
         throw new GetRequestTypeIdentifierException(ex);
     }
 }
Exemplo n.º 13
0
        public static void SetRequestStatus(HubRequest request, HubRequestStatus status, IDictionary <string, string> requestProperties, NetWorkSetting ns)
        {
            // BMS: Don't report inprocess or awaitingresponseapproval until portal can display status in routings.
            //if (status.Code == DTO.DataMartClient.Enums.DMCRoutingStatus.AwaitingResponseApproval)
            //{
            //    return;
            //}

            try
            {
                using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
                {
                    AsyncHelpers.RunSync(() => web.SetRequestStatus(request.Source.ID, request.DataMartId, status.Code, status.Message, requestProperties.EmptyIfNull().Select(p => new DTO.DataMartClient.RoutingProperty {
                        Name = p.Key, Value = p.Value
                    }).ToArray()));
                }
            }
            catch (Exception ex)
            {
                throw new SetRequestStatusFailed(ex);
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Starts sending the document data to the portal and returns a stream of "bytes written so far" values
        /// TODO: Replace IObservable[int] with a specific type that would reflect the semantics
        /// </summary>
        public static IObservable <int> PostResponseDocumentContent(Guid documentId, Stream stream, NetWorkSetting ns)
        {
            byte[] buffer = new byte[0x400000];
            return(Observable.Generate(
                       new { offset = 0, finish = false },
                       st => !st.finish,
                       st =>
            {
                int bytesRead = stream.Read(buffer, 0, buffer.Length);
                if (bytesRead == 0)
                {
                    return new { st.offset, finish = true }
                }
                ;

                byte[] data = buffer;
                if (bytesRead < data.Length)
                {
                    data = new byte[bytesRead];

                    Buffer.BlockCopy(buffer, 0, data, 0, bytesRead);
                }

                try
                {
                    using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
                    {
                        AsyncHelpers.RunSync(() => web.PostResponseDocumentChunk(documentId, data));
                    }
                }
                catch (Exception ex)
                {
                    throw new PostResponseDocumentContentFailed(ex);
                }

                return new { offset = st.offset + bytesRead, finish = false };
            },
                       st => st.offset
                       ));
        }
Exemplo n.º 15
0
 public static Guid[] PostResponseDocuments(string requestId, Guid dataMartId, Lpp.Dns.DataMart.Model.Document[] documents, NetWorkSetting ns)
 {
     try
     {
         using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
         {
             var result = AsyncHelpers.RunSync <IEnumerable <Guid> >(() => web.PostResponseDocuments(new Guid(requestId), dataMartId, documents.Select(d => new Lpp.Dns.DTO.DataMartClient.Document {
                 Name = d.Filename, MimeType = d.MimeType, Size = d.Size, IsViewable = d.IsViewable
             })));
             return(result.ToArray());
         }
     }
     catch (Exception ex)
     {
         throw new PostResponseDocumentsFailed(ex);
     }
 }
Exemplo n.º 16
0
        public static void PostDocumentChunk(string uploadIdentifier, Dns.DTO.DataMartClient.Criteria.DocumentMetadata doc, Stream stream, NetWorkSetting ns)
        {
            _log.Debug($"{uploadIdentifier} - Posting response document to API for: {doc.Name} (Chunk Index: {doc.CurrentChunkIndex}, ID: {doc.ID.ToString("D") }); RequestID: {doc.RequestID}, DataMartID: {doc.DataMartID.ToString("D")}");

            using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
            {
                byte[] buffer = new byte[0x400000];
                int    index  = 0;
                int    bytesRead;
                while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) >= 0)
                {
                    byte[] data = buffer;
                    if (bytesRead < data.Length)
                    {
                        data = new byte[bytesRead];
                        Buffer.BlockCopy(buffer, 0, data, 0, bytesRead);
                    }
                    try
                    {
                        doc.CurrentChunkIndex = index;

                        Task.Run(async() => await web.PostDocumentChunk(doc, data)).Wait(TimeSpan.FromDays(2));
                    }
                    catch (Exception ex)
                    {
                        _log.Error($"{uploadIdentifier} - Unable to post response document content for: {doc.Name} (Chunk Index: {doc.CurrentChunkIndex}, ID: { doc.ID.ToString("D") }); RequestID: {doc.RequestID }, DataMartID: {doc.DataMartID.ToString("D") }.", ex);
                        throw new PostResponseDocumentContentFailed(ex);
                    }

                    if (bytesRead == 0)
                    {
                        //post zero content document, and do not continue.
                        break;
                    }

                    index++;
                }
            }

            _log.Debug($"{uploadIdentifier} - Finished posting document content to API for: {doc.Name} (ID: {doc.ID.ToString("D") }); RequestID: {doc.RequestID}, DataMartID: {doc.DataMartID.ToString("D")}");
        }