Search in sources :

Example 1 with ResourceType

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

the class ResourceReferenceHandlerServlet method getResourceReference.

private ResourceReference getResourceReference(HttpServletRequest httpRequest) throws ServletException {
    // Get the Resource Type from the request's attribute, where it's been put by the RoutingFilter.
    ResourceType resourceType = (ResourceType) httpRequest.getAttribute(RoutingFilter.RESOURCE_TYPE_NAME);
    // Get the ExtendedURL from the request's attribute too (so that we don't have to compute it again).
    ExtendedURL extendedURL = (ExtendedURL) httpRequest.getAttribute(RoutingFilter.RESOURCE_EXTENDEDURL);
    // Extract the Resource Reference, passing the already extracted Resource Type
    ResourceReferenceResolver<ExtendedURL> urlResolver = getResourceReferenceResolver();
    try {
        // the store.
        return urlResolver.resolve(extendedURL, resourceType, Collections.<String, Object>emptyMap());
    } catch (Exception e) {
        // This shouldn't happen, raise an exception
        throw new ServletException(String.format("Failed to extract the Resource Reference from the URL [%s]", extendedURL.getWrappedURL()), e);
    }
}
Also used : ServletException(javax.servlet.ServletException) ResourceType(org.xwiki.resource.ResourceType) ExtendedURL(org.xwiki.url.ExtendedURL) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) ServletContainerException(org.xwiki.container.servlet.ServletContainerException)

Example 2 with ResourceType

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

the class RoutingFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    // Only handle HTTP Servlet requests...
    if (!(request instanceof HttpServletRequest)) {
        chain.doFilter(request, response);
        return;
    }
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    // Step 1: Construct an ExtendedURL to make it easy to manipulate the URL segments
    ExtendedURL extendedURL = constructExtendedURL(httpRequest);
    // Step 2: Extract the Resource Type from the ExtendedURL
    ResourceTypeResolver<ExtendedURL> urlResourceTypeResolver = getResourceTypeResolver();
    ResourceType resourceType;
    try {
        resourceType = urlResourceTypeResolver.resolve(extendedURL, Collections.<String, Object>emptyMap());
    } catch (Exception e) {
        // Failed to resolve the passed ExtendedURL. This means it's not a URL that should be handled by a Resource
        // Reference Handler and we let it go through so that the next Filter or Servlet from web.xml will handle
        // it. Note that since some URL schemes may want to handle features like short URLs where the Resource Type
        // is omitted, this catch will not be called in this case and this is why we need Step 2 below in order
        // to recognize static resources and serve them!
        chain.doFilter(request, response);
        return;
    }
    // content of web.xml and would serve static files using its File Servlet.
    if (resourceType.equals(ResourcesResourceReference.TYPE) || resourceType.equals(SkinsResourceReference.TYPE)) {
        chain.doFilter(request, response);
        return;
    }
    // Step 4: Check if there's a Handler available for the Resource Type
    ResourceReferenceHandlerManager<ResourceType> resourceReferenceHandlerManager = getResourceReferenceHandlerManager();
    // "rest", "webdav" and "xmlrpc" Resource Types.
    if (!resourceReferenceHandlerManager.canHandle(resourceType)) {
        // Let it go through so that the next Filter or Servlet from web.xml will handle it.
        chain.doFilter(request, response);
        return;
    }
    // Step 4: There is a Handler to handle our request, call the Resource Handler Servlet. Note that calling a
    // Sevlet gives us more flexibility if we wish to execute some more Filters before the Servlet executes for
    // example.
    // 
    // However before doing that, we save the Resource Type so that the Servlet doesn't have to extract it again!
    // We also save the URL since we don't want to have to compute the full URL again in the Resource Reference
    // Handler Servlet!
    request.setAttribute(RESOURCE_TYPE_NAME, resourceType);
    request.setAttribute(RESOURCE_EXTENDEDURL, extendedURL);
    this.servletContext.getNamedDispatcher("resourceReferenceHandler").forward(request, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ResourceType(org.xwiki.resource.ResourceType) ExtendedURL(org.xwiki.url.ExtendedURL) MalformedURLException(java.net.MalformedURLException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) CreateResourceReferenceException(org.xwiki.resource.CreateResourceReferenceException)

Example 3 with ResourceType

use of org.xwiki.resource.ResourceType 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 4 with ResourceType

use of org.xwiki.resource.ResourceType 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 5 with ResourceType

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

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