Ejemplo n.º 1
0
        /// <summary>
        /// Put the specified request.
        /// </summary>
        /// <param name="request">Request.</param>
        public object Put(DataPackageUpdateDefaultRequestTep request)
        {
            var context = TepWebContext.GetWebContext(PagePrivileges.UserView);
            WebDataPackageTep result;

            try{
                context.Open();
                context.LogInfo(this, string.Format("/data/package/default PUT"));
                DataPackage def = DataPackage.GetTemporaryForCurrentUser(context);
                //we want to do the copy of a datapackage into the default one
                if (def.Identifier != request.Identifier)
                {
                    foreach (RemoteResource res in def.Resources)
                    {
                        res.Delete();
                    }
                    def.LoadItems();
                    var tmp = DataPackage.FromIdentifier(context, request.Identifier);
                    foreach (RemoteResource res in tmp.Resources)
                    {
                        RemoteResource tmpres = new RemoteResource(context);
                        tmpres.Location = res.Location;
                        def.AddResourceItem(tmpres);
                    }
                    ActivityTep activity = new ActivityTep(context, tmp, EntityOperationType.View);
                    activity.SetParam("items", tmp.Resources.Count + "");
                    activity.Store();
                }
                else
                {
                    def = (DataPackage)request.ToEntity(context, def);
                }

                result = new WebDataPackageTep(def);
                context.Close();
            }catch (Exception e) {
                context.LogError(this, e.Message, e);
                context.Close();
                throw e;
            }

            return(result);
        }
Ejemplo n.º 2
0
        public object Post(ShareCreateRequestTep request)
        {
            var context = TepWebContext.GetWebContext(PagePrivileges.UserView);

            context.Open();
            context.LogInfo(this, string.Format("/share POST self='{0}',to='{1}'", request.self, request.to != null ? string.Join("", request.to) : ""));

            var settings   = MasterCatalogue.OpenSearchFactorySettings;
            var entitySelf = new UrlBasedOpenSearchable(context, new OpenSearchUrl(request.self), settings).Entity;

            //case WpsJob
            if (entitySelf is EntityList <WpsJob> )
            {
                var entitylist = entitySelf as EntityList <WpsJob>;
                var wpsjobs    = entitylist.GetItemsAsList();
                if (wpsjobs.Count == 0)
                {
                    return(new WebResponseBool(false));
                }

                //if to is null, we share publicly
                if (request.to == null)
                {
                    foreach (var job in wpsjobs)   //the entitySelf can return several entities
                    //first we remove the current sharing (in case of restricted)
                    {
                        job.RevokePermissionsFromAll(true, false);
                        if (job.Owner != null && job.DomainId != job.Owner.DomainId)
                        {
                            job.DomainId = job.Owner.DomainId;
                            job.Store();
                        }
                        //secondly we share for all
                        job.GrantPermissionsToAll();

                        //share on store
                        try {
                            DataGatewayFactory.ShareOnStore(context.GetConfigValue("SiteName"), job.StatusLocation, "results", "public");
                        } catch (Exception e) {
                            context.LogError(this, "Unable to share on STORE : " + e.Message, e);
                        }

                        //publish on community index
                        if (request.publish)
                        {
                            job.PublishToIndex(context.GetConfigValue("catalog-communityIndex"), context.GetConfigValue("catalog-communityUsername"), context.GetConfigValue("catalog-communityApikey"), request.id);
                        }

                        Activity activity = new Activity(context, job, EntityOperationType.Share);
                        activity.Store();
                    }
                }

                //we share with restriction (community + users)
                else
                {
                    foreach (var job in wpsjobs)   //the entitySelf can return several entities

                    //remove previous visibility sharing
                    {
                        job.RevokePermissionsFromAll(true, false);
                        if (job.Owner != null && job.DomainId != job.Owner.DomainId)
                        {
                            job.DomainId = job.Owner.DomainId;
                            job.Store();
                        }

                        //unpublish on community index
                        job.UnPublishFromIndex(context.GetConfigValue("catalog-communityIndex"), context.GetConfigValue("catalog-communityUsername"), context.GetConfigValue("catalog-communityApikey"));

                        var sharedUsers       = new List <string>();
                        var sharedCommunities = new List <ThematicCommunity>();

                        foreach (var to in request.to)
                        {
                            var entityTo = new UrlBasedOpenSearchable(context, new OpenSearchUrl(to), settings).Entity;

                            //case community
                            if (entityTo is EntityList <ThematicCommunity> )
                            {
                                var entityTolist = entityTo as EntityList <ThematicCommunity>;
                                var communities  = entityTolist.GetItemsAsList();
                                if (communities.Count == 0)
                                {
                                    return(new WebResponseBool(false));
                                }

                                var community = communities[0];

                                //the entitySelflist can return several entities but we only take the first one (we can share with only one community)
                                community.ShareEntity(job);
                                job.DomainId = community.Id;
                                job.Store();

                                sharedCommunities.Add(community);

                                ActivityTep activity = new ActivityTep(context, job, EntityOperationType.Share);
                                activity.AppId    = request.id;
                                activity.DomainId = communities[0].Id;
                                activity.Store();
                            }

                            //case user
                            else if (entityTo is EntityList <UserTep> )
                            {
                                var entityTolist = entityTo as EntityList <UserTep>;
                                var users        = entityTolist.GetItemsAsList();
                                if (users.Count == 0)
                                {
                                    return(new WebResponseBool(false));
                                }
                                job.GrantPermissionsToUsers(users);

                                foreach (var usr in users)
                                {
                                    if (string.IsNullOrEmpty(usr.TerradueCloudUsername))
                                    {
                                        usr.LoadCloudUsername();
                                    }
                                    if (!string.IsNullOrEmpty(usr.TerradueCloudUsername))
                                    {
                                        sharedUsers.Add(usr.TerradueCloudUsername);
                                    }
                                }

                                ActivityTep activity = new ActivityTep(context, job, EntityOperationType.Share);
                                activity.AppId = request.id;
                                activity.Store();
                            }
                        }

                        //share on store
                        try {
                            DataGatewayFactory.ShareOnStore(context.GetConfigValue("SiteName"), job.StatusLocation, "results", "restricted", sharedUsers, sharedCommunities);
                        } catch (Exception e) {
                            context.LogError(this, "Unable to share on STORE : " + e.Message, e);
                        }
                    }
                }
            }

            //case DataPackage
            else if (entitySelf is EntityList <DataPackage> )
            {
                var entitylist   = entitySelf as EntityList <DataPackage>;
                var datapackages = entitylist.GetItemsAsList();
                if (datapackages.Count == 0)
                {
                    return(new WebResponseBool(false));
                }

                //if to is null, we share publicly
                if (request.to == null)
                {
                    foreach (var dp in datapackages)   //the entitySelf can return several entities
                    {
                        dp.GrantPermissionsToAll();

                        Activity activity = new Activity(context, dp, EntityOperationType.Share);
                        activity.Store();
                    }
                }

                //we share with restriction (community + users)
                else
                {
                    foreach (var dp in datapackages)   //the entitySelf can return several entities
                    //remove previous visibility sharing
                    {
                        dp.RevokePermissionsFromAll(true, false);
                        if (dp.Owner != null && dp.DomainId != dp.Owner.DomainId)
                        {
                            dp.DomainId = dp.Owner.DomainId;
                            dp.Store();
                        }

                        foreach (var to in request.to)
                        {
                            var entityTo = new UrlBasedOpenSearchable(context, new OpenSearchUrl(to), settings).Entity;

                            //case community
                            if (entityTo is EntityList <ThematicCommunity> )
                            {
                                var entityTolist = entityTo as EntityList <ThematicCommunity>;
                                var communities  = entityTolist.GetItemsAsList();
                                if (communities.Count == 0)
                                {
                                    return(new WebResponseBool(false));
                                }
                                //the entitySelflist can return several entities but we only take the first one (we can share with only one community)
                                dp.DomainId = communities[0].Id;
                                dp.Store();

                                ActivityTep activity = new ActivityTep(context, dp, EntityOperationType.Share);
                                activity.AppId    = request.id;
                                activity.DomainId = communities[0].Id;
                                activity.Store();
                            }

                            //case user
                            else if (entityTo is EntityList <UserTep> )
                            {
                                var entityTolist = entityTo as EntityList <UserTep>;
                                var users        = entityTolist.GetItemsAsList();
                                if (users.Count == 0)
                                {
                                    return(new WebResponseBool(false));
                                }
                                dp.GrantPermissionsToUsers(users);

                                ActivityTep activity = new ActivityTep(context, dp, EntityOperationType.Share);
                                activity.AppId = request.id;
                                activity.Store();
                            }
                        }
                    }
                }
            }
            else if (entitySelf is EntityList <WpsProcessOffering> )
            {
                var entitylist = entitySelf as EntityList <WpsProcessOffering>;
                var services   = entitylist.GetItemsAsList();
                if (services.Count == 0)
                {
                    return(new WebResponseBool(false));
                }

                //if to is null, we share publicly
                if (request.to == null)
                {
                    foreach (var s in services)   //the entitySelf can return several entities
                    {
                        s.GrantPermissionsToAll();

                        Activity activity = new Activity(context, s, EntityOperationType.Share);
                        activity.Store();
                    }
                }

                //we share with restriction (community + users)
                else
                {
                    foreach (var s in services)   //the entitySelf can return several entities
                    //remove previous visibility sharing
                    {
                        s.RevokePermissionsFromAll(true, false);
                        if (s.Owner != null && s.DomainId != s.Owner.DomainId)
                        {
                            s.DomainId = s.Owner.DomainId;
                            s.Store();
                        }

                        foreach (var to in request.to)
                        {
                            var entityTo = new UrlBasedOpenSearchable(context, new OpenSearchUrl(to), settings).Entity;

                            //case community
                            if (entityTo is EntityList <ThematicCommunity> )
                            {
                                var entityTolist = entityTo as EntityList <ThematicCommunity>;
                                var communities  = entityTolist.GetItemsAsList();
                                if (communities.Count == 0)
                                {
                                    return(new WebResponseBool(false));
                                }
                                //the entitySelflist can return several entities but we only take the first one (we can share with only one community)
                                s.DomainId = communities[0].Id;
                                s.Store();

                                ActivityTep activity = new ActivityTep(context, s, EntityOperationType.Share);
                                activity.AppId    = request.id;
                                activity.DomainId = communities[0].Id;
                                activity.Store();
                            }

                            //case user
                            else if (entityTo is EntityList <UserTep> )
                            {
                                var entityTolist = entityTo as EntityList <UserTep>;
                                var users        = entityTolist.GetItemsAsList();
                                if (users.Count == 0)
                                {
                                    return(new WebResponseBool(false));
                                }
                                s.GrantPermissionsToUsers(users);

                                ActivityTep activity = new ActivityTep(context, s, EntityOperationType.Share);
                                activity.AppId = request.id;
                                activity.Store();
                            }
                        }
                    }
                }
            }

            context.Close();
            return(new WebResponseBool(true));
        }
Ejemplo n.º 3
0
        /// <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));
        }