Exemplo n.º 1
0
        /// <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));
        }
Exemplo n.º 2
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));
        }
Exemplo n.º 3
0
        public void SearchRssFeeds()
        {
            var ose      = MasterCatalogue.OpenSearchEngine;
            var settings = new OpenSearchableFactorySettings(ose);
            List <Terradue.OpenSearch.IOpenSearchable> osentities = new List <Terradue.OpenSearch.IOpenSearchable>();
            var parameters = new NameValueCollection();

            parameters.Set("count", "20");

            try {
                EntityList <RssNews> rsss = new EntityList <RssNews>(context);
                rsss.Load();
                if (rsss != null)
                {
                    foreach (RssNews rss in rsss)
                    {
                        osentities.Add(rss);
                    }
                }
                MultiGenericOpenSearchable  multiOSE = new MultiGenericOpenSearchable(osentities, settings);
                IOpenSearchResultCollection osr      = ose.Query(multiOSE, parameters);
                Assert.That(osr.TotalResults > 0);
            } catch (Exception e) {
                Assert.Fail(e.Message);
            } finally {
                context.EndImpersonation();
            }
        }
Exemplo n.º 4
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));
        }
Exemplo n.º 5
0
        public virtual IOpenSearchable CreateOpenSearchable(IEnumerable <Uri> baseUrls, string queryFormatArg, OpenSearchEngine ose, IEnumerable <NetworkCredential> netCreds, OpenSearchableFactorySettings settings)
        {
            List <IOpenSearchable> entities = new List <IOpenSearchable>();

            for (int i = 0; i < baseUrls.Count(); i++)
            {
                var url = baseUrls.ElementAt(i);

                settings.Credentials = netCreds == null ? null : netCreds.ElementAt(i);

                if (string.IsNullOrEmpty(queryFormatArg))
                {
                    try
                    {
                        entities.Add(OpenSearchFactory.FindOpenSearchable(settings, url, null));
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
                else
                {
                    var e = OpenSearchFactory.FindOpenSearchable(settings, url, ose.GetExtensionByExtensionName(queryFormatArg).DiscoveryContentType);
                    entities.Add(e);
                }
            }

            IOpenSearchable entity;
            OpenSearchableFactorySettings settings2 = new OpenSearchableFactorySettings(ose);

            if (entities.Count > 1)
            {
                entity = new MultiGenericOpenSearchable(entities, settings2);
            }
            else
            {
                entity = entities[0];
            }

            return(entity);
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        public void CacheTest1()
        {
            TestOpenSearchable entity1 = TestOpenSearchable.GenerateNumberedItomFeed("A", 100, new TimeSpan(0));

            ose.Query(entity1, new NameValueCollection());

            List<IOpenSearchable> entities = new List<IOpenSearchable>();
            entities.Add(entity1);

            entity1.Items.First().Identifier = "AA1";
            entity1.OnOpenSearchableChange(this, new OnOpenSearchableChangeEventArgs(entity1));

            ose.Query(entity1, new NameValueCollection());

            Thread.Sleep(1000);

            ose.Query(entity1, new NameValueCollection());

            IOpenSearchable multiEntity = new MultiGenericOpenSearchable(entities, ose, true);
        }
Exemplo n.º 8
0
        private static List <WpsServiceOverview> GetWpsServiceOverview(IfyContext context, OwcOffering offering, string appUid, string appTitle, string appIcon)
        {
            List <WpsServiceOverview> wpsOverviews = new List <WpsServiceOverview>();

            if (offering != null)
            {
                if (offering.Operations != null)
                {
                    foreach (var ops in offering.Operations)
                    {
                        if (ops.Code == "ListProcess")
                        {
                            var href = ops.Href;
                            //replace usernames in apps
                            try {
                                var user = UserTep.FromId(context, context.UserId);
                                href = href.Replace("${USERNAME}", user.Username);
                                href = href.Replace("${T2USERNAME}", user.TerradueCloudUsername);
                                href = href.Replace("${T2APIKEY}", user.GetSessionApiKey());
                            } catch (Exception e) {
                                context.LogError(context, e.Message);
                            }
                            var uri = new Uri(href.Replace("file://", context.BaseUrl));
                            var nvc = HttpUtility.ParseQueryString(uri.Query);
                            nvc.Set("count", "100");
                            Terradue.OpenSearch.Engine.OpenSearchEngine ose = MasterCatalogue.OpenSearchEngine;
                            var responseType = ose.GetExtensionByExtensionName("atom").GetTransformType();
                            EntityList <WpsProcessOffering> wpsProcesses = new EntityList <WpsProcessOffering>(context);
                            wpsProcesses.SetFilter("Available", "true");
                            wpsProcesses.OpenSearchEngine = ose;
                            wpsProcesses.Identifier       = string.Format("servicewps-{0}", context.Username);

                            CloudWpsFactory wpsOneProcesses = new CloudWpsFactory(context);
                            wpsOneProcesses.OpenSearchEngine = ose;

                            wpsProcesses.Identifier = "service/wps";
                            var entities = new List <IOpenSearchable> {
                                wpsProcesses, wpsOneProcesses
                            };

                            var settings = MasterCatalogue.OpenSearchFactorySettings;
                            MultiGenericOpenSearchable  multiOSE = new MultiGenericOpenSearchable(entities, settings);
                            IOpenSearchResultCollection osr      = ose.Query(multiOSE, nvc, responseType);

                            OpenSearchFactory.ReplaceOpenSearchDescriptionLinks(wpsProcesses, osr);

                            foreach (var itemWps in osr.Items)
                            {
                                string uid         = "";
                                var    identifiers = itemWps.ElementExtensions.ReadElementExtensions <string>("identifier", "http://purl.org/dc/elements/1.1/");
                                if (identifiers.Count > 0)
                                {
                                    uid = identifiers[0];
                                }
                                string description = "";
                                if (itemWps.Content is TextSyndicationContent)
                                {
                                    var content = itemWps.Content as TextSyndicationContent;
                                    description = content.Text;
                                }
                                string version  = "";
                                var    versions = itemWps.ElementExtensions.ReadElementExtensions <string>("version", "https://www.terradue.com/");
                                if (versions.Count > 0)
                                {
                                    version = versions[0];
                                }
                                string publisher  = "";
                                var    publishers = itemWps.ElementExtensions.ReadElementExtensions <string>("publisher", "http://purl.org/dc/elements/1.1/");
                                if (publishers.Count > 0)
                                {
                                    publisher = publishers[0];
                                }
                                var serviceUrl = new UriBuilder(context.GetConfigValue("BaseUrl"));
                                serviceUrl.Path  = "/t2api/service/wps/search";
                                serviceUrl.Query = "id=" + uid;
                                var url = new UriBuilder(context.GetConfigValue("BaseUrl"));
                                url.Path  = "t2api/share";
                                url.Query = "url=" + HttpUtility.UrlEncode(serviceUrl.Uri.AbsoluteUri) + "&id=" + appUid;
                                var icon = itemWps.Links.FirstOrDefault(l => l.RelationshipType == "icon");
                                //entry.Links.Add(new SyndicationLink(new Uri(this.IconUrl), "icon", null, null, 0));
                                wpsOverviews.Add(new WpsServiceOverview {
                                    Identifier = uid,
                                    App        = new AppOverview {
                                        Icon  = appIcon ?? "",
                                        Title = appTitle,
                                        Uid   = appUid
                                    },
                                    Name        = itemWps.Title != null ? itemWps.Title.Text : uid,
                                    Description = description,
                                    Version     = version,
                                    Provider    = publisher,
                                    Url         = url.Uri.AbsoluteUri,
                                    Icon        = icon != null ? icon.Uri.AbsoluteUri : ""
                                });
                            }
                        }
                    }
                }
            }
            return(wpsOverviews);
        }
Exemplo n.º 9
0
        public override IOpenSearchable CreateOpenSearchable(IEnumerable <Uri> baseUrls, string queryFormatArg, OpenSearchEngine ose, IEnumerable <NetworkCredential> netCreds, OpenSearchableFactorySettings settings)
        {
            List <IOpenSearchable> entities = new List <IOpenSearchable>();

            IOpenSearchEngineExtension ext;

            if (string.IsNullOrEmpty(queryFormatArg))
            {
                ext = ose.GetExtensionByExtensionName("atom");
            }
            else
            {
                ext = ose.GetExtensionByExtensionName(queryFormatArg);
            }

            for (int i = 0; i < baseUrls.Count(); i++)
            {
                var url = baseUrls.ElementAt(i);

                settings.Credentials = netCreds == null ? null : netCreds.ElementAt(i);

                IOpenSearchable e = null;
                // QC Sentinel1 case
                if (url.Host == "qc.sentinel1.eo.esa.int")
                {
                    log.DebugFormat("QC Sentinel1 source. Trying to get the earthobservation profile");
                    e = new Sentinel1QcOpenSearchable(url, settings.OpenSearchEngine);
                    entities.Add(e);
                    continue;
                }
                // USGS case
                if (url.Host == "earthexplorer.usgs.gov")
                {
                    log.DebugFormat("Usgs source. Trying to get the earthobservation profile");

                    // usgsOpenSearchable
                    e = new Terradue.OpenSearch.Usgs.UsgsOpenSearchable(url, settings);
                    entities.Add(e);
                    continue;
                }
                e = OpenSearchFactory.FindOpenSearchable(settings, url, ext.DiscoveryContentType);
                if (!e.DefaultMimeType.Contains("profile=http://earth.esa.int/eop"))
                {
                    try {
                        e = OpenSearchFactory.FindOpenSearchable(settings, url, "application/atom+xml; profile=http://earth.esa.int/eop/2.1");
                    } catch (InvalidOperationException) {
                        e = OpenSearchFactory.FindOpenSearchable(settings, url, "application/atom+xml");
                    }
                    if (!e.DefaultMimeType.Contains("xml"))
                    {
                        throw new InvalidOperationException("No Url in the OpenSearch Description Document that could fit the EOP data model");
                    }
                }
                // Fedeo case
                if (url.Host == "fedeo.esa.int" && e.DefaultMimeType == "application/atom+xml" && e is Terradue.OpenSearch.GenericOpenSearchable)
                {
                    log.DebugFormat("Fedeo source. Trying to get the earthobservation profile");
                    e = FedeoOpenSearchable.CreateFrom(url, settings.OpenSearchEngine);
                }
                // Cwic case
                if (url.Host == "cwic.wgiss.ceos.org" && e.DefaultMimeType == "application/atom+xml" && e is Terradue.OpenSearch.GenericOpenSearchable)
                {
                    log.DebugFormat("Cwic source. Trying to get the earthobservation profile");
                    e = CwicOpenSearchable.CreateFrom((Terradue.OpenSearch.GenericOpenSearchable)e, settings.OpenSearchEngine);
                }



                entities.Add(e);
            }

            IOpenSearchable entity;

            OpenSearchableFactorySettings settings2 = new OpenSearchableFactorySettings(ose);

            if (entities.Count > 1)
            {
                entity = new MultiGenericOpenSearchable(entities, settings2);
            }
            else
            {
                entity = entities[0];
            }

            return(entity);
        }
Exemplo n.º 10
0
        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));
        }
Exemplo n.º 11
0
        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));
        }
        public void StartIndexMultiOpenSearchableTest()
        {
            AddinManager.Initialize();
            AddinManager.Registry.Update(null);

            OpenSearchEngine ose = new OpenSearchEngine();
            ose.LoadPlugins();

            IOpenSearchable entity1 = TestOpenSearchable.GenerateNumberedItomFeed("A", 4, new TimeSpan(0));

            List<IOpenSearchable> entities = new List<IOpenSearchable>();
            entities.Add(entity1);

            IOpenSearchable multiEntity = new MultiGenericOpenSearchable(entities, ose, true);

            NameValueCollection nvc = new NameValueCollection();

            nvc = new NameValueCollection();
            nvc.Set("count", "2");
            nvc.Set("startIndex", "5");

            var osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(4, osr.TotalResults);
            Assert.AreEqual(0, osr.Count);

            nvc.Set("count", "2");
            nvc.Set("startIndex", "4");

            osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(4, osr.TotalResults);
            Assert.AreEqual(1, osr.Count);
            Assert.AreEqual("A4", osr.Items.First().Identifier);
            Assert.AreEqual("A4", osr.Items.Last().Identifier);

            nvc.Set("count", "2");
            nvc.Set("startIndex", "3");

            osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(4, osr.TotalResults);
            Assert.AreEqual(2, osr.Count);
            Assert.AreEqual("A3", osr.Items.First().Identifier);
            Assert.AreEqual("A4", osr.Items.Last().Identifier);
        }
        public void GenericProxiedOpenSearchableTest()
        {
            AddinManager.Initialize();
            AddinManager.Registry.Update(null);

            OpenSearchEngine ose = new OpenSearchEngine();
            ose.LoadPlugins();

            IOpenSearchable entity1 = TestOpenSearchable.GenerateNumberedItomFeed("A", 100, new TimeSpan(0));

            List<IOpenSearchable> entities = new List<IOpenSearchable>();
            entities.Add(entity1);

            IOpenSearchable multiEntity = new MultiGenericOpenSearchable(entities, ose, true);

            NameValueCollection nvc = new NameValueCollection();

            var osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(100, osr.TotalResults);
            Assert.AreEqual(OpenSearchEngine.DEFAULT_COUNT, osr.Count);
            string totalResults = osr.ElementExtensions.ReadElementExtensions<string>("totalResults", "http://a9.com/-/spec/opensearch/1.1/")[0];
            Assert.AreEqual("100", totalResults);

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

            osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(100, osr.TotalResults);
            Assert.AreEqual(100, osr.Count);
            Assert.AreEqual("A1", osr.Items.First().Identifier);
            Assert.AreEqual("A100", osr.Items.Last().Identifier);

            nvc.Set("count", "6");
            nvc.Set("startPage", "1");

            osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(100, osr.TotalResults);
            Assert.AreEqual(6, osr.Count);
            Assert.AreEqual("A1", osr.Items.First().Identifier);
            Assert.AreEqual("A6", osr.Items.Last().Identifier);

            nvc.Set("count", "3");
            nvc.Set("startPage", "2");

            osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(100, osr.TotalResults);
            Assert.AreEqual(3, osr.Count);
            Assert.AreEqual("A4", osr.Items.First().Identifier);
            Assert.AreEqual("A6", osr.Items.Last().Identifier);

            nvc.Set("startIndex", "16");
            nvc.Set("startPage", "1");
            nvc.Set("count", "100");

            osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(85, osr.Count);
            Assert.AreEqual("A16", osr.Items.First().Identifier);
            Assert.AreEqual("A100", osr.Items.Last().Identifier);

            nvc.Set("count", "5");
            nvc.Set("startIndex", "5");
            nvc.Set("startPage", "1");

            osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(5, osr.Count);
            Assert.AreEqual("A5", osr.Items.First().Identifier);
            Assert.AreEqual("A9", osr.Items.Last().Identifier);

            nvc.Set("count", "5");
            nvc.Set("startIndex", "5");
            nvc.Set("startPage", "2");

            osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(5, osr.Count);
            Assert.AreEqual("A10", osr.Items.First().Identifier);
            Assert.AreEqual("A14", osr.Items.Last().Identifier);

            nvc.Set("count", "5");
            nvc.Set("startIndex", "5");
            nvc.Set("startPage", "4");

            osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(5, osr.Count);
            Assert.AreEqual("A20", osr.Items.First().Identifier);
            Assert.AreEqual("A24", osr.Items.Last().Identifier);

            nvc.Set("count", "5");
            nvc.Set("startIndex", "5");
            nvc.Set("startPage", "5");

            osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(5, osr.Count);
            Assert.AreEqual("A25", osr.Items.First().Identifier);
            Assert.AreEqual("A29", osr.Items.Last().Identifier);

            nvc.Set("count", "5");
            nvc.Set("startIndex", "1");
            nvc.Set("startPage", "1");

            osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(5, osr.Count);
            Assert.AreEqual("A1", osr.Items.First().Identifier);
            Assert.AreEqual("A5", osr.Items.Last().Identifier);

            nvc.Set("count", "5");
            nvc.Set("startIndex", "1");
            nvc.Set("startPage", "5");

            osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(5, osr.Count);
            Assert.AreEqual("A21", osr.Items.First().Identifier);
            Assert.AreEqual("A25", osr.Items.Last().Identifier);

            IOpenSearchable entity2 = TestOpenSearchable.GenerateNumberedItomFeed("B", 100, TimeSpan.FromHours(-1));

            entities.Add(entity2);
            multiEntity = new MultiGenericOpenSearchable(entities, ose, true);

            nvc = new NameValueCollection();
            nvc.Set("count", "10");

            osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(200, osr.TotalResults);
            Assert.AreEqual(10, osr.Count);
            Assert.AreEqual("A1", osr.Items.First().Identifier);
            Assert.AreEqual("B5", osr.Items.Last().Identifier);

            osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(200, osr.TotalResults);
            Assert.AreEqual(10, osr.Count);
            Assert.AreEqual("A1", osr.Items.First().Identifier);
            Assert.AreEqual("B5", osr.Items.Last().Identifier);

            nvc.Set("count", "4");
            nvc.Set("startIndex", "2");
            nvc.Set("startPage", "1");

            osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(4, osr.Count);
            Assert.AreEqual("B1", osr.Items.First().Identifier);
            Assert.AreEqual("A3", osr.Items.Last().Identifier);

            nvc.Set("count", "4");
            nvc.Set("startIndex", "4");
            nvc.Set("startPage", "1");

            osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(4, osr.Count);
            Assert.AreEqual("B2", osr.Items.First().Identifier);
            Assert.AreEqual("A4", osr.Items.Last().Identifier);

            nvc.Set("count", "4");
            nvc.Set("startIndex", "4");
            nvc.Set("startPage", "2");

            osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(4, osr.Count);
            Assert.AreEqual("B4", osr.Items.First().Identifier);
            Assert.AreEqual("A6", osr.Items.Last().Identifier);
        }
        public void MultiLimitTwoTest()
        {
            AddinManager.Initialize();
            AddinManager.Registry.Update(null);

            OpenSearchEngine ose = new OpenSearchEngine();
            ose.LoadPlugins();

            IOpenSearchable entity1 = TestOpenSearchable.GenerateNumberedItomFeed("A", 1, new TimeSpan(0));
            IOpenSearchable entity2 = TestOpenSearchable.GenerateNumberedItomFeed("B", 1, TimeSpan.FromHours(-1));

            List<IOpenSearchable> entities = new List<IOpenSearchable>();
            entities.Add(entity1);
            entities.Add(entity2);

            IOpenSearchable multiEntity = new MultiGenericOpenSearchable(entities, ose, true);

            NameValueCollection nvc = new NameValueCollection();

            var osr = ose.Query(multiEntity, nvc, "atom");

            Assert.AreEqual(2, osr.Count);
        }
        public virtual IOpenSearchable CreateOpenSearchable(IEnumerable<Uri> baseUrls, string queryFormatArg, OpenSearchEngine ose, IEnumerable<NetworkCredential> netCreds, bool lax)
        {
            List<IOpenSearchable> entities = new List<IOpenSearchable>();
            foreach (var url in baseUrls) {
                if (string.IsNullOrEmpty(queryFormatArg))
                    try {
                        entities.Add(OpenSearchFactory.FindOpenSearchable(ose, url, null, lax));
                    } catch (Exception e) {
                        throw e;
                    } else {
                    var e = OpenSearchFactory.FindOpenSearchable(ose, url, ose.GetExtensionByExtensionName(queryFormatArg).DiscoveryContentType, lax);
                    entities.Add(e);
                }
            }

            IOpenSearchable entity;

            if (entities.Count > 1) {
                entity = new MultiGenericOpenSearchable(entities, ose);
            } else {
                entity = entities[0];
            }

            return entity;
        }
Exemplo n.º 16
0
        /// <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 override IOpenSearchable CreateOpenSearchable(IEnumerable<Uri> baseUrls, string queryFormatArg, OpenSearchEngine ose, IEnumerable<NetworkCredential> netCreds, bool lax)
        {
            List<IOpenSearchable> entities = new List<IOpenSearchable>();

            IOpenSearchEngineExtension ext;

            if (string.IsNullOrEmpty(queryFormatArg)) {
                ext = ose.GetExtensionByExtensionName("atom");
            } else {
                ext = ose.GetExtensionByExtensionName(queryFormatArg);
            }

            foreach (var url in baseUrls) {
                IOpenSearchable e = null;
                // QC Sentinel1 case
                if (url.Host == "qc.sentinel1.eo.esa.int")
                {
                    log.DebugFormat("QC Sentinel1 source. Trying to get the earthobservation profile");
                    e = new Sentinel1QcOpenSearchable(url, ose);
                    entities.Add(e);
                    continue;
                }
                e = OpenSearchFactory.FindOpenSearchable(ose, url, ext.DiscoveryContentType, lax);
                if (!e.DefaultMimeType.Contains("profile=http://earth.esa.int/eop")) {
                    try {
                        e = OpenSearchFactory.FindOpenSearchable(ose, url, "application/atom+xml; profile=http://earth.esa.int/eop/2.1", lax);
                    } catch (InvalidOperationException){
                        e = OpenSearchFactory.FindOpenSearchable(ose, url, "application/atom+xml", lax);
                    }
                    if (!e.DefaultMimeType.Contains("xml"))
                        throw new InvalidOperationException("No Url in the OpenSearch Description Document that could fit the EOP data model");
                }
                // Fedeo case
                if (url.Host == "fedeo.esa.int" && e.DefaultMimeType == "application/atom+xml" && e is Terradue.OpenSearch.GenericOpenSearchable) {
                    log.DebugFormat("Fedeo source. Trying to get the earthobservation profile");
                    e = FedeoOpenSearchable.CreateFrom(url, ose);
                }
                // Cwic case
                if (url.Host == "cwic.wgiss.ceos.org" && e.DefaultMimeType == "application/atom+xml" && e is Terradue.OpenSearch.GenericOpenSearchable) {
                    log.DebugFormat("Cwic source. Trying to get the earthobservation profile");
                    e = CwicOpenSearchable.CreateFrom((Terradue.OpenSearch.GenericOpenSearchable)e, ose);
                }

                entities.Add(e);
            }

            IOpenSearchable entity;

            if (entities.Count > 1) {
                entity = new MultiGenericOpenSearchable(entities, ose);
            } else {
                entity = entities[0];
            }

            return entity;
        }