private IOpenSearchResultCollection QueryOpenSearch(OpenSearchEngine ose, IOpenSearchable entity, NameValueCollection parameters)
        {
            IOpenSearchResultCollection osr;

            if (string.IsNullOrEmpty(queryFormatArg))
            {
                osr = ose.Query(entity, parameters);
            }
            else
            {
                osr = ose.Query(entity, parameters, queryFormatArg);
            }

            return(osr);
        }
        /// <summary>
        /// Get the specified request.
        /// </summary>
        /// <param name="request">Request.</param>
        public object Get(GetOpensearchSearchs request)
        {
            // This page is public

            // But its content will be adapted accrding to context (user id, ...)

            // Load the complete request
            HttpRequest httpRequest            = HttpContext.Current.Request;
            var         context                = TepWebContext.GetWebContext(PagePrivileges.EverybodyView);
            IOpenSearchResultCollection result = null;

            try{
                context.Open();
                context.LogInfo(this, string.Format("/data/collection/search GET"));

                EntityList <Collection> collections = new EntityList <Collection>(context);
                collections.AddSort("Name", SortDirection.Ascending);
                OpenSearchEngine ose = MasterCatalogue.OpenSearchEngine;

                Type responseType = OpenSearchFactory.ResolveTypeFromRequest(httpRequest.QueryString, httpRequest.Headers, ose);
                result = ose.Query(collections, httpRequest.QueryString, responseType);

                context.Close();
            }catch (Exception e) {
                context.LogError(this, e.Message, e);
                context.Close();
                throw e;
            }

            return(new HttpResult(result.SerializeToString(), result.ContentType));
        }
Exemple #3
0
        public object Get(CommunitySearchRequestTep request)
        {
            var context = TepWebContext.GetWebContext(PagePrivileges.EverybodyView);

            context.Open();
            context.LogInfo(this, string.Format("/community/search GET"));

            CommunityCollection domains = new CommunityCollection(context);

            domains.UserStatus = Request.QueryString != null ? Request.QueryString["status"] : null;
            domains.SetFilter("Kind", (int)DomainKind.Public + "," + (int)DomainKind.Private + "," + (int)DomainKind.Hidden);

            // Load the complete request
            HttpRequest      httpRequest  = HttpContext.Current.Request;
            OpenSearchEngine ose          = MasterCatalogue.OpenSearchEngine;
            Type             responseType = OpenSearchFactory.ResolveTypeFromRequest(httpRequest.QueryString, httpRequest.Headers, ose);

            // the opensearch cache system uses the query parameters
            // we add to the parameters the filters added to the load in order to avoir wrong cache
            // we use 't2-' in order to not interfer with possibly used query parameters
            var qs = new NameValueCollection(Request.QueryString);

            foreach (var filter in domains.FilterValues)
            {
                qs.Add("t2-" + filter.Key.FieldName, filter.Value.ToString());
            }

            IOpenSearchResultCollection osr = ose.Query(domains, httpRequest.QueryString, responseType);

            OpenSearchFactory.ReplaceOpenSearchDescriptionLinks(domains, osr);

            context.Close();
            return(new HttpResult(osr.SerializeToString(), osr.ContentType));
        }
Exemple #4
0
        public object Get(UserSearchRequest request)
        {
            var context = TepWebContext.GetWebContext(PagePrivileges.UserView);

            context.Open();
            context.LogInfo(this, string.Format("/user/search GET"));

            EntityList <UserTep> users = new EntityList <UserTep> (context);

            users.AddSort("Identifier", SortDirection.Ascending);

            // Load the complete request
            HttpRequest httpRequest = HttpContext.Current.Request;

            OpenSearchEngine ose = MasterCatalogue.OpenSearchEngine;

            string format;

            if (Request.QueryString ["format"] == null)
            {
                format = "atom";
            }
            else
            {
                format = Request.QueryString ["format"];
            }

            Type responseType = OpenSearchFactory.ResolveTypeFromRequest(httpRequest.QueryString, httpRequest.Headers, ose);
            IOpenSearchResultCollection osr = ose.Query(users, httpRequest.QueryString, responseType);

            OpenSearchFactory.ReplaceOpenSearchDescriptionLinks(users, osr);

            context.Close();
            return(new HttpResult(osr.SerializeToString(), osr.ContentType));
        }
Exemple #5
0
        public object Get(ServiceServiceTep request)
        {
            var context = TepWebContext.GetWebContext(PagePrivileges.UserView);

            context.Open();
            EntityList <Terradue.Portal.Service> services = new EntityList <Terradue.Portal.Service>(context);

            services.Load();

            // Load the complete request
            HttpRequest httpRequest = HttpContext.Current.Request;

            OpenSearchEngine ose = MasterCatalogue.OpenSearchEngine;

            string format;

            if (Request.QueryString["format"] == null)
            {
                format = "atom";
            }
            else
            {
                format = Request.QueryString["format"];
            }

            Type responseType = OpenSearchFactory.ResolveTypeFromRequest(httpRequest.QueryString, httpRequest.Headers, ose);
            IOpenSearchResultCollection osr = ose.Query(services, httpRequest.QueryString, responseType);

            context.Close();

            return(new HttpResult(osr.SerializeToString(), osr.ContentType));
        }
        /// <summary>
        /// Get the specified request.
        /// </summary>
        /// <param name="request">Request.</param>
        public object Get(DataPackageSearchDefaultRequestTep request)
        {
            var context = TepWebContext.GetWebContext(PagePrivileges.EverybodyView);
            IOpenSearchResultCollection result = null;

            try {
                context.Open();
                context.LogInfo(this, string.Format("/data/package/default/search GET"));
                Terradue.Tep.DataPackage datapackage = DataPackage.GetTemporaryForCurrentUser(context);
                datapackage.SetOpenSearchEngine(MasterCatalogue.OpenSearchEngine);

                OpenSearchEngine ose = MasterCatalogue.OpenSearchEngine;

                Type responseType = OpenSearchFactory.ResolveTypeFromRequest(HttpContext.Current.Request.QueryString, HttpContext.Current.Request.Headers, ose);

                List <Terradue.OpenSearch.IOpenSearchable> osentities = new List <Terradue.OpenSearch.IOpenSearchable>();
                osentities.AddRange(datapackage.GetOpenSearchableArray());

                var settings = MasterCatalogue.OpenSearchFactorySettings;
                MultiGenericOpenSearchable multiOSE = new MultiGenericOpenSearchable(osentities, settings);
                result = ose.Query(multiOSE, Request.QueryString, responseType);

                MasterCatalogue.ReplaceSelfLinksFormat(result, Request.QueryString);

                context.Close();
            } catch (Exception e) {
                context.LogError(this, e.Message, e);
                context.Close();
                throw e;
            }

            return(new HttpResult(result.SerializeToString(), result.ContentType));
        }
Exemple #7
0
        public object Get(SearchRssNews request)
        {
            var context = TepWebContext.GetWebContext(PagePrivileges.EverybodyView);
            IOpenSearchResultCollection result = null;

            try{
                context.Open();
                context.LogInfo(this, string.Format("/news/rss/search GET"));

                // Load the complete request
                HttpRequest httpRequest = HttpContext.Current.Request;

                OpenSearchEngine ose = MasterCatalogue.OpenSearchEngine;

                Type type = OpenSearchFactory.ResolveTypeFromRequest(httpRequest.QueryString, httpRequest.Headers, ose);

                EntityList <RssNews> rss = new EntityList <RssNews>(context);
                rss.Load();

                var settings = MasterCatalogue.OpenSearchFactorySettings;
                MultiGenericOpenSearchable multiOSE = new MultiGenericOpenSearchable(rss.Cast <IOpenSearchable>().ToList(), settings);

                result = ose.Query(multiOSE, httpRequest.QueryString, type);

                context.Close();
            }catch (Exception e) {
                context.LogError(this, e.Message, e);
                context.Close();
                throw e;
            }

            return(new HttpResult(result.SerializeToString(), result.ContentType));
        }
        public void RemoteResourceIProxiedOpenSearchable()
        {
            RemoteResourceSet set = new RemoteResourceSet(context);

            RemoteResource rr = new RemoteResource(context);

            rr.ResourceSet = set;
            set.Resources  = new RemoteResourceEntityCollection(context);
            set.Resources.Include(rr);
            rr.Location          = "http://catalogue.terradue.int/catalogue/search/MER_FRS_1P/rdf?startIndex=0&q=MER_FRS_1P&start=1992-01-01&stop=2014-10-24&bbox=-72,47,-57,58";
            set.OpenSearchEngine = ose;
            set.Identifier       = "test";

            OpenSearchDescription osd = set.GetProxyOpenSearchDescription();

            OpenSearchDescriptionUrl url = osd.Url.FirstOrDefault(p => p.Relation == "self");

            Assert.That(url != null);
            Assert.AreEqual("http://loacalhost:8877/sID/remoteresource/test/description", url.Template);

            NameValueCollection nvc = new NameValueCollection();

            nvc.Set("count", "100");

            var osr = ose.Query(set, nvc, "rdf");

            Assert.LessOrEqual(osr.Count, 100);
        }
Exemple #9
0
        public object Get(DomainSearchRequestTep request)
        {
            var context = TepWebContext.GetWebContext(PagePrivileges.EverybodyView);

            context.Open();
            context.LogInfo(this, string.Format("/domain/search GET"));

            EntityList <Domain> domains = new EntityList <Domain> (context);

            domains.Load();

            // Load the complete request
            HttpRequest httpRequest = HttpContext.Current.Request;

            OpenSearchEngine ose = MasterCatalogue.OpenSearchEngine;

            string format;

            if (Request.QueryString ["format"] == null)
            {
                format = "atom";
            }
            else
            {
                format = Request.QueryString ["format"];
            }

            Type responseType = OpenSearchFactory.ResolveTypeFromRequest(httpRequest.QueryString, httpRequest.Headers, ose);
            IOpenSearchResultCollection osr = ose.Query(domains, httpRequest.QueryString, responseType);

            OpenSearchFactory.ReplaceOpenSearchDescriptionLinks(domains, osr);

            context.Close();
            return(new HttpResult(osr.SerializeToString(), osr.ContentType));
        }
Exemple #10
0
        public object Get(ActivitySearchRequestTep request)
        {
            var context = TepWebContext.GetWebContext(PagePrivileges.UserView);

            context.AccessLevel = EntityAccessLevel.Administrator;
            context.Open();
            context.LogInfo(this, string.Format("/activity/search GET nologin='******'", request.nologin));

            EntityList <ActivityTep> activities = new EntityList <ActivityTep>(context);

            activities.AddSort("CreationTime", SortDirection.Descending);

            // Load the complete request
            HttpRequest      httpRequest = HttpContext.Current.Request;
            OpenSearchEngine ose         = MasterCatalogue.OpenSearchEngine;

            Type responseType = OpenSearchFactory.ResolveTypeFromRequest(httpRequest.QueryString, httpRequest.Headers, ose);
            IOpenSearchResultCollection osr = ose.Query(activities, httpRequest.QueryString, responseType);

            activities.Identifier = "activity";

            OpenSearchFactory.ReplaceOpenSearchDescriptionLinks(activities, osr);

            context.Close();
            return(new HttpResult(osr.SerializeToString(), osr.ContentType));
        }
Exemple #11
0
        public object Get(SearchNews request)
        {
            var context = TepWebContext.GetWebContext(PagePrivileges.EverybodyView);
            IOpenSearchResultCollection result = null;

            try{
                context.Open();
                context.LogInfo(this, string.Format("/news/search GET"));

                // Load the complete request
                HttpRequest httpRequest = HttpContext.Current.Request;

                OpenSearchEngine ose = MasterCatalogue.OpenSearchEngine;

                Type type = OpenSearchFactory.ResolveTypeFromRequest(httpRequest.QueryString, httpRequest.Headers, ose);

                List <Terradue.OpenSearch.IOpenSearchable> osentities = new List <Terradue.OpenSearch.IOpenSearchable>();

                //try{
                //    EntityList<Article> articles = new EntityList<Article>(context);
                //    articles.Load();
                //    osentities.Add(articles);
                //}catch(Exception){}

                try{
                    var twitters = TwitterNews.LoadTwitterCollection(context);
                    osentities.Add(twitters);
                }catch (Exception) {}

                try{
                    EntityList <RssNews> rsss = new EntityList <RssNews>(context);
                    rsss.Load();
                    if (rsss != null)
                    {
                        foreach (RssNews rss in rsss)
                        {
                            osentities.Add(rss);
                        }
                    }
                }catch (Exception) {}

                var settings = MasterCatalogue.OpenSearchFactorySettings;
                MultiGenericOpenSearchable multiOSE = new MultiGenericOpenSearchable(osentities, settings);

                result = ose.Query(multiOSE, httpRequest.QueryString, type);


                context.Close();
            }catch (Exception e) {
                context.LogError(this, e.Message, e);
                context.Close();
                throw e;
            }

            return(new HttpResult(result.SerializeToString(), result.ContentType));
        }
Exemple #12
0
        public void SearchAllWpsJobs()
        {
            context.AccessLevel = EntityAccessLevel.Privilege;
            var usr1 = User.FromUsername(context, "testusr1");

            context.StartImpersonation(usr1.Id);

            try {
                EntityList <WpsJob> wpsjobs = new EntityList <WpsJob>(context);

                //get all jobs
                var parameters = new NameValueCollection();
                IOpenSearchResultCollection osr = ose.Query(wpsjobs, parameters);
                Assert.AreEqual(NBJOBS_USR1_ALL, osr.TotalResults);
            } catch (Exception e) {
                Assert.Fail(e.Message);
            } finally {
                context.EndImpersonation();
            }
        }
        /// <summary>
        /// Get the specified request.
        /// </summary>
        /// <param name="request">Request.</param>
        public object Get(DataPackagesSearchRequest request)
        {
            var context = TepWebContext.GetWebContext(PagePrivileges.EverybodyView);
            IOpenSearchResultCollection result = null;

            try {
                context.Open();
                context.LogInfo(this, string.Format("/data/package/search GET"));

                EntityList <Terradue.Tep.DataPackage> datapackages = new EntityList <DataPackage>(context);
                if (!string.IsNullOrEmpty(request.Key))
                {
                    UserTep user = UserTep.FromApiKey(context, request.Key);
                    datapackages.UserId = user.Id;
                    context.AccessLevel = EntityAccessLevel.Privilege;
                }
                datapackages.SetFilter("Kind", RemoteResourceSet.KINDRESOURCESETNORMAL.ToString());

                // Load the complete request
                HttpRequest      httpRequest = HttpContext.Current.Request;
                OpenSearchEngine ose         = MasterCatalogue.OpenSearchEngine;

                // the opensearch cache system uses the query parameters
                // we add to the parameters the filters added to the load in order to avoir wrong cache
                // we use 't2-' in order to not interfer with possibly used query parameters
                var qs = new NameValueCollection(Request.QueryString);
                foreach (var filter in datapackages.FilterValues)
                {
                    qs.Add("t2-" + filter.Key.FieldName, filter.Value.ToString());
                }

                if (qs["visibility"] != null && qs["visibility"] != "all")
                {
                    datapackages.AccessLevel = EntityAccessLevel.Privilege;
                }

                if (context.UserLevel == UserLevel.Administrator && (qs["visibility"] == null || qs["visibility"] != "owned"))
                {
                    datapackages.SetFilter("Identifier", "!_index_*,!_series_*,!_products_*");
                }

                Type type = OpenSearchFactory.ResolveTypeFromRequest(httpRequest.QueryString, httpRequest.Headers, ose);
                result = ose.Query(datapackages, qs, type);

                context.Close();
            } catch (Exception e) {
                context.LogError(this, e.Message, e);
                context.Close();
                throw e;
            }

            return(new HttpResult(result.SerializeToString(), result.ContentType));
        }
Exemple #14
0
        private IOpenSearchResultCollection GetActivityResultCollection(EntityList <Activity> activities)
        {
            // Load the complete request
            HttpRequest      httpRequest = HttpContext.Current.Request;
            OpenSearchEngine ose         = MasterCatalogue.OpenSearchEngine;

            Type responseType = OpenSearchFactory.ResolveTypeFromRequest(httpRequest.QueryString, httpRequest.Headers, ose);
            IOpenSearchResultCollection osr = ose.Query(activities, httpRequest.QueryString, responseType);

            OpenSearchFactory.ReplaceOpenSearchDescriptionLinks(activities, osr);

            return(osr);
        }
Exemple #15
0
        public object Put(WpsJobUpdateRequestTep request)
        {
            var context = TepWebContext.GetWebContext(PagePrivileges.UserView);

            context.Open();
            context.LogInfo(this, string.Format("/job/wps PUT Id='{0}'", request.Id));

            WpsJob job = WpsJob.FromIdentifier(context, request.Identifier);

            try{
                job.Name = request.Name;
                job.Store();
                context.LogDebug(this, string.Format("WpsJob '{0}' updated", job.Name));
            }catch (Exception e) {
                throw e;
            }

            EntityList <WpsJob> wpsjobs = new EntityList <WpsJob>(context);

            wpsjobs.ItemVisibility = EntityItemVisibility.OwnedOnly;
            wpsjobs.Load();

            OpenSearchEngine ose = MasterCatalogue.OpenSearchEngine;

            string format;

            if (Request.QueryString["format"] == null)
            {
                format = "atom";
            }
            else
            {
                format = Request.QueryString["format"];
            }

            NameValueCollection nvc = new NameValueCollection();

            nvc.Add("id", job.Identifier);

            Type responseType = OpenSearchFactory.ResolveTypeFromRequest(HttpContext.Current.Request.QueryString, HttpContext.Current.Request.Headers, ose);
            IOpenSearchResultCollection osr = ose.Query(wpsjobs, nvc, responseType);

            OpenSearchFactory.ReplaceOpenSearchDescriptionLinks(wpsjobs, osr);

            context.Close();
            return(new HttpResult(osr.SerializeToString(), osr.ContentType));
        }
Exemple #16
0
        public object Get(WpsJobSearchRequestTep request)
        {
            var context = TepWebContext.GetWebContext(PagePrivileges.EverybodyView);

            context.Open();
            context.LogInfo(this, string.Format("/job/wps/search GET"));

            EntityList <WpsJob> wpsjobs = new EntityList <WpsJob>(context);

            wpsjobs.AddSort("Id", SortDirection.Descending);
            wpsjobs.IncludeOwnerFieldsInSearch = true;

            // Load the complete request
            HttpRequest httpRequest = HttpContext.Current.Request;
            var         qs          = new NameValueCollection(httpRequest.QueryString);

            OpenSearchEngine ose = MasterCatalogue.OpenSearchEngine;

            if (qs["visibility"] != null && qs["visibility"] != "all")
            {
                wpsjobs.AccessLevel = EntityAccessLevel.Privilege;
            }

            if (!string.IsNullOrEmpty(qs["key"]))
            {
                try{
                    UserTep user = UserTep.FromApiKey(context, qs["key"]);
                    wpsjobs.UserId      = user.Id;
                    context.AccessLevel = EntityAccessLevel.Privilege;
                }catch (Exception) {}
            }

            if (string.IsNullOrEmpty(qs["id"]) && string.IsNullOrEmpty(qs["uid"]) && string.IsNullOrEmpty(qs["archivestatus"]))
            {
                qs.Set("archivestatus", (int)WpsJobArchiveStatus.NOT_ARCHIVED + "");
            }

            Type responseType = OpenSearchFactory.ResolveTypeFromRequest(httpRequest.QueryString, httpRequest.Headers, ose);
            IOpenSearchResultCollection osr = ose.Query(wpsjobs, qs, responseType);

            OpenSearchFactory.ReplaceOpenSearchDescriptionLinks(wpsjobs, osr);
//            OpenSearchFactory.ReplaceSelfLinks(wpsjobs, httpRequest.QueryString, osr.Result, EntrySelfLinkTemplate);

            context.Close();
            return(new HttpResult(osr.SerializeToString(), osr.ContentType));
        }
        /// <summary>
        /// Get the specified request.
        /// </summary>
        /// <param name="request">Request.</param>
        public object Get(GetOpensearchSearch request)
        {
            var context = TepWebContext.GetWebContext(PagePrivileges.EverybodyView);
            IOpenSearchResultCollection result = null;

            try{
                context.Open();
                context.LogInfo(this, string.Format("/data/collection/{{serieId}}/search GET serieId='{0}'", request.serieId));

                // Load the complete request
                HttpRequest httpRequest = HttpContext.Current.Request;

                if (request.serieId == null)
                {
                    throw new ArgumentNullException(Terradue.Tep.WebServer.CustomErrorMessages.WRONG_IDENTIFIER);
                }

                Terradue.Tep.Collection serie = Terradue.Tep.Collection.FromIdentifier(context, request.serieId);

                OpenSearchEngine ose = MasterCatalogue.OpenSearchEngine;
                ose.DefaultTimeOut = 60000;

                Type type = OpenSearchFactory.ResolveTypeFromRequest(httpRequest.QueryString, httpRequest.Headers, ose);

                result = ose.Query(serie, httpRequest.QueryString, type);

                var descriptionUrl = serie.GetDescriptionBaseUrl("application/opensearchdescription+xml");
                if (descriptionUrl != null)
                {
                    result.Links.Add(new SyndicationLink(new Uri(descriptionUrl.Template), descriptionUrl.Relation, "OpenSearch Description link", descriptionUrl.Type, 0));
                }

                MasterCatalogue.ReplaceSelfLinksFormat(result, Request.QueryString);

                context.Close();
            }catch (Exception e) {
                context.LogError(this, e.Message, e);
                context.Close();
                throw e;
            }

            return(new HttpResult(result.SerializeToString(), result.ContentType));
        }
Exemple #18
0
        public object Get(ProxyGetUrlRequestTep request)
        {
            var uri  = new UriBuilder(request.url);
            var host = uri.Host;

            if (!((host.EndsWith("terradue.int") || host.EndsWith("terradue.com")) || !host.Equals(HttpContext.Current.Request.Url.Host)))
            {
                throw new Exception("Non Terradue urls are not accepted");
            }

            var        context = TepWebContext.GetWebContext(PagePrivileges.EverybodyView);
            HttpResult result  = null;

            context.Open();
            context.LogInfo(this, string.Format("/proxy GET url='{0}'", request.url));

            OpenSearchEngine ose = MasterCatalogue.OpenSearchEngine;
            var settings         = MasterCatalogue.OpenSearchFactorySettings;

            if (uri.Path.EndsWith("/description") || uri.Path.EndsWith("/OSDD"))
            {
                GenericOpenSearchable urlToShare = new GenericOpenSearchable(new OpenSearchUrl(request.url), settings);
                Terradue.OpenSearch.Schema.OpenSearchDescription osd = urlToShare.GetOpenSearchDescription();
                result = new HttpResult(osd, "application/opensearchdescription+xml");
            }
            else
            {
                var e = OpenSearchFactory.FindOpenSearchable(settings, new Uri(request.url), "application/atom+xml");
                if (e.DefaultMimeType != "application/atom+xml" && !e.DefaultMimeType.Contains("xml"))
                {
                    throw new InvalidOperationException("No Url in the OpenSearch Description Document that query fully qualified model");
                }

                Type type = OpenSearchFactory.ResolveTypeFromRequest(HttpContext.Current.Request.QueryString, HttpContext.Current.Request.Headers, ose);
                var  res  = ose.Query(e, new NameValueCollection(), type);

                result = new HttpResult(res.SerializeToString(), res.ContentType);
            }

            context.Close();
            return(result);
        }
        public void SearchDataPackage()
        {
            Terradue.Tep.DataPackage datapackage = DataPackage.GetTemporaryForCurrentUser(context);
            datapackage.LoadItems();
            foreach (RemoteResource res in datapackage.Resources)
            {
                res.Delete();
            }
            datapackage.LoadItems();
            Assert.AreEqual(0, datapackage.Items.Count);

            var ressourceItem = new RemoteResource(context);

            ressourceItem.Location = "https://catalog.terradue.com:443/sentinel1/search?uid=S1A_EW_OCN__2SDH_20200108T051332_20200108T051348_030704_038509_0C1C";
            datapackage.AddResourceItem(ressourceItem);

            ressourceItem          = new RemoteResource(context);
            ressourceItem.Location = "https://catalog.terradue.com:443/sentinel1/search?uid=S1A_IW_RAW__0SSH_20200108T050838_20200108T050910_030704_038508_ED48";
            datapackage.AddResourceItem(ressourceItem);

            ressourceItem          = new RemoteResource(context);
            ressourceItem.Location = "https://catalog.terradue.com:443/sentinel1/search?uid=S1A_IW_OCN__2SDV_20200108T043336_20200108T043412_030704_038504_3458";
            datapackage.AddResourceItem(ressourceItem);

            datapackage.LoadItems();
            Assert.AreEqual(3, datapackage.Items.Count);

            datapackage.SetOpenSearchEngine(MasterCatalogue.OpenSearchEngine);

            List <Terradue.OpenSearch.IOpenSearchable> osentities = new List <Terradue.OpenSearch.IOpenSearchable>();

            osentities.AddRange(datapackage.GetOpenSearchableArray());

            var settings = MasterCatalogue.OpenSearchFactorySettings;
            MultiGenericOpenSearchable multiOSE = new MultiGenericOpenSearchable(osentities, settings, true);

            var parameters = new NameValueCollection();

            IOpenSearchResultCollection osr = ose.Query(multiOSE, parameters);

            Assert.AreEqual(3, osr.TotalResults);
        }
Exemple #20
0
        public object Post(TypeImportRequest request)
        {
            OpenSearchEngine ose = new OpenSearchEngine();

            ose.LoadPlugins();
            ose.DefaultTimeOut = 60000;

            OpenSearchUrl url = new OpenSearchUrl(request.url);

            IOpenSearchable entity = new GenericOpenSearchable(url, ose);

            IOpenSearchableElasticType type = ecf.GetOpenSearchableElasticTypeByNameOrDefault(request.IndexName, request.TypeName);

            IOpenSearchResultCollection osres = ose.Query(entity, new NameValueCollection());

            OpenSearchFactory.RemoveLinksByRel(ref osres, "alternate");
            OpenSearchFactory.RemoveLinksByRel(ref osres, "via");
            OpenSearchFactory.RemoveLinksByRel(ref osres, "self");
            OpenSearchFactory.RemoveLinksByRel(ref osres, "search");

            IElasticCollection documents = type.FromOpenSearchResultCollection(osres);

            BulkRequest bulkRequest = new BulkRequest()
            {
                Refresh     = true,
                Consistency = Consistency.One,
                Index       = request.IndexName
            };

            foreach (var doc in documents.Items)
            {
                bulkRequest.Operations.Add(new BulkIndexOperation <IElasticItem>((IElasticItem)doc)
                {
                    Id = doc.Id
                });
            }

            var response = client.Bulk(bulkRequest);

            return(response);
        }
        public static IOpenSearchResultCollection QueryResult(IOpenSearchableElasticType type, NameValueCollection parameters, Type resultType = null)
        {
            OpenSearchEngine ose = type.GetOpenSearchEngine(parameters);

            if (resultType == null)
            {
                resultType = OpenSearchFactory.ResolveTypeFromRequest(HttpContext.Current.Request, ose);
            }

            if (resultType == typeof(ParametersResult))
            {
                return(type.DescribeParameters());
            }

            var result = ose.Query(type, parameters, resultType);

            OpenSearchFactory.ReplaceSelfLinks(type, parameters, result, type.EntrySelfLinkTemplate, result.ContentType);
            OpenSearchFactory.ReplaceOpenSearchDescriptionLinks(type, result);

            result.Title = new TextSyndicationContent(string.Format("Result for OpenSearch query over type {0} in index {1}", type.Type.Name, type.Index.Name));

            return(result);
        }
        /// <summary>
        /// Get the specified request.
        /// </summary>
        /// <param name="request">Request.</param>
        /// /apps/{identifier}/search GET
        public object Get(ThematicAppSearchRequestTep request)
        {
            IfyWebContext context = TepWebContext.GetWebContext(PagePrivileges.EverybodyView);

            context.Open();
            context.LogInfo(this, string.Format("/apps/search GET -- cache = {0}", request.cache));

            IOpenSearchResultCollection result;
            OpenSearchEngine            ose = MasterCatalogue.OpenSearchEngine;
            HttpRequest httpRequest         = HttpContext.Current.Request;
            Type        responseType        = OpenSearchFactory.ResolveTypeFromRequest(httpRequest.QueryString, httpRequest.Headers, ose);

            //first we get the communities the user can see
            var communities = new EntityList <ThematicCommunity>(context);

            if (context.UserId == 0)
            {
                communities.SetFilter("Kind", (int)DomainKind.Public + "");
            }
            else
            {
                communities.SetFilter("Kind", (int)DomainKind.Public + "," + (int)DomainKind.Hidden + "," + (int)DomainKind.Private);
                communities.AddSort("Kind", SortDirection.Ascending);
            }
            communities.Load();

            if (request.cache)
            {
                List <int> ids = new List <int>();
                foreach (var c in communities)
                {
                    if (c.IsUserJoined(context.UserId))
                    {
                        ids.Add(c.Id);
                    }
                }

                EntityList <ThematicApplicationCached> appsCached = new EntityList <ThematicApplicationCached>(context);
                var filterValues = new List <object>();
                filterValues.Add(string.Join(",", ids));
                filterValues.Add(SpecialSearchValue.Null);
                appsCached.SetFilter("DomainId", filterValues.ToArray());
                appsCached.SetGroupFilter("UId");
                appsCached.AddSort("LastUpdate", SortDirection.Descending);

                result = ose.Query(appsCached, httpRequest.QueryString, responseType);
                OpenSearchFactory.ReplaceOpenSearchDescriptionLinks(appsCached, result);
            }
            else
            {
                List <Terradue.OpenSearch.IOpenSearchable> osentities = new List <Terradue.OpenSearch.IOpenSearchable>();

                var settings     = MasterCatalogue.OpenSearchFactorySettings;
                var specsettings = (OpenSearchableFactorySettings)settings.Clone();
                if (context.UserId != 0)
                {
                    var user     = UserTep.FromId(context, context.UserId);
                    var apikey   = user.GetSessionApiKey();
                    var t2userid = user.TerradueCloudUsername;
                    if (!string.IsNullOrEmpty(apikey))
                    {
                        specsettings.Credentials = new System.Net.NetworkCredential(t2userid, apikey);
                    }
                }

                //get apps link from the communities the user can see
                foreach (var community in communities.Items)
                {
                    if (community.IsUserJoined(context.UserId))
                    {
                        var app = community.GetThematicApplication();
                        if (app != null)
                        {
                            app.LoadItems();
                            foreach (var item in app.Items)
                            {
                                if (!string.IsNullOrEmpty(item.Location))
                                {
                                    try {
                                        var ios = OpenSearchFactory.FindOpenSearchable(specsettings, new OpenSearchUrl(item.Location));
                                        osentities.Add(ios);
                                        context.LogDebug(this, string.Format("Apps search -- Add '{0}'", item.Location));
                                    } catch (Exception e) {
                                        context.LogError(this, e.Message, e);
                                    }
                                }
                            }
                        }
                    }
                }

                //get thematic apps without any domain
                var apps = new EntityList <ThematicApplication>(context);
                apps.SetFilter("DomainId", SpecialSearchValue.Null);
                apps.SetFilter("Kind", ThematicApplication.KINDRESOURCESETAPPS + "");
                apps.Load();
                foreach (var app in apps)
                {
                    app.LoadItems();
                    foreach (var item in app.Items)
                    {
                        if (!string.IsNullOrEmpty(item.Location))
                        {
                            try {
                                var ios = OpenSearchFactory.FindOpenSearchable(specsettings, new OpenSearchUrl(item.Location));
                                osentities.Add(ios);
                                context.LogDebug(this, string.Format("Apps search -- Add '{0}'", item.Location));
                            } catch (Exception e) {
                                context.LogError(this, e.Message, e);
                            }
                        }
                    }
                }

                MultiGenericOpenSearchable multiOSE = new MultiGenericOpenSearchable(osentities, specsettings);
                result = ose.Query(multiOSE, httpRequest.QueryString, responseType);
            }

            var sresult = result.SerializeToString();

            //replace usernames in apps
            try {
                var user = UserTep.FromId(context, context.UserId);
                sresult = sresult.Replace("${USERNAME}", user.Username);
                sresult = sresult.Replace("${T2USERNAME}", user.TerradueCloudUsername);
                sresult = sresult.Replace("${T2APIKEY}", user.GetSessionApiKey());
            } catch (Exception e) {
                context.LogError(this, e.Message, e);
            }

            if (!string.IsNullOrEmpty(httpRequest.QueryString["uid"]))
            {
                try{
                    var user = UserTep.FromId(context, context.UserId);
                    EventUserLoggerTep eventLogger = new EventUserLoggerTep(context);
                    eventLogger.GetLogEvent(user, "portal_user_access_workspace", "User workspace access").ContinueWith <Event>(
                        usrevent => {
                        if (usrevent != null)
                        {
                            var callId = httpRequest.QueryString["uid"];
                            usrevent.Result.Item.Properties.Add("app_id", callId);
                            EventFactory.Log(context, usrevent.Result);
                        }
                        return(usrevent.Result);
                    }
                        );
                }catch (Exception) {}
            }

            context.Close();
            return(new HttpResult(sresult, result.ContentType));
        }
        public object Get(ThematicAppCurrentUserSearchRequestTep request)
        {
            IfyWebContext context = TepWebContext.GetWebContext(PagePrivileges.EverybodyView);

            context.Open();
            context.LogInfo(this, string.Format("/user/current/apps/search GET"));

            IOpenSearchResultCollection result;
            OpenSearchEngine            ose = MasterCatalogue.OpenSearchEngine;
            var  httpRequest  = HttpContext.Current.Request;
            Type responseType = OpenSearchFactory.ResolveTypeFromRequest(httpRequest.QueryString, httpRequest.Headers, ose);
            List <Terradue.OpenSearch.IOpenSearchable> osentities = new List <Terradue.OpenSearch.IOpenSearchable>();
            var settings = MasterCatalogue.OpenSearchFactorySettings;
            OpenSearchableFactorySettings specsettings = (OpenSearchableFactorySettings)settings.Clone();

            UserTep user = null;

            if (context.UserId != 0)
            {
                user = UserTep.FromId(context, context.UserId);
                if (request.cache)
                {
                    var domain = user.GetPrivateDomain();
                    EntityList <ThematicApplicationCached> appsCached = new EntityList <ThematicApplicationCached>(context);
                    appsCached.SetFilter("DomainId", domain.Id);
                    appsCached.AddSort("LastUpdate", SortDirection.Descending);

                    result = ose.Query(appsCached, Request.QueryString, responseType);
                    OpenSearchFactory.ReplaceOpenSearchDescriptionLinks(appsCached, result);
                }
                else
                {
                    //get user private thematic app
                    var apikey   = user.GetSessionApiKey();
                    var t2userid = user.TerradueCloudUsername;
                    if (!string.IsNullOrEmpty(apikey))
                    {
                        specsettings.Credentials = new System.Net.NetworkCredential(t2userid, apikey);
                    }
                    var app = user.GetPrivateThematicApp();
                    if (app != null)
                    {
                        foreach (var item in app.Items)
                        {
                            if (!string.IsNullOrEmpty(item.Location))
                            {
                                try {
                                    var sgOs = OpenSearchFactory.FindOpenSearchable(specsettings, new OpenSearchUrl(item.Location));
                                    osentities.Add(sgOs);
                                    context.LogDebug(this, string.Format("Apps search -- Add '{0}'", item.Location));
                                } catch (Exception e) {
                                    context.LogError(this, e.Message, e);
                                }
                            }
                        }
                    }
                    MultiGenericOpenSearchable multiOSE = new MultiGenericOpenSearchable(osentities, specsettings);
                    result = ose.Query(multiOSE, Request.QueryString, responseType);
                }
            }
            else
            {
                result = ose.Query(new MultiGenericOpenSearchable(osentities, specsettings), Request.QueryString, responseType);
            }

            string sresult = result.SerializeToString();

            //replace usernames in apps
            if (user != null)
            {
                try {
                    sresult = sresult.Replace("${USERNAME}", user.Username);
                    sresult = sresult.Replace("${T2USERNAME}", user.TerradueCloudUsername);
                    sresult = sresult.Replace("${T2APIKEY}", user.GetSessionApiKey());
                } catch (Exception e) {
                    context.LogError(this, e.Message, e);
                }
            }

            context.Close();

            return(new HttpResult(sresult, result.ContentType));
        }
        public object Get(ThematicAppByCommunitySearchRequestTep request)
        {
            IfyWebContext context = TepWebContext.GetWebContext(PagePrivileges.EverybodyView);

            context.Open();
            context.LogInfo(this, string.Format("/community/{{domain}}/apps/search GET domain='{0}'", request.Domain));

            var domain = ThematicCommunity.FromIdentifier(context, request.Domain);
            IOpenSearchResultCollection result;
            OpenSearchEngine            ose = MasterCatalogue.OpenSearchEngine;
            HttpRequest httpRequest         = HttpContext.Current.Request;
            Type        responseType        = OpenSearchFactory.ResolveTypeFromRequest(httpRequest.QueryString, httpRequest.Headers, ose);

            if (request.cache)
            {
                bool isjoined = domain.IsUserJoined(context.UserId);

                EntityList <ThematicApplicationCached> appsCached = new EntityList <ThematicApplicationCached>(context);
                if (isjoined)
                {
                    appsCached.SetFilter("DomainId", domain.Id.ToString());
                }
                else
                {
                    appsCached.SetFilter("DomainId", "-1");//if user is not joined we dont want him to see results
                }
                appsCached.AddSort("LastUpdate", SortDirection.Descending);
                result = ose.Query(appsCached, httpRequest.QueryString, responseType);
                OpenSearchFactory.ReplaceOpenSearchDescriptionLinks(appsCached, result);
            }
            else
            {
                if (MasterCatalogue.SearchCache != null)
                {
                    MasterCatalogue.SearchCache.ClearCache(".*", DateTime.Now);
                }

                var apps = new EntityList <DataPackage>(context);
                apps.SetFilter("Kind", ThematicApplication.KINDRESOURCESETAPPS.ToString());
                apps.SetFilter("DomainId", domain.Id.ToString());
                apps.Load();

                // the opensearch cache system uses the query parameters
                // we add to the parameters the filters added to the load in order to avoir wrong cache
                // we use 't2-' in order to not interfer with possibly used query parameters
                var qs = new NameValueCollection(Request.QueryString);
                foreach (var filter in apps.FilterValues)
                {
                    qs.Add("t2-" + filter.Key.FieldName, filter.Value.ToString());
                }

                apps.OpenSearchEngine = ose;

                List <Terradue.OpenSearch.IOpenSearchable> osentities = new List <Terradue.OpenSearch.IOpenSearchable>();
                foreach (var app in apps.Items)
                {
                    app.OpenSearchEngine = ose;
                    osentities.AddRange(app.GetOpenSearchableArray());
                }

                var settings = MasterCatalogue.OpenSearchFactorySettings;
                MultiGenericOpenSearchable multiOSE = new MultiGenericOpenSearchable(osentities, settings);
                result = ose.Query(multiOSE, httpRequest.QueryString, responseType);
            }

            var sresult = result.SerializeToString();

            //replace usernames in apps
            try {
                var user = UserTep.FromId(context, context.UserId);
                sresult = sresult.Replace("${USERNAME}", user.Username);
                sresult = sresult.Replace("${T2USERNAME}", user.TerradueCloudUsername);
                sresult = sresult.Replace("${T2APIKEY}", user.GetSessionApiKey());
            } catch (Exception e) {
                context.LogError(this, e.Message, e);
            }

            context.Close();
            return(new HttpResult(sresult, result.ContentType));
        }
Exemple #25
0
        public object Get(WpsJobProductSearchRequestTep request)
        {
            var        context = TepWebContext.GetWebContext(PagePrivileges.EverybodyView);
            HttpResult result  = null;

            try {
                context.Open();
                context.LogInfo(this, string.Format("/job/wps/{0}/products/search GET", request.JobId));

                WpsJob wpsjob = null;

                try {
                    wpsjob = WpsJob.FromIdentifier(context, request.JobId);
                } catch (Exception e) {
                    if (request.Key != null)                     //or if public
                    {
                        context.AccessLevel = EntityAccessLevel.Administrator;
                        wpsjob = WpsJob.FromIdentifier(context, request.JobId);
                        if (request.Key != null && !request.Key.Equals(wpsjob.AccessKey))
                        {
                            throw new UnauthorizedAccessException(CustomErrorMessages.WRONG_ACCESSKEY);
                        }
                    }
                    else
                    {
                        throw e;
                    }
                }

                OpenSearchEngine ose         = MasterCatalogue.OpenSearchEngine;
                HttpRequest      httpRequest = HttpContext.Current.Request;
                Type             type        = OpenSearchFactory.ResolveTypeFromRequest(httpRequest.QueryString, httpRequest.Headers, ose);
                var nvc = httpRequest.QueryString;

                if (CatalogueFactory.IsCatalogUrl(new Uri(wpsjob.StatusLocation)))
                {
                    var settings = MasterCatalogue.OpenSearchFactorySettings;
                    OpenSearchableFactorySettings specsettings = (OpenSearchableFactorySettings)settings.Clone();

                    //get credentials from current user
                    if (context.UserId != 0)
                    {
                        var user     = UserTep.FromId(context, context.UserId);
                        var apikey   = user.GetSessionApiKey();
                        var t2userid = user.TerradueCloudUsername;
                        if (!string.IsNullOrEmpty(apikey))
                        {
                            specsettings.Credentials = new System.Net.NetworkCredential(t2userid, apikey);
                        }
                    }
                    GenericOpenSearchable urlToShare = new GenericOpenSearchable(new OpenSearchUrl(wpsjob.StatusLocation), specsettings);
                    var res = ose.Query(urlToShare, nvc, type);
                    result = new HttpResult(res.SerializeToString(), res.ContentType);
                }
                else
                {
                    WpsJobProductOpenSearchable wpsjobProductOs = new WpsJobProductOpenSearchable(wpsjob, context);

                    //var nvc = wpsjobUrl.GetParameters ();
                    var res = ose.Query(wpsjobProductOs, nvc, type);
                    OpenSearchFactory.ReplaceSelfLinks(wpsjobProductOs, httpRequest.QueryString, res, EntrySelfLinkTemplate);
                    OpenSearchFactory.ReplaceOpenSearchDescriptionLinks(wpsjobProductOs, res);
                    result = new HttpResult(res.SerializeToString(), res.ContentType);
                }

                context.Close();
            } catch (Exception e) {
                context.LogError(this, e.Message, e);
                context.Close();
                throw e;
            }
            return(result);
        }
Exemple #26
0
        public object Get(ActivityByCommunitySearchRequestTep request)
        {
            var context = TepWebContext.GetWebContext(PagePrivileges.UserView);

            context.AccessLevel = EntityAccessLevel.Administrator;
            context.Open();
            context.LogInfo(this, string.Format("/community/{{community}}/activity/search GET - community='{0}'", request.Domain));

            ThematicCommunity domain = ThematicCommunity.FromIdentifier(context, request.Domain);

            //We only want some Privileges
            var privlist = new List <int> ();

            var privs = Privilege.Get(EntityType.GetEntityType(typeof(WpsJob)));

            foreach (var priv in privs)
            {
                privlist.Add(priv.Id);
            }

            privs = Privilege.Get(EntityType.GetEntityType(typeof(DataPackage)));
            foreach (var priv in privs)
            {
                privlist.Add(priv.Id);
            }

            privs = Privilege.Get(EntityType.GetEntityType(typeof(Series)));
            foreach (var priv in privs)
            {
                privlist.Add(priv.Id);
            }

            EntityList <ActivityTep> activities = new EntityList <ActivityTep> (context);

            activities.AddSort("CreationTime", SortDirection.Descending);
            activities.SetFilter("PrivilegeId", string.Join(",", privlist));
            activities.SetFilter("DomainId", domain.Id.ToString());
            activities.Identifier = "activity";
            activities.Load();

            // Load the complete request
            HttpRequest      httpRequest = HttpContext.Current.Request;
            OpenSearchEngine ose         = MasterCatalogue.OpenSearchEngine;

            // the opensearch cache system uses the query parameters
            // we add to the parameters the filters added to the load in order to avoir wrong cache
            // we use 't2-' in order to not interfer with possibly used query parameters
            var qs = new NameValueCollection(Request.QueryString);

            foreach (var filter in activities.FilterValues)
            {
                qs.Set("t2-" + filter.Key.FieldName, filter.Value.ToString());
            }

            Type responseType = OpenSearchFactory.ResolveTypeFromRequest(httpRequest.QueryString, httpRequest.Headers, ose);
            IOpenSearchResultCollection osr = ose.Query(activities, qs, responseType);

            OpenSearchFactory.ReplaceOpenSearchDescriptionLinks(activities, osr);

            context.Close();
            return(new HttpResult(osr.SerializeToString(), osr.ContentType));
        }
        /// <summary>
        /// Get the specified request.
        /// </summary>
        /// <param name="request">Request.</param>
        public object Get(DataPackageSearchRequest request)
        {
            var context = TepWebContext.GetWebContext(PagePrivileges.EverybodyView);
            IOpenSearchResultCollection result = null;

            try {
                context.Open();
                context.LogInfo(this, string.Format("/data/package/{{DataPackageId}}/search GET DataPackageId='{0}'", request.DataPackageId));

                Terradue.Tep.DataPackage datapackage;

                try{
                    datapackage = DataPackage.FromIdentifier(context, request.DataPackageId);
                }catch (Exception) {
                    if (request.Key != null) //or if public
                    {
                        context.AccessLevel = EntityAccessLevel.Administrator;
                        datapackage         = DataPackage.FromIdentifier(context, request.DataPackageId);
                        if (request.Key != null && !request.Key.Equals(datapackage.AccessKey))
                        {
                            throw new UnauthorizedAccessException(CustomErrorMessages.WRONG_ACCESSKEY);
                        }
                    }
                    else
                    {
                        datapackage = DataPackage.FromIdentifier(context, request.DataPackageId);
                    }
                }

                if (Request.QueryString != null && Request.QueryString["cache"] == "false")
                {
                    datapackage.SetOpenSearchEngine(MasterCatalogue.GetNewOpenSearchEngine());
                }
                else if (Request.QueryString != null && Request.QueryString["clearcache"] == "true")
                {
                    datapackage.SetOpenSearchEngine(MasterCatalogue.ClearOpenSearchEngine());
                }
                else
                {
                    datapackage.SetOpenSearchEngine(MasterCatalogue.OpenSearchEngine);
                }

                OpenSearchEngine ose = MasterCatalogue.OpenSearchEngine;

                Type responseType = OpenSearchFactory.ResolveTypeFromRequest(HttpContext.Current.Request.QueryString, HttpContext.Current.Request.Headers, ose);

                result = ose.Query(datapackage, Request.QueryString, responseType);

                var openSearchDescription = datapackage.GetLocalOpenSearchDescription();
                var uri_s = datapackage.GetSearchBaseUrl();
                OpenSearchDescriptionUrl openSearchUrlByRel = OpenSearchFactory.GetOpenSearchUrlByRel(openSearchDescription, "self");
                Uri uri_d;
                if (openSearchUrlByRel != null)
                {
                    uri_d = new Uri(openSearchUrlByRel.Template);
                }
                else
                {
                    uri_d = openSearchDescription.Originator;
                }
                if (uri_d != null)
                {
                    result.Links.Add(new SyndicationLink(uri_d, "search", "OpenSearch Description link", "application/opensearchdescription+xml", 0));
                }
                if (uri_s != null)
                {
                    result.Links.Add(new SyndicationLink(uri_s, "self", "OpenSearch Search link", "application/atom+xml", 0));
                }

                MasterCatalogue.ReplaceSelfLinksFormat(result, Request.QueryString);

                ActivityTep activity = new ActivityTep(context, datapackage, EntityOperationType.Search);
                activity.SetParam("items", result.TotalResults + "");
                activity.Store();

                context.Close();
            } catch (Exception e) {
                context.LogError(this, e.Message, e);
                context.Close();
                throw e;
            }

            return(new HttpResult(result.SerializeToString(), result.ContentType));
        }
Exemple #28
0
        public void SearchWpsServicesByTags()
        {
            WpsProvider        provider = CreateProvider("test-wps-search-1", "test provider 1", "http://dem.terradue.int:8080/wps/WebProcessingService", false);
            WpsProcessOffering process  = CreateProcess(provider, "com.test.provider.1", "test provider 1", true);

            process.AddTag("mytag");
            process.AddTag("mytag1");
            process.AddTag("mytag2");
            process.Store();

            EntityList <WpsProcessOffering> services = new EntityList <WpsProcessOffering>(context);

            var parameters = new NameValueCollection();

            parameters.Set("tag", "mytag");

            IOpenSearchResultCollection osr = ose.Query(services, parameters);

            Assert.AreEqual(1, osr.Items.Count());

            services = new EntityList <WpsProcessOffering>(context);
            parameters.Set("tag", "tag");

            osr = ose.Query(services, parameters);
            Assert.AreEqual(0, osr.Items.Count());

            services = new EntityList <WpsProcessOffering>(context);
            parameters.Set("tag", "tag,mytag");

            osr = ose.Query(services, parameters);
            Assert.AreEqual(0, osr.Items.Count());

            services = new EntityList <WpsProcessOffering>(context);
            parameters.Set("tag", "mytag,mytag1");

            osr = ose.Query(services, parameters);
            Assert.AreEqual(1, osr.Items.Count());

            provider.Delete();
        }
Exemple #29
0
        internal TestUnitResult MakeQuery(IOpenSearchable entity, FiltersDefinition fd)
        {
            List <IMetric> metrics = new List <IMetric>();

            log.DebugFormat("[{1}] > Query {0} {2}...", fd.Name, Task.CurrentId, fd.Label);

            Stopwatch sw = new Stopwatch();

            sw.Start();
            DateTimeOffset timeStart = DateTimeOffset.UtcNow;

            var parameters = fd.GetNameValueCollection();

            return(catalogue_task_factory.StartNew(() =>
            {
                return ose.Query(entity, parameters);
            }).ContinueWith <TestUnitResult>(task =>
            {
                var respTime = sw.ElapsedMilliseconds;
                Terradue.OpenSearch.Result.IOpenSearchResultCollection results = null;
                try
                {
                    results = task.Result;
                }
                catch (AggregateException e)
                {
                    log.DebugFormat("[{0}] < No results for {2}. Exception: {1}", Task.CurrentId, e.InnerException.Message, fd.Label);
                    log.Debug(e.InnerException.StackTrace);
                    metrics.Add(new ExceptionMetric(e.InnerException));
                    metrics.Add(new LongMetric(MetricName.maxTotalResults, -1, "#"));
                    metrics.Add(new LongMetric(MetricName.totalReadResults, -1, "#"));
                }
                finally
                {
                    sw.Stop();
                }
                DateTimeOffset timeStop = DateTimeOffset.UtcNow;
                metrics.Add(new DateTimeMetric(MetricName.startTime, timeStart, "dateTime"));
                metrics.Add(new DateTimeMetric(MetricName.endTime, timeStop, "dateTime"));
                metrics.Add(new StringMetric(MetricName.dataCollectionDivision, fd.Label, "string"));
                if (results != null)
                {
                    foundItems.AddRange(results.Items);

                    long serializedSize = 0;
                    try
                    {
                        serializedSize = Encoding.Default.GetBytes(results.SerializeToString()).Length;
                    }
                    catch
                    {
                    }
                    var metricsArray = results.ElementExtensions.ReadElementExtensions <Terradue.OpenSearch.Benchmarking.Metrics>("Metrics", "http://www.terradue.com/metrics", Terradue.OpenSearch.Benchmarking.MetricFactory.Serializer);
                    if (metricsArray == null || metricsArray.Count() == 0)
                    {
                        log.Warn("No query metrics found! Response Time and error rate may be biased!");
                        metrics.Add(new LongMetric(MetricName.responseTime, respTime, "ms"));
                        metrics.Add(new LongMetric(MetricName.size, serializedSize, "bytes"));
                        metrics.Add(new LongMetric(MetricName.beginGetResponseTime, timeStart.Ticks, "ticks"));
                        metrics.Add(new LongMetric(MetricName.endGetResponseTime, timeStop.Ticks, "ticks"));
                    }
                    else
                    {
                        Terradue.OpenSearch.Benchmarking.Metrics osMetrics = metricsArray.First();
                        Terradue.OpenSearch.Benchmarking.Metric _sizeMetric = osMetrics.Metric.FirstOrDefault(m => m.Identifier == "size");
                        Terradue.OpenSearch.Benchmarking.Metric _responseTimeMetric = osMetrics.Metric.FirstOrDefault(m => m.Identifier == "responseTime");
                        Terradue.OpenSearch.Benchmarking.Metric _retryNumberMetric = osMetrics.Metric.FirstOrDefault(m => m.Identifier == "retryNumber");
                        Terradue.OpenSearch.Benchmarking.Metric _beginGetResponseTime = osMetrics.Metric.FirstOrDefault(m => m.Identifier == "beginGetResponseTime");
                        Terradue.OpenSearch.Benchmarking.Metric _endGetResponseTime = osMetrics.Metric.FirstOrDefault(m => m.Identifier == "endGetResponseTime");

                        log.DebugFormat("[{4}] < {0}/{1} entries for {6} {5}. {2}bytes in {3}ms", results.Count, results.TotalResults, _sizeMetric.Value, _responseTimeMetric.Value, Task.CurrentId, fd.Label, fd.Name);
                        if (_responseTimeMetric != null)
                        {
                            metrics.Add(new LongMetric(MetricName.responseTime, Convert.ToInt64(_responseTimeMetric.Value), "ms"));
                        }
                        else
                        {
                            metrics.Add(new LongMetric(MetricName.responseTime, respTime, "ms"));
                        }

                        if (_sizeMetric != null)
                        {
                            metrics.Add(new LongMetric(MetricName.size, Convert.ToInt64(_sizeMetric.Value), "bytes"));
                        }
                        else
                        {
                            metrics.Add(new LongMetric(MetricName.size, serializedSize, "bytes"));
                        }

                        if (_retryNumberMetric != null)
                        {
                            metrics.Add(new LongMetric(MetricName.retryNumber, Convert.ToInt64(_retryNumberMetric.Value), "#"));
                        }
                        else
                        {
                            metrics.Add(new LongMetric(MetricName.retryNumber, 1, "#"));
                        }

                        if (_beginGetResponseTime != null && _endGetResponseTime != null)
                        {
                            metrics.Add(new LongMetric(MetricName.beginGetResponseTime, Convert.ToInt64(_beginGetResponseTime.Value), "ticks"));
                            metrics.Add(new LongMetric(MetricName.endGetResponseTime, Convert.ToInt64(_endGetResponseTime.Value), "ticks"));
                        }
                        else
                        {
                            metrics.Add(new LongMetric(MetricName.beginGetResponseTime, timeStart.Ticks, "ticks"));
                            metrics.Add(new LongMetric(MetricName.endGetResponseTime, timeStop.Ticks, "ticks"));
                        }
                    }
                    metrics.Add(new LongMetric(MetricName.maxTotalResults, results.TotalResults, "#"));
                    metrics.Add(new LongMetric(MetricName.totalReadResults, results.Count, "#"));

                    if (results.Count == 0)
                    {
                        if (results.TotalResults > 0 && !ignoreEmptyResult)
                        {
                            log.WarnFormat("[{1}] no entries for {2} whilst {0} totalResult. This seems to be an error.", results.TotalResults, Task.CurrentId, fd.Label);
                            metrics.Add(new ExceptionMetric(new InvalidOperationException("No entries found")));
                        }
                    }
                    else
                    {
                        try
                        {
                            Stopwatch sw3 = new Stopwatch();
                            sw3.Start();
                            metrics.AddRange(AnalyzeResults(results, fd));
                            sw3.Stop();
                            metrics.Add(new LongMetric(MetricName.analysisTime, sw3.ElapsedMilliseconds, "msec"));
                        }
                        catch (Exception e)
                        {
                            metrics.Add(new ExceptionMetric(e));
                            log.ErrorFormat("[{0}] < Analysis failed for results {2} : {1}", Task.CurrentId, e.Message, fd.Label);
                            log.Debug(e.StackTrace);
                        }
                    }
                }

                return new TestUnitResult(metrics, fd);
            }).Result);
        }