Search in sources :

Example 21 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class AbstractExtendedURLResourceTypeResolver method resolve.

protected ResourceType resolve(String hintPrefix, ExtendedURL extendedURL, Map<String, Object> parameters) throws CreateResourceTypeException {
    ResourceType resourceType;
    // Find the Resource Type, which is the first segment in the ExtendedURL.
    // 
    // Note that we need to remove the type from the ExtendedURL instance since it's passed to the specific
    // resolvers and they shouldn't be aware of where it was located since they need to be able to resolve the
    // rest of the URL independently of the URL scheme, in case they wish to have a single URL syntax for all URL
    // schemes.
    // 
    // Examples:
    // - scheme 1: /<type>/something
    // - scheme 2: /something?type=<type>
    // 
    // The specific resolver for type <type> needs to be passed an ExtendedURL independent of the type, in this
    // case, "/something" for both examples.
    // 
    // However since we also want this code to work when short URLs are enabled, we only remove the segment part
    // if a Resource type has been identified (see below) and if not, we assume the URL is pointing to an Entity
    // Resource.
    List<String> segments = extendedURL.getSegments();
    resourceType = this.defaultStringResourceTypeResolver.resolve(segments.get(0), Collections.<String, Object>emptyMap());
    // Second, if not found, try to locate a URL Resolver registered for all URL schemes
    if (this.componentManager.hasComponent(new DefaultParameterizedType(null, ResourceReferenceResolver.class, ExtendedURL.class), computeHint(hintPrefix, resourceType.getId()))) {
        extendedURL.getSegments().remove(0);
    } else if (this.componentManager.hasComponent(new DefaultParameterizedType(null, ResourceReferenceResolver.class, ExtendedURL.class), resourceType.getId())) {
        extendedURL.getSegments().remove(0);
    } else {
        // No specific Resource Type Resolver has been found. In order to support short URLs (ie. without the
        // "bin" or "wiki" part specified), we assume the URL is pointing to an Entity Resource Reference.
        // Since the "wiki" type was not selected, we're assuming that the we'll use the "bin" entity resolver.
        resourceType = EntityResourceReference.TYPE;
    }
    return resourceType;
}
Also used : ResourceReferenceResolver(org.xwiki.resource.ResourceReferenceResolver) ResourceType(org.xwiki.resource.ResourceType) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) ExtendedURL(org.xwiki.url.ExtendedURL)

Example 22 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class DefaultResourceReferenceResolver method resolve.

@Override
public ResourceReference resolve(ExtendedURL extendedURL, ResourceType type, Map<String, Object> parameters) throws CreateResourceReferenceException, UnsupportedResourceReferenceException {
    ResourceReferenceResolver resolver;
    // Step 1: Look for a URL-scheme-specific Resolver (a general one that is independent of the passed
    // Resource Type). This allows URL-scheme implementation to completely override handling of any
    // Resource Type if they wish.
    DefaultParameterizedType parameterizedType = new DefaultParameterizedType(null, ResourceReferenceResolver.class, ExtendedURL.class);
    String hint = this.configuration.getURLFormatId();
    if (this.componentManager.hasComponent(parameterizedType, hint)) {
        try {
            resolver = this.componentManager.getInstance(parameterizedType, hint);
        } catch (ComponentLookupException e) {
            throw new CreateResourceReferenceException(String.format("Failed to create Resource Reference for [%s].", extendedURL.getWrappedURL()), e);
        }
    } else {
        // Step 2: If not found, use the Generic Resolver, which tries to find a Resolver registered for the
        // specific Resource Type.
        resolver = this.genericResourceReferenceResolver;
    }
    return resolver.resolve(extendedURL, type, parameters);
}
Also used : ResourceReferenceResolver(org.xwiki.resource.ResourceReferenceResolver) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) CreateResourceReferenceException(org.xwiki.resource.CreateResourceReferenceException)

Example 23 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class DefaultStringResourceTypeResolver method resolve.

@Override
public ResourceType resolve(String type, Map<String, Object> parameters) throws CreateResourceTypeException {
    ResourceTypeResolver resolver;
    DefaultParameterizedType parameterizedType = new DefaultParameterizedType(null, ResourceTypeResolver.class, String.class);
    String hint = this.configuration.getURLFormatId();
    if (this.componentManager.hasComponent(parameterizedType, hint)) {
        try {
            resolver = this.componentManager.getInstance(parameterizedType, hint);
        } catch (ComponentLookupException e) {
            throw new CreateResourceTypeException(String.format("Failed to convert Resource Type from String [%s] to [%s]", type, ResourceType.class.getSimpleName()), e);
        }
    } else {
        // No specific String Resource Type Resolver for the Scheme URL, use the generic one!
        resolver = this.genericResourceTypeResolver;
    }
    return resolver.resolve(type, parameters);
}
Also used : ResourceTypeResolver(org.xwiki.resource.ResourceTypeResolver) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ResourceType(org.xwiki.resource.ResourceType) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) CreateResourceTypeException(org.xwiki.resource.CreateResourceTypeException)

Example 24 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class DefaultIconSetCacheTest method setUp.

@Before
public void setUp() throws Exception {
    cacheManager = mocker.getInstance(CacheManager.class);
    entityReferenceSerializer = mocker.getInstance(new DefaultParameterizedType(null, EntityReferenceSerializer.class, String.class));
    cache = mock(Cache.class);
    CacheFactory cacheFactory = mock(CacheFactory.class);
    when(cacheManager.getCacheFactory()).thenReturn(cacheFactory);
    CacheConfiguration configuration = new CacheConfiguration("iconset");
    when(cacheFactory.<IconSet>newCache(eq(configuration))).thenReturn(cache);
}
Also used : CacheManager(org.xwiki.cache.CacheManager) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) CacheFactory(org.xwiki.cache.CacheFactory) CacheConfiguration(org.xwiki.cache.config.CacheConfiguration) Cache(org.xwiki.cache.Cache) IconSet(org.xwiki.icon.IconSet) Before(org.junit.Before)

Example 25 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class SerializedFilesMimeMessageFactoryTest method createMessageWhenNoExecution.

@Test
public void createMessageWhenNoExecution() throws Exception {
    Provider<ComponentManager> componentManagerProvider = this.mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
    when(componentManagerProvider.get()).thenReturn(this.mocker);
    try {
        this.mocker.getComponentUnderTest().createMessage("batchId", null);
        fail("Should have thrown an exception");
    } catch (MessagingException expected) {
        assertEquals("Failed to find an Environment Component", expected.getMessage());
    }
}
Also used : MessagingException(javax.mail.MessagingException) ComponentManager(org.xwiki.component.manager.ComponentManager) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Provider(javax.inject.Provider) Test(org.junit.Test)

Aggregations

DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)104 Test (org.junit.Test)44 Before (org.junit.Before)32 Provider (javax.inject.Provider)27 DocumentReference (org.xwiki.model.reference.DocumentReference)24 XWikiContext (com.xpn.xwiki.XWikiContext)19 ComponentManager (org.xwiki.component.manager.ComponentManager)19 ExecutionContext (org.xwiki.context.ExecutionContext)17 ExtendedURL (org.xwiki.url.ExtendedURL)15 Execution (org.xwiki.context.Execution)14 HashMap (java.util.HashMap)13 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)13 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)12 ResourceReferenceSerializer (org.xwiki.resource.ResourceReferenceSerializer)10 VfsResourceReference (org.xwiki.vfs.VfsResourceReference)10 Properties (java.util.Properties)9 MimeBodyPartFactory (org.xwiki.mail.MimeBodyPartFactory)9 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)8 XWiki (com.xpn.xwiki.XWiki)7 File (java.io.File)7