Search in sources :

Example 11 with ResourceType

use of org.xwiki.resource.ResourceType in project xwiki-platform by xwiki.

the class AbstractResourceReferenceResolver method findResourceResolver.

/**
 * Find the right Resolver for the passed Resource type and call it.
 */
protected ResourceReferenceResolver<ExtendedURL> findResourceResolver(String hintPrefix, ResourceType type) throws UnsupportedResourceReferenceException {
    ResourceReferenceResolver<ExtendedURL> resolver;
    Type roleType = new DefaultParameterizedType(null, ResourceReferenceResolver.class, ExtendedURL.class);
    String roleHint = computeResolverHint(hintPrefix, type.getId());
    // Step 1: Look for a Resolver specific to the scheme and specific to the Resource Type
    if (this.componentManager.hasComponent(roleType, roleHint)) {
        try {
            resolver = this.componentManager.getInstance(roleType, roleHint);
        } catch (ComponentLookupException cle) {
            // There's no Resolver registered for the passed Resource Type
            throw new UnsupportedResourceReferenceException(String.format("Failed to lookup Resource Reference Resolver for hint [%s]", roleHint), cle);
        }
    } else {
        // schemes
        try {
            resolver = this.componentManager.getInstance(roleType, type.getId());
        } catch (ComponentLookupException cle) {
            // There's no Resolver registered for the passed Resource Type
            throw new UnsupportedResourceReferenceException(String.format("Failed to lookup Resource Reference Resolver for type [%s]", type), cle);
        }
    }
    return resolver;
}
Also used : DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Type(java.lang.reflect.Type) ResourceType(org.xwiki.resource.ResourceType) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) UnsupportedResourceReferenceException(org.xwiki.resource.UnsupportedResourceReferenceException) ExtendedURL(org.xwiki.url.ExtendedURL) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType)

Example 12 with ResourceType

use of org.xwiki.resource.ResourceType in project xwiki-platform by xwiki.

the class MainResourceReferenceHandlerManagerTest method handleWithOrder.

@Test
public void handleWithOrder() throws Exception {
    // First Handler component will lower priority
    ResourceReferenceHandler testHandler = mock(ResourceReferenceHandler.class, "handler1");
    when(testHandler.getSupportedResourceReferences()).thenReturn(Arrays.asList(new ResourceType("test")));
    // Second Handler component will higher priority so that it's executed first
    ResourceReferenceHandler beforeTestHandler = mock(ResourceReferenceHandler.class, "handler2");
    when(beforeTestHandler.getSupportedResourceReferences()).thenReturn(Arrays.asList(new ResourceType("test")));
    // We return 1 to mean that the second Handler has a higher priority than the first Handler
    when(beforeTestHandler.compareTo(testHandler)).thenReturn(-1);
    ComponentManager contextComponentManager = this.componentManager.getInstance(ComponentManager.class, "context");
    when(contextComponentManager.<ResourceReferenceHandler>getInstanceList(new DefaultParameterizedType(null, ResourceReferenceHandler.class, ResourceType.class))).thenReturn(Arrays.asList(testHandler, beforeTestHandler));
    ResourceReference reference = mock(ResourceReference.class);
    when(reference.getType()).thenReturn(new ResourceType("test"));
    this.componentManager.getComponentUnderTest().handle(reference);
    // Verify that the second Action is called (since it has a higher priority).
    verify(beforeTestHandler).handle(same(reference), any(ResourceReferenceHandlerChain.class));
}
Also used : ResourceReferenceHandler(org.xwiki.resource.ResourceReferenceHandler) ComponentManager(org.xwiki.component.manager.ComponentManager) ResourceType(org.xwiki.resource.ResourceType) ResourceReference(org.xwiki.resource.ResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) ResourceReferenceHandlerChain(org.xwiki.resource.ResourceReferenceHandlerChain) Test(org.junit.Test)

Aggregations

ResourceType (org.xwiki.resource.ResourceType)12 ExtendedURL (org.xwiki.url.ExtendedURL)9 EntityResourceReference (org.xwiki.resource.entity.EntityResourceReference)6 URL (java.net.URL)5 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)5 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)5 IOException (java.io.IOException)4 ServletException (javax.servlet.ServletException)3 ResourceReference (org.xwiki.resource.ResourceReference)3 MalformedURLException (java.net.MalformedURLException)2 Test (org.junit.Test)2 ResourceReferenceResolver (org.xwiki.resource.ResourceReferenceResolver)2 ResourceTypeResolver (org.xwiki.resource.ResourceTypeResolver)2 ResourcesResourceReference (org.xwiki.resource.resources.ResourcesResourceReference)2 SkinsResourceReference (org.xwiki.resource.skins.SkinsResourceReference)2 FileNotFoundException (java.io.FileNotFoundException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Type (java.lang.reflect.Type)1 NamingException (javax.naming.NamingException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1