Exemplo n.º 1
0
        public void Test_WithType_NullIdentity(string identity)
        {
            IResourceResolverProvider provider = GetResolverProvider( );
            IResourceResolver         resolver = provider.GetResolverForType(new EntityRef("test:employee").Id);

            Assert.Throws <ArgumentNullException>(() => resolver.ResolveResource(identity));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Attempts to return a fully qualified resource name from the specified name, which may be partially
        /// qualified or entirely unqualified, by searching the assemblies associated with this <see cref="ResourceResolver"/> in order.
        /// </summary>
        /// <param name="resourceName">A partially qualified or unqualified resource name.</param>
        /// <returns>A qualified resource name, if found, otherwise an exception is thrown.</returns>
        /// <exception cref="MissingManifestResourceException">if the resource name could not be resolved.</exception>
        public string ResolveResource(string resourceName)
        {
            //resources are qualified internally in the manifest with '.' characters, so let's first try
            //to find a resource that matches 'exactly' by preceding the name with a '.'
            string exactMatch = String.Format(".{0}", resourceName);

            foreach (Assembly asm in _assemblies)
            {
                foreach (string match in GetResourcesEndingWith(asm, exactMatch))
                {
                    return(match);
                }

                //next we'll just try to find the first match ending with the resource name.
                foreach (string match in GetResourcesEndingWith(asm, resourceName))
                {
                    return(match);
                }
            }

            // try the fallback
            if (_fallbackResovler != null)
            {
                return(_fallbackResovler.ResolveResource(resourceName));
            }

            // not found - throw exception
            throw new MissingManifestResourceException(string.Format(SR.ExceptionResourceNotFound, resourceName));
        }
Exemplo n.º 3
0
        private IEntity LocateResource(ConnectorRequest request, ApiResourceMapping mapping, out ConnectorResponse errorResponse)
        {
            // Check path
            if (request.ApiPath == null || request.ApiPath.Length < 3)
            {
                errorResponse = new ConnectorResponse(HttpStatusCode.MethodNotAllowed);
                return(null);
            }

            if (request.ApiPath.Length > 3)
            {
                errorResponse = new ConnectorResponse(HttpStatusCode.NotFound);
                return(null);
            }

            // Get resource ID field value (i.e. the string being used to locate the resource, e.g. name)
            string resourceIdFieldValue = request.ApiPath [2];

            if (string.IsNullOrEmpty(resourceIdFieldValue))
            {
                throw new Exception("Empty resource identity was received.");   // assert false .. this should be blocked earlier.
            }
            // Resolve resource from string
            IResourceResolver     resolver      = _resourceResolverProvider.GetResolverForResourceMapping(mapping);
            ResourceResolverEntry resolveResult = resolver.ResolveResource(resourceIdFieldValue);

            if (resolveResult.Entity == null)
            {
                throw new WebArgumentNotFoundException( );   // Resource could not be located, so ensure a 404 is thrown
            }
            errorResponse = null;
            return(resolveResult.Entity);
        }
Exemplo n.º 4
0
        public void Test_Resolve_WithType_NotFound(string identity, string type, ResourceResolverError expectedError)
        {
            IResourceResolverProvider provider = GetResolverProvider( );
            long typeId = new EntityRef(type).Id;
            IResourceResolver resolver = provider.GetResolverForType(typeId);

            ResourceResolverEntry entry = resolver.ResolveResource(identity);

            Assert.That(entry.Entity, Is.Null);
            Assert.That(entry.Error, Is.EqualTo(expectedError));
        }
Exemplo n.º 5
0
 /// <summary>
 /// Gets a string identifier that uniquely identifies the resolved icon, suitable for dictionary keying purposes.
 /// </summary>
 /// <remarks>
 /// The base implementation resolves the specified image resource using the provided
 /// <paramref name="resourceResolver"/> and returns the resource's fully qualified resource name.
 /// </remarks>
 /// <param name="iconSize">The size of the desired icon.</param>
 /// <param name="resourceResolver">The resource resolver with which to resolve the requested icon resource.</param>
 /// <returns>A string identifier that uniquely identifies the resolved icon.</returns>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="resourceResolver"/> is null.</exception>
 /// <exception cref="ArgumentException">Thrown if <paramref name="resourceResolver"/> was unable to resolve the requested icon resource.</exception>
 public virtual string GetIconKey(IconSize iconSize, IResourceResolver resourceResolver)
 {
     Platform.CheckForNullReference(resourceResolver, "resourceResolver");
     try
     {
         return(resourceResolver.ResolveResource(this[iconSize]));
     }
     catch (MissingManifestResourceException ex)
     {
         throw new ArgumentException("The provided resource resolver was unable to resolve the requested icon resource.", ex);
     }
 }
Exemplo n.º 6
0
        public void Test_ResolveResource_WithType_ByName(string identity, string type)
        {
            IResourceResolverProvider provider = GetResolverProvider( );
            long typeId = new EntityRef(type).Id;
            IResourceResolver resolver = provider.GetResolverForType(typeId);

            ResourceResolverEntry entry = resolver.ResolveResource(identity);
            IEntity entity = entry.Entity;

            Assert.That(entity, Is.Not.Null);
            Assert.That(entity.As <Resource>( ).Name, Is.EqualTo(identity));
        }
Exemplo n.º 7
0
        public void Test_Resolve_WithMapping_NotFound(string identity, string type, ResourceResolverError expectedError)
        {
            IResourceResolverProvider provider = GetResolverProvider( );
            ApiResourceMapping        mapping  = new ApiResourceMapping( );

            mapping.MappedType = Entity.Get <EntityType>("test:allFields");
            mapping.Save( );
            IResourceResolver resolver = provider.GetResolverForResourceMapping(mapping);

            ResourceResolverEntry entry = resolver.ResolveResource(identity);

            Assert.That(entry.Entity, Is.Null);
            Assert.That(entry.Error, Is.EqualTo(expectedError));
        }
Exemplo n.º 8
0
        public void Test_ResolveResource_WithMapping_NoIdentityField_ByName(string identity, string type)
        {
            IResourceResolverProvider provider = GetResolverProvider( );
            ApiResourceMapping        mapping  = new ApiResourceMapping( );

            mapping.MappedType = Entity.Get <EntityType>(type);
            mapping.Save( );
            IResourceResolver resolver = provider.GetResolverForResourceMapping(mapping);

            ResourceResolverEntry entry = resolver.ResolveResource(identity);
            IEntity entity = entry.Entity;

            Assert.That(entity, Is.Not.Null);
            Assert.That(entity.As <Resource>( ).Name, Is.EqualTo(identity));
        }
Exemplo n.º 9
0
        public void Test_ResolveResource_WithType_ByGuid(string alias, string type, string name)
        {
            IEntity entity1  = Entity.Get(new EntityRef(alias));
            string  identity = entity1.UpgradeId.ToString( );

            IResourceResolverProvider provider = GetResolverProvider( );
            long typeId = new EntityRef(type).Id;
            IResourceResolver resolver = provider.GetResolverForType(typeId);

            ResourceResolverEntry entry = resolver.ResolveResource(identity);
            IEntity entity = entry.Entity;

            Assert.That(entity, Is.Not.Null);
            Assert.That(entity.As <Resource>( ).Name, Is.EqualTo(name));
        }
Exemplo n.º 10
0
        public void Test_ResolveResource_WithMapping_NoIdentityField_ByGuid(string alias, string type, string name)
        {
            IEntity entity1  = Entity.Get(new EntityRef(alias));
            string  identity = entity1.UpgradeId.ToString();

            IResourceResolverProvider provider = GetResolverProvider( );
            ApiResourceMapping        mapping  = new ApiResourceMapping( );

            mapping.MappedType = Entity.Get <EntityType>(type);
            mapping.Save( );
            IResourceResolver resolver = provider.GetResolverForResourceMapping(mapping);

            ResourceResolverEntry entry = resolver.ResolveResource(identity);
            IEntity entity = entry.Entity;

            Assert.That(entity, Is.Not.Null);
            Assert.That(entity.As <Resource>( ).Name, Is.EqualTo(name));
        }
Exemplo n.º 11
0
        /// <summary>
        /// Attempts to return a fully qualified resource name from the specified name, which may be partially
        /// qualified or entirely unqualified, by searching the assemblies associated with this <see cref="ResourceResolver"/> in order.
        /// </summary>
        /// <param name="resourceName">A partially qualified or unqualified resource name.</param>
        /// <returns>A qualified resource name, if found, otherwise an exception is thrown.</returns>
        /// <exception cref="MissingManifestResourceException">if the resource name could not be resolved.</exception>
        public string ResolveResource(string resourceName)
        {
            Platform.CheckForEmptyString(resourceName, @"resourceName");

            foreach (var asm in _assemblies)
            {
                var result = ResolveResource(resourceName, asm);
                if (result != null)
                {
                    return(result);
                }
            }

            // try the fallback
            if (_fallbackResolver != null)
            {
                return(_fallbackResolver.ResolveResource(resourceName));
            }

            // not found - throw exception
            throw new MissingManifestResourceException(string.Format(SR.ExceptionResourceNotFound, resourceName));
        }