Search in sources :

Example 11 with ExtendedURL

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

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

the class XWikiRequestProcessor method processPath.

@Override
protected String processPath(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
    String url = httpServletRequest.getRequestURL().toString();
    try {
        ExtendedURL extendedURL = new ExtendedURL(new URL(url), httpServletRequest.getContextPath());
        ResourceType type = this.typeResolver.resolve(extendedURL, Collections.<String, Object>emptyMap());
        EntityResourceReference entityResourceReference = (EntityResourceReference) this.resolver.resolve(extendedURL, type, Collections.<String, Object>emptyMap());
        return "/" + entityResourceReference.getAction().getActionName() + "/";
    } catch (Exception e) {
        throw new IOException(String.format("Failed to extract the Entity Action from URL [%s]", url), e);
    }
}
Also used : EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) ResourceType(org.xwiki.resource.ResourceType) IOException(java.io.IOException) ExtendedURL(org.xwiki.url.ExtendedURL) ExtendedURL(org.xwiki.url.ExtendedURL) URL(java.net.URL) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 13 with ExtendedURL

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

the class WebJarsScriptService method url.

/**
 * Creates an URL that can be used to load a resource (JavaScript, CSS, etc.) from a WebJar in the passed namespace.
 *
 * @param webjarId the id of the WebJar that contains the resource; the format of the WebJar id is
 *            {@code groupId:artifactId} (e.g. {@code org.xwiki.platform:xwiki-platform-job-webjar}), where the
 *            {@code groupId} can be omitted if it is {@link #DEFAULT_WEBJAR_GROUP_ID} (i.e. {@code angular}
 *            translates to {@code org.webjars:angular})
 * @param namespace the namespace in which the webjars resources will be loaded from (e.g. for a wiki namespace you
 *                  should use the format {@code wiki:<wikiId>}). If null then defaults to the current wiki
 *                  namespace. And if the passed namespace doesn't exist, falls back to the main wiki namespace
 * @param path the path within the WebJar, starting from the version folder (e.g. you should pass just
 *            {@code angular.js} if the actual path is {@code META-INF/resources/webjars/angular/2.1.11/angular.js})
 * @param params additional query string parameters to add to the returned URL; there are two known (reserved)
 *            parameters: {@code version} (the WebJar version) and {@code evaluate} (a boolean parameter that
 *            specifies if the requested resource has Velocity code that needs to be evaluated); besides these you
 *            can pass whatever parameters you like (they will be taken into account or not depending on the
 *            resource)
 * @return the URL to load the WebJar resource (relative to the context path of the web application)
 * @since 8.1M2
 */
public String url(String webjarId, String namespace, String path, Map<String, ?> params) {
    if (StringUtils.isEmpty(webjarId)) {
        return null;
    }
    String groupId = DEFAULT_WEBJAR_GROUP_ID;
    String artifactId = webjarId;
    int groupSeparatorPosition = webjarId.indexOf(':');
    if (groupSeparatorPosition >= 0) {
        // A different group id.
        groupId = webjarId.substring(0, groupSeparatorPosition);
        artifactId = webjarId.substring(groupSeparatorPosition + 1);
    }
    Map<String, Object> urlParams = new LinkedHashMap<>();
    if (params != null) {
        urlParams.putAll(params);
    }
    // For backward-compatibility reasons we still support passing the target wiki in parameters. However we need
    // to remove it from the params if that's the case since we don't want to output a URL with the wiki id in the
    // query string (since the namespace is now part of the URL).
    urlParams.remove(WIKI);
    Object version = urlParams.remove(VERSION);
    if (version == null) {
        // Try to determine the version based on the extensions that are currently installed or provided by default.
        version = getVersion(String.format("%s:%s", groupId, artifactId), namespace);
    }
    // Construct a WebJarsResourceReference so that we can serialize it!
    WebJarsResourceReference resourceReference = getResourceReference(artifactId, version, namespace, path, urlParams);
    ExtendedURL extendedURL;
    try {
        extendedURL = this.defaultResourceReferenceSerializer.serialize(resourceReference);
    } catch (SerializeResourceReferenceException | UnsupportedResourceReferenceException e) {
        this.logger.warn("Error while serializing WebJar URL for id [{}], path = [{}]. Root cause = [{}]", webjarId, path, ExceptionUtils.getRootCauseMessage(e));
        return null;
    }
    return extendedURL.serialize();
}
Also used : SerializeResourceReferenceException(org.xwiki.resource.SerializeResourceReferenceException) WebJarsResourceReference(org.xwiki.webjars.internal.WebJarsResourceReference) UnsupportedResourceReferenceException(org.xwiki.resource.UnsupportedResourceReferenceException) ExtendedURL(org.xwiki.url.ExtendedURL) LinkedHashMap(java.util.LinkedHashMap)

Example 14 with ExtendedURL

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

the class WebJarsScriptServiceTest method computeURLWithVersion.

@Test
public void computeURLWithVersion() throws Exception {
    WikiDescriptorManager wikiDescriptorManager = this.mocker.getInstance(WikiDescriptorManager.class);
    when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("math");
    WebJarsResourceReference resourceReference = new WebJarsResourceReference("wiki:math", Arrays.asList("ang:ular", "2.1.11", "angular.css"));
    // Test that colon is not interpreted as groupId/artifactId separator (for backwards compatibility).
    when(this.serializer.serialize(resourceReference)).thenReturn(new ExtendedURL(Arrays.asList("xwiki", "ang:ular", "2.1.11", "angular.css")));
    assertEquals("/xwiki/ang%3Aular/2.1.11/angular.css", this.mocker.getComponentUnderTest().url("ang:ular/2.1.11/angular.css"));
}
Also used : WebJarsResourceReference(org.xwiki.webjars.internal.WebJarsResourceReference) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) ExtendedURL(org.xwiki.url.ExtendedURL) Test(org.junit.Test)

Example 15 with ExtendedURL

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

the class WebJarsScriptServiceTest method computeURLWithoutVersionAndNoExtensionMatchingWebJarId.

@Test
public void computeURLWithoutVersionAndNoExtensionMatchingWebJarId() throws Exception {
    WikiDescriptorManager wikiDescriptorManager = this.mocker.getInstance(WikiDescriptorManager.class);
    when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("math");
    WebJarsResourceReference resourceReference = new WebJarsResourceReference("wiki:math", Arrays.asList("angular", "angular.css"));
    when(this.serializer.serialize(resourceReference)).thenReturn(new ExtendedURL(Arrays.asList("xwiki", "angular", "angular.css")));
    assertEquals("/xwiki/angular/angular.css", this.mocker.getComponentUnderTest().url("angular", "angular.css"));
}
Also used : WebJarsResourceReference(org.xwiki.webjars.internal.WebJarsResourceReference) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) ExtendedURL(org.xwiki.url.ExtendedURL) Test(org.junit.Test)

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