Пример #1
0
        public void RefreshCachedApp(string uid)
        {
            this.LogInfo(string.Format("RefreshCachedApps -- Get public apps"));
            var apps = new EntityList <ThematicApplicationCached>(context);

            apps.SetFilter("UId", uid);
            apps.Load();
            if (apps.Count == 1)
            {
                var app     = apps.GetItemsAsList()[0];
                var entries = app.Feed;
                if (entries == null)
                {
                    entries = ThematicAppCachedFactory.GetOwsContextAtomFeed(app.TextFeed);
                }
                var itemFeed = entries.Items.First();
                var link     = itemFeed.Links.FirstOrDefault(l => l.RelationshipType == "self");
                if (link == null)
                {
                    return;
                }
                var url   = link.Uri.AbsoluteUri;
                var urib  = new UriBuilder(url);
                var query = HttpUtility.ParseQueryString(urib.Query);

                //add user apikey
                var user   = UserTep.FromId(context, context.UserId);
                var apikey = user.GetSessionApiKey();
                if (!string.IsNullOrEmpty(apikey))
                {
                    query.Set("apikey", apikey);
                }

                //add random for cache
                var random = new Random();
                query.Set("random", random.Next() + "");
                var queryString = Array.ConvertAll(query.AllKeys, key => string.Format("{0}={1}", key, query[key]));
                urib.Query = string.Join("&", queryString);
                url        = urib.Uri.AbsoluteUri;

                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
                using (var resp = httpRequest.GetResponse())
                {
                    using (var stream = resp.GetResponseStream())
                    {
                        var feed  = GetOwsContextAtomFeed(stream);
                        var entry = feed.Items.First();
                        if (feed.Items != null && feed.Items.Count() == 1)
                        {
                            app.Feed       = feed;
                            app.TextFeed   = GetOwsContextAtomFeedAsString(feed);
                            app.Searchable = GetSearchableTextFromAtomEntry(entry);
                            app.LastUpdate = entry.LastUpdatedTime.DateTime == DateTime.MinValue ? DateTime.UtcNow : entry.LastUpdatedTime.DateTime;
                            app.Store();
                            this.LogInfo(string.Format("ThematicAppCachedFactory -- Cached '{0}'", app.UId));
                        }
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Checks the identifier availability on index.
        /// </summary>
        /// <returns><c>true</c>, if identifier exists on index, <c>false</c> otherwise.</returns>
        /// <param name="context">Context.</param>
        /// <param name="index">Index.</param>
        /// <param name="identifier">Identifier.</param>
        /// <param name="apikey">Apikey.</param>
        public static bool CheckIdentifierExists(IfyContext context, string index, string identifier, string apikey)
        {
            var baseurl = context.GetConfigValue("catalog-baseurl");

            if (string.IsNullOrEmpty(index))
            {
                throw new Exception("invalid index");
            }
            var url = (index.StartsWith("http://") || index.StartsWith("https://") ? index : baseurl + "/" + index) + "/search?uid=" + identifier + "&apikey=" + apikey;

            bool result = false;

            try {
                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
                using (var resp = httpRequest.GetResponse()) {
                    using (var stream = resp.GetResponseStream()) {
                        var feed = ThematicAppCachedFactory.GetOwsContextAtomFeed(stream);
                        if (feed.Items != null)
                        {
                            foreach (OwsContextAtomEntry item in feed.Items)
                            {
                                result = true;
                            }
                        }
                    }
                }
            } catch (Exception e) {
                context.LogError(context, string.Format("CheckIdentifierExists -- {0} - {1}", e.Message, e.StackTrace));
            }
            return(result);
        }
Пример #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:Terradue.Tep.ThematicApplicationCached"/> class.
 /// </summary>
 /// <param name="context">Context.</param>
 /// <param name="identifier">Identifier.</param>
 /// <param name="domainid">Domainid.</param>
 /// <param name="feed">Feed.</param>
 public ThematicApplicationCached(IfyContext context, string identifier, int domainid, string feed) : base(context)
 {
     this.UId      = identifier;
     this.DomainId = domainid;
     this.TextFeed = feed;
     this.Feed     = ThematicAppCachedFactory.GetOwsContextAtomFeed(feed);
 }
Пример #4
0
        public static List <OwsContextAtomEntry> GetRemoteWpsServiceEntriesFromUrl(IfyContext context, string href)
        {
            var result      = new List <OwsContextAtomEntry>();
            var httpRequest = (HttpWebRequest)WebRequest.Create(href);

            if (context.UserId != 0)
            {
                var user   = UserTep.FromId(context, context.UserId);
                var apikey = user.GetSessionApiKey();
                if (!string.IsNullOrEmpty(user.Username) && !string.IsNullOrEmpty(apikey))
                {
                    httpRequest.Headers.Set(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(user.Username + ":" + apikey)));
                }
            }

            //TODO: manage case of /description
            //TODO: manage case of total result > 20
            using (var resp = httpRequest.GetResponse()) {
                using (var stream = resp.GetResponseStream()) {
                    var feed = ThematicAppCachedFactory.GetOwsContextAtomFeed(stream);
                    if (feed.Items != null)
                    {
                        foreach (OwsContextAtomEntry item in feed.Items)
                        {
                            result.Add(item);
                        }
                    }
                }
            }
            return(result);
        }
Пример #5
0
        /// <summary>
        /// Synchronize WPS services
        /// </summary>
        /// <param name="context"></param>
        /// <param name="appcached"></param>
        public static void SyncWpsServices(IfyContext context, ThematicApplicationCached appcached)
        {
            context.LogDebug(context, string.Format("Sync WPS app : {0}/{1}", appcached.Index, appcached.UId));

            //get app self url
            var feed  = ThematicAppCachedFactory.GetOwsContextAtomFeed(appcached.TextFeed);
            var entry = feed.Items.First();

            SyncWpsServices(context, entry);
        }
Пример #6
0
        public List <KeyValuePair <string, string> > GetDataCollectionsFromApps()
        {
            List <KeyValuePair <string, string> > result = new List <KeyValuePair <string, string> >();
            var currentContextAccessLevel = Context.AccessLevel;

            Context.AccessLevel = EntityAccessLevel.Administrator;
            var eodata         = new List <string>();
            var eobasedproduct = new List <string>();

            try {
                EntityList <ThematicApplicationCached> appsCached = new EntityList <ThematicApplicationCached>(Context);
                appsCached.Load();
                foreach (var item in appsCached.Items)
                {
                    var feed = ThematicAppCachedFactory.GetOwsContextAtomFeed(item.TextFeed);
                    if (feed.Items != null)
                    {
                        foreach (var entry in feed.Items)
                        {
                            var offering = entry.Offerings.First(p => p.Code == "http://www.terradue.com/spec/owc/1.0/req/atom/opensearch");
                            if (offering.Operations != null)
                            {
                                foreach (var operation in offering.Operations)
                                {
                                    if (operation.Any != null && operation.Any.Length > 0)
                                    {
                                        foreach (var any in operation.Any)
                                        {
                                            switch (any.Name)
                                            {
                                            case "datacontext":
                                                var context      = any.InnerText;
                                                var contextsplit = any.InnerText.Split('/');
                                                if (contextsplit.Length > 1)
                                                {
                                                    var menu = contextsplit[0];
                                                    menu = menu.ToLower();
                                                    var collection = contextsplit[1];
                                                    switch (menu)
                                                    {
                                                    case "eo-data":
                                                    case "eo data":
                                                        if (!collection.Equals("*") && !eodata.Contains(collection))
                                                        {
                                                            eodata.Add(collection);
                                                        }
                                                        break;

                                                    case "eo-based products":
                                                    case "eo based products":
                                                        if (!eobasedproduct.Contains(collection))
                                                        {
                                                            eobasedproduct.Add(collection);
                                                        }
                                                        break;

                                                    default:
                                                        break;
                                                    }
                                                }
                                                break;

                                            default:
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                Context.AccessLevel = currentContextAccessLevel;
                throw e;
            }
            Context.AccessLevel = currentContextAccessLevel;
            if (eodata.Count > 0)
            {
                eodata.Sort();
                result.Add(new KeyValuePair <string, string>("EO Data", string.Join(",", eodata)));
            }
            if (eobasedproduct.Count > 0)
            {
                eobasedproduct.Sort();
                result.Add(new KeyValuePair <string, string>("EO-based products", string.Join(",", eobasedproduct)));
            }
            return(result);
        }
Пример #7
0
        public override object GetFilterForParameter(string parameter, string value)
        {
            switch (parameter)
            {
            case "correlatedTo":
                var settings = MasterCatalogue.OpenSearchFactorySettings;
                var urlBOS   = new UrlBasedOpenSearchable(context, new OpenSearchUrl(value), settings);
                var entity   = urlBOS.Entity;
                if (entity is EntityList <ThematicApplicationCached> )
                {
                    var entitylist = entity as EntityList <ThematicApplicationCached>;
                    var items      = entitylist.GetItemsAsList();
                    if (items.Count > 0)
                    {
                        var feed = ThematicAppCachedFactory.GetOwsContextAtomFeed(items[0].TextFeed);
                        if (feed != null)
                        {
                            var entry = feed.Items.First();
                            foreach (var offering in entry.Offerings)
                            {
                                switch (offering.Code)
                                {
                                case "http://www.opengis.net/spec/owc/1.0/req/atom/wps":
                                    if (offering.Operations != null && offering.Operations.Length > 0)
                                    {
                                        foreach (var operation in offering.Operations)
                                        {
                                            var href = operation.Href;
                                            switch (operation.Code)
                                            {
                                            case "ListProcess":
                                                var result = new List <KeyValuePair <string, string> >();
                                                var uri    = new Uri(href);
                                                var nvc    = HttpUtility.ParseQueryString(uri.Query);
                                                foreach (var key in nvc.AllKeys)
                                                {
                                                    switch (key)
                                                    {
                                                    case "domain":
                                                        if (nvc[key] != null)
                                                        {
                                                            string domainIdentifier = null;
                                                            if (nvc[key].Contains("${USERNAME}"))
                                                            {
                                                                var user = UserTep.FromId(context, context.UserId);
                                                                user.LoadCloudUsername();
                                                                domainIdentifier = nvc[key].Replace("${USERNAME}", user.TerradueCloudUsername);
                                                            }
                                                            else
                                                            {
                                                                domainIdentifier = nvc[key];
                                                            }
                                                            if (!string.IsNullOrEmpty(domainIdentifier))
                                                            {
                                                                var domain = Domain.FromIdentifier(context, domainIdentifier);
                                                                result.Add(new KeyValuePair <string, string>("DomainId", domain.Id + ""));
                                                            }
                                                        }
                                                        break;

                                                    case "tag":
                                                        if (!string.IsNullOrEmpty(nvc[key]))
                                                        {
                                                            var tags = nvc[key].Split(",".ToArray());
                                                            IEnumerable <IEnumerable <string> > permutations = GetPermutations(tags, tags.Count());
                                                            var r1         = permutations.Select(subset => string.Join("*", subset.Select(t => t).ToArray())).ToArray();
                                                            var tagsresult = string.Join(",", r1.Select(t => "*" + t + "*"));
                                                            result.Add(new KeyValuePair <string, string>("Tags", tagsresult));
                                                        }
                                                        break;

                                                    default:
                                                        break;
                                                    }
                                                }
                                                return(result);

                                            default:
                                                break;
                                            }
                                        }
                                    }
                                    break;

                                default:
                                    break;
                                }
                            }
                        }
                    }
                }
                return(new KeyValuePair <string, string>("DomainId", "-1"));  //we don't want any result to be returned, as no service is returned to the app (no wps search link)

            default:
                return(base.GetFilterForParameter(parameter, value));
            }
        }