Search in sources :

Example 1 with ResourceReferenceResolver

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

the class XWiki method initializeResourceFromURL.

private static EntityResourceReference initializeResourceFromURL(XWikiContext context) throws XWikiException {
    // Extract the Entity Resource from the URL
    // TODO: This code should be put in an ExecutionContextInitializer but we couldn't do yet since this code
    // requires that the XWiki object be initialized first (the line above). Thus we'll be able to to move it only
    // after the XWiki init is done also in an ExecutionContextInitializer (and with priorities).
    @SuppressWarnings("deprecation") EntityResourceReference entityResourceReference;
    URL url = context.getURL();
    try {
        ExtendedURL extendedURL = new ExtendedURL(url, context.getRequest().getContextPath());
        ResourceTypeResolver<ExtendedURL> typeResolver = Utils.getComponent(new DefaultParameterizedType(null, ResourceTypeResolver.class, ExtendedURL.class));
        ResourceType type = typeResolver.resolve(extendedURL, Collections.<String, Object>emptyMap());
        ResourceReferenceResolver<ExtendedURL> resourceResolver = Utils.getComponent(new DefaultParameterizedType(null, ResourceReferenceResolver.class, ExtendedURL.class));
        ResourceReference reference = resourceResolver.resolve(extendedURL, type, Collections.<String, Object>emptyMap());
        entityResourceReference = reference instanceof EntityResourceReference ? (EntityResourceReference) reference : null;
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI, XWikiException.ERROR_XWIKI_APP_URL_EXCEPTION, String.format("Failed to extract Entity Resource Reference from URL [%s]", url), e);
    }
    Utils.getComponent(Execution.class).getContext().setProperty(ResourceReferenceManager.RESOURCE_CONTEXT_PROPERTY, entityResourceReference);
    return entityResourceReference;
}
Also used : ResourceReferenceResolver(org.xwiki.resource.ResourceReferenceResolver) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) ResourceType(org.xwiki.resource.ResourceType) ExtendedURL(org.xwiki.url.ExtendedURL) ExtendedURL(org.xwiki.url.ExtendedURL) URL(java.net.URL) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) IOException(java.io.IOException) JobException(org.xwiki.job.JobException) ParseException(org.xwiki.rendering.parser.ParseException) QueryException(org.xwiki.query.QueryException) URIException(org.apache.commons.httpclient.URIException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HibernateException(org.hibernate.HibernateException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) NamingException(javax.naming.NamingException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) ResourceTypeResolver(org.xwiki.resource.ResourceTypeResolver) ResourceReference(org.xwiki.resource.ResourceReference) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType)

Example 2 with ResourceReferenceResolver

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

the class StandardExtendedURLResourceTypeResolver method registerEntityResourceReferenceResolver.

private void registerEntityResourceReferenceResolver(String registrationHint, Class<? extends ResourceReferenceResolver<ExtendedURL>> registrationImplementation, String wikiExtractorHint) throws InitializationException {
    DefaultComponentDescriptor<ResourceReferenceResolver<ExtendedURL>> resolverDescriptor = new DefaultComponentDescriptor<>();
    resolverDescriptor.setImplementation(registrationImplementation);
    resolverDescriptor.setInstantiationStrategy(ComponentInstantiationStrategy.SINGLETON);
    String hint = computeHint(registrationHint);
    resolverDescriptor.setRoleHint(hint);
    resolverDescriptor.setRoleType(new DefaultParameterizedType(null, ResourceReferenceResolver.class, ExtendedURL.class));
    // Register dependencies
    DefaultComponentDependency<WikiReferenceExtractor> wikiReferenceExtractorDependency = new DefaultComponentDependency<>();
    wikiReferenceExtractorDependency.setRoleType(WikiReferenceExtractor.class);
    wikiReferenceExtractorDependency.setRoleHint(wikiExtractorHint);
    wikiReferenceExtractorDependency.setName("wikiExtractor");
    resolverDescriptor.addComponentDependency(wikiReferenceExtractorDependency);
    DefaultComponentDependency<EntityReferenceResolver<EntityReference>> entityReferenceResolverDependency = new DefaultComponentDependency<>();
    entityReferenceResolverDependency.setRoleType(new DefaultParameterizedType(null, EntityReferenceResolver.class, EntityReference.class));
    entityReferenceResolverDependency.setName("defaultReferenceEntityReferenceResolver");
    resolverDescriptor.addComponentDependency(entityReferenceResolverDependency);
    DefaultComponentDependency<StandardURLConfiguration> standardURLConfigurationDependency = new DefaultComponentDependency<>();
    standardURLConfigurationDependency.setRoleType(StandardURLConfiguration.class);
    standardURLConfigurationDependency.setName("configuration");
    resolverDescriptor.addComponentDependency(standardURLConfigurationDependency);
    DefaultComponentDependency<EntityResourceActionLister> entityResourceActionListerDependency = new DefaultComponentDependency<>();
    entityResourceActionListerDependency.setRoleType(EntityResourceActionLister.class);
    entityResourceActionListerDependency.setName("entityResourceActionLister");
    resolverDescriptor.addComponentDependency(entityResourceActionListerDependency);
    try {
        this.rootComponentManager.registerComponent(resolverDescriptor);
    } catch (ComponentRepositoryException e) {
        throw new InitializationException(String.format("Failed to dynamically register Resource Reference Resolver for hint [%s]", hint), e);
    }
}
Also used : DefaultComponentDependency(org.xwiki.component.descriptor.DefaultComponentDependency) EntityReferenceResolver(org.xwiki.model.reference.EntityReferenceResolver) BinEntityResourceReferenceResolver(org.xwiki.url.internal.standard.entity.BinEntityResourceReferenceResolver) ResourceReferenceResolver(org.xwiki.resource.ResourceReferenceResolver) WikiEntityResourceReferenceResolver(org.xwiki.url.internal.standard.entity.WikiEntityResourceReferenceResolver) ComponentRepositoryException(org.xwiki.component.manager.ComponentRepositoryException) ExtendedURL(org.xwiki.url.ExtendedURL) InitializationException(org.xwiki.component.phase.InitializationException) EntityResourceActionLister(org.xwiki.resource.internal.entity.EntityResourceActionLister) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) EntityReference(org.xwiki.model.reference.EntityReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType)

Example 3 with ResourceReferenceResolver

use of org.xwiki.resource.ResourceReferenceResolver 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)

Aggregations

DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)3 ResourceReferenceResolver (org.xwiki.resource.ResourceReferenceResolver)3 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)2 ExtendedURL (org.xwiki.url.ExtendedURL)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 NamingException (javax.naming.NamingException)1 URIException (org.apache.commons.httpclient.URIException)1 HibernateException (org.hibernate.HibernateException)1 DefaultComponentDependency (org.xwiki.component.descriptor.DefaultComponentDependency)1 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)1 ComponentRepositoryException (org.xwiki.component.manager.ComponentRepositoryException)1 InitializationException (org.xwiki.component.phase.InitializationException)1 JobException (org.xwiki.job.JobException)1 EntityReference (org.xwiki.model.reference.EntityReference)1 EntityReferenceResolver (org.xwiki.model.reference.EntityReferenceResolver)1 QueryException (org.xwiki.query.QueryException)1