Search in sources :

Example 1 with ExtendedURL

use of org.xwiki.url.ExtendedURL in project xwiki-platform by xwiki.

the class ResourceReferenceHandlerServlet method getResourceReferenceResolver.

private ResourceReferenceResolver<ExtendedURL> getResourceReferenceResolver() throws ServletException {
    ResourceReferenceResolver<ExtendedURL> urlResolver;
    try {
        Type role = new DefaultParameterizedType(null, ResourceReferenceResolver.class, ExtendedURL.class);
        urlResolver = this.rootComponentManager.getInstance(role);
    } catch (ComponentLookupException e) {
        // Should not happen since a URL provider should exist on the system.
        throw new ServletException("Failed to locate an ExtendedURL Resource Reference Resolver component", e);
    }
    return urlResolver;
}
Also used : ServletException(javax.servlet.ServletException) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Type(java.lang.reflect.Type) ResourceType(org.xwiki.resource.ResourceType) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ExtendedURL(org.xwiki.url.ExtendedURL) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType)

Example 2 with ExtendedURL

use of org.xwiki.url.ExtendedURL 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 3 with ExtendedURL

use of org.xwiki.url.ExtendedURL in project xwiki-platform by xwiki.

the class RoutingFilter method constructExtendedURL.

private ExtendedURL constructExtendedURL(HttpServletRequest httpRequest) throws ServletException {
    ExtendedURL extendedURL;
    URL url = getRequestURL(httpRequest);
    try {
        extendedURL = new ExtendedURL(url, httpRequest.getContextPath());
    } catch (CreateResourceReferenceException e) {
        throw new ServletException(String.format("Invalid URL [%s]", url), e);
    }
    return extendedURL;
}
Also used : ServletException(javax.servlet.ServletException) ExtendedURL(org.xwiki.url.ExtendedURL) ExtendedURL(org.xwiki.url.ExtendedURL) URL(java.net.URL) CreateResourceReferenceException(org.xwiki.resource.CreateResourceReferenceException)

Example 4 with ExtendedURL

use of org.xwiki.url.ExtendedURL 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 5 with ExtendedURL

use of org.xwiki.url.ExtendedURL in project xwiki-platform by xwiki.

the class ExtendedURLTemporaryResourceReferenceSerializer method serialize.

@Override
public ExtendedURL serialize(TemporaryResourceReference reference) throws SerializeResourceReferenceException, UnsupportedResourceReferenceException {
    List<String> segments = new LinkedList<String>();
    segments.add("tmp");
    segments.add(reference.getModuleId());
    segments.add(serialize(reference.getOwningEntityReference()));
    segments.addAll(reference.getResourcePath());
    // A modifiable map is used here so parameters can be added to the URL later.
    Map<String, List<String>> parameters = new HashMap<String, List<String>>(reference.getParameters());
    ExtendedURL result = new ExtendedURL(segments, parameters);
    return this.extendedURLNormalizer.normalize(result);
}
Also used : HashMap(java.util.HashMap) List(java.util.List) LinkedList(java.util.LinkedList) ExtendedURL(org.xwiki.url.ExtendedURL) LinkedList(java.util.LinkedList)

Aggregations

ExtendedURL (org.xwiki.url.ExtendedURL)46 Test (org.junit.Test)26 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)13 ResourceType (org.xwiki.resource.ResourceType)10 URL (java.net.URL)9 WebJarsResourceReference (org.xwiki.webjars.internal.WebJarsResourceReference)8 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)7 VfsResourceReference (org.xwiki.vfs.VfsResourceReference)7 HashMap (java.util.HashMap)6 List (java.util.List)6 EntityResourceReference (org.xwiki.resource.entity.EntityResourceReference)6 ServletException (javax.servlet.ServletException)5 WikiDescriptorManager (org.xwiki.wiki.descriptor.WikiDescriptorManager)5 IOException (java.io.IOException)4 Type (java.lang.reflect.Type)3 ExtensionId (org.xwiki.extension.ExtensionId)3 InstalledExtension (org.xwiki.extension.InstalledExtension)3 InstalledExtensionRepository (org.xwiki.extension.repository.InstalledExtensionRepository)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 ResourceReference (org.xwiki.resource.ResourceReference)3